From c0766061fa4781eb0d0bbd7e8048fdf81a2d3a7d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:49 +1030 Subject: [PATCH] dns: don't allow non-stream sockets. It was "connecting" to "localhost 30000" successfully, because it was using UDP. Oops. This reveals another issue: init_conn() uses the wrong address to connect to, as we'd already incremented the pointer in try_connect_one(). Signed-off-by: Rusty Russell --- daemon/dns.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/daemon/dns.c b/daemon/dns.c index 77e4760b4..3bfeb2629 100644 --- a/daemon/dns.c +++ b/daemon/dns.c @@ -34,8 +34,13 @@ static void lookup_and_write(int fd, const char *name, const char *port) struct addrinfo *addr, *i; struct netaddr *addresses; size_t num; + struct addrinfo hints; - if (getaddrinfo(name, port, NULL, &addr) != 0) + /* We don't want UDP sockets (yet?) */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + if (getaddrinfo(name, port, &hints, &addr) != 0) return; num = 0; @@ -87,6 +92,11 @@ static struct io_plan *init_conn(struct io_conn *conn, struct dns_async *d) struct addrinfo a; netaddr_to_addrinfo(&a, &d->addresses[0]); + + /* Consume that address. */ + d->addresses++; + d->num_addresses--; + io_set_finish(conn, connect_failed, d); /* That new connection owns d */ @@ -101,10 +111,6 @@ static void try_connect_one(struct dns_async *d) while (d->num_addresses) { const struct netaddr *a = &d->addresses[0]; - /* Consume that address. */ - d->addresses++; - d->num_addresses--; - /* Now we can warn if it's overlength */ if (a->addrlen > sizeof(a->saddr)) { log_broken(d->dstate->base_log, @@ -119,6 +125,10 @@ static void try_connect_one(struct dns_async *d) return; } } + + /* Consume that address. */ + d->addresses++; + d->num_addresses--; } /* We're out of things to try. Fail. */