Browse Source

connectd: generate message for lightningd inside peer_connected().

We used to generate this in the caller, then save it in case we needed
to retry.  We're about to change the message we send to lightningd, so
we'll need to regenerate it every time; just hand all the extra args
into peer_connected() and we can generate the `connect_peer_connected`
msg there.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>

1diff --git a/connectd/connectd.c b/connectd/connectd.c
index 94fe50b56..459c9ac63 100644
htlc_accepted_hook
Rusty Russell 6 years ago
parent
commit
a40f45af55
  1. 38
      connectd/connectd.c
  2. 9
      connectd/connectd.h
  3. 9
      connectd/peer_exchange_initmsg.c

38
connectd/connectd.c

@ -338,7 +338,9 @@ static bool get_gossipfds(struct daemon *daemon,
struct peer_reconnected { struct peer_reconnected {
struct daemon *daemon; struct daemon *daemon;
struct node_id id; struct node_id id;
const u8 *peer_connected_msg; struct wireaddr_internal addr;
struct crypto_state cs;
const u8 *globalfeatures;
const u8 *localfeatures; const u8 *localfeatures;
}; };
@ -356,8 +358,8 @@ static struct io_plan *retry_peer_connected(struct io_conn *conn,
/*~ Usually the pattern is to return this directly, but we have to free /*~ Usually the pattern is to return this directly, but we have to free
* our temporary structure. */ * our temporary structure. */
plan = peer_connected(conn, pr->daemon, &pr->id, plan = peer_connected(conn, pr->daemon, &pr->id, &pr->addr, &pr->cs,
take(pr->peer_connected_msg), take(pr->globalfeatures),
take(pr->localfeatures)); take(pr->localfeatures));
tal_free(pr); tal_free(pr);
return plan; return plan;
@ -368,7 +370,9 @@ static struct io_plan *retry_peer_connected(struct io_conn *conn,
static struct io_plan *peer_reconnected(struct io_conn *conn, static struct io_plan *peer_reconnected(struct io_conn *conn,
struct daemon *daemon, struct daemon *daemon,
const struct node_id *id, const struct node_id *id,
const u8 *peer_connected_msg TAKES, const struct wireaddr_internal *addr,
const struct crypto_state *cs,
const u8 *globalfeatures TAKES,
const u8 *localfeatures TAKES) const u8 *localfeatures TAKES)
{ {
u8 *msg; u8 *msg;
@ -385,13 +389,14 @@ static struct io_plan *peer_reconnected(struct io_conn *conn,
pr = tal(daemon, struct peer_reconnected); pr = tal(daemon, struct peer_reconnected);
pr->daemon = daemon; pr->daemon = daemon;
pr->id = *id; pr->id = *id;
pr->cs = *cs;
pr->addr = *addr;
/*~ Note that tal_dup_arr() will do handle the take() of /*~ Note that tal_dup_arr() will do handle the take() of
* peer_connected_msg and localfeatures (turning it into a simply * globalfeatures and localfeatures (turning it into a simply
* tal_steal() in those cases). */ * tal_steal() in those cases). */
pr->peer_connected_msg pr->globalfeatures
= tal_dup_arr(pr, u8, peer_connected_msg, = tal_dup_arr(pr, u8, globalfeatures, tal_count(globalfeatures), 0);
tal_count(peer_connected_msg), 0);
pr->localfeatures pr->localfeatures
= tal_dup_arr(pr, u8, localfeatures, tal_count(localfeatures), 0); = tal_dup_arr(pr, u8, localfeatures, tal_count(localfeatures), 0);
@ -411,19 +416,24 @@ static struct io_plan *peer_reconnected(struct io_conn *conn,
struct io_plan *peer_connected(struct io_conn *conn, struct io_plan *peer_connected(struct io_conn *conn,
struct daemon *daemon, struct daemon *daemon,
const struct node_id *id, const struct node_id *id,
const u8 *peer_connected_msg TAKES, const struct wireaddr_internal *addr,
const struct crypto_state *cs,
const u8 *globalfeatures TAKES,
const u8 *localfeatures TAKES) const u8 *localfeatures TAKES)
{ {
u8 *msg;
int gossip_fd, gossip_store_fd; int gossip_fd, gossip_store_fd;
if (node_set_get(&daemon->peers, id)) if (node_set_get(&daemon->peers, id))
return peer_reconnected(conn, daemon, id, peer_connected_msg, return peer_reconnected(conn, daemon, id, addr, cs,
localfeatures); globalfeatures, localfeatures);
/* We've successfully connected. */ /* We've successfully connected. */
connected_to_peer(daemon, conn, id); connected_to_peer(daemon, conn, id);
/* We promised we'd take it by marking it TAKEN above; prepare to free it. */ /* We promised we'd take it by marking it TAKEN above; prepare to free it. */
if (taken(globalfeatures))
tal_steal(tmpctx, globalfeatures);
if (taken(localfeatures)) if (taken(localfeatures))
tal_steal(tmpctx, localfeatures); tal_steal(tmpctx, localfeatures);
@ -431,10 +441,14 @@ struct io_plan *peer_connected(struct io_conn *conn,
if (!get_gossipfds(daemon, id, localfeatures, &gossip_fd, &gossip_store_fd)) if (!get_gossipfds(daemon, id, localfeatures, &gossip_fd, &gossip_store_fd))
return io_close(conn); return io_close(conn);
/* Create message to tell master peer has connected. */
msg = towire_connect_peer_connected(NULL, id, addr, cs,
globalfeatures, localfeatures);
/*~ daemon_conn is a message queue for inter-daemon communication: we /*~ daemon_conn is a message queue for inter-daemon communication: we
* queue up the `connect_peer_connected` message to tell lightningd * queue up the `connect_peer_connected` message to tell lightningd
* we have connected, and give the the peer and gossip fds. */ * we have connected, and give the the peer and gossip fds. */
daemon_conn_send(daemon->master, peer_connected_msg); daemon_conn_send(daemon->master, take(msg));
/* io_conn_fd() extracts the fd from ccan/io's io_conn */ /* io_conn_fd() extracts the fd from ccan/io's io_conn */
daemon_conn_send_fd(daemon->master, io_conn_fd(conn)); daemon_conn_send_fd(daemon->master, io_conn_fd(conn));
daemon_conn_send_fd(daemon->master, gossip_fd); daemon_conn_send_fd(daemon->master, gossip_fd);

9
connectd/connectd.h

@ -8,6 +8,7 @@ struct io_conn;
struct connecting; struct connecting;
struct daemon; struct daemon;
struct node_id; struct node_id;
struct wireaddr_internal;
/* Called by io_tor_connect once it has a connection out. */ /* Called by io_tor_connect once it has a connection out. */
struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect); struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect);
@ -15,8 +16,10 @@ struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect)
/* Called by peer_exchange_initmsg if successful. */ /* Called by peer_exchange_initmsg if successful. */
struct io_plan *peer_connected(struct io_conn *conn, struct io_plan *peer_connected(struct io_conn *conn,
struct daemon *daemon, struct daemon *daemon,
const struct node_id *id TAKES, const struct node_id *id,
const u8 *peer_connected_msg TAKES, const struct wireaddr_internal *addr,
const u8 *lfeatures TAKES); const struct crypto_state *cs,
const u8 *globalfeatures TAKES,
const u8 *localfeatures TAKES);
#endif /* LIGHTNING_CONNECTD_CONNECTD_H */ #endif /* LIGHTNING_CONNECTD_CONNECTD_H */

9
connectd/peer_exchange_initmsg.c

@ -80,15 +80,12 @@ static struct io_plan *peer_init_received(struct io_conn *conn,
return io_write(conn, msg, tal_count(msg), io_close_cb, NULL); return io_write(conn, msg, tal_count(msg), io_close_cb, NULL);
} }
/* Create message to tell master peer has connected. */
msg = towire_connect_peer_connected(NULL, &peer->id, &peer->addr,
&peer->cs,
globalfeatures, localfeatures);
/* Usually return io_close_taken_fd, but may wait for old peer to /* Usually return io_close_taken_fd, but may wait for old peer to
* be disconnected if it's a reconnect. */ * be disconnected if it's a reconnect. */
return peer_connected(conn, peer->daemon, &peer->id, return peer_connected(conn, peer->daemon, &peer->id,
take(msg), take(localfeatures)); &peer->addr, &peer->cs,
take(globalfeatures),
take(localfeatures));
} }
static struct io_plan *peer_init_hdr_received(struct io_conn *conn, static struct io_plan *peer_init_hdr_received(struct io_conn *conn,

Loading…
Cancel
Save