|
24 | 24 | #include <ccan/str/str.h> |
25 | 25 | #include <ccan/take/take.h> |
26 | 26 | #include <ccan/tal/str/str.h> |
| 27 | +#include <ccan/timer/timer.h> |
27 | 28 | #include <common/bech32.h> |
28 | 29 | #include <common/bech32_util.h> |
29 | 30 | #include <common/cryptomsg.h> |
|
38 | 39 | #include <common/pseudorand.h> |
39 | 40 | #include <common/status.h> |
40 | 41 | #include <common/subdaemon.h> |
| 42 | +#include <common/timeout.h> |
41 | 43 | #include <common/type_to_string.h> |
42 | 44 | #include <common/utils.h> |
43 | 45 | #include <common/version.h> |
@@ -121,6 +123,10 @@ struct daemon { |
121 | 123 | /* pubkey equivalent. */ |
122 | 124 | struct pubkey mykey; |
123 | 125 |
|
| 126 | + /* Base for timeout timers, and how long to wait for init msg */ |
| 127 | + struct timers timers; |
| 128 | + u32 timeout_secs; |
| 129 | + |
124 | 130 | /* Peers that we've handed to `lightningd`, which it hasn't told us |
125 | 131 | * have disconnected. */ |
126 | 132 | struct node_set peers; |
@@ -509,6 +515,12 @@ static struct io_plan *handshake_in_success(struct io_conn *conn, |
509 | 515 | cs, &id, addr); |
510 | 516 | } |
511 | 517 |
|
| 518 | +/*~ If the timer goes off, we simply free everything, which hangs up. */ |
| 519 | +static void conn_timeout(struct io_conn *conn) |
| 520 | +{ |
| 521 | + tal_free(conn); |
| 522 | +} |
| 523 | + |
512 | 524 | /*~ When we get a connection in we set up its network address then call |
513 | 525 | * handshake.c to set up the crypto state. */ |
514 | 526 | static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon) |
@@ -544,7 +556,11 @@ static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon |
544 | 556 | return io_close(conn); |
545 | 557 | } |
546 | 558 |
|
547 | | - /* FIXME: Timeout */ |
| 559 | + /* If they don't complete handshake in reasonable time, hang up */ |
| 560 | + notleak(new_reltimer(&daemon->timers, conn, |
| 561 | + time_from_sec(daemon->timeout_secs), |
| 562 | + conn_timeout, conn)); |
| 563 | + |
548 | 564 | /*~ The crypto handshake differs depending on whether you received or |
549 | 565 | * initiated the socket connection, so there are two entry points. |
550 | 566 | * Note, again, the notleak() to avoid our simplistic leak detection |
@@ -583,7 +599,10 @@ struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect) |
583 | 599 | return io_close(conn); |
584 | 600 | } |
585 | 601 |
|
586 | | - /* FIXME: Timeout */ |
| 602 | + /* If they don't complete handshake in reasonable time, hang up */ |
| 603 | + notleak(new_reltimer(&connect->daemon->timers, conn, |
| 604 | + time_from_sec(connect->daemon->timeout_secs), |
| 605 | + conn_timeout, conn)); |
587 | 606 | status_peer_debug(&connect->id, "Connected out, starting crypto"); |
588 | 607 |
|
589 | 608 | connect->connstate = "Cryptographic handshake"; |
@@ -1243,7 +1262,8 @@ static struct io_plan *connect_init(struct io_conn *conn, |
1243 | 1262 | &proxyaddr, &daemon->use_proxy_always, |
1244 | 1263 | &daemon->dev_allow_localhost, &daemon->use_dns, |
1245 | 1264 | &tor_password, |
1246 | | - &daemon->use_v3_autotor)) { |
| 1265 | + &daemon->use_v3_autotor, |
| 1266 | + &daemon->timeout_secs)) { |
1247 | 1267 | /* This is a helper which prints the type expected and the actual |
1248 | 1268 | * message, then exits (it should never be called!). */ |
1249 | 1269 | master_badmsg(WIRE_CONNECTD_INIT, msg); |
@@ -1638,6 +1658,7 @@ int main(int argc, char *argv[]) |
1638 | 1658 | memleak_add_helper(daemon, memleak_daemon_cb); |
1639 | 1659 | list_head_init(&daemon->connecting); |
1640 | 1660 | daemon->listen_fds = tal_arr(daemon, struct listen_fd, 0); |
| 1661 | + timers_init(&daemon->timers, time_mono()); |
1641 | 1662 | /* stdin == control */ |
1642 | 1663 | daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL, |
1643 | 1664 | daemon); |
|
0 commit comments