Skip to content

Commit 7f2b332

Browse files
committed
connectd: implement connection timeout (60 seconds).
This is simple, and we now can multifundchannel to every node on testnet (one simply hangs once we connect). Changelog-Fixed: Protocol: We now hang up if peer doesn't respond to init message after 60 seconds.
1 parent 1deba1c commit 7f2b332

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed

connectd/connectd.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <ccan/str/str.h>
2525
#include <ccan/take/take.h>
2626
#include <ccan/tal/str/str.h>
27+
#include <ccan/timer/timer.h>
2728
#include <common/bech32.h>
2829
#include <common/bech32_util.h>
2930
#include <common/cryptomsg.h>
@@ -38,6 +39,7 @@
3839
#include <common/pseudorand.h>
3940
#include <common/status.h>
4041
#include <common/subdaemon.h>
42+
#include <common/timeout.h>
4143
#include <common/type_to_string.h>
4244
#include <common/utils.h>
4345
#include <common/version.h>
@@ -121,6 +123,10 @@ struct daemon {
121123
/* pubkey equivalent. */
122124
struct pubkey mykey;
123125

126+
/* Base for timeout timers, and how long to wait for init msg */
127+
struct timers timers;
128+
u32 timeout_secs;
129+
124130
/* Peers that we've handed to `lightningd`, which it hasn't told us
125131
* have disconnected. */
126132
struct node_set peers;
@@ -509,6 +515,12 @@ static struct io_plan *handshake_in_success(struct io_conn *conn,
509515
cs, &id, addr);
510516
}
511517

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+
512524
/*~ When we get a connection in we set up its network address then call
513525
* handshake.c to set up the crypto state. */
514526
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
544556
return io_close(conn);
545557
}
546558

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+
548564
/*~ The crypto handshake differs depending on whether you received or
549565
* initiated the socket connection, so there are two entry points.
550566
* 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)
583599
return io_close(conn);
584600
}
585601

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));
587606
status_peer_debug(&connect->id, "Connected out, starting crypto");
588607

589608
connect->connstate = "Cryptographic handshake";
@@ -1243,7 +1262,8 @@ static struct io_plan *connect_init(struct io_conn *conn,
12431262
&proxyaddr, &daemon->use_proxy_always,
12441263
&daemon->dev_allow_localhost, &daemon->use_dns,
12451264
&tor_password,
1246-
&daemon->use_v3_autotor)) {
1265+
&daemon->use_v3_autotor,
1266+
&daemon->timeout_secs)) {
12471267
/* This is a helper which prints the type expected and the actual
12481268
* message, then exits (it should never be called!). */
12491269
master_badmsg(WIRE_CONNECTD_INIT, msg);
@@ -1638,6 +1658,7 @@ int main(int argc, char *argv[])
16381658
memleak_add_helper(daemon, memleak_daemon_cb);
16391659
list_head_init(&daemon->connecting);
16401660
daemon->listen_fds = tal_arr(daemon, struct listen_fd, 0);
1661+
timers_init(&daemon->timers, time_mono());
16411662
/* stdin == control */
16421663
daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL,
16431664
daemon);

connectd/connectd_wire.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ msgdata,connectd_init,dev_allow_localhost,bool,
1717
msgdata,connectd_init,use_dns,bool,
1818
msgdata,connectd_init,tor_password,wirestring,
1919
msgdata,connectd_init,use_v3_autotor,bool,
20+
msgdata,connectd_init,timeout_secs,u32,
2021

2122
# Connectd->master, here are the addresses I bound, can announce.
2223
msgtype,connectd_init_reply,2100

connectd/connectd_wiregen.c

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connectd/connectd_wiregen.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lightningd/connect_control.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ int connectd_init(struct lightningd *ld)
371371
ld->proxyaddr, ld->use_proxy_always || ld->pure_tor_setup,
372372
IFDEV(ld->dev_allow_localhost, false), ld->config.use_dns,
373373
ld->tor_service_password ? ld->tor_service_password : "",
374-
ld->config.use_v3_autotor);
374+
ld->config.use_v3_autotor,
375+
ld->config.connection_timeout_secs);
375376

376377
subd_req(ld->connectd, ld->connectd, take(msg), -1, 0,
377378
connect_init_done, NULL);

lightningd/lightningd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct config {
6969

7070
/* This is the key we use to encrypt `hsm_secret`. */
7171
struct secret *keypass;
72+
73+
/* How long before we give up waiting for INIT msg */
74+
u32 connection_timeout_secs;
7275
};
7376

7477
typedef STRMAP(const char *) alt_subdaemon_map;

lightningd/options.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ static void dev_register_opts(struct lightningd *ld)
593593
"Make builtin plugins unimportant so you can plugin stop them.");
594594
opt_register_arg("--dev-force-features", opt_force_featureset, NULL, ld,
595595
"Force the init/globalinit/node_announce/channel/bolt11 features, each comma-separated bitnumbers");
596+
opt_register_arg("--dev-timeout-secs", opt_set_u32, opt_show_u32,
597+
&ld->config.connection_timeout_secs,
598+
"Seconds to timeout if we don't receive INIT from peer");
596599
}
597600
#endif /* DEVELOPER */
598601

@@ -638,6 +641,9 @@ static const struct config testnet_config = {
638641
.min_capacity_sat = 10000,
639642

640643
.use_v3_autotor = true,
644+
645+
/* 1 minute should be enough for anyone! */
646+
.connection_timeout_secs = 60,
641647
};
642648

643649
/* aka. "Dude, where's my coins?" */
@@ -694,6 +700,9 @@ static const struct config mainnet_config = {
694700

695701
/* Allow to define the default behavior of tor services calls*/
696702
.use_v3_autotor = true,
703+
704+
/* 1 minute should be enough for anyone! */
705+
.connection_timeout_secs = 60,
697706
};
698707

699708
static void check_config(struct lightningd *ld)

0 commit comments

Comments
 (0)