Skip to content

Commit aacd2d5

Browse files
committed
peer_exchange: set remote_addr on incoming IP connections
Changelog-Added: Protocol: set remote_addr on init tlvs
1 parent a03abd0 commit aacd2d5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

connectd/peer_exchange_initmsg.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ struct remote_addr_any {
5353
} u;
5454
}__attribute__((packed));
5555

56+
struct remote_addr_ipv4 {
57+
u8 type; /* ipv4:1 */
58+
u8 addr[4];
59+
u16 port;
60+
}__attribute__((packed));
61+
62+
struct remote_addr_ipv6 {
63+
u8 type; /* ipv6:2 */
64+
u8 addr[16];
65+
u16 port;
66+
}__attribute__((packed));
67+
5668
static struct io_plan *peer_init_received(struct io_conn *conn,
5769
struct peer *peer)
5870
{
@@ -182,6 +194,8 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
182194
struct peer *peer = tal(conn, struct peer);
183195
struct io_plan *(*next)(struct io_conn *, struct peer *);
184196
struct tlv_init_tlvs *tlvs;
197+
struct remote_addr_ipv4 *remote_addr_ipv4;
198+
struct remote_addr_ipv6 *remote_addr_ipv6;
185199

186200
peer->daemon = daemon;
187201
peer->id = *id;
@@ -202,6 +216,33 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
202216
tlvs->networks = tal_dup_arr(tlvs, struct bitcoin_blkid,
203217
&chainparams->genesis_blockhash, 1, 0);
204218

219+
/* BOLT #1:
220+
* The sending node:
221+
* ...
222+
* - SHOULD set `remote_addr` to reflect the remote IP address (and port) of an
223+
* incoming connection, if the node is the receiver and the connection was done
224+
* via IP. IP addresses within private networks (RFC-1918) MUST not be used.
225+
* IP addresses that are within private networks (RFC-1918) MUST not be used.
226+
*/
227+
if (incoming && addr->itype == ADDR_INTERNAL_WIREADDR) {
228+
if (addr->u.wireaddr.type == ADDR_TYPE_IPV4) {
229+
remote_addr_ipv4 = tal(tlvs, struct remote_addr_ipv4);
230+
remote_addr_ipv4->type = 1;
231+
remote_addr_ipv4->port = addr->u.wireaddr.port;
232+
memcpy(&remote_addr_ipv4->addr,
233+
addr->u.wireaddr.addr, 4);
234+
tlvs->remote_addr = (u8*)remote_addr_ipv4;
235+
}
236+
if (addr->u.wireaddr.type == ADDR_TYPE_IPV6) {
237+
remote_addr_ipv6 = tal(tlvs, struct remote_addr_ipv6);
238+
remote_addr_ipv6->type = 2;
239+
remote_addr_ipv6->port = addr->u.wireaddr.port;
240+
memcpy(&remote_addr_ipv6->addr,
241+
addr->u.wireaddr.addr, 16);
242+
tlvs->remote_addr = (u8*)remote_addr_ipv6;
243+
}
244+
}
245+
205246
/* Initially, there were two sets of feature bits: global and local.
206247
* Local affected peer nodes only, global affected everyone. Both were
207248
* sent in the `init` message, but node_announcement only advertized

0 commit comments

Comments
 (0)