Skip to content

Commit

Permalink
wireaddr: fix ipv6 formatting with ports in fmt_wireaddr
Browse files Browse the repository at this point in the history
Correctly format ipv6 address with ports. This will also make it more compatible
with the new parse_wireaddr, which has been updated to parse ports. They are
inverses now.

Also add some tests that check this.

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 authored and rustyrussell committed Dec 21, 2017
1 parent d89eb32 commit ce1d709
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions common/test/run-ip_port_parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)

int main(void)
{
struct wireaddr addr;
tal_t *ctx = tal_tmpctx(NULL);
char *ip;
u16 port;
Expand All @@ -35,6 +36,10 @@ int main(void)
assert(streq(ip, "::1"));
assert(port == 80);

assert(!parse_ip_port(ctx, "ip6-localhost", &ip, &port));
assert(streq(ip, "ip6-localhost"));
assert(port == 0);

assert(!parse_ip_port(ctx, "::1", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);
Expand All @@ -47,6 +52,33 @@ int main(void)
assert(streq(ip, "192.168.2.255"));
assert(port == 0);

// unusual but possibly valid case
assert(!parse_ip_port(ctx, "[::1]", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);

// service names not supported yet
assert(!parse_ip_port(ctx, "[::1]:http", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);

// localhost hostnames for backward compat
parse_wireaddr("localhost", &addr, 200);
assert(addr.port == 200);

// string should win the port battle
parse_wireaddr("[::1]:9735", &addr, 500);
assert(addr.port == 9735);
ip = fmt_wireaddr(ctx, &addr);
assert(streq(ip, "[::1]:9735"));

// should use argument if we have no port in string
parse_wireaddr("2001:db8:85a3::8a2e:370:7334", &addr, 9777);
assert(addr.port == 9777);

ip = fmt_wireaddr(ctx, &addr);
assert(streq(ip, "[2001:db8:85a3::8a2e:370:7334]:9777"));

tal_free(ctx);
return 0;
}
4 changes: 2 additions & 2 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void towire_wireaddr(u8 **pptr, const struct wireaddr *addr)
towire_u16(pptr, addr->port);
}

static char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a)
char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a)
{
char addrstr[INET6_ADDRSTRLEN];
char *ret, *hex;
Expand All @@ -51,7 +51,7 @@ static char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a)
case ADDR_TYPE_IPV6:
if (!inet_ntop(AF_INET6, a->addr, addrstr, INET6_ADDRSTRLEN))
return "Unprintable-ipv6-address";
return tal_fmt(ctx, "%s:%u", addrstr, a->port);
return tal_fmt(ctx, "[%s]:%u", addrstr, a->port);
case ADDR_TYPE_PADDING:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr);
bool parse_ip_port(tal_t *ctx, const char *arg, char **ip, u16 *port);
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 port);

char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a);

#endif /* LIGHTNING_COMMON_WIREADDR_H */

0 comments on commit ce1d709

Please sign in to comment.