Browse Source
Structure for a net address. We can expand it later to cover exotic address types (Tor?). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>ppa-0.6.1
Rusty Russell
9 years ago
5 changed files with 78 additions and 36 deletions
@ -0,0 +1,44 @@ |
|||
#include "netaddr.h" |
|||
#include <arpa/inet.h> |
|||
#include <ccan/cast/cast.h> |
|||
#include <ccan/tal/str/str.h> |
|||
#include <netdb.h> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
|
|||
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); |
|||
} |
@ -0,0 +1,29 @@ |
|||
#ifndef LIGHTNING_DAEMON_NETADDR_H |
|||
#define LIGHTNING_DAEMON_NETADDR_H |
|||
#include "config.h" |
|||
#include <ccan/tal/tal.h> |
|||
#include <netinet/in.h> |
|||
#include <netinet/ip.h> |
|||
#include <sys/socket.h> |
|||
|
|||
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 */ |
Loading…
Reference in new issue