jsonrpc: adds optional remote_addr to listpeers

Changelog-Added: jsonrpc: adds optional `remote_addr` to listpeers
This commit is contained in:
Michael Schmoock 2022-06-01 13:18:55 +02:00 committed by Rusty Russell
parent a2b75b66ba
commit 475e4c9bd9
5 changed files with 22 additions and 2 deletions

View file

@ -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: <https://github.com/ElementsProject/lightning> Lightning
RFC site (BOLT \#9):
<https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md>
[comment]: # ( SHA256STAMP:4f76b5ac19d3dfdaf3d04f5dd5de2312a2ab0ccffe779508c416aaf6e644612a)
[comment]: # ( SHA256STAMP:e6829e8ced923131b95bcfa4f366dd04286fe85485039e9ebc89e79899937df6)

View file

@ -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"

View file

@ -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);
}

View file

@ -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;

View file

@ -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