From b3a2a0623c018cdb34745b4585ad64c5c04dc302 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 23 May 2017 21:47:34 +0930 Subject: [PATCH] lightningd: set up reconnect timer if we don't want to forget peer. Signed-off-by: Rusty Russell --- lightningd/new_connection.c | 5 ++++ lightningd/new_connection.h | 1 + lightningd/peer_control.c | 59 ++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/lightningd/new_connection.c b/lightningd/new_connection.c index 4b2b32610..afffa74ad 100644 --- a/lightningd/new_connection.c +++ b/lightningd/new_connection.c @@ -248,3 +248,8 @@ struct io_plan *connection_in(struct io_conn *conn, struct lightningd *ld) } return hsm_then_handshake(conn, ld, c); } + +const struct pubkey *connection_known_id(const struct connection *c) +{ + return c->known_id; +} diff --git a/lightningd/new_connection.h b/lightningd/new_connection.h index 5a17ad35a..428c79d20 100644 --- a/lightningd/new_connection.h +++ b/lightningd/new_connection.h @@ -22,4 +22,5 @@ struct io_plan *connection_out(struct io_conn *conn, struct io_plan *connection_in(struct io_conn *conn, struct lightningd *ld); +const struct pubkey *connection_known_id(const struct connection *c); #endif /* LIGHTNING_LIGHTNINGD_NEW_CONNECTION_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 0a72f12c4..512be11a3 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -50,9 +51,55 @@ static void destroy_peer(struct peer *peer) close(peer->fd); } +static struct peer *peer_by_pubkey(struct lightningd *ld, const struct pubkey *id) +{ + struct peer *peer; + list_for_each(&ld->peers, peer, list) { + if (pubkey_cmp(id, peer->id) == 0) + return peer; + } + return NULL; +} + +/* Mutual recursion, sets timer. */ +static void peer_reconnect(struct peer *peer); + +static void reconnect_failed(struct lightningd_state *dstate, + struct connection *c) +{ + /* Figure out what peer, set reconnect timer. */ + struct lightningd *ld = ld_from_dstate(dstate); + struct peer *peer = peer_by_pubkey(ld, connection_known_id(c)); + + tal_free(c); + peer_reconnect(peer); +} + +static void try_reconnect(struct peer *peer) +{ + struct connection *c; + struct netaddr *addrs; + + /* We may already be reconnected (another incoming connection) */ + if (peer->fd != -1) { + log_debug(peer->log, "try_reconnect: already reconnected"); + return; + } + + c = new_connection(peer, peer->ld, NULL, peer->id); + + /* FIXME: Combine known address with gossip addresses and possibly + * DNS seed addresses. */ + addrs = tal_dup_arr(c, struct netaddr, &peer->netaddr, 1, 0); + multiaddress_connect(&peer->ld->dstate, addrs, + connection_out, reconnect_failed, c); +} + static void peer_reconnect(struct peer *peer) { - /* FIXME: Set timer, etc. */ + new_reltimer(&peer->ld->dstate.timers, + peer, peer->ld->dstate.config.poll_time, + try_reconnect, peer); } void peer_fail(struct peer *peer, const char *fmt, ...) @@ -967,16 +1014,6 @@ fail: tal_free(hend); } -static struct peer *peer_by_pubkey(struct lightningd *ld, const struct pubkey *id) -{ - struct peer *peer; - list_for_each(&ld->peers, peer, list) { - if (pubkey_cmp(id, peer->id) == 0) - return peer; - } - return NULL; -} - /* * A catchall in case outgoing peer disconnects before getting fwd. *