2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2011 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, hint = {0};
39 hint.ai_family = addressfamily;
40 hint.ai_socktype = socktype;
42 err = getaddrinfo(address, service, &hint, &ai);
45 logger(LOG_WARNING, "Error looking up %s port %s: %s", address,
46 service, gai_strerror(err));
53 sockaddr_t str2sockaddr(const char *address, const char *port) {
54 struct addrinfo *ai, hint = {0};
58 hint.ai_family = AF_UNSPEC;
59 hint.ai_flags = AI_NUMERICHOST;
60 hint.ai_socktype = SOCK_STREAM;
62 err = getaddrinfo(address, port, &hint, &ai);
66 logger(LOG_DEBUG, "Unknown type address %s port %s", address, port);
67 result.sa.sa_family = AF_UNKNOWN;
68 result.unknown.address = xstrdup(address);
69 result.unknown.port = xstrdup(port);
73 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
79 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
80 char address[NI_MAXHOST];
81 char port[NI_MAXSERV];
85 if(sa->sa.sa_family == AF_UNKNOWN) {
86 *addrstr = xstrdup(sa->unknown.address);
87 *portstr = xstrdup(sa->unknown.port);
91 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
94 logger(LOG_ERR, "Error while translating addresses: %s",
99 scopeid = strchr(address, '%');
102 *scopeid = '\0'; /* Descope. */
105 *addrstr = xstrdup(address);
107 *portstr = xstrdup(port);
110 char *sockaddr2hostname(const sockaddr_t *sa) {
112 char address[NI_MAXHOST] = "unknown";
113 char port[NI_MAXSERV] = "unknown";
116 if(sa->sa.sa_family == AF_UNKNOWN) {
117 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
121 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
122 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
124 logger(LOG_ERR, "Error while looking up hostname: %s",
128 xasprintf(&str, "%s port %s", address, port);
133 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
136 result = a->sa.sa_family - b->sa.sa_family;
141 switch (a->sa.sa_family) {
146 return strcmp(a->unknown.address, b->unknown.address);
149 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
152 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
155 logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
161 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
164 result = a->sa.sa_family - b->sa.sa_family;
169 switch (a->sa.sa_family) {
174 result = strcmp(a->unknown.address, b->unknown.address);
179 return strcmp(a->unknown.port, b->unknown.port);
182 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
187 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
190 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
195 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
198 logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
204 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
205 if(b->sa.sa_family != AF_UNKNOWN) {
208 a->unknown.family = AF_UNKNOWN;
209 a->unknown.address = xstrdup(b->unknown.address);
210 a->unknown.port = xstrdup(b->unknown.port);
214 void sockaddrfree(sockaddr_t *a) {
215 if(a->sa.sa_family == AF_UNKNOWN) {
216 free(a->unknown.address);
217 free(a->unknown.port);
221 void sockaddrunmap(sockaddr_t *sa) {
222 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
223 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
224 sa->in.sin_family = AF_INET;
228 /* Subnet mask handling */
230 int maskcmp(const void *va, const void *vb, int masklen) {
235 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
236 result = a[i] - b[i];
242 return (a[i] & (0x100 - (1 << (8 - m)))) -
243 (b[i] & (0x100 - (1 << (8 - m))));
248 void mask(void *va, int masklen, int len) {
256 a[i++] &= (0x100 - (1 << (8 - masklen)));
262 void maskcpy(void *va, const void *vb, int masklen, int len) {
267 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
271 a[i] = b[i] & (0x100 - (1 << (8 - m)));
279 bool maskcheck(const void *va, int masklen, int len) {
286 if(masklen && a[i++] & (0xff >> masklen))