lightningd: set up reconnect timer if we don't want to forget peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-05-23 21:47:34 +09:30
parent 58bf67c310
commit b3a2a0623c
3 changed files with 54 additions and 11 deletions

View File

@ -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;
}

View File

@ -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 */

View File

@ -13,6 +13,7 @@
#include <daemon/invoice.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <daemon/timeout.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/build_utxos.h>
@ -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.
*