Browse Source

peer: don't ever set up listener on dynamic port.

Simplifies the logic somewhat.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
dec3b9d030
  1. 5
      daemon/lightningd.c
  2. 23
      daemon/peer.c
  3. 2
      daemon/peer.h

5
daemon/lightningd.c

@ -93,9 +93,8 @@ int main(int argc, char *argv[])
/* Create RPC socket (if any) */
setup_jsonrpc(dstate, dstate->rpc_filename);
/* Set up connections from peers. */
if (dstate->portnum != 0)
setup_listeners(dstate, dstate->portnum);
/* Set up connections from peers (if dstate->portnum is set) */
setup_listeners(dstate);
/* set up IRC peer discovery */
if (dstate->config.use_irc)

23
daemon/peer.c

@ -3079,26 +3079,28 @@ fail:
return -1;
}
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
void setup_listeners(struct lightningd_state *dstate)
{
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
socklen_t len;
int fd1, fd2;
if (!dstate->portnum)
return;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(portnum);
addr.sin_port = htons(dstate->portnum);
memset(&addr6, 0, sizeof(addr6));
addr6.sin6_family = AF_INET6;
addr6.sin6_addr = in6addr_any;
addr6.sin6_port = htons(portnum);
addr6.sin6_port = htons(dstate->portnum);
/* IPv6, since on Linux that (usually) binds to IPv4 too. */
fd1 = make_listen_fd(dstate, AF_INET6, portnum ? &addr6 : NULL,
sizeof(addr6));
fd1 = make_listen_fd(dstate, AF_INET6, &addr6, sizeof(addr6));
if (fd1 >= 0) {
struct sockaddr_in6 in6;
@ -3110,8 +3112,8 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
close_noerr(fd1);
} else {
addr.sin_port = in6.sin6_port;
dstate->portnum = ntohs(addr.sin_port);
log_info(dstate->base_log,
assert(dstate->portnum == ntohs(addr.sin_port));
log_debug(dstate->base_log,
"Creating IPv6 listener on port %u",
dstate->portnum);
io_new_listener(dstate, fd1, peer_connected_in, dstate);
@ -3119,8 +3121,7 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
}
/* Just in case, aim for the same port... */
fd2 = make_listen_fd(dstate, AF_INET,
addr.sin_port ? &addr : NULL, sizeof(addr));
fd2 = make_listen_fd(dstate, AF_INET, &addr, sizeof(addr));
if (fd2 >= 0) {
len = sizeof(addr);
if (getsockname(fd2, (void *)&addr, &len) != 0) {
@ -3129,8 +3130,8 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
strerror(errno));
close_noerr(fd2);
} else {
dstate->portnum = ntohs(addr.sin_port);
log_info(dstate->base_log,
assert(dstate->portnum == ntohs(addr.sin_port));
log_debug(dstate->base_log,
"Creating IPv4 listener on port %u",
dstate->portnum);
io_new_listener(dstate, fd2, peer_connected_in, dstate);

2
daemon/peer.h

@ -240,7 +240,7 @@ struct peer_address {
struct netaddr addr;
};
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum);
void setup_listeners(struct lightningd_state *dstate);
struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id);
struct peer *find_peer_by_pkhash(struct lightningd_state *dstate, const u8 *pkhash);

Loading…
Cancel
Save