uapi: use sockaddr union instead of sockaddr_storage

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2017-01-10 05:52:06 +01:00
parent 48f7c3522a
commit f43b43376b
3 changed files with 17 additions and 17 deletions

View File

@ -119,7 +119,7 @@ static inline bool parse_ip(struct wgipmask *ipmask, const char *value)
return true; return true;
} }
static inline bool parse_endpoint(struct sockaddr_storage *endpoint, const char *value) static inline bool parse_endpoint(struct sockaddr *endpoint, const char *value)
{ {
char *mutable = strdup(value); char *mutable = strdup(value);
char *begin, *end; char *begin, *end;
@ -293,7 +293,7 @@ static bool process_line(struct config_ctx *ctx, const char *line)
goto error; goto error;
} else if (ctx->is_peer_section) { } else if (ctx->is_peer_section) {
if (key_match("Endpoint")) if (key_match("Endpoint"))
ret = parse_endpoint(&peer_from_offset(ctx->buf.dev, ctx->peer_offset)->endpoint, value); ret = parse_endpoint(&peer_from_offset(ctx->buf.dev, ctx->peer_offset)->endpoint.addr, value);
else if (key_match("PublicKey")) else if (key_match("PublicKey"))
ret = parse_key(peer_from_offset(ctx->buf.dev, ctx->peer_offset)->public_key, value); ret = parse_key(peer_from_offset(ctx->buf.dev, ctx->peer_offset)->public_key, value);
else if (key_match("AllowedIPs")) else if (key_match("AllowedIPs"))
@ -500,7 +500,7 @@ bool config_read_cmd(struct wgdevice **device, char *argv[], int argc)
argv += 1; argv += 1;
argc -= 1; argc -= 1;
} else if (!strcmp(argv[0], "endpoint") && argc >= 2 && buf.dev->num_peers) { } else if (!strcmp(argv[0], "endpoint") && argc >= 2 && buf.dev->num_peers) {
if (!parse_endpoint(&peer_from_offset(buf.dev, peer_offset)->endpoint, argv[1])) if (!parse_endpoint(&peer_from_offset(buf.dev, peer_offset)->endpoint.addr, argv[1]))
goto error; goto error;
argv += 2; argv += 2;
argc -= 2; argc -= 2;

View File

@ -107,7 +107,7 @@ static char *ip(const struct wgipmask *ip)
return buf; return buf;
} }
static char *endpoint(const struct sockaddr_storage *addr) static char *endpoint(const struct sockaddr *addr)
{ {
char host[4096 + 1]; char host[4096 + 1];
char service[512 + 1]; char service[512 + 1];
@ -116,16 +116,16 @@ static char *endpoint(const struct sockaddr_storage *addr)
socklen_t addr_len = 0; socklen_t addr_len = 0;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
if (addr->ss_family == AF_INET) if (addr->sa_family == AF_INET)
addr_len = sizeof(struct sockaddr_in); addr_len = sizeof(struct sockaddr_in);
else if (addr->ss_family == AF_INET6) else if (addr->sa_family == AF_INET6)
addr_len = sizeof(struct sockaddr_in6); addr_len = sizeof(struct sockaddr_in6);
ret = getnameinfo((struct sockaddr *)addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST); ret = getnameinfo(addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST);
if (ret) if (ret)
strncpy(buf, gai_strerror(ret), sizeof(buf) - 1); strncpy(buf, gai_strerror(ret), sizeof(buf) - 1);
else else
snprintf(buf, sizeof(buf) - 1, (addr->ss_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s" : "%s:%s", host, service); snprintf(buf, sizeof(buf) - 1, (addr->sa_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s" : "%s:%s", host, service);
return buf; return buf;
} }
@ -228,8 +228,8 @@ static void pretty_print(struct wgdevice *device)
} }
for_each_wgpeer(device, peer, i) { for_each_wgpeer(device, peer, i) {
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "peer" TERMINAL_RESET ": " TERMINAL_FG_YELLOW "%s" TERMINAL_RESET "\n", key(peer->public_key)); terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "peer" TERMINAL_RESET ": " TERMINAL_FG_YELLOW "%s" TERMINAL_RESET "\n", key(peer->public_key));
if (peer->endpoint.ss_family == AF_INET || peer->endpoint.ss_family == AF_INET6) if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
terminal_printf(" " TERMINAL_BOLD "endpoint" TERMINAL_RESET ": %s\n", endpoint(&peer->endpoint)); terminal_printf(" " TERMINAL_BOLD "endpoint" TERMINAL_RESET ": %s\n", endpoint(&peer->endpoint.addr));
terminal_printf(" " TERMINAL_BOLD "allowed ips" TERMINAL_RESET ": "); terminal_printf(" " TERMINAL_BOLD "allowed ips" TERMINAL_RESET ": ");
if (peer->num_ipmasks) { if (peer->num_ipmasks) {
for_each_wgipmask(peer, ipmask, j) for_each_wgipmask(peer, ipmask, j)
@ -276,8 +276,8 @@ static bool ugly_print(struct wgdevice *device, const char *param, bool with_int
printf("%s\t", device->interface); printf("%s\t", device->interface);
for_each_wgpeer(device, peer, i) { for_each_wgpeer(device, peer, i) {
printf("%s\t", key(peer->public_key)); printf("%s\t", key(peer->public_key));
if (peer->endpoint.ss_family == AF_INET || peer->endpoint.ss_family == AF_INET6) if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
printf("%s\n", endpoint(&peer->endpoint)); printf("%s\n", endpoint(&peer->endpoint.addr));
else else
printf("(none)\n"); printf("(none)\n");
} }

View File

@ -75,18 +75,18 @@ int showconf_main(int argc, char *argv[])
if (peer->num_ipmasks) if (peer->num_ipmasks)
printf("\n"); printf("\n");
if (peer->endpoint.ss_family == AF_INET || peer->endpoint.ss_family == AF_INET6) { if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) {
char host[4096 + 1]; char host[4096 + 1];
char service[512 + 1]; char service[512 + 1];
static char buf[sizeof(host) + sizeof(service) + 4]; static char buf[sizeof(host) + sizeof(service) + 4];
socklen_t addr_len = 0; socklen_t addr_len = 0;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
if (peer->endpoint.ss_family == AF_INET) if (peer->endpoint.addr.sa_family == AF_INET)
addr_len = sizeof(struct sockaddr_in); addr_len = sizeof(struct sockaddr_in);
else if (peer->endpoint.ss_family == AF_INET6) else if (peer->endpoint.addr.sa_family == AF_INET6)
addr_len = sizeof(struct sockaddr_in6); addr_len = sizeof(struct sockaddr_in6);
if (!getnameinfo((struct sockaddr *)&peer->endpoint, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST)) { if (!getnameinfo(&peer->endpoint.addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST)) {
snprintf(buf, sizeof(buf) - 1, (peer->endpoint.ss_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s" : "%s:%s", host, service); snprintf(buf, sizeof(buf) - 1, (peer->endpoint.addr.sa_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s" : "%s:%s", host, service);
printf("Endpoint = %s\n", buf); printf("Endpoint = %s\n", buf);
} }
} }