lightningd: get connection direction from connectd.

This matters: if we connected, the address is probably usable for future connections.
But if they connected, the port is probably not (but the IP address may be).

Changelog-Added: JSON-RPC: `connect` returns "direction" ("in": they iniatated, or "out": we initiated)
Changelog-Added: plugins: `peer_connected` hook and `connect` notifications have "direction" field.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-03-25 14:23:31 +10:30
parent b689d33e97
commit b0d6996ed6
17 changed files with 79 additions and 46 deletions

View File

@@ -981,6 +981,7 @@ struct peer_connected_hook_payload {
struct lightningd *ld;
struct channel *channel;
struct wireaddr_internal addr;
bool incoming;
struct peer *peer;
struct per_peer_state *pps;
u8 *error;
@@ -993,6 +994,7 @@ peer_connected_serialize(struct peer_connected_hook_payload *payload,
const struct peer *p = payload->peer;
json_object_start(stream, "peer");
json_add_node_id(stream, "id", &p->id);
json_add_string(stream, "direction", payload->incoming ? "in" : "out");
json_add_string(
stream, "addr",
type_to_string(stream, struct wireaddr_internal, &payload->addr));
@@ -1079,7 +1081,7 @@ static void peer_connected_hook_final(struct peer_connected_hook_payload *payloa
abort();
}
notify_connect(ld, &peer->id, &addr);
notify_connect(ld, &peer->id, payload->incoming, &addr);
/* No err, all good. */
error = NULL;
@@ -1167,9 +1169,10 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
hook_payload->ld = ld;
hook_payload->error = NULL;
if (!fromwire_connectd_peer_connected(hook_payload, msg,
&id, &hook_payload->addr,
&hook_payload->pps,
&their_features))
&id, &hook_payload->addr,
&hook_payload->incoming,
&hook_payload->pps,
&their_features))
fatal("Connectd gave bad CONNECT_PEER_CONNECTED message %s",
tal_hex(msg, msg));
@@ -1188,7 +1191,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
peer_update_features(peer, their_features);
/* Complete any outstanding connect commands. */
connect_succeeded(ld, peer, &hook_payload->addr);
connect_succeeded(ld, peer, hook_payload->incoming, &hook_payload->addr);
/* Can't be opening, since we wouldn't have sent peer_disconnected. */
assert(!peer->uncommitted_channel);