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.
29 Turn a string into a struct addrinfo.
30 Return NULL on failure.
32 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
36 struct addrinfo hint = {
37 .ai_family = AF_UNSPEC,
38 .ai_socktype = socktype,
41 err = getaddrinfo(address, service, &hint, &ai);
44 logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
51 sockaddr_t str2sockaddr(const char *address, const char *port) {
56 memset(&result, 0, sizeof(result));
58 struct addrinfo hint = {
59 .ai_family = AF_UNSPEC,
60 .ai_flags = NI_NUMERICHOST | NI_NUMERICSERV,
61 .ai_socktype = SOCK_STREAM,
64 err = getaddrinfo(address, port, &hint, &ai);
67 logger(NULL, MESHLINK_DEBUG, "Unknown type address %s port %s", address, port);
68 result.sa.sa_family = AF_UNKNOWN;
69 result.unknown.address = xstrdup(address);
70 result.unknown.port = xstrdup(port);
74 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
80 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
81 char address[NI_MAXHOST];
82 char port[NI_MAXSERV];
86 if(sa->sa.sa_family == AF_UNKNOWN) {
88 *addrstr = xstrdup(sa->unknown.address);
92 *portstr = xstrdup(sa->unknown.port);
98 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
101 logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
105 scopeid = strchr(address, '%');
108 *scopeid = '\0'; /* Descope. */
112 *addrstr = xstrdup(address);
116 *portstr = xstrdup(port);
120 char *sockaddr2hostname(const sockaddr_t *sa) {
122 char address[NI_MAXHOST] = "unknown";
123 char port[NI_MAXSERV] = "unknown";
126 if(sa->sa.sa_family == AF_UNKNOWN) {
127 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
131 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
134 logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
138 xasprintf(&str, "%s port %s", address, port);
143 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
146 result = a->sa.sa_family - b->sa.sa_family;
152 switch(a->sa.sa_family) {
157 return strcmp(a->unknown.address, b->unknown.address);
160 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
163 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
166 logger(NULL, MESHLINK_ERROR, "sockaddrcmp() was called with unknown address family %d, exitting!",
172 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
175 result = a->sa.sa_family - b->sa.sa_family;
181 switch(a->sa.sa_family) {
186 result = strcmp(a->unknown.address, b->unknown.address);
192 return strcmp(a->unknown.port, b->unknown.port);
195 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a)->in.sin_addr);
201 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a)->in.sin_port);
204 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a)->in6.sin6_addr);
210 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a)->in6.sin6_port);
213 logger(NULL, MESHLINK_ERROR, "sockaddrcmp() was called with unknown address family %d, exitting!",
219 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
220 if(b->sa.sa_family != AF_UNKNOWN) {
223 a->unknown.family = AF_UNKNOWN;
224 a->unknown.address = xstrdup(b->unknown.address);
225 a->unknown.port = xstrdup(b->unknown.port);
229 void sockaddrcpy_setport(sockaddr_t *a, const sockaddr_t *b, uint16_t port) {
232 switch(b->sa.sa_family) {
234 a->in.sin_port = htons(port);
238 a->in6.sin6_port = htons(port);
246 void sockaddrfree(sockaddr_t *a) {
247 if(a->sa.sa_family == AF_UNKNOWN) {
248 free(a->unknown.address);
249 free(a->unknown.port);
253 void sockaddrunmap(sockaddr_t *sa) {
254 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
255 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
256 sa->in.sin_family = AF_INET;
260 void packmsg_add_sockaddr(packmsg_output_t *out, const sockaddr_t *sa) {
261 switch(sa->sa.sa_family) {
264 memcpy(buf + 0, &sa->in.sin_port, 2);
265 memcpy(buf + 2, &sa->in.sin_addr, 4);
266 packmsg_add_ext(out, 4, buf, sizeof(buf));
272 memcpy(buf + 0, &sa->in6.sin6_port, 2);
273 memcpy(buf + 2, &sa->in6.sin6_addr, 16);
274 packmsg_add_ext(out, 6, buf, sizeof(buf));
279 packmsg_output_invalidate(out);
284 sockaddr_t packmsg_get_sockaddr(packmsg_input_t *in) {
289 uint32_t len = packmsg_get_ext_raw(in, &type, &data);
294 packmsg_input_invalidate(in);
298 sa.sa.sa_family = AF_INET;
299 memcpy(&sa.in.sin_port, (uint8_t *)data + 0, 2);
300 memcpy(&sa.in.sin_addr, (uint8_t *)data + 2, 4);
305 packmsg_input_invalidate(in);
309 sa.sa.sa_family = AF_INET6;
310 memcpy(&sa.in6.sin6_port, (uint8_t *)data + 0, 2);
311 memcpy(&sa.in6.sin6_addr, (uint8_t *)data + 2, 16);
315 packmsg_input_invalidate(in);