diff --git a/daemon/Makefile b/daemon/Makefile index f26e75f19..ab7bb3ff2 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -16,6 +16,7 @@ DAEMON_LIB_OBJS := $(DAEMON_LIB_SRC:.c=.o) DAEMON_SRC := \ daemon/jsonrpc.c \ daemon/lightningd.c \ + daemon/netaddr.c \ daemon/peer.c \ daemon/timeout.c DAEMON_OBJS := $(DAEMON_SRC:.c=.o) @@ -32,6 +33,7 @@ DAEMON_HEADERS := \ daemon/jsonrpc.h \ daemon/lightningd.h \ daemon/log.h \ + daemon/netaddr.h \ daemon/peer.h \ daemon/pseudorand.h \ daemon/timeout.h diff --git a/daemon/netaddr.c b/daemon/netaddr.c new file mode 100644 index 000000000..6eb9280ba --- /dev/null +++ b/daemon/netaddr.c @@ -0,0 +1,44 @@ +#include "netaddr.h" +#include +#include +#include +#include +#include +#include + +void netaddr_to_addrinfo(struct addrinfo *ai, const struct netaddr *a) +{ + ai->ai_flags = 0; + ai->ai_family = a->saddr.s.sa_family; + ai->ai_socktype = a->type; + ai->ai_protocol = a->protocol; + ai->ai_addrlen = a->addrlen; + ai->ai_addr = cast_const(struct sockaddr *, &a->saddr.s); + ai->ai_canonname = NULL; + ai->ai_next = NULL; +} + +char *netaddr_name(const tal_t *ctx, const struct netaddr *a) +{ + char name[INET6_ADDRSTRLEN]; + const void *sockaddr; + uint16_t port; + + switch (a->saddr.s.sa_family) { + case AF_INET: + sockaddr = &a->saddr.ipv4.sin_addr; + port = ntohs(a->saddr.ipv4.sin_port); + break; + case AF_INET6: + sockaddr = &a->saddr.ipv6.sin6_addr; + port = ntohs(a->saddr.ipv6.sin6_port); + break; + default: + return tal_fmt(ctx, "Unknown protocol %u", a->saddr.s.sa_family); + } + + if (!inet_ntop(a->saddr.s.sa_family, sockaddr, name, sizeof(name))) + sprintf(name, "Unprintable-%u-address", a->saddr.s.sa_family); + + return tal_fmt(ctx, "%s:%u", name, port); +} diff --git a/daemon/netaddr.h b/daemon/netaddr.h new file mode 100644 index 000000000..13f1c3ec5 --- /dev/null +++ b/daemon/netaddr.h @@ -0,0 +1,29 @@ +#ifndef LIGHTNING_DAEMON_NETADDR_H +#define LIGHTNING_DAEMON_NETADDR_H +#include "config.h" +#include +#include +#include +#include + +struct addrinfo; + +/* This can be extended to support other protocols in future. */ +struct netaddr { + int type; /* See socket(2): SOCK_STREAM currently */ + int protocol; /* See socket(2): 0 currently */ + socklen_t addrlen; + union { + struct sockaddr s; + struct sockaddr_in ipv4; + struct sockaddr_in6 ipv6; + } saddr; +}; + +/* Get the name for this netaddr. */ +char *netaddr_name(const tal_t *ctx, const struct netaddr *a); + +/* Create a addrinfo (as wanted by io_connect) for this address. */ +void netaddr_to_addrinfo(struct addrinfo *ai, const struct netaddr *a); + +#endif /* LIGHTNING_DAEMON_NETADDR_H */ diff --git a/daemon/peer.c b/daemon/peer.c index 2200c1af4..44f8da614 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -1,7 +1,6 @@ #include "lightningd.h" #include "log.h" #include "peer.h" -#include #include #include #include @@ -13,18 +12,6 @@ #include #include -static u16 get_port(const struct netaddr *addr) -{ - switch (addr->saddr.s.sa_family) { - case AF_INET: - return ntohs(addr->saddr.ipv4.sin_port); - case AF_INET6: - return ntohs(addr->saddr.ipv6.sin6_port); - default: - abort(); - } -} - static void destroy_peer(struct peer *peer) { list_del_from(&peer->state->peers, &peer->list); @@ -36,7 +23,6 @@ static struct peer *new_peer(struct lightningd_state *state, const char *in_or_out) { struct peer *peer = tal(state, struct peer); - char name[INET6_ADDRSTRLEN]; /* FIXME: Stop listening if too many peers? */ list_add(&state->peers, &peer->list); @@ -57,13 +43,9 @@ static struct peer *new_peer(struct lightningd_state *state, return tal_free(peer); } - if (!inet_ntop(peer->addr.saddr.s.sa_family, &peer->addr.saddr, - name, sizeof(name))) - strcpy(name, "UNCONVERTABLE-ADDR"); - - peer->log = new_log(peer, state->log_record, "%s-%s:%s:%u", + peer->log = new_log(peer, state->log_record, "%s%s:%s:", log_prefix(state->base_log), in_or_out, - name, get_port(&peer->addr)); + netaddr_name(peer, &peer->addr)); return peer; } diff --git a/daemon/peer.h b/daemon/peer.h index 958cb2f68..50cea69a1 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -1,23 +1,8 @@ #ifndef LIGHTNING_DAEMON_PEER_H #define LIGHTNING_DAEMON_PEER_H #include "config.h" +#include "netaddr.h" #include -#include -#include -#include -#include - -/* This can be extended to support other protocols in future. */ -struct netaddr { - int type; /* See socket(2): SOCK_STREAM currently */ - int protocol; /* See socket(2): 0 currently */ - socklen_t addrlen; - union { - struct sockaddr s; - struct sockaddr_in ipv4; - struct sockaddr_in6 ipv6; - } saddr; -}; struct peer { /* state->peers list */