Clean up network options.

It's become clear that our network options are insufficient, with the coming
addition of Tor and unix domain support.

Currently:

1. We always bind to local IPv4 and IPv6 sockets, unless --port=0, --offline,
   or any address is specified explicitly.  If they're routable, we announce.
2. --addr is used to announce, but not to control binding.

After this change:

1. --port is deprecated.
2. --addr controls what we bind to and announce.
3. --bind-addr/--announce-addr can be used to control one and not the other.
4. Unless --autolisten=0, we add local IPv4 & IPv6 port 9735 (and announce if they are routable).
5. --offline still overrides listening (though announcing is still the same).

This means we can bind to as many ports/interfaces as we want, and for
special effects we can announce different things (eg. we're sitting
behind a port forward or a proxy).

What remains to implement is semi-automatic binding: we should be able
to say '--addr=0.0.0.0:9999' and have the address resolve at bind
time, or even '--addr=0.0.0.0:0' and have the port autoresolve too
(you could determine what it was from 'lightning-cli getinfo'.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-05-07 13:58:12 +09:30
committed by Christian Decker
parent 00537fde43
commit fe96fe10c7
13 changed files with 228 additions and 109 deletions

View File

@@ -102,11 +102,15 @@ static char *opt_set_u32(const char *arg, u32 *u)
return NULL;
}
static char *opt_set_u16(const char *arg, u16 *u)
static char *opt_set_port(const char *arg, struct lightningd *ld)
{
char *endp;
unsigned long l;
log_broken(ld->log, "--port has been deprecated, use --autolisten=0 or --addr=:<port>");
if (!deprecated_apis)
return "--port is deprecated";
assert(arg != NULL);
/* This is how the manpage says to do it. Yech. */
@@ -114,9 +118,13 @@ static char *opt_set_u16(const char *arg, u16 *u)
l = strtoul(arg, &endp, 0);
if (*endp || !arg[0])
return tal_fmt(NULL, "'%s' is not a number", arg);
*u = l;
if (errno || *u != l)
ld->portnum = l;
if (errno || ld->portnum != l)
return tal_fmt(NULL, "'%s' is out of range", arg);
if (ld->portnum == 0)
ld->autolisten = false;
return NULL;
}
@@ -138,7 +146,9 @@ static char *opt_set_s32(const char *arg, s32 *u)
return NULL;
}
static char *opt_add_addr(const char *arg, struct lightningd *ld)
static char *opt_add_addr_withtype(const char *arg,
struct lightningd *ld,
enum addr_listen_announce ala)
{
size_t n = tal_count(ld->wireaddrs);
char const *err_msg;
@@ -146,6 +156,8 @@ static char *opt_add_addr(const char *arg, struct lightningd *ld)
assert(arg != NULL);
tal_resize(&ld->wireaddrs, n+1);
tal_resize(&ld->listen_announce, n+1);
ld->listen_announce[n] = ala;
if (!parse_wireaddr(arg, &ld->wireaddrs[n], ld->portnum, &err_msg)) {
return tal_fmt(NULL, "Unable to parse IP address '%s': %s", arg, err_msg);
@@ -155,6 +167,21 @@ static char *opt_add_addr(const char *arg, struct lightningd *ld)
}
static char *opt_add_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_LISTEN_AND_ANNOUNCE);
}
static char *opt_add_bind_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_LISTEN);
}
static char *opt_add_announce_addr(const char *arg, struct lightningd *ld)
{
return opt_add_addr_withtype(arg, ld, ADDR_ANNOUNCE);
}
static char *opt_add_ipaddr(const char *arg, struct lightningd *ld)
{
log_broken(ld->log, "--ipaddr has been deprecated, use --addr");
@@ -177,11 +204,6 @@ static void opt_show_s32(char buf[OPT_SHOW_LEN], const s32 *u)
snprintf(buf, OPT_SHOW_LEN, "%"PRIi32, *u);
}
static void opt_show_u16(char buf[OPT_SHOW_LEN], const u16 *u)
{
snprintf(buf, OPT_SHOW_LEN, "%u", *u);
}
static char *opt_set_network(const char *arg, struct lightningd *ld)
{
assert(arg != NULL);
@@ -339,9 +361,19 @@ static void config_register_opts(struct lightningd *ld)
ld, opt_hidden);
opt_register_arg("--addr", opt_add_addr, NULL,
ld,
"Set the IP address (v4 or v6) to listen on and announce to the network for incoming connections");
"Set an IP address (v4 or v6) to listen on and announce to the network for incoming connections");
opt_register_arg("--bind-addr", opt_add_bind_addr, NULL,
ld,
"Set an IP address (v4 or v6) to listen on, but not announce");
opt_register_arg("--announce-addr", opt_add_announce_addr, NULL,
ld,
"Set an IP address (v4 or v6) to announce, but not listen on");
opt_register_noarg("--offline", opt_set_offline, ld,
"Start in offline-mode (do not automatically reconnect and do not accept incoming connections)");
opt_register_arg("--autolisten", opt_set_bool_arg, opt_show_bool,
&ld->autolisten,
"If true, listen on default port and announce if it seems to be a public interface");
opt_register_early_arg("--network", opt_set_network, opt_show_network,
ld,
@@ -649,8 +681,8 @@ void register_opts(struct lightningd *ld)
/* --port needs to be an early arg to force it being parsed
* before --ipaddr which may depend on it */
opt_register_early_arg("--port", opt_set_u16, opt_show_u16, &ld->portnum,
"Port to bind to (0 means don't listen)");
opt_register_early_arg("--port", opt_set_port, NULL, ld,
opt_hidden);
opt_register_arg("--bitcoin-datadir", opt_set_talstr, NULL,
&ld->topology->bitcoind->datadir,
"-datadir arg for bitcoin-cli");
@@ -778,11 +810,11 @@ void handle_opts(struct lightningd *ld, int argc, char *argv[])
check_config(ld);
if (ld->portnum && ld->listen && tal_count(ld->wireaddrs) == 0)
if (ld->listen && ld->autolisten)
guess_addresses(ld);
else
log_debug(ld->log, "Not guessing addresses: %s",
(ld->portnum && ld->listen) ? "manually set" : "port set to zero");
!ld->listen ? "--offline" : "--autolisten=0");
}
/* FIXME: This is a hack! Expose somehow in ccan/opt.*/
@@ -801,6 +833,21 @@ static const char *next_name(const char *names, unsigned *len)
return first_name(names + 1, len);
}
static void json_add_opt_addrs(struct json_result *response,
const char *name0,
const struct wireaddr *wireaddrs,
const enum addr_listen_announce *listen_announce,
enum addr_listen_announce ala)
{
for (size_t i = 0; i < tal_count(wireaddrs); i++) {
if (listen_announce[i] != ala)
continue;
json_add_string(response,
name0,
fmt_wireaddr(name0, wireaddrs+i));
}
}
static void add_config(struct lightningd *ld,
struct json_result *response,
const struct opt_table *opt,
@@ -870,16 +917,25 @@ static void add_config(struct lightningd *ld,
topo->override_fee_rate[0],
topo->override_fee_rate[1],
topo->override_fee_rate[2]);
} else if (opt->cb_arg == (void *)opt_set_port) {
if (!deprecated_apis)
answer = tal_fmt(name0, "%u", ld->portnum);
} else if (opt->cb_arg == (void *)opt_add_ipaddr) {
/* Covered by opt_add_addr below */
} else if (opt->cb_arg == (void *)opt_add_addr) {
/* This is a bit weird, we can have multiple args */
for (size_t i = 0; i < tal_count(ld->wireaddrs); i++) {
json_add_string(response,
name0,
fmt_wireaddr(name0,
ld->wireaddrs+i));
}
json_add_opt_addrs(response, name0,
ld->wireaddrs, ld->listen_announce,
ADDR_LISTEN_AND_ANNOUNCE);
return;
} else if (opt->cb_arg == (void *)opt_add_bind_addr) {
json_add_opt_addrs(response, name0,
ld->wireaddrs, ld->listen_announce,
ADDR_LISTEN);
return;
} else if (opt->cb_arg == (void *)opt_add_announce_addr) {
json_add_opt_addrs(response, name0,
ld->wireaddrs, ld->listen_announce,
ADDR_ANNOUNCE);
return;
#if DEVELOPER
} else if (strstarts(name, "dev-")) {