mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
wireaddr: rework port parsing for weird addresses.
We save wireaddr to databases as a string (which is pretty dumb) but it turned out that my local node saved '[::ffff:127.0.0.1]:49150' which our parser can't parse. Thus I've reworked the parser to make fewer assumptions: parse_ip_port() is renamed to separate_address_and_port() and is now far more accepting of different forms, and returns failure only on grossly malformed strings. Otherwise it overwrites its *port arg only if there's a port specified. I also made it static. Then fromwire_wireaddr() hands the resulting address to inet_pton to figure out if it's actually valid. Cc: William Casarin <jb55@jb55.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
cc9ca82821
commit
eb0603bd13
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
|
||||
/* AUTOGENERATED MOCKS START */
|
||||
/* Generated stub for fromwire */
|
||||
@@ -31,36 +32,44 @@ int main(void)
|
||||
char *ip;
|
||||
u16 port;
|
||||
|
||||
/* Grossly invalid. */
|
||||
assert(!separate_address_and_port(ctx, "[", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "[123", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "[::1]:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:0", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:ff", &ip, &port));
|
||||
|
||||
/* ret = getaddrinfo("[::1]:80", NULL, NULL, &res); */
|
||||
assert(parse_ip_port(ctx, "[::1]:80", &ip, &port));
|
||||
assert(separate_address_and_port(ctx, "[::1]:80", &ip, &port));
|
||||
assert(streq(ip, "::1"));
|
||||
assert(port == 80);
|
||||
|
||||
assert(!parse_ip_port(ctx, "ip6-localhost", &ip, &port));
|
||||
port = 0;
|
||||
assert(separate_address_and_port(ctx, "ip6-localhost", &ip, &port));
|
||||
assert(streq(ip, "ip6-localhost"));
|
||||
assert(port == 0);
|
||||
|
||||
assert(!parse_ip_port(ctx, "::1", &ip, &port));
|
||||
assert(separate_address_and_port(ctx, "::1", &ip, &port));
|
||||
assert(streq(ip, "::1"));
|
||||
assert(port == 0);
|
||||
|
||||
assert(parse_ip_port(ctx, "192.168.1.1:8000", &ip, &port));
|
||||
assert(separate_address_and_port(ctx, "192.168.1.1:8000", &ip, &port));
|
||||
assert(streq(ip, "192.168.1.1"));
|
||||
assert(port == 8000);
|
||||
|
||||
assert(!parse_ip_port(ctx, "192.168.2.255", &ip, &port));
|
||||
port = 0;
|
||||
assert(separate_address_and_port(ctx, "192.168.2.255", &ip, &port));
|
||||
assert(streq(ip, "192.168.2.255"));
|
||||
assert(port == 0);
|
||||
|
||||
// unusual but possibly valid case
|
||||
assert(!parse_ip_port(ctx, "[::1]", &ip, &port));
|
||||
assert(separate_address_and_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);
|
||||
assert(!separate_address_and_port(ctx, "[::1]:http", &ip, &port));
|
||||
|
||||
// localhost hostnames for backward compat
|
||||
parse_wireaddr("localhost", &addr, 200);
|
||||
@@ -79,6 +88,8 @@ int main(void)
|
||||
ip = fmt_wireaddr(ctx, &addr);
|
||||
assert(streq(ip, "[2001:db8:85a3::8a2e:370:7334]:9777"));
|
||||
|
||||
assert(parse_wireaddr("[::ffff:127.0.0.1]:49150", &addr, 1));
|
||||
assert(addr.port == 49150);
|
||||
tal_free(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user