/*
netutl.c -- some supporting network utility code
- Copyright (C) 1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>
- 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
+ Copyright (C) 1998-2005 Ivo Timmermans
+ 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "system.h"
+
+#include "net.h"
+#include "netutl.h"
+#include "logger.h"
+#include "utils.h"
+#include "xalloc.h"
+
+bool hostnames = false;
- $Id: netutl.c,v 1.12.4.20 2001/10/27 12:13:17 guus Exp $
+/*
+ Turn a string into a struct addrinfo.
+ Return NULL on failure.
*/
+struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
+ struct addrinfo *ai, hint = {0};
+ int err;
-#include "config.h"
+ hint.ai_family = addressfamily;
+ hint.ai_socktype = socktype;
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <syslog.h>
+ err = getaddrinfo(address, service, &hint, &ai);
-#include <utils.h>
-#include <xalloc.h>
+ if(err) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Error looking up %s port %s: %s", address,
+ service, gai_strerror(err));
+ return NULL;
+ }
-#include "errno.h"
-#include "conf.h"
-#include "net.h"
-#include "netutl.h"
+ return ai;
+}
-#include "system.h"
+sockaddr_t str2sockaddr(const char *address, const char *port) {
+ struct addrinfo *ai, hint = {0};
+ sockaddr_t result;
+ int err;
+
+ hint.ai_family = AF_UNSPEC;
+ hint.ai_flags = AI_NUMERICHOST;
+ hint.ai_socktype = SOCK_STREAM;
+
+ err = getaddrinfo(address, port, &hint, &ai);
-char *hostlookup(unsigned long addr)
-{
- char *name;
- struct hostent *host = NULL;
- struct in_addr in;
- int lookup_hostname = 0;
-cp
- in.s_addr = addr;
-
- get_config_int(lookup_config(config_tree, "Hostnames"), &lookup_hostname);
-
- if(lookup_hostname)
- host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
-
- if(!lookup_hostname || !host)
- {
- asprintf(&name, "%s", inet_ntoa(in));
- }
- else
- {
- asprintf(&name, "%s", host->h_name);
- }
-cp
- return name;
+ if(err || !ai) {
+ logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Unknown type address %s port %s", address, port);
+ result.sa.sa_family = AF_UNKNOWN;
+ result.unknown.address = xstrdup(address);
+ result.unknown.port = xstrdup(port);
+ return result;
+ }
+
+ memcpy(&result, ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+
+ return result;
}
-/*
- Turn a string into an IP addy with netmask
- return NULL on failure
-*/
-ip_mask_t *strtoip(char *str)
-{
- ip_mask_t *ip;
- int masker;
- char *q, *p;
- struct hostent *h;
-cp
- p = str;
- if((q = strchr(p, '/')))
- {
- *q = '\0';
- q++; /* q now points to netmask part, or NULL if no mask */
- }
-
- if(!(h = gethostbyname(p)))
- {
- if(debug_lvl >= DEBUG_ERROR)
- syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
-
- return NULL;
- }
-
- masker = 0;
- if(q)
- {
- masker = strtol(q, &p, 10);
- if(q == p || (*p))
- return NULL;
- }
-
- ip = xmalloc(sizeof(*ip));
- ip->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
-
- ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
-cp
- return ip;
+void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
+ char address[NI_MAXHOST];
+ char port[NI_MAXSERV];
+ char *scopeid;
+ int err;
+
+ if(sa->sa.sa_family == AF_UNKNOWN) {
+ if(addrstr)
+ *addrstr = xstrdup(sa->unknown.address);
+ if(portstr)
+ *portstr = xstrdup(sa->unknown.port);
+ return;
+ }
+
+ err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port, NI_NUMERICHOST | NI_NUMERICSERV);
+
+ if(err) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error while translating addresses: %s",
+ gai_strerror(err));
+ abort();
+ }
+
+ scopeid = strchr(address, '%');
+
+ if(scopeid)
+ *scopeid = '\0'; /* Descope. */
+
+ if(addrstr)
+ *addrstr = xstrdup(address);
+ if(portstr)
+ *portstr = xstrdup(port);
}
+char *sockaddr2hostname(const sockaddr_t *sa) {
+ char *str;
+ char address[NI_MAXHOST] = "unknown";
+ char port[NI_MAXSERV] = "unknown";
+ int err;
+
+ if(sa->sa.sa_family == AF_UNKNOWN) {
+ xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
+ return str;
+ }
+
+ err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port,
+ hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
+ if(err) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error while looking up hostname: %s",
+ gai_strerror(err));
+ }
+
+ xasprintf(&str, "%s port %s", address, port);
+
+ return str;
+}
+
+int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
+ int result;
+
+ result = a->sa.sa_family - b->sa.sa_family;
+
+ if(result)
+ return result;
+
+ switch (a->sa.sa_family) {
+ case AF_UNSPEC:
+ return 0;
+
+ case AF_UNKNOWN:
+ return strcmp(a->unknown.address, b->unknown.address);
+
+ case AF_INET:
+ return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
+
+ case AF_INET6:
+ return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
+ a->sa.sa_family);
+ abort();
+ }
+}
+
+int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
+ int result;
+
+ result = a->sa.sa_family - b->sa.sa_family;
+
+ if(result)
+ return result;
+
+ switch (a->sa.sa_family) {
+ case AF_UNSPEC:
+ return 0;
+
+ case AF_UNKNOWN:
+ result = strcmp(a->unknown.address, b->unknown.address);
+
+ if(result)
+ return result;
+
+ return strcmp(a->unknown.port, b->unknown.port);
+
+ case AF_INET:
+ result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof a->in.sin_addr);
+
+ if(result)
+ return result;
+
+ return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof a->in.sin_port);
+
+ case AF_INET6:
+ result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof a->in6.sin6_addr);
+
+ if(result)
+ return result;
+
+ return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof a->in6.sin6_port);
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
+ a->sa.sa_family);
+ abort();
+ }
+}
+
+void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
+ if(b->sa.sa_family != AF_UNKNOWN) {
+ *a = *b;
+ } else {
+ a->unknown.family = AF_UNKNOWN;
+ a->unknown.address = xstrdup(b->unknown.address);
+ a->unknown.port = xstrdup(b->unknown.port);
+ }
+}
+
+void sockaddrfree(sockaddr_t *a) {
+ if(a->sa.sa_family == AF_UNKNOWN) {
+ free(a->unknown.address);
+ free(a->unknown.port);
+ }
+}
+
+void sockaddrunmap(sockaddr_t *sa) {
+ if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
+ sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
+ sa->in.sin_family = AF_INET;
+ }
+}