Skip to content

Commit

Permalink
gossipd: handle ping messages for remote peers too.
Browse files Browse the repository at this point in the history
This simplifies our ping handling: make gossipd always do it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 25, 2018
1 parent f8fa421 commit a52d522
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 136 deletions.
1 change: 0 additions & 1 deletion channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ CHANNELD_COMMON_OBJS := \
common/key_derive.o \
common/memleak.o \
common/msg_queue.o \
common/ping.o \
common/peer_billboard.o \
common/peer_failed.o \
common/permute_tx.o \
Expand Down
52 changes: 1 addition & 51 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,18 +1603,6 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
abort();
}

static void handle_pong(struct peer *peer, const u8 *pong)
{
const char *err = got_pong(pong, &peer->num_pings_outstanding);
if (err)
peer_failed(&peer->cs,
&peer->channel_id,
"%s", err);

wire_sync_write(MASTER_FD,
take(towire_channel_ping_reply(NULL, tal_len(pong))));
}

static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown)
{
struct channel_id channel_id;
Expand Down Expand Up @@ -1695,9 +1683,6 @@ static void peer_in(struct peer *peer, const u8 *msg)
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
handle_peer_fail_malformed_htlc(peer, msg);
return;
case WIRE_PONG:
handle_pong(peer, msg);
return;
case WIRE_SHUTDOWN:
handle_peer_shutdown(peer, msg);
return;
Expand All @@ -1721,6 +1706,7 @@ static void peer_in(struct peer *peer, const u8 *msg)
case WIRE_GOSSIP_TIMESTAMP_FILTER:
case WIRE_REPLY_SHORT_CHANNEL_IDS_END:
case WIRE_PING:
case WIRE_PONG:
case WIRE_ERROR:
abort();
}
Expand Down Expand Up @@ -2266,38 +2252,6 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)
abort();
}

static void handle_ping_cmd(struct peer *peer, const u8 *inmsg)
{
u16 num_pong_bytes, ping_len;
u8 *ping;

if (!fromwire_channel_ping(inmsg, &num_pong_bytes, &ping_len))
master_badmsg(WIRE_CHANNEL_PING, inmsg);

ping = make_ping(NULL, num_pong_bytes, ping_len);
if (tal_len(ping) > 65535)
status_failed(STATUS_FAIL_MASTER_IO, "Oversize channel_ping");

enqueue_peer_msg(peer, take(ping));

status_trace("sending ping expecting %sresponse",
num_pong_bytes >= 65532 ? "no " : "");

/* BOLT #1:
*
* - if `num_pong_bytes` is less than 65532:
* - MUST respond by sending a `pong` message, with `byteslen` equal
* to `num_pong_bytes`.
* - otherwise (`num_pong_bytes` is **not** less than 65532):
* - MUST ignore the `ping`.
*/
if (num_pong_bytes >= 65532)
wire_sync_write(MASTER_FD,
take(towire_channel_ping_reply(NULL, 0)));
else
peer->num_pings_outstanding++;
}

static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
{
if (!fromwire_channel_send_shutdown(inmsg))
Expand Down Expand Up @@ -2339,9 +2293,6 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNEL_FAIL_HTLC:
handle_fail(peer, msg);
return;
case WIRE_CHANNEL_PING:
handle_ping_cmd(peer, msg);
return;
case WIRE_CHANNEL_SEND_SHUTDOWN:
handle_shutdown_cmd(peer, msg);
return;
Expand All @@ -2352,7 +2303,6 @@ static void req_in(struct peer *peer, const u8 *msg)
#endif /* DEVELOPER */
case WIRE_CHANNEL_INIT:
case WIRE_CHANNEL_OFFER_HTLC_REPLY:
case WIRE_CHANNEL_PING_REPLY:
case WIRE_CHANNEL_SENDING_COMMITSIG:
case WIRE_CHANNEL_GOT_COMMITSIG:
case WIRE_CHANNEL_GOT_REVOKE:
Expand Down
8 changes: 0 additions & 8 deletions channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ channel_fail_htlc,,errcode,u16
# If errcode & UPDATE, this says which outgoing channel failed.
channel_fail_htlc,,which_channel,struct short_channel_id

# Ping/pong test.
channel_ping,1011
channel_ping,,num_pong_bytes,u16
channel_ping,,len,u16

channel_ping_reply,1111
channel_ping_reply,,totlen,u16

# When we receive funding_locked.
channel_got_funding_locked,1019
channel_got_funding_locked,,next_per_commit_point,struct pubkey
Expand Down
1 change: 0 additions & 1 deletion closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ CLOSINGD_COMMON_OBJS := \
common/peer_billboard.o \
common/peer_failed.o \
common/permute_tx.o \
common/ping.o \
common/read_peer_msg.o \
common/socket_close.o \
common/status.o \
Expand Down
32 changes: 0 additions & 32 deletions common/read_peer_msg.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <common/crypto_sync.h>
#include <common/peer_failed.h>
#include <common/ping.h>
#include <common/read_peer_msg.h>
#include <common/status.h>
#include <common/type_to_string.h>
Expand All @@ -12,32 +11,6 @@
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>

static void handle_ping(const u8 *msg,
int peer_fd,
struct crypto_state *cs,
const struct channel_id *channel,
bool (*send_reply)(struct crypto_state *, int,
const u8 *, void *),
void *arg)
{
u8 *pong;

if (!check_ping_make_pong(msg, msg, &pong)) {
send_reply(cs, peer_fd,
take(towire_errorfmt(NULL, channel,
"Bad ping %s",
tal_hex(msg, msg))), arg);
peer_failed_connection_lost();
}

status_debug("Got ping, sending %s", pong ?
wire_type_name(fromwire_peektype(pong))
: "nothing");

if (pong && !send_reply(cs, peer_fd, pong, arg))
peer_failed_connection_lost();
}

void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd,
struct crypto_state *cs,
bool (*send_msg)(struct crypto_state *cs, int fd,
Expand Down Expand Up @@ -111,11 +84,6 @@ u8 *read_peer_msg_(const tal_t *ctx,
return NULL;
}

if (fromwire_peektype(msg) == WIRE_PING) {
handle_ping(msg, peer_fd, cs, channel, send_reply, arg);
return tal_free(msg);
}

if (fromwire_peektype(msg) == WIRE_ERROR) {
char *err = sanitize_error(msg, msg, &chanid);

Expand Down
22 changes: 13 additions & 9 deletions gossipd/gossip.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ struct local_peer_state {
/* If we're exiting due to non-gossip msg, otherwise release */
u8 *nongossip_msg;

/* How many pongs are we expecting? */
size_t num_pings_outstanding;

/* Message queue for outgoing. */
struct msg_queue peer_out;
};
Expand Down Expand Up @@ -267,6 +264,9 @@ struct peer {
/* How many query responses are we expecting? */
size_t num_scid_queries_outstanding;

/* How many pongs are we expecting? */
size_t num_pings_outstanding;

/* Map of outstanding channel_range requests. */
u8 *query_channel_blocks;
u32 first_channel_range;
Expand Down Expand Up @@ -368,7 +368,6 @@ new_local_peer_state(struct peer *peer, const struct crypto_state *cs)
init_peer_crypto_state(peer, &lps->pcs);
lps->pcs.cs = *cs;
lps->return_to_master = false;
lps->num_pings_outstanding = 0;
msg_queue_init(&lps->peer_out, lps);

return lps;
Expand Down Expand Up @@ -427,6 +426,7 @@ static struct peer *new_peer(const tal_t *ctx,
peer->query_channel_blocks = NULL;
peer->gossip_timestamp_min = 0;
peer->gossip_timestamp_max = UINT32_MAX;
peer->num_pings_outstanding = 0;

return peer;
}
Expand Down Expand Up @@ -1101,18 +1101,18 @@ static void handle_ping(struct peer *peer, u8 *ping)
{
u8 *pong;

if (!check_ping_make_pong(peer, ping, &pong)) {
if (!check_ping_make_pong(NULL, ping, &pong)) {
peer_error(peer, "Bad ping");
return;
}

if (pong)
msg_enqueue(&peer->local->peer_out, take(pong));
queue_peer_msg(peer, take(pong));
}

static void handle_pong(struct peer *peer, const u8 *pong)
{
const char *err = got_pong(pong, &peer->local->num_pings_outstanding);
const char *err = got_pong(pong, &peer->num_pings_outstanding);

if (err) {
peer_error(peer, "%s", err);
Expand Down Expand Up @@ -1889,6 +1889,10 @@ static struct io_plan *owner_msg_in(struct io_conn *conn,
handle_query_channel_range(peer, dc->msg_in);
} else if (type == WIRE_REPLY_CHANNEL_RANGE) {
handle_reply_channel_range(peer, dc->msg_in);
} else if (type == WIRE_PING) {
handle_ping(peer, dc->msg_in);
} else if (type == WIRE_PONG) {
handle_pong(peer, dc->msg_in);
} else {
status_broken("peer %s: send us unknown msg of type %s",
type_to_string(tmpctx, struct pubkey, &peer->id),
Expand Down Expand Up @@ -2314,7 +2318,7 @@ static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
if (tal_len(ping) > 65535)
status_failed(STATUS_FAIL_MASTER_IO, "Oversize ping");

msg_enqueue(&peer->local->peer_out, take(ping));
queue_peer_msg(peer, take(ping));
status_trace("sending ping expecting %sresponse",
num_pong_bytes >= 65532 ? "no " : "");

Expand All @@ -2332,7 +2336,7 @@ static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
daemon_conn_send(&daemon->master,
take(towire_gossip_ping_reply(NULL, true, 0)));
else
peer->local->num_pings_outstanding++;
peer->num_pings_outstanding++;

out:
return daemon_conn_read_next(conn, &daemon->master);
Expand Down
2 changes: 0 additions & 2 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNEL_OFFER_HTLC:
case WIRE_CHANNEL_FULFILL_HTLC:
case WIRE_CHANNEL_FAIL_HTLC:
case WIRE_CHANNEL_PING:
case WIRE_CHANNEL_GOT_COMMITSIG_REPLY:
case WIRE_CHANNEL_GOT_REVOKE_REPLY:
case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY:
Expand All @@ -163,7 +162,6 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNEL_FEERATES:
/* Replies go to requests. */
case WIRE_CHANNEL_OFFER_HTLC_REPLY:
case WIRE_CHANNEL_PING_REPLY:
case WIRE_CHANNEL_DEV_REENABLE_COMMIT_REPLY:
break;
}
Expand Down
34 changes: 5 additions & 29 deletions lightningd/dev_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ static void ping_reply(struct subd *subd, const u8 *msg, const int *fds UNUSED,
bool ok, sent = true;

log_debug(subd->ld->log, "Got ping reply!");
if (streq(subd->name, "lightning_channeld"))
ok = fromwire_channel_ping_reply(msg, &totlen);
else
ok = fromwire_gossip_ping_reply(msg, &sent, &totlen);
ok = fromwire_gossip_ping_reply(msg, &sent, &totlen);

if (!ok)
command_fail(cmd, LIGHTNINGD, "Bad reply message");
Expand All @@ -41,11 +38,9 @@ static void ping_reply(struct subd *subd, const u8 *msg, const int *fds UNUSED,
static void json_dev_ping(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
struct peer *peer;
u8 *msg;
unsigned int len, pongbytes;
struct pubkey id;
struct subd *owner;

if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id),
Expand Down Expand Up @@ -81,29 +76,10 @@ static void json_dev_ping(struct command *cmd,
return;
}

/* First, see if it's in channeld. */
peer = peer_by_id(cmd->ld, &id);
if (peer) {
struct channel *channel = peer_active_channel(peer);

if (!channel
|| !channel->owner
|| !streq(channel->owner->name, "lightning_channeld")) {
command_fail(cmd, LIGHTNINGD, "Peer in %s",
channel && channel->owner
? channel->owner->name
: "unattached");
return;
}
msg = towire_channel_ping(cmd, pongbytes, len);
owner = channel->owner;
} else {
/* We assume it's in gossipd. */
msg = towire_gossip_ping(cmd, &id, pongbytes, len);
owner = cmd->ld->gossip;
}

subd_req(owner, owner, take(msg), -1, 0, ping_reply, cmd);
/* gossipd handles all pinging, even if it's in another daemon. */
msg = towire_gossip_ping(NULL, &id, pongbytes, len);
subd_req(cmd->ld->gossip, cmd->ld->gossip,
take(msg), -1, 0, ping_reply, cmd);
command_still_pending(cmd);
}

Expand Down
1 change: 0 additions & 1 deletion openingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ OPENINGD_COMMON_OBJS := \
common/keyset.o \
common/memleak.o \
common/msg_queue.o \
common/ping.o \
common/peer_billboard.o \
common/peer_failed.o \
common/permute_tx.o \
Expand Down
4 changes: 2 additions & 2 deletions wire/peer_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ bool is_msg_for_gossipd(const u8 *cursor)
case WIRE_QUERY_CHANNEL_RANGE:
case WIRE_REPLY_CHANNEL_RANGE:
case WIRE_GOSSIP_TIMESTAMP_FILTER:
case WIRE_PING:
case WIRE_PONG:
return true;
case WIRE_INIT:
case WIRE_ERROR:
Expand All @@ -64,8 +66,6 @@ bool is_msg_for_gossipd(const u8 *cursor)
case WIRE_COMMITMENT_SIGNED:
case WIRE_REVOKE_AND_ACK:
case WIRE_UPDATE_FEE:
case WIRE_PING:
case WIRE_PONG:
case WIRE_CHANNEL_REESTABLISH:
case WIRE_ANNOUNCEMENT_SIGNATURES:
break;
Expand Down

0 comments on commit a52d522

Please sign in to comment.