2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2013 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 #if HAVE_DECL_RES_INIT
45 err = getaddrinfo(address, service, &hint, &ai);
48 logger(DEBUG_ALWAYS, LOG_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
55 sockaddr_t str2sockaddr(const char *address, const char *port) {
56 struct addrinfo *ai, hint = {0};
57 sockaddr_t result = {0};
60 hint.ai_family = AF_UNSPEC;
61 hint.ai_flags = AI_NUMERICHOST;
62 hint.ai_socktype = SOCK_STREAM;
64 err = getaddrinfo(address, port, &hint, &ai);
67 logger(DEBUG_SCARY_THINGS, LOG_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_UNSPEC) {
88 *addrstr = xstrdup("unspec");
92 *portstr = xstrdup("unspec");
96 } else if(sa->sa.sa_family == AF_UNKNOWN) {
98 *addrstr = xstrdup(sa->unknown.address);
102 *portstr = xstrdup(sa->unknown.port);
108 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
111 logger(DEBUG_ALWAYS, LOG_ERR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
115 scopeid = strchr(address, '%');
118 *scopeid = '\0'; /* Descope. */
122 *addrstr = xstrdup(address);
126 *portstr = xstrdup(port);
130 char *sockaddr2hostname(const sockaddr_t *sa) {
132 char address[NI_MAXHOST] = "unknown";
133 char port[NI_MAXSERV] = "unknown";
136 if(sa->sa.sa_family == AF_UNSPEC) {
137 xasprintf(&str, "unspec port unspec");
139 } else if(sa->sa.sa_family == AF_UNKNOWN) {
140 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
144 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
145 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
148 logger(DEBUG_ALWAYS, LOG_ERR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
151 xasprintf(&str, "%s port %s", address, port);
156 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
159 result = a->sa.sa_family - b->sa.sa_family;
165 switch(a->sa.sa_family) {
170 return strcmp(a->unknown.address, b->unknown.address);
173 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
176 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
179 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
185 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
188 result = a->sa.sa_family - b->sa.sa_family;
194 switch(a->sa.sa_family) {
199 result = strcmp(a->unknown.address, b->unknown.address);
205 return strcmp(a->unknown.port, b->unknown.port);
208 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
214 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
217 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
223 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
226 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
232 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
233 if(b->sa.sa_family != AF_UNKNOWN) {
236 a->unknown.family = AF_UNKNOWN;
237 a->unknown.address = xstrdup(b->unknown.address);
238 a->unknown.port = xstrdup(b->unknown.port);
242 void sockaddrfree(sockaddr_t *a) {
243 if(a->sa.sa_family == AF_UNKNOWN) {
244 free(a->unknown.address);
245 free(a->unknown.port);
249 void sockaddrunmap(sockaddr_t *sa) {
250 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
251 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
252 sa->in.sin_family = AF_INET;
256 void sockaddr_setport(sockaddr_t *sa, const char *port) {
257 uint16_t portnum = htons(atoi(port));
263 switch(sa->sa.sa_family) {
265 sa->in.sin_port = portnum;
269 sa->in6.sin6_port = portnum;
273 free(sa->unknown.port);
274 sa->unknown.port = xstrdup(port);