From 93e445daf5966bde966ac605077106a44acb9e7d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 9 Aug 2018 12:23:17 +0930 Subject: [PATCH] channeld: send our own pings whenever we indicate we want to send a commitment. This doesn't do much (though we might get an error before we send the commitment_signed), but it's infrastructure for the next patch. Signed-off-by: Rusty Russell --- channeld/Makefile | 1 + channeld/channel.c | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/channeld/Makefile b/channeld/Makefile index 115413636..9c281d005 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -57,6 +57,7 @@ CHANNELD_COMMON_OBJS := \ common/peer_billboard.o \ common/peer_failed.o \ common/permute_tx.o \ + common/ping.o \ common/pseudorand.o \ common/read_peer_msg.o \ common/sphinx.o \ diff --git a/channeld/channel.c b/channeld/channel.c index fe9fe0ecb..59a28eff9 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -112,8 +112,8 @@ struct peer { u64 commit_timer_attempts; u32 commit_msec; - /* Don't accept a pong we didn't ping for. */ - size_t num_pings_outstanding; + /* Are we expecting a pong? */ + bool expecting_pong; /* The feerate we want. */ u32 desired_feerate; @@ -928,6 +928,17 @@ static struct commit_sigs *calc_commitsigs(const tal_t *ctx, return commit_sigs; } +static void maybe_send_ping(struct peer *peer) +{ + /* Already have a ping in flight? */ + if (peer->expecting_pong) + return; + + sync_crypto_write_no_delay(&peer->cs, PEER_FD, + take(make_ping(NULL, 1, 0))); + peer->expecting_pong = true; +} + static void send_commit(struct peer *peer) { u8 *msg; @@ -1045,6 +1056,9 @@ static void send_commit(struct peer *peer) static void start_commit_timer(struct peer *peer) { + /* We should send a ping now if we need a liveness check. */ + maybe_send_ping(peer); + /* Already armed? */ if (peer->commit_timer) return; @@ -1573,6 +1587,12 @@ static void peer_in(struct peer *peer, const u8 *msg) { enum wire_type type = fromwire_peektype(msg); + /* Catch our own ping replies. */ + if (type == WIRE_PONG && peer->expecting_pong) { + peer->expecting_pong = false; + return; + } + if (handle_peer_gossip_or_error(PEER_FD, GOSSIP_FD, &peer->cs, &peer->channel_id, msg)) @@ -2424,7 +2444,7 @@ int main(int argc, char *argv[]) subdaemon_setup(argc, argv); peer = tal(NULL, struct peer); - peer->num_pings_outstanding = 0; + peer->expecting_pong = false; timers_init(&peer->timers, time_mono()); peer->commit_timer = NULL; peer->have_sigs[LOCAL] = peer->have_sigs[REMOTE] = false;