2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2016 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 bool hostnames = false;
32 Turn a string into a struct addrinfo.
33 Return NULL on failure.
35 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
36 struct addrinfo *ai = NULL, hint = {0};
39 hint.ai_family = addressfamily;
40 hint.ai_socktype = socktype;
42 #if HAVE_DECL_RES_INIT
43 // ensure glibc reloads /etc/resolv.conf.
46 err = getaddrinfo(address, service, &hint, &ai);
49 logger(LOG_WARNING, "Error looking up %s port %s: %s", address,
50 service, gai_strerror(err));
57 sockaddr_t str2sockaddr(const char *address, const char *port) {
58 struct addrinfo *ai = NULL, hint = {0};
62 hint.ai_family = AF_UNSPEC;
63 hint.ai_flags = AI_NUMERICHOST;
64 hint.ai_socktype = SOCK_STREAM;
66 err = getaddrinfo(address, port, &hint, &ai);
70 logger(LOG_DEBUG, "Unknown type address %s port %s", address, port);
71 result.sa.sa_family = AF_UNKNOWN;
72 result.unknown.address = xstrdup(address);
73 result.unknown.port = xstrdup(port);
77 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
83 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
84 char address[NI_MAXHOST];
85 char port[NI_MAXSERV];
89 if(sa->sa.sa_family == AF_UNKNOWN) {
91 *addrstr = xstrdup(sa->unknown.address);
93 *portstr = xstrdup(sa->unknown.port);
97 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
100 logger(LOG_ERR, "Error while translating addresses: %s",
105 scopeid = strchr(address, '%');
108 *scopeid = '\0'; /* Descope. */
111 *addrstr = xstrdup(address);
113 *portstr = xstrdup(port);
116 char *sockaddr2hostname(const sockaddr_t *sa) {
118 char address[NI_MAXHOST] = "unknown";
119 char port[NI_MAXSERV] = "unknown";
122 if(sa->sa.sa_family == AF_UNKNOWN) {
123 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
127 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
128 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
130 logger(LOG_ERR, "Error while looking up hostname: %s",
134 xasprintf(&str, "%s port %s", address, port);
139 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
142 result = a->sa.sa_family - b->sa.sa_family;
147 switch (a->sa.sa_family) {
152 return strcmp(a->unknown.address, b->unknown.address);
155 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
158 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
161 logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
167 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
170 result = a->sa.sa_family - b->sa.sa_family;
175 switch (a->sa.sa_family) {
180 result = strcmp(a->unknown.address, b->unknown.address);
185 return strcmp(a->unknown.port, b->unknown.port);
188 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
193 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
196 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
201 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
204 logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
210 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
211 if(b->sa.sa_family != AF_UNKNOWN) {
214 a->unknown.family = AF_UNKNOWN;
215 a->unknown.address = xstrdup(b->unknown.address);
216 a->unknown.port = xstrdup(b->unknown.port);
220 void sockaddrfree(sockaddr_t *a) {
221 if(a->sa.sa_family == AF_UNKNOWN) {
222 free(a->unknown.address);
223 free(a->unknown.port);
227 void sockaddrunmap(sockaddr_t *sa) {
228 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
229 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
230 sa->in.sin_family = AF_INET;
234 void sockaddr_setport(sockaddr_t *sa, const char *port) {
235 uint16_t portnum = htons(atoi(port));
238 switch(sa->sa.sa_family) {
240 sa->in.sin_port = portnum;
243 sa->in6.sin6_port = portnum;
246 free(sa->unknown.port);
247 sa->unknown.port = xstrdup(port);
253 /* Subnet mask handling */
255 int maskcmp(const void *va, const void *vb, int masklen) {
260 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
261 result = a[i] - b[i];
267 return (a[i] & (0x100 - (1 << (8 - m)))) -
268 (b[i] & (0x100 - (1 << (8 - m))));
273 void mask(void *va, int masklen, int len) {
281 a[i++] &= (0x100 - (1 << (8 - masklen)));
287 void maskcpy(void *va, const void *vb, int masklen, int len) {
292 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
296 a[i] = b[i] & (0x100 - (1 << (8 - m)));
304 bool maskcheck(const void *va, int masklen, int len) {
311 if(masklen && a[i++] & (0xff >> masklen))