From 09b33015c46f5eff32b610c202c8967d23e35509 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Oct 2018 11:22:43 +1030 Subject: [PATCH] connectd: give user a hint when wrong key is used. When the wrong key is used, the remote end simply hangs up. We used to get a random errno, which tends to be "Operation now in progress." Now it's defined to be 0, detect and provide a better error. Signed-off-by: Rusty Russell --- connectd/connectd.c | 10 +++++++++- tests/test_connection.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/connectd/connectd.c b/connectd/connectd.c index 2c1697101..f75fbaf1f 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -566,11 +567,18 @@ static void PRINTF_FMT(5,6) static void destroy_io_conn(struct io_conn *conn, struct connecting *connect) { /*~ tal_append_fmt appends to a tal string. It's terribly convenient */ + const char *errstr = strerror(errno); + /* errno 0 means they hung up on us. */ + if (errno == 0) { + errstr = "peer closed connection"; + if (streq(connect->connstate, "Cryptographic handshake")) + errstr = "peer closed connection (wrong key?)"; + } tal_append_fmt(&connect->errors, "%s: %s: %s. ", type_to_string(tmpctx, struct wireaddr_internal, &connect->addrs[connect->addrnum]), - connect->connstate, strerror(errno)); + connect->connstate, errstr); connect->addrnum++; try_connect_one_addr(connect); } diff --git a/tests/test_connection.py b/tests/test_connection.py index c682dfdca..3bd7781e3 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -41,7 +41,7 @@ def test_connect(node_factory): l1.rpc.connect('032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e', 'localhost', 1) # Should get reasonable error if wrong key for peer. - with pytest.raises(RpcError, match=r'Cryptographic handshake: '): + with pytest.raises(RpcError, match=r'Cryptographic handshake: peer closed connection \(wrong key\?\)'): l1.rpc.connect('032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e', 'localhost', l2.port)