common/wireaddr.h: simplify parse_wireaddr API.

1. Make it the standard "return the error" pattern.
2. Rather than flags to indicate what types are allowed, have the callers
   check the return explicitly.
3. Document the APIs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-05-30 13:58:18 +09:30
parent ddb79162ab
commit d40379885d
11 changed files with 223 additions and 188 deletions

View File

@@ -196,37 +196,36 @@ int main(int argc, char *argv[])
assert(!separate_address_and_port(tmpctx, "[::1]:http", &ip, &port));
// localhost hostnames for backward compat
assert(parse_wireaddr("localhost", &addr, 200, false, NULL));
assert(parse_wireaddr(tmpctx, "localhost", 200, false, &addr) == NULL);
assert(addr.port == 200);
// string should win the port battle
assert(parse_wireaddr("[::1]:9735", &addr, 500, false, NULL));
assert(parse_wireaddr(tmpctx, "[::1]:9735", 500, false, &addr) == NULL);
assert(addr.port == 9735);
ip = fmt_wireaddr(tmpctx, &addr);
assert(streq(ip, "[::1]:9735"));
// should use argument if we have no port in string
assert(parse_wireaddr("2001:db8:85a3::8a2e:370:7334", &addr, 9777, false, NULL));
assert(parse_wireaddr(tmpctx, "2001:db8:85a3::8a2e:370:7334", 9777, false, &addr) == NULL);
assert(addr.port == 9777);
ip = fmt_wireaddr(tmpctx, &addr);
assert(streq(ip, "[2001:db8:85a3::8a2e:370:7334]:9777"));
assert(parse_wireaddr("[::ffff:127.0.0.1]:49150", &addr, 1, false, NULL));
assert(parse_wireaddr(tmpctx, "[::ffff:127.0.0.1]:49150", 1, false, &addr) == NULL);
assert(addr.port == 49150);
assert(parse_wireaddr("4ruvswpqec5i2gogopxl4vm5bruzknbvbylov2awbo4rxiq4cimdldad.onion:49150", &addr, 1, false, NULL));
assert(parse_wireaddr(tmpctx, "4ruvswpqec5i2gogopxl4vm5bruzknbvbylov2awbo4rxiq4cimdldad.onion:49150", 1, false, &addr) == NULL);
assert(addr.port == 49150);
assert(parse_wireaddr("4ruvswpqec5i2gogopxl4vm5bruzknbvbylov2awbo4rxiq4cimdldad.onion", &addr, 1, false, NULL));
assert(parse_wireaddr(tmpctx, "4ruvswpqec5i2gogopxl4vm5bruzknbvbylov2awbo4rxiq4cimdldad.onion", 1, false, &addr) == NULL);
assert(addr.port == 1);
/* We don't accept torv2 any more */
assert(!parse_wireaddr("odpzvneidqdf5hdq.onion:49150", &addr, 1, false, NULL));
assert(!parse_wireaddr("odpzvneidqdf5hdq.onion", &addr, 1, false, NULL));
assert(parse_wireaddr(tmpctx, "odpzvneidqdf5hdq.onion:49150", 1, false, &addr) != NULL);
assert(parse_wireaddr(tmpctx, "odpzvneidqdf5hdq.onion", 1, false, &addr) != NULL);
assert(!parse_wireaddr_internal("odpzvneidqdf5hdq.onion", &addr_int, 1,
false, false, false, NULL));
assert(parse_wireaddr_internal(tmpctx, "odpzvneidqdf5hdq.onion", 1, false, &addr_int) != NULL);
assert(wireaddr_from_hostname(tmpctx, "odpzvneidqdf5hdq.onion", 1, NULL, NULL, NULL) == NULL);
assert(wireaddr_from_hostname(tmpctx, "aaa.onion", 1, NULL, NULL, NULL) == NULL);