wireaddr: add ip[:port] parsing

* Add port parsing support to parse_wireaddr. This is in preparation for storing
addresses in the peers table. This also makes parse_wireaddr a proper inverse of
fmt_wireaddr.

* Move parse_wireaddr to common/wireaddr.c this seems like a better place for
it. I bring along parse_ip_port with it for convenience. This also fixes some
issues with the upcoming ip/port parsing tests.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2017-12-18 02:10:43 -08:00
committed by Rusty Russell
parent 90a5ba043c
commit 7ecccd50b9
5 changed files with 91 additions and 29 deletions

View File

@@ -296,31 +296,3 @@ void guess_addresses(struct lightningd *ld)
if (!guess_one_address(ld, &ld->wireaddrs[n], ld->portnum, ADDR_TYPE_IPV6))
tal_resize(&ld->wireaddrs, n);
}
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 port)
{
struct in6_addr v6;
struct in_addr v4;
/* FIXME: change arg to addr[:port] and use getaddrinfo? */
if (streq(arg, "localhost"))
arg = "127.0.0.1";
else if (streq(arg, "ip6-localhost"))
arg = "::1";
memset(&addr->addr, 0, sizeof(addr->addr));
if (inet_pton(AF_INET, arg, &v4) == 1) {
addr->type = ADDR_TYPE_IPV4;
addr->addrlen = 4;
addr->port = port;
memcpy(&addr->addr, &v4, addr->addrlen);
return true;
} else if (inet_pton(AF_INET6, arg, &v6) == 1) {
addr->type = ADDR_TYPE_IPV6;
addr->addrlen = 16;
addr->port = port;
memcpy(&addr->addr, &v6, addr->addrlen);
return true;
}
return false;
}

View File

@@ -7,6 +7,5 @@ struct lightningd;
void guess_addresses(struct lightningd *ld);
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 port);
#endif /* LIGHTNING_LIGHTNINGD_NETADDRESS_H */