2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2009 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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)
37 struct addrinfo *ai, hint = {0};
42 hint.ai_family = addressfamily;
43 hint.ai_socktype = socktype;
45 err = getaddrinfo(address, service, &hint, &ai);
48 logger(LOG_WARNING, _("Error looking up %s port %s: %s"), address,
49 service, gai_strerror(err));
56 sockaddr_t str2sockaddr(const char *address, const char *port)
58 struct addrinfo *ai, hint = {0};
64 hint.ai_family = AF_UNSPEC;
65 hint.ai_flags = AI_NUMERICHOST;
66 hint.ai_socktype = SOCK_STREAM;
68 err = getaddrinfo(address, port, &hint, &ai);
72 logger(LOG_DEBUG, "Unknown type address %s port %s", address, port);
73 result.sa.sa_family = AF_UNKNOWN;
74 result.unknown.address = xstrdup(address);
75 result.unknown.port = xstrdup(port);
79 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
85 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr)
87 char address[NI_MAXHOST];
88 char port[NI_MAXSERV];
94 if(sa->sa.sa_family == AF_UNKNOWN) {
95 *addrstr = xstrdup(sa->unknown.address);
96 *portstr = xstrdup(sa->unknown.port);
100 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
103 logger(LOG_ERR, _("Error while translating addresses: %s"),
110 scopeid = strchr(address, '%');
113 *scopeid = '\0'; /* Descope. */
115 *addrstr = xstrdup(address);
116 *portstr = xstrdup(port);
119 char *sockaddr2hostname(const sockaddr_t *sa)
122 char address[NI_MAXHOST] = "unknown";
123 char port[NI_MAXSERV] = "unknown";
128 if(sa->sa.sa_family == AF_UNKNOWN) {
129 xasprintf(&str, _("%s port %s"), sa->unknown.address, sa->unknown.port);
133 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
134 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
136 logger(LOG_ERR, _("Error while looking up hostname: %s"),
140 xasprintf(&str, _("%s port %s"), address, port);
145 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b)
151 result = a->sa.sa_family - b->sa.sa_family;
156 switch (a->sa.sa_family) {
161 return strcmp(a->unknown.address, b->unknown.address);
164 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
167 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
170 logger(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"),
178 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b)
184 result = a->sa.sa_family - b->sa.sa_family;
189 switch (a->sa.sa_family) {
194 result = strcmp(a->unknown.address, b->unknown.address);
199 return strcmp(a->unknown.port, b->unknown.port);
202 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
207 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
210 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
215 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
218 logger(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"),
226 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
229 if(b->sa.sa_family != AF_UNKNOWN) {
232 a->unknown.family = AF_UNKNOWN;
233 a->unknown.address = xstrdup(b->unknown.address);
234 a->unknown.port = xstrdup(b->unknown.port);
238 void sockaddrfree(sockaddr_t *a) {
241 if(a->sa.sa_family == AF_UNKNOWN) {
242 free(a->unknown.address);
243 free(a->unknown.port);
247 void sockaddrunmap(sockaddr_t *sa)
251 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
252 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
253 sa->in.sin_family = AF_INET;
257 /* Subnet mask handling */
259 int maskcmp(const void *va, const void *vb, int masklen)
267 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
268 result = a[i] - b[i];
274 return (a[i] & (0x100 - (1 << (8 - m)))) -
275 (b[i] & (0x100 - (1 << (8 - m))));
280 void mask(void *va, int masklen, int len)
291 a[i++] &= (0x100 - (1 << (8 - masklen)));
297 void maskcpy(void *va, const void *vb, int masklen, int len)
305 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
309 a[i] = b[i] & (0x100 - (1 << (8 - m)));
317 bool maskcheck(const void *va, int masklen, int len)
327 if(masklen && a[i++] & (0xff >> masklen))