Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipdiscovery: adds custom port parameter #5842

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/lightning-listconfigs.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ On success, an object is returned, containing:
- **disable-dns** (boolean, optional): `true` if `disable-dns` was set in config or cmdline
- **disable-ip-discovery** (boolean, optional): `true` if `disable-ip-discovery` was set in config or cmdline **deprecated, removal in v23.11**
- **announce-addr-discovered** (string, optional): `true`/`false`/`auto` depending on how `announce-addr-discovered` was set in config or cmdline *(added v23.02)*
- **announce-addr-discovered-port** (integer, optional): Sets the announced TCP port for dynamically discovered IPs. *(added v23.02)*
- **encrypted-hsm** (boolean, optional): `true` if `encrypted-hsm` was set in config or cmdline
- **rpc-file-mode** (string, optional): `rpc-file-mode` field from config or cmdline, or default
- **log-level** (string, optional): `log-level` field from config or cmdline, or default
Expand Down Expand Up @@ -221,4 +222,4 @@ RESOURCES

Main web site: <https://github.com/ElementsProject/lightning>

[comment]: # ( SHA256STAMP:9953b3545acb82bed816b86a65ba51ff4b043d3848c4a3ae460aa68db1a4b542)
[comment]: # ( SHA256STAMP:2f325aa6ef0506dc627409e3253c8627c49a7d1749ea9dbf24a5baa0fc8c307c)
5 changes: 5 additions & 0 deletions doc/lightningd-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ use the RPC call lightning-setchannel(7).
Note: You also need to open TCP port 9735 on your router towords your node.
Note: Will always be disabled if you use 'always-use-proxy'.

* **announce-addr-discovered-port**
Sets the public TCP port to use for announcing dynamically discovered IPs.
If unset, this defaults to the selected networks lightning port,
which is 9735 on mainnet.

### Lightning channel and HTLC options

* **large-channels**
Expand Down
5 changes: 5 additions & 0 deletions doc/schemas/listconfigs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@
"description": "`true`/`false`/`auto` depending on how `announce-addr-discovered` was set in config or cmdline",
"added": "v23.02"
},
"announce-addr-discovered-port": {
"type": "integer",
"description": "Sets the announced TCP port for dynamically discovered IPs.",
"added": "v23.02"
},
"encrypted-hsm": {
"type": "boolean",
"description": "`true` if `encrypted-hsm` was set in config or cmdline"
Expand Down
3 changes: 3 additions & 0 deletions lightningd/lightningd.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ struct config {
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
enum opt_autobool ip_discovery;

/* Public TCP port assumed for IP discovery. Defaults to chainparams. */
u32 ip_discovery_port;

/* Minimal amount of effective funding_satoshis for accepting channels */
u64 min_capacity_sat;

Expand Down
12 changes: 12 additions & 0 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,9 @@ static const struct config testnet_config = {
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
.ip_discovery = OPT_AUTOBOOL_AUTO,

/* Public TCP port assumed for IP discovery. Defaults to chainparams. */
.ip_discovery_port = 0,

/* Sets min_effective_htlc_capacity - at 1000$/BTC this is 10ct */
.min_capacity_sat = 10000,

Expand Down Expand Up @@ -912,6 +915,9 @@ static const struct config mainnet_config = {
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
.ip_discovery = OPT_AUTOBOOL_AUTO,

/* Public TCP port assumed for IP discovery. Defaults to chainparams. */
.ip_discovery_port = 0,

/* Sets min_effective_htlc_capacity - at 1000$/BTC this is 10ct */
.min_capacity_sat = 10000,

Expand Down Expand Up @@ -1223,6 +1229,9 @@ static void register_opts(struct lightningd *ld)
opt_register_arg("--announce-addr-discovered", opt_set_autobool_arg, opt_show_autobool,
&ld->config.ip_discovery,
"Explicitly turns IP discovery 'on' or 'off'.");
opt_register_arg("--announce-addr-discovered-port", opt_set_uintval,
opt_show_uintval, &ld->config.ip_discovery_port,
"Sets the public TCP port to use for announcing discovered IPs.");

opt_register_noarg("--offline", opt_set_offline, ld,
"Start in offline-mode (do not automatically reconnect and do not accept incoming connections)");
Expand Down Expand Up @@ -1427,6 +1436,9 @@ void handle_early_opts(struct lightningd *ld, int argc, char *argv[])
else
ld->config = mainnet_config;

/* Set the ln_port given from chainparams */
ld->config.ip_discovery_port = chainparams->ln_port;

/* Now we can initialize wallet_dsn */
ld->wallet_dsn = tal_fmt(ld, "sqlite3://%s/lightningd.sqlite3",
ld->config_netdir);
Expand Down
11 changes: 2 additions & 9 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1305,18 +1305,11 @@ static void update_remote_addr(struct lightningd *ld,
const struct wireaddr *remote_addr,
const struct node_id peer_id)
{
u16 public_port;

/* failsafe to prevent privacy leakage. */
if (ld->always_use_proxy ||
ld->config.ip_discovery == OPT_AUTOBOOL_FALSE)
return;

/* Peers will have likey reported our dynamic outbound TCP port.
* Best guess is that we use default port for the selected network,
* until we add a commandline switch to override this. */
public_port = chainparams_get_ln_port(chainparams);

switch (remote_addr->type) {
case ADDR_TYPE_IPV4:
/* init pointers first time */
Expand All @@ -1334,7 +1327,7 @@ static void update_remote_addr(struct lightningd *ld,
if (wireaddr_eq_without_port(ld->remote_addr_v4, remote_addr)) {
ld->discovered_ip_v4 = tal_dup(ld, struct wireaddr,
ld->remote_addr_v4);
ld->discovered_ip_v4->port = public_port;
ld->discovered_ip_v4->port = ld->config.ip_discovery_port;
subd_send_msg(ld->gossip, towire_gossipd_discovered_ip(
tmpctx,
ld->discovered_ip_v4));
Expand All @@ -1357,7 +1350,7 @@ static void update_remote_addr(struct lightningd *ld,
if (wireaddr_eq_without_port(ld->remote_addr_v6, remote_addr)) {
ld->discovered_ip_v6 = tal_dup(ld, struct wireaddr,
ld->remote_addr_v6);
ld->discovered_ip_v6->port = public_port;
ld->discovered_ip_v6->port = ld->config.ip_discovery_port;
subd_send_msg(ld->gossip, towire_gossipd_discovered_ip(
tmpctx,
ld->discovered_ip_v6));
Expand Down
52 changes: 52 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,58 @@ def test_remote_addr_disabled(node_factory, bitcoind):
assert not l2.daemon.is_in_log("Update our node_announcement for discovered address")


@pytest.mark.developer("needs DEVELOPER=1 for fast gossip and --dev-allow-localhost for local remote_addr")
def test_remote_addr_port(node_factory, bitcoind):
"""Check address discovery (BOLT1 #917) can be done with non-default TCP ports
We perform logic tests on L2, setup same as above:
l1 --> [l2] <-- l3
"""
port = 1234
opts = {'dev-allow-localhost': None,
'may_reconnect': True,
'dev-no-reconnect': None,
'announce-addr-discovered-port': port}
l1, l2, l3 = node_factory.get_nodes(3, opts=[opts, opts, opts])

# Disable announcing local autobind addresses with dev-allow-localhost.
# We need to have l2 opts 'bind-addr' to the (generated) value of 'addr'.
# So we stop, set 'bind-addr' option, delete 'addr' and restart first.
l2.stop()
l2.daemon.opts['bind-addr'] = l2.daemon.opts['addr']
del l2.daemon.opts['addr']
l2.start()
assert len(l2.rpc.getinfo()['address']) == 0

# l1->l2
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}")
l1.fundchannel(l2)
bitcoind.generate_block(5)
l1.daemon.wait_for_log(f"Received node_announcement for node {l2.info['id']}")
# l2->l3
l2.rpc.connect(l3.info['id'], 'localhost', l3.port)
l2.daemon.wait_for_log("Peer says it sees our address as: 127.0.0.1:[0-9]{5}")
l2.fundchannel(l3)
bitcoind.generate_block(5)

# restart both and wait for channels to be ready
l1.restart()
l2.rpc.connect(l1.info['id'], 'localhost', l1.port)
l2.daemon.wait_for_log("Already have funding locked in")
l3.restart()
l2.rpc.connect(l3.info['id'], 'localhost', l3.port)
l2.daemon.wait_for_log("Already have funding locked in")

# if ip discovery would have been enabled, we would have send an updated
# node_annoucement by now. Check we didn't...
assert l2.daemon.wait_for_log("Update our node_announcement for discovered address")
info = l2.rpc.getinfo()
assert len(info['address']) == 1
assert info['address'][0]['type'] == 'ipv4'
assert info['address'][0]['address'] == '127.0.0.1'
assert info['address'][0]['port'] == port


def test_connect_standard_addr(node_factory):
"""Test standard node@host:port address
"""
Expand Down