Skip to content

Commit e1290b4

Browse files
m-schmoockniftynei
authored andcommitted
peer_control: getinfo show correct port on discovered IPs
Changelog-Fixed: peer_control: getinfo shows the correct port on discovered IPs
1 parent 9f4dba1 commit e1290b4

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

gossipd/gossipd.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,6 @@ static void handle_remote_addr(struct daemon *daemon, const u8 *msg)
350350
if (!fromwire_gossipd_remote_addr(msg, &remote_addr))
351351
master_badmsg(WIRE_GOSSIPD_REMOTE_ADDR, msg);
352352

353-
/* Best guess is that we use default port for the selected network */
354-
remote_addr.port = chainparams_get_ln_port(chainparams);
355-
356353
switch (remote_addr.type) {
357354
case ADDR_TYPE_IPV4:
358355
if (daemon->remote_addr_v4 != NULL &&

lightningd/lightningd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
209209

210210
ld->remote_addr_v4 = NULL;
211211
ld->remote_addr_v6 = NULL;
212+
ld->discovered_ip_v4 = NULL;
213+
ld->discovered_ip_v6 = NULL;
212214
ld->listen = true;
213215
ld->autolisten = true;
214216
ld->reconnect = true;

lightningd/lightningd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ struct lightningd {
158158
struct node_id remote_addr_v4_peer;
159159
struct node_id remote_addr_v6_peer;
160160

161+
/* verified discovered IPs to be used for anouncement */
162+
struct wireaddr *discovered_ip_v4;
163+
struct wireaddr *discovered_ip_v6;
164+
161165
/* Bearer of all my secrets. */
162166
int hsm_fd;
163167
struct subd *hsm;

lightningd/peer_control.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,10 +1274,17 @@ static void update_remote_addr(struct lightningd *ld,
12741274
const struct wireaddr *remote_addr,
12751275
const struct node_id peer_id)
12761276
{
1277+
u16 public_port;
1278+
12771279
/* failsafe to prevent privacy leakage. */
12781280
if (ld->always_use_proxy || ld->config.disable_ip_discovery)
12791281
return;
12801282

1283+
/* Peers will have likey reported our dynamic outbound TCP port.
1284+
* Best guess is that we use default port for the selected network,
1285+
* until we add a commandline switch to override this. */
1286+
public_port = chainparams_get_ln_port(chainparams);
1287+
12811288
switch (remote_addr->type) {
12821289
case ADDR_TYPE_IPV4:
12831290
/* init pointers first time */
@@ -1292,10 +1299,14 @@ static void update_remote_addr(struct lightningd *ld,
12921299
break;
12931300
}
12941301
/* tell gossip we have a valid update */
1295-
if (wireaddr_eq_without_port(ld->remote_addr_v4, remote_addr))
1302+
if (wireaddr_eq_without_port(ld->remote_addr_v4, remote_addr)) {
1303+
ld->discovered_ip_v4 = tal_dup(ld, struct wireaddr,
1304+
ld->remote_addr_v4);
1305+
ld->discovered_ip_v4->port = public_port;
12961306
subd_send_msg(ld->gossip, towire_gossipd_remote_addr(
12971307
tmpctx,
1298-
ld->remote_addr_v4));
1308+
ld->discovered_ip_v4));
1309+
}
12991310
/* store latest values */
13001311
*ld->remote_addr_v4 = *remote_addr;
13011312
ld->remote_addr_v4_peer = peer_id;
@@ -1311,10 +1322,14 @@ static void update_remote_addr(struct lightningd *ld,
13111322
*ld->remote_addr_v6 = *remote_addr;
13121323
break;
13131324
}
1314-
if (wireaddr_eq_without_port(ld->remote_addr_v6, remote_addr))
1325+
if (wireaddr_eq_without_port(ld->remote_addr_v6, remote_addr)) {
1326+
ld->discovered_ip_v6 = tal_dup(ld, struct wireaddr,
1327+
ld->remote_addr_v6);
1328+
ld->discovered_ip_v6->port = public_port;
13151329
subd_send_msg(ld->gossip, towire_gossipd_remote_addr(
13161330
tmpctx,
1317-
ld->remote_addr_v6));
1331+
ld->discovered_ip_v6));
1332+
}
13181333
*ld->remote_addr_v6 = *remote_addr;
13191334
ld->remote_addr_v6_peer = peer_id;
13201335
break;
@@ -2247,22 +2262,22 @@ static struct command_result *json_getinfo(struct command *cmd,
22472262
for (size_t i = 0; i < count_announceable; i++)
22482263
json_add_address(response, NULL, cmd->ld->announceable+i);
22492264

2250-
/* Currently, IP discovery will only be announced by gossipd, if we
2251-
* don't already have usable addresses.
2265+
/* Currently, IP discovery will only be announced by gossipd,
2266+
* if we don't already have usable addresses.
22522267
* See `create_node_announcement` in `gossip_generation.c`. */
22532268
if (count_announceable == 0) {
2254-
if (cmd->ld->remote_addr_v4 != NULL &&
2269+
if (cmd->ld->discovered_ip_v4 != NULL &&
22552270
!wireaddr_arr_contains(
22562271
cmd->ld->announceable,
2257-
cmd->ld->remote_addr_v4))
2272+
cmd->ld->discovered_ip_v4))
22582273
json_add_address(response, NULL,
2259-
cmd->ld->remote_addr_v4);
2260-
if (cmd->ld->remote_addr_v6 != NULL &&
2274+
cmd->ld->discovered_ip_v4);
2275+
if (cmd->ld->discovered_ip_v6 != NULL &&
22612276
!wireaddr_arr_contains(
22622277
cmd->ld->announceable,
2263-
cmd->ld->remote_addr_v6))
2278+
cmd->ld->discovered_ip_v6))
22642279
json_add_address(response, NULL,
2265-
cmd->ld->remote_addr_v6);
2280+
cmd->ld->discovered_ip_v6);
22662281
}
22672282
json_array_end(response);
22682283

tests/test_connection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_remote_addr(node_factory, bitcoind):
8383
l2.daemon.opts['bind-addr'] = l2.daemon.opts['addr']
8484
del l2.daemon.opts['addr']
8585
l2.start()
86+
assert len(l2.rpc.getinfo()['address']) == 0
8687

8788
l2.rpc.connect(l1.info['id'], 'localhost', l1.port)
8889
logmsg = l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}")
@@ -95,6 +96,7 @@ def test_remote_addr(node_factory, bitcoind):
9596
bitcoind.generate_block(5)
9697
l1.daemon.wait_for_log(f"Received node_announcement for node {l2.info['id']}")
9798
assert(len(l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses']) == 0)
99+
assert len(l2.rpc.getinfo()['address']) == 0
98100

99101
def_port = default_ln_port(l2.info["network"])
100102

@@ -110,6 +112,7 @@ def test_remote_addr(node_factory, bitcoind):
110112
# Now l1 sees l2 but without announced addresses.
111113
assert(len(l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses']) == 0)
112114
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:{}".format(def_port))
115+
assert len(l2.rpc.getinfo()['address']) == 0
113116

114117
# connect second node. This will not yet trigger `node_annoucement` update,
115118
# as we again do not have a channel at the time we connected.
@@ -120,6 +123,7 @@ def test_remote_addr(node_factory, bitcoind):
120123
l2.fundchannel(l3, wait_for_active=True)
121124
bitcoind.generate_block(5)
122125
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address: 127.0.0.1:{}".format(def_port))
126+
assert len(l2.rpc.getinfo()['address']) == 0
123127

124128
# restart, reconnect and re-check for updated node_annoucement. This time
125129
# l2 sees that two different peers with channel reported the same `remote_addr`.
@@ -129,11 +133,19 @@ def test_remote_addr(node_factory, bitcoind):
129133
l2.daemon.wait_for_log("Update our node_announcement for discovered address: 127.0.0.1:{}".format(def_port))
130134
l1.daemon.wait_for_log(f"Received node_announcement for node {l2.info['id']}")
131135

136+
# check l1 sees the updated node announcement via CLI listnodes
132137
address = l1.rpc.listnodes(l2.info['id'])['nodes'][0]['addresses'][0]
133138
assert address['type'] == "ipv4"
134139
assert address['address'] == "127.0.0.1"
135140
assert address['port'] == def_port
136141

142+
# also check l2 returns the announced address (and port) via CLI getinfo
143+
getinfo = l2.rpc.getinfo()
144+
assert len(getinfo['address']) == 1
145+
assert getinfo['address'][0]['type'] == 'ipv4'
146+
assert getinfo['address'][0]['address'] == '127.0.0.1'
147+
assert getinfo['address'][0]['port'] == def_port
148+
137149

138150
@pytest.mark.developer("needs DEVELOPER=1 for fast gossip and --dev-allow-localhost for local remote_addr")
139151
def test_remote_addr_disabled(node_factory, bitcoind):

0 commit comments

Comments
 (0)