mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
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:
parent
d89eb32c85
commit
ce1d709d44
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user