diff --git a/common/wireaddr.c b/common/wireaddr.c index 28af08d80..d3a623d8c 100644 --- a/common/wireaddr.c +++ b/common/wireaddr.c @@ -413,3 +413,59 @@ bool wireaddr_to_sockname(const struct wireaddr_internal *addr, memcpy(sun->sun_path, addr->u.sockname, sizeof(addr->u.sockname)); return true; } + +struct addrinfo *wireaddr_internal_to_addrinfo(const tal_t *ctx, + const struct wireaddr_internal *wireaddr) +{ + struct addrinfo *ai = talz(ctx, struct addrinfo); + struct sockaddr_un *sun; + + ai->ai_socktype = SOCK_STREAM; + + switch (wireaddr->itype) { + case ADDR_INTERNAL_SOCKNAME: + sun = tal(ai, struct sockaddr_un); + wireaddr_to_sockname(wireaddr, sun); + ai->ai_family = sun->sun_family; + ai->ai_addrlen = sizeof(*sun); + ai->ai_addr = (struct sockaddr *)sun; + return ai; + case ADDR_INTERNAL_ALLPROTO: + break; + case ADDR_INTERNAL_WIREADDR: + return wireaddr_to_addrinfo(ctx, &wireaddr->u.wireaddr); + } + abort(); +} + +struct addrinfo *wireaddr_to_addrinfo(const tal_t *ctx, + const struct wireaddr *wireaddr) +{ + struct addrinfo *ai = talz(ctx, struct addrinfo); + struct sockaddr_in *sin; + struct sockaddr_in6 *sin6; + + ai->ai_socktype = SOCK_STREAM; + + switch (wireaddr->type) { + case ADDR_TYPE_IPV4: + sin = tal(ai, struct sockaddr_in); + wireaddr_to_ipv4(wireaddr, sin); + ai->ai_family = sin->sin_family; + ai->ai_addrlen = sizeof(*sin); + ai->ai_addr = (struct sockaddr *)sin; + return ai; + case ADDR_TYPE_IPV6: + sin6 = tal(ai, struct sockaddr_in6); + wireaddr_to_ipv6(wireaddr, sin6); + ai->ai_family = sin6->sin6_family; + ai->ai_addrlen = sizeof(*sin6); + ai->ai_addr = (struct sockaddr *)sin6; + return ai; + case ADDR_TYPE_TOR_V2: + case ADDR_TYPE_TOR_V3: + case ADDR_TYPE_PADDING: + break; + } + abort(); +} diff --git a/common/wireaddr.h b/common/wireaddr.h index 5a62b7bc3..5881803e1 100644 --- a/common/wireaddr.h +++ b/common/wireaddr.h @@ -117,4 +117,9 @@ void wireaddr_from_sockname(struct wireaddr_internal *addr, const char *sockname); bool wireaddr_to_sockname(const struct wireaddr_internal *addr, struct sockaddr_un *sun); + +struct addrinfo *wireaddr_to_addrinfo(const tal_t *ctx, + const struct wireaddr *wireaddr); +struct addrinfo *wireaddr_internal_to_addrinfo(const tal_t *ctx, + const struct wireaddr_internal *wireaddr); #endif /* LIGHTNING_COMMON_WIREADDR_H */ diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 883cf883f..8f2b6179c 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -1881,25 +1881,12 @@ static void connect_failed(struct io_conn *conn, struct reaching *reach) static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach) { - struct addrinfo ai; - struct sockaddr_in sin; - struct sockaddr_in6 sin6; - struct sockaddr_un sun; + struct addrinfo *ai; bool use_tor = false; - /* FIXME: make generic */ - ai.ai_flags = 0; - ai.ai_socktype = SOCK_STREAM; - ai.ai_protocol = 0; - ai.ai_canonname = NULL; - ai.ai_next = NULL; - switch (reach->addr.itype) { case ADDR_INTERNAL_SOCKNAME: - wireaddr_to_sockname(&reach->addr, &sun); - ai.ai_family = sun.sun_family; - ai.ai_addrlen = sizeof(sin); - ai.ai_addr = (struct sockaddr *)&sun; + ai = wireaddr_internal_to_addrinfo(tmpctx, &reach->addr); break; case ADDR_INTERNAL_ALLPROTO: status_failed(STATUS_FAIL_INTERNAL_ERROR, @@ -1908,16 +1895,9 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach) case ADDR_INTERNAL_WIREADDR: switch (reach->addr.u.wireaddr.type) { case ADDR_TYPE_IPV4: - wireaddr_to_ipv4(&reach->addr.u.wireaddr, &sin); - ai.ai_family = sin.sin_family; - ai.ai_addrlen = sizeof(sin); - ai.ai_addr = (struct sockaddr *)&sin; - break; case ADDR_TYPE_IPV6: - wireaddr_to_ipv6(&reach->addr.u.wireaddr, &sin6); - ai.ai_family = sin6.sin6_family; - ai.ai_addrlen = sizeof(sin6); - ai.ai_addr = (struct sockaddr *)&sin6; + ai = wireaddr_to_addrinfo(tmpctx, + &reach->addr.u.wireaddr); break; case ADDR_TYPE_TOR_V2: case ADDR_TYPE_TOR_V3: @@ -1942,7 +1922,7 @@ static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach) return io_tor_connect(conn, reach->daemon->tor_proxyaddr, &reach->addr.u.wireaddr, reach); } - return io_connect(conn, &ai, connection_out, reach); + return io_connect(conn, ai, connection_out, reach); } static struct addrhint *