2002-02-11 10:16:18 +00:00
|
|
|
/*
|
|
|
|
netutl.c -- some supporting network utility code
|
2006-04-26 13:52:58 +00:00
|
|
|
Copyright (C) 1998-2005 Ivo Timmermans
|
2009-09-24 21:29:46 +00:00
|
|
|
2000-2009 Guus Sliepen <guus@tinc-vpn.org>
|
2002-02-11 10:16:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2009-09-24 22:01:00 +00:00
|
|
|
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.
|
2002-02-11 10:16:18 +00:00
|
|
|
*/
|
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "system.h"
|
|
|
|
|
2002-02-11 10:16:18 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "netutl.h"
|
2003-07-06 22:11:37 +00:00
|
|
|
#include "logger.h"
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "xalloc.h"
|
2002-02-11 10:16:18 +00:00
|
|
|
|
2003-07-22 20:55:21 +00:00
|
|
|
bool hostnames = false;
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Turn a string into a struct addrinfo.
|
|
|
|
Return NULL on failure.
|
|
|
|
*/
|
2009-09-24 22:14:03 +00:00
|
|
|
struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
|
2003-07-30 11:50:45 +00:00
|
|
|
struct addrinfo *ai, hint = {0};
|
2002-09-09 21:25:28 +00:00
|
|
|
int err;
|
|
|
|
|
2003-07-30 11:50:45 +00:00
|
|
|
hint.ai_family = addressfamily;
|
|
|
|
hint.ai_socktype = socktype;
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
err = getaddrinfo(address, service, &hint, &ai);
|
|
|
|
|
|
|
|
if(err) {
|
2009-09-24 22:54:07 +00:00
|
|
|
logger(LOG_WARNING, "Error looking up %s port %s: %s", address,
|
2002-09-09 21:25:28 +00:00
|
|
|
service, gai_strerror(err));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ai;
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
sockaddr_t str2sockaddr(const char *address, const char *port) {
|
2003-07-30 11:50:45 +00:00
|
|
|
struct addrinfo *ai, hint = {0};
|
2002-09-09 21:25:28 +00:00
|
|
|
sockaddr_t result;
|
|
|
|
int err;
|
|
|
|
|
2003-07-30 11:50:45 +00:00
|
|
|
hint.ai_family = AF_UNSPEC;
|
|
|
|
hint.ai_flags = AI_NUMERICHOST;
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
err = getaddrinfo(address, port, &hint, &ai);
|
|
|
|
|
|
|
|
if(err || !ai) {
|
2003-08-22 11:18:42 +00:00
|
|
|
ifdebug(SCARY_THINGS)
|
|
|
|
logger(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;
|
2002-09-09 21:25:28 +00:00
|
|
|
}
|
|
|
|
|
2006-06-11 18:53:27 +00:00
|
|
|
memcpy(&result, ai->ai_addr, ai->ai_addrlen);
|
2002-09-09 21:25:28 +00:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
|
|
|
|
return result;
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
|
2002-09-09 21:25:28 +00:00
|
|
|
char address[NI_MAXHOST];
|
|
|
|
char port[NI_MAXSERV];
|
|
|
|
char *scopeid;
|
|
|
|
int err;
|
|
|
|
|
2003-08-22 11:18:42 +00:00
|
|
|
if(sa->sa.sa_family == AF_UNKNOWN) {
|
|
|
|
*addrstr = xstrdup(sa->unknown.address);
|
|
|
|
*portstr = xstrdup(sa->unknown.port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
|
|
|
|
if(err) {
|
2009-09-24 22:54:07 +00:00
|
|
|
logger(LOG_ERR, "Error while translating addresses: %s",
|
2002-09-09 21:25:28 +00:00
|
|
|
gai_strerror(err));
|
2011-05-08 10:40:44 +00:00
|
|
|
abort();
|
2002-09-09 21:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scopeid = strchr(address, '%');
|
|
|
|
|
|
|
|
if(scopeid)
|
|
|
|
*scopeid = '\0'; /* Descope. */
|
|
|
|
|
2010-04-03 08:46:45 +00:00
|
|
|
if(addrstr)
|
|
|
|
*addrstr = xstrdup(address);
|
|
|
|
if(portstr)
|
|
|
|
*portstr = xstrdup(port);
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
char *sockaddr2hostname(const sockaddr_t *sa) {
|
2002-09-09 21:25:28 +00:00
|
|
|
char *str;
|
|
|
|
char address[NI_MAXHOST] = "unknown";
|
|
|
|
char port[NI_MAXSERV] = "unknown";
|
|
|
|
int err;
|
|
|
|
|
2003-08-22 11:18:42 +00:00
|
|
|
if(sa->sa.sa_family == AF_UNKNOWN) {
|
2009-09-24 22:54:07 +00:00
|
|
|
xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
|
2003-08-22 11:18:42 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
|
|
|
|
hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
|
|
|
|
if(err) {
|
2009-09-24 22:54:07 +00:00
|
|
|
logger(LOG_ERR, "Error while looking up hostname: %s",
|
2002-09-09 21:25:28 +00:00
|
|
|
gai_strerror(err));
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:54:07 +00:00
|
|
|
xasprintf(&str, "%s port %s", address, port);
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
return str;
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
|
2009-04-02 23:05:23 +00:00
|
|
|
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:
|
2009-09-24 22:54:07 +00:00
|
|
|
logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
|
2009-04-02 23:05:23 +00:00
|
|
|
a->sa.sa_family);
|
2011-05-08 10:40:44 +00:00
|
|
|
abort();
|
2009-04-02 23:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
|
2002-09-09 21:25:28 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
result = a->sa.sa_family - b->sa.sa_family;
|
|
|
|
|
2002-02-26 22:47:51 +00:00
|
|
|
if(result)
|
2002-09-09 21:25:28 +00:00
|
|
|
return result;
|
|
|
|
|
|
|
|
switch (a->sa.sa_family) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
return 0;
|
|
|
|
|
2003-08-22 11:18:42 +00:00
|
|
|
case AF_UNKNOWN:
|
|
|
|
result = strcmp(a->unknown.address, b->unknown.address);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
return strcmp(a->unknown.port, b->unknown.port);
|
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
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:
|
2009-09-24 22:54:07 +00:00
|
|
|
logger(LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
|
2002-09-09 21:25:28 +00:00
|
|
|
a->sa.sa_family);
|
2011-05-08 10:40:44 +00:00
|
|
|
abort();
|
2002-09-09 21:25:28 +00:00
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2003-08-22 11:18:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
void sockaddrunmap(sockaddr_t *sa) {
|
2002-09-09 21:25:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-03-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
/* Subnet mask handling */
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
int maskcmp(const void *va, const void *vb, int masklen) {
|
2002-09-09 21:25:28 +00:00
|
|
|
int i, m, result;
|
2003-07-24 12:08:16 +00:00
|
|
|
const char *a = va;
|
|
|
|
const char *b = vb;
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
|
|
|
|
result = a[i] - b[i];
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m)
|
|
|
|
return (a[i] & (0x100 - (1 << (8 - m)))) -
|
|
|
|
(b[i] & (0x100 - (1 << (8 - m))));
|
|
|
|
|
|
|
|
return 0;
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
void mask(void *va, int masklen, int len) {
|
2002-09-09 21:25:28 +00:00
|
|
|
int i;
|
|
|
|
char *a = va;
|
|
|
|
|
|
|
|
i = masklen / 8;
|
|
|
|
masklen %= 8;
|
|
|
|
|
|
|
|
if(masklen)
|
2006-04-12 08:38:35 +00:00
|
|
|
a[i++] &= (0x100 - (1 << (8 - masklen)));
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
for(; i < len; i++)
|
|
|
|
a[i] = 0;
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
void maskcpy(void *va, const void *vb, int masklen, int len) {
|
2002-09-09 21:25:28 +00:00
|
|
|
int i, m;
|
|
|
|
char *a = va;
|
2003-07-24 12:08:16 +00:00
|
|
|
const char *b = vb;
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
for(m = masklen, i = 0; m >= 8; m -= 8, i++)
|
|
|
|
a[i] = b[i];
|
|
|
|
|
|
|
|
if(m) {
|
2006-04-12 08:38:35 +00:00
|
|
|
a[i] = b[i] & (0x100 - (1 << (8 - m)));
|
2002-09-09 21:25:28 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; i < len; i++)
|
|
|
|
a[i] = 0;
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:14:03 +00:00
|
|
|
bool maskcheck(const void *va, int masklen, int len) {
|
2002-09-09 21:25:28 +00:00
|
|
|
int i;
|
2003-07-24 12:08:16 +00:00
|
|
|
const char *a = va;
|
2002-09-09 21:25:28 +00:00
|
|
|
|
|
|
|
i = masklen / 8;
|
|
|
|
masklen %= 8;
|
2002-06-07 11:14:05 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
if(masklen && a[i++] & (0xff >> masklen))
|
2003-07-22 20:55:21 +00:00
|
|
|
return false;
|
2002-06-07 11:14:05 +00:00
|
|
|
|
2002-09-09 21:25:28 +00:00
|
|
|
for(; i < len; i++)
|
|
|
|
if(a[i] != 0)
|
2003-07-22 20:55:21 +00:00
|
|
|
return false;
|
2002-02-18 16:25:19 +00:00
|
|
|
|
2003-07-22 20:55:21 +00:00
|
|
|
return true;
|
2002-02-18 16:25:19 +00:00
|
|
|
}
|