wireaddr: fix ipv6 formatting with ports in fmt_wireaddr

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 <jb55@jb55.com>
This commit is contained in:
William Casarin
2017-12-18 02:25:16 -08:00
committed by Rusty Russell
parent d89eb32c85
commit ce1d709d44
3 changed files with 36 additions and 2 deletions

View File

@@ -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;
@@ -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);
@@ -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;
}