2 netutl.c -- some supporting network utility code
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "meshlink_internal.h"
30 Turn a string into a struct addrinfo.
31 Return NULL on failure.
33 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
37 struct addrinfo hint = {
38 .ai_family = AF_UNSPEC,
39 .ai_socktype = socktype,
42 err = getaddrinfo(address, service, &hint, &ai);
45 logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
52 sockaddr_t str2sockaddr(const char *address, const char *port) {
57 memset(&result, 0, sizeof(result));
59 struct addrinfo hint = {
60 .ai_family = AF_UNSPEC,
61 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
62 .ai_socktype = SOCK_STREAM,
65 err = getaddrinfo(address, port, &hint, &ai);
68 logger(NULL, MESHLINK_DEBUG, "Unknown type address %s port %s", address, port);
69 result.sa.sa_family = AF_UNKNOWN;
70 result.unknown.address = xstrdup(address);
71 result.unknown.port = xstrdup(port);
75 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
81 sockaddr_t str2sockaddr_random(struct meshlink_handle *mesh, const char *address, const char *port) {
86 memset(&result, 0, sizeof(result));
88 struct addrinfo hint = {
89 .ai_family = AF_UNSPEC,
90 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
91 .ai_socktype = SOCK_STREAM,
94 err = getaddrinfo(address, port, &hint, &ai);
97 result.sa.sa_family = AF_UNKNOWN;
98 result.unknown.address = NULL;
99 result.unknown.port = NULL;
105 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
109 struct addrinfo *aip = ai;
111 for(count = prng(mesh, count); count--; aip = aip->ai_next);
113 memcpy(&result, aip->ai_addr, aip->ai_addrlen);
119 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
120 char address[NI_MAXHOST];
121 char port[NI_MAXSERV];
125 if(sa->sa.sa_family == AF_UNKNOWN) {
127 *addrstr = xstrdup(sa->unknown.address);
131 *portstr = xstrdup(sa->unknown.port);
137 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
140 logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
144 scopeid = strchr(address, '%');
147 *scopeid = '\0'; /* Descope. */
151 *addrstr = xstrdup(address);
155 *portstr = xstrdup(port);
159 char *sockaddr2hostname(const sockaddr_t *sa) {
161 char address[NI_MAXHOST] = "unknown";
162 char port[NI_MAXSERV] = "unknown";
165 if(sa->sa.sa_family == AF_UNKNOWN) {
166 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
170 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
173 logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
177 xasprintf(&str, "%s port %s", address, port);
182 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
185 result = a->sa.sa_family - b->sa.sa_family;
191 switch(a->sa.sa_family) {
196 return strcmp(a->unknown.address, b->unknown.address);
199 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
202 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
205 logger(NULL, MESHLINK_ERROR, "sockaddrcmp() was called with unknown address family %d, exitting!",
211 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
214 result = a->sa.sa_family - b->sa.sa_family;
220 switch(a->sa.sa_family) {
225 result = strcmp(a->unknown.address, b->unknown.address);
231 return strcmp(a->unknown.port, b->unknown.port);
234 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a)->in.sin_addr);
240 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a)->in.sin_port);
243 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a)->in6.sin6_addr);
249 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a)->in6.sin6_port);
252 logger(NULL, MESHLINK_ERROR, "sockaddrcmp() was called with unknown address family %d, exitting!",
258 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
259 if(b->sa.sa_family != AF_UNKNOWN) {
262 a->unknown.family = AF_UNKNOWN;
263 a->unknown.address = xstrdup(b->unknown.address);
264 a->unknown.port = xstrdup(b->unknown.port);
268 void sockaddrcpy_setport(sockaddr_t *a, const sockaddr_t *b, uint16_t port) {
271 switch(b->sa.sa_family) {
273 a->in.sin_port = htons(port);
277 a->in6.sin6_port = htons(port);
285 void sockaddrfree(sockaddr_t *a) {
286 if(a->sa.sa_family == AF_UNKNOWN) {
287 free(a->unknown.address);
288 free(a->unknown.port);
292 void sockaddrunmap(sockaddr_t *sa) {
293 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
294 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
295 sa->in.sin_family = AF_INET;
299 void packmsg_add_sockaddr(packmsg_output_t *out, const sockaddr_t *sa) {
300 switch(sa->sa.sa_family) {
303 memcpy(buf + 0, &sa->in.sin_port, 2);
304 memcpy(buf + 2, &sa->in.sin_addr, 4);
305 packmsg_add_ext(out, 4, buf, sizeof(buf));
311 memcpy(buf + 0, &sa->in6.sin6_port, 2);
312 memcpy(buf + 2, &sa->in6.sin6_addr, 16);
313 packmsg_add_ext(out, 6, buf, sizeof(buf));
318 packmsg_output_invalidate(out);
323 sockaddr_t packmsg_get_sockaddr(packmsg_input_t *in) {
325 memset(&sa, 0, sizeof sa);
329 uint32_t len = packmsg_get_ext_raw(in, &type, &data);
334 packmsg_input_invalidate(in);
338 sa.sa.sa_family = AF_INET;
339 memcpy(&sa.in.sin_port, (uint8_t *)data + 0, 2);
340 memcpy(&sa.in.sin_addr, (uint8_t *)data + 2, 4);
345 packmsg_input_invalidate(in);
349 sa.sa.sa_family = AF_INET6;
350 memcpy(&sa.in6.sin6_port, (uint8_t *)data + 0, 2);
351 memcpy(&sa.in6.sin6_addr, (uint8_t *)data + 2, 16);
355 packmsg_input_invalidate(in);