Browse Source

Tidy up parse_wireaddr_from_hostname

travis-debug
darosior 6 years ago
committed by Rusty Russell
parent
commit
a4204226b4
  1. 6
      common/test/run-ip_port_parsing.c
  2. 63
      common/wireaddr.c
  3. 12
      common/wireaddr.h
  4. 19
      connectd/connectd.c

6
common/test/run-ip_port_parsing.c

@ -40,7 +40,6 @@ int main(void)
setup_locale(); setup_locale();
struct wireaddr addr; struct wireaddr addr;
struct wireaddr *addresses = tal_arr(NULL, struct wireaddr, 0);
char *ip; char *ip;
u16 port; u16 port;
@ -117,9 +116,8 @@ int main(void)
assert(parse_wireaddr("odpzvneidqdf5hdq.onion", &addr, 1, false, NULL)); assert(parse_wireaddr("odpzvneidqdf5hdq.onion", &addr, 1, false, NULL));
assert(addr.port == 1); assert(addr.port == 1);
assert(wireaddr_from_hostname(&addresses, "odpzvneidqdf5hdq.onion", 1, NULL, NULL, NULL)); assert(tal_count(wireaddr_from_hostname(tmpctx, "odpzvneidqdf5hdq.onion", 1, NULL, NULL, NULL)) > 0);
assert(! wireaddr_from_hostname(&addresses, "aaa.onion", 1, NULL, NULL, NULL)); assert(wireaddr_from_hostname(tmpctx, "aaa.onion", 1, NULL, NULL, NULL) == NULL);
tal_free(addresses);
tal_free(tmpctx); tal_free(tmpctx);
return 0; return 0;

63
common/wireaddr.c

@ -289,18 +289,21 @@ static bool separate_address_and_port(const tal_t *ctx, const char *arg,
return true; return true;
} }
bool wireaddr_from_hostname(struct wireaddr **addrs, const char *hostname, struct wireaddr *
const u16 port, bool *no_dns, wireaddr_from_hostname(const tal_t *ctx,
struct sockaddr *broken_reply, const char *hostname,
const char **err_msg) const u16 port, bool *no_dns,
struct sockaddr *broken_reply,
const char **err_msg)
{ {
struct wireaddr *addrs;
struct sockaddr_in6 *sa6; struct sockaddr_in6 *sa6;
struct sockaddr_in *sa4; struct sockaddr_in *sa4;
struct addrinfo *addrinfo, *addrinfos; struct addrinfo *addrinfo, *addrinfos;
struct addrinfo hints; struct addrinfo hints;
int gai_err; int gai_err;
bool res = false;
addrs = tal_arr(ctx, struct wireaddr, 0);
if (no_dns) if (no_dns)
*no_dns = false; *no_dns = false;
@ -308,22 +311,21 @@ bool wireaddr_from_hostname(struct wireaddr **addrs, const char *hostname,
if (strends(hostname, ".onion")) { if (strends(hostname, ".onion")) {
u8 *dec = b32_decode(tmpctx, hostname, u8 *dec = b32_decode(tmpctx, hostname,
strlen(hostname) - strlen(".onion")); strlen(hostname) - strlen(".onion"));
if (tal_count(*addrs) == 0) tal_resize(&addrs, 1);
tal_resize(addrs, 1); if (tal_count(dec) == TOR_V2_ADDRLEN) {
if (tal_count(dec) == TOR_V2_ADDRLEN) addrs[0].type = ADDR_TYPE_TOR_V2;
(*addrs)[0].type = ADDR_TYPE_TOR_V2; } else if (tal_count(dec) == TOR_V3_ADDRLEN) {
else if (tal_count(dec) == TOR_V3_ADDRLEN) addrs[0].type = ADDR_TYPE_TOR_V3;
(*addrs)[0].type = ADDR_TYPE_TOR_V3; } else {
else {
if (err_msg) if (err_msg)
*err_msg = "Invalid Tor address"; *err_msg = "Invalid Tor address";
return false; return tal_free(addrs);
} }
(*addrs)[0].addrlen = tal_count(dec); addrs[0].addrlen = tal_count(dec);
(*addrs)[0].port = port; addrs[0].port = port;
memcpy((*addrs)[0].addr, dec, tal_count(dec)); memcpy(addrs[0].addr, dec, tal_count(dec));
return true; return addrs;
} }
/* Tell them we wanted DNS and fail. */ /* Tell them we wanted DNS and fail. */
@ -331,7 +333,7 @@ bool wireaddr_from_hostname(struct wireaddr **addrs, const char *hostname,
if (err_msg) if (err_msg)
*err_msg = "Needed DNS, but lookups suppressed"; *err_msg = "Needed DNS, but lookups suppressed";
*no_dns = true; *no_dns = true;
return false; return tal_free(addrs);
} }
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
@ -344,34 +346,32 @@ bool wireaddr_from_hostname(struct wireaddr **addrs, const char *hostname,
if (gai_err != 0) { if (gai_err != 0) {
if (err_msg) if (err_msg)
*err_msg = gai_strerror(gai_err); *err_msg = gai_strerror(gai_err);
return false; return tal_free(addrs);
} }
if (broken_reply != NULL && memeq(addrinfos->ai_addr, addrinfos->ai_addrlen, broken_reply, tal_count(broken_reply))) { if (broken_reply != NULL && memeq(addrinfos->ai_addr, addrinfos->ai_addrlen, broken_reply, tal_count(broken_reply)))
res = false;
goto cleanup; goto cleanup;
}
for (addrinfo = addrinfos; addrinfo; addrinfo = addrinfo->ai_next) { for (addrinfo = addrinfos; addrinfo; addrinfo = addrinfo->ai_next) {
struct wireaddr addr; struct wireaddr addr;
if (addrinfo->ai_family == AF_INET) { if (addrinfo->ai_family == AF_INET) {
sa4 = (struct sockaddr_in *) addrinfo->ai_addr; sa4 = (struct sockaddr_in *) addrinfo->ai_addr;
wireaddr_from_ipv4(&addr, &sa4->sin_addr, port); wireaddr_from_ipv4(&addr, &sa4->sin_addr, port);
res = true;
} else if (addrinfo->ai_family == AF_INET6) { } else if (addrinfo->ai_family == AF_INET6) {
sa6 = (struct sockaddr_in6 *) addrinfo->ai_addr; sa6 = (struct sockaddr_in6 *) addrinfo->ai_addr;
wireaddr_from_ipv6(&addr, &sa6->sin6_addr, port); wireaddr_from_ipv6(&addr, &sa6->sin6_addr, port);
res = true;
} else } else
/* Ignore any other address types. */ /* Ignore any other address types. */
continue; continue;
tal_arr_expand(addrs, addr); tal_arr_expand(&addrs, addr);
} }
cleanup: cleanup:
/* Clean up */ /* Clean up */
freeaddrinfo(addrinfos); freeaddrinfo(addrinfos);
return res; if (tal_count(addrs))
return addrs;
return tal_free(addrs);
} }
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport, bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport,
@ -408,10 +408,13 @@ bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport,
/* Resolve with getaddrinfo */ /* Resolve with getaddrinfo */
if (!res) { if (!res) {
struct wireaddr *addresses = tal_arr(NULL, struct wireaddr, 0); struct wireaddr *addresses = wireaddr_from_hostname(NULL, ip, port,
res = wireaddr_from_hostname(&addresses, ip, port, no_dns, NULL, err_msg); no_dns, NULL, err_msg);
*addr = addresses[0]; if (addresses) {
tal_free(addresses); *addr = addresses[0];
tal_free(addresses);
res = true;
}
} }
finish: finish:

12
common/wireaddr.h

@ -84,12 +84,14 @@ bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 port,
char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a); char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a);
char *fmt_wireaddr_without_port(const tal_t *ctx, const struct wireaddr *a); char *fmt_wireaddr_without_port(const tal_t *ctx, const struct wireaddr *a);
/* If no_dns is non-NULL, we will set it to true and return false if /* If no_dns is non-NULL, we will set it to true and return NULL if
* we wanted to do a DNS lookup. */ * we wanted to do a DNS lookup. */
bool wireaddr_from_hostname(struct wireaddr **addrs, const char *hostname, struct wireaddr *
const u16 port, bool *no_dns, wireaddr_from_hostname(const tal_t *ctx,
struct sockaddr *broken_reply, const char *hostname,
const char **err_msg); const u16 port, bool *no_dns,
struct sockaddr *broken_reply,
const char **err_msg);
void wireaddr_from_ipv4(struct wireaddr *addr, void wireaddr_from_ipv4(struct wireaddr *addr,
const struct in_addr *ip4, const struct in_addr *ip4,

19
connectd/connectd.c

@ -1270,27 +1270,24 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
struct sockaddr *broken_reply) struct sockaddr *broken_reply)
{ {
struct wireaddr *new_addrs; struct wireaddr *new_addrs;
const char **hostnames; const char **hostnames = seednames(tmpctx, id);
new_addrs = tal_arr(tmpctx, struct wireaddr, 0);
hostnames = seednames(tmpctx, id);
for (size_t i = 0; i < tal_count(hostnames); i++) { for (size_t i = 0; i < tal_count(hostnames); i++) {
status_debug("Resolving %s", hostnames[i]); status_debug("Resolving %s", hostnames[i]);
if (!wireaddr_from_hostname(&new_addrs, hostnames[i], DEFAULT_PORT, NULL, new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], DEFAULT_PORT,
broken_reply, NULL)) { NULL, broken_reply, NULL);
status_debug("Could not resolve %s", hostnames[i]); if (new_addrs) {
} else {
for (size_t i = 0; i < tal_count(new_addrs); i++) { for (size_t i = 0; i < tal_count(new_addrs); i++) {
struct wireaddr_internal a; struct wireaddr_internal a;
a.itype = ADDR_INTERNAL_WIREADDR; a.itype = ADDR_INTERNAL_WIREADDR;
a.u.wireaddr = new_addrs[i]; a.u.wireaddr = new_addrs[i];
status_debug("Resolved %s to %s", hostnames[i], status_debug("Resolved %s to %s", hostnames[i],
type_to_string(tmpctx, struct wireaddr, type_to_string(tmpctx, struct wireaddr,
&a.u.wireaddr)); &a.u.wireaddr));
tal_arr_expand(addrs, a); tal_arr_expand(addrs, a);
} }
} } else
status_debug("Could not resolve %s", hostnames[i]);
} }
} }

Loading…
Cancel
Save