From 475e4c9bd97124b058b0a271fbfa9fac4249264c Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Wed, 1 Jun 2022 13:18:55 +0200 Subject: [PATCH] jsonrpc: adds optional `remote_addr` to listpeers Changelog-Added: jsonrpc: adds optional `remote_addr` to listpeers --- doc/lightning-listpeers.7.md | 3 ++- doc/schemas/listpeers.schema.json | 4 ++++ lightningd/peer_control.c | 10 ++++++++++ lightningd/peer_control.h | 3 +++ tests/test_connection.py | 4 +++- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/lightning-listpeers.7.md b/doc/lightning-listpeers.7.md index 31c3d204c..647d3bdfc 100644 --- a/doc/lightning-listpeers.7.md +++ b/doc/lightning-listpeers.7.md @@ -159,6 +159,7 @@ If **connected** is *true*: - **netaddr** (array of strings): A single entry array: - address, e.g. 1.2.3.4:1234 - **features** (hex): bitmap of BOLT #9 features from peer's INIT message + - **remote_addr** (string, optional): The public IPv4/6 address the peer sees us from, e.g. 1.2.3.4:1234 [comment]: # (GENERATE-FROM-SCHEMA-END) @@ -380,4 +381,4 @@ Main web site: Lightning RFC site (BOLT \#9): -[comment]: # ( SHA256STAMP:4f76b5ac19d3dfdaf3d04f5dd5de2312a2ab0ccffe779508c416aaf6e644612a) +[comment]: # ( SHA256STAMP:e6829e8ced923131b95bcfa4f366dd04286fe85485039e9ebc89e79899937df6) diff --git a/doc/schemas/listpeers.schema.json b/doc/schemas/listpeers.schema.json index f50cef596..f9ac1cad2 100644 --- a/doc/schemas/listpeers.schema.json +++ b/doc/schemas/listpeers.schema.json @@ -1114,6 +1114,10 @@ "description": "address, e.g. 1.2.3.4:1234" } }, + "remote_addr": { + "type": "string", + "description": "The public IPv4/6 address the peer sees us from, e.g. 1.2.3.4:1234" + }, "features": { "type": "hex", "description": "bitmap of BOLT #9 features from peer's INIT message" diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 5a49f7bce..6dd8eb3fa 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -97,6 +97,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid, peer->uncommitted_channel = NULL; peer->addr = *addr; peer->connected_incoming = connected_incoming; + peer->remote_addr = NULL; peer->their_features = NULL; list_head_init(&peer->channels); peer->direction = node_id_idx(&peer->ld->id, &peer->id); @@ -1154,6 +1155,9 @@ void peer_connected(struct lightningd *ld, const u8 *msg) /* Update peer address and direction */ peer->addr = hook_payload->addr; peer->connected_incoming = hook_payload->incoming; + if (peer->remote_addr) + tal_free(peer->remote_addr); + peer->remote_addr = NULL; peer_update_features(peer, their_features); tal_steal(peer, hook_payload); @@ -1172,6 +1176,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg) if (hook_payload->remote_addr) { log_peer_info(ld->log, &id, "Peer says it sees our address as: %s", fmt_wireaddr(tmpctx, hook_payload->remote_addr)); + peer->remote_addr = tal_dup(peer, struct wireaddr, + hook_payload->remote_addr); /* Currently only from peers we have a channel with, until we * do stuff like probing for remote_addr to a random node. */ if (!list_empty(&peer->channels)) @@ -1671,6 +1677,10 @@ static void json_add_peer(struct lightningd *ld, struct wireaddr_internal, &p->addr)); json_array_end(response); + /* If peer reports our IP remote_addr, add that here */ + if (p->remote_addr) + json_add_string(response, "remote_addr", + fmt_wireaddr(response, p->remote_addr)); json_add_hex_talarr(response, "features", p->their_features); } diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 77037e17d..3f69729b1 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -40,6 +40,9 @@ struct peer { struct wireaddr_internal addr; bool connected_incoming; + /* They send what they see as our address as remote_addr */ + struct wireaddr *remote_addr; + /* We keep a copy of their feature bits */ const u8 *their_features; diff --git a/tests/test_connection.py b/tests/test_connection.py index 9de27ae8a..308ca2805 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -86,7 +86,9 @@ def test_remote_addr(node_factory, bitcoind): l2.start() l2.rpc.connect(l1.info['id'], 'localhost', l1.port) - l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}") + logmsg = l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}") + # check 'listpeers' contains the 'remote_addr' as logged + assert logmsg.endswith(l2.rpc.listpeers()['peers'][0]['remote_addr']) # Fund first channel so initial node_announcement is send # and also check no addresses have been announced yet