2002-02-11 10:16:18 +00:00
|
|
|
/*
|
|
|
|
netutl.c -- some supporting network utility code
|
|
|
|
Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
|
|
|
|
2000-2002 Guus Sliepen <guus@sliepen.warande.net>
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2002-06-08 12:57:10 +00:00
|
|
|
$Id: netutl.c,v 1.12.4.38 2002/06/08 12:57:10 guus Exp $
|
2002-02-11 10:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-06-08 12:57:10 +00:00
|
|
|
#ifndef HAVE_STDINT_H
|
2002-06-05 00:25:55 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
2002-02-11 10:16:18 +00:00
|
|
|
#include <string.h>
|
2002-02-18 16:25:19 +00:00
|
|
|
#include <signal.h>
|
2002-02-11 10:16:18 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <utils.h>
|
|
|
|
#include <xalloc.h>
|
|
|
|
|
|
|
|
#include "errno.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "netutl.h"
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
int hostnames = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Turn a string into a struct addrinfo.
|
|
|
|
Return NULL on failure.
|
|
|
|
*/
|
|
|
|
struct addrinfo *str2addrinfo(char *address, char *service, int socktype)
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
struct addrinfo hint, *ai;
|
|
|
|
int err;
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
2002-02-18 16:25:19 +00:00
|
|
|
memset(&hint, 0, sizeof(hint));
|
2002-02-11 10:16:18 +00:00
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
hint.ai_family = addressfamily;
|
|
|
|
hint.ai_socktype = socktype;
|
2002-02-11 10:16:18 +00:00
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
if((err = getaddrinfo(address, service, &hint, &ai)))
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
if(debug_lvl >= DEBUG_ERROR)
|
|
|
|
syslog(LOG_WARNING, _("Error looking up %s port %s: %s\n"), address, service, gai_strerror(err));
|
|
|
|
cp_trace();
|
|
|
|
return NULL;
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
cp
|
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_t str2sockaddr(char *address, char *port)
|
|
|
|
{
|
|
|
|
struct addrinfo hint, *ai;
|
|
|
|
sockaddr_t result;
|
|
|
|
int err;
|
|
|
|
cp
|
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
|
|
|
|
hint.ai_family = AF_UNSPEC;
|
|
|
|
hint.ai_flags = AI_NUMERICHOST;
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if((err = getaddrinfo(address, port, &hint, &ai) || !ai))
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err));
|
|
|
|
cp_trace();
|
|
|
|
raise(SIGFPE);
|
|
|
|
exit(0);
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
result = *(sockaddr_t *)ai->ai_addr;
|
|
|
|
freeaddrinfo(ai);
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
2002-02-18 16:25:19 +00:00
|
|
|
return result;
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr)
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
char address[NI_MAXHOST];
|
|
|
|
char port[NI_MAXSERV];
|
2002-02-26 22:47:51 +00:00
|
|
|
char *scopeid;
|
2002-02-18 16:25:19 +00:00
|
|
|
int err;
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
2002-02-20 22:37:38 +00:00
|
|
|
if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
2002-02-18 16:25:19 +00:00
|
|
|
syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
|
|
|
|
cp_trace();
|
|
|
|
raise(SIGFPE);
|
|
|
|
exit(0);
|
2002-02-11 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2002-02-26 22:47:51 +00:00
|
|
|
if((scopeid = strchr(address, '%')))
|
|
|
|
*scopeid = '\0'; /* Descope. */
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
*addrstr = xstrdup(address);
|
|
|
|
*portstr = xstrdup(port);
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
char *sockaddr2hostname(sockaddr_t *sa)
|
2002-02-11 10:16:18 +00:00
|
|
|
{
|
|
|
|
char *str;
|
2002-02-18 16:25:19 +00:00
|
|
|
char address[NI_MAXHOST] = "unknown";
|
|
|
|
char port[NI_MAXSERV] = "unknown";
|
|
|
|
int err;
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
2002-02-20 22:37:38 +00:00
|
|
|
if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
|
2002-02-18 16:25:19 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
asprintf(&str, _("%s port %s"), address, port);
|
2002-02-11 10:16:18 +00:00
|
|
|
cp
|
|
|
|
return str;
|
|
|
|
}
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
cp
|
|
|
|
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_INET:
|
2002-02-26 22:47:51 +00:00
|
|
|
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));
|
2002-02-18 16:25:19 +00:00
|
|
|
case AF_INET6:
|
2002-02-26 22:47:51 +00:00
|
|
|
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));
|
2002-02-18 16:25:19 +00:00
|
|
|
default:
|
|
|
|
syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
|
|
|
|
cp_trace();
|
|
|
|
raise(SIGFPE);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
cp
|
|
|
|
}
|
|
|
|
|
2002-03-17 15:59:29 +00:00
|
|
|
void sockaddrunmap(sockaddr_t *sa)
|
|
|
|
{
|
2002-03-17 16:08:39 +00:00
|
|
|
if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
|
2002-03-17 15:59:29 +00:00
|
|
|
{
|
2002-03-19 00:08:23 +00:00
|
|
|
sa->in.sin_addr.s_addr = ((uint32_t *)&sa->in6.sin6_addr)[3];
|
2002-03-17 15:59:29 +00:00
|
|
|
sa->in.sin_family = AF_INET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
/* Subnet mask handling */
|
|
|
|
|
2002-06-08 12:57:10 +00:00
|
|
|
int maskcmp(void *va, void *vb, int masklen, int len)
|
2002-02-18 16:25:19 +00:00
|
|
|
{
|
|
|
|
int i, m, result;
|
2002-06-08 12:57:10 +00:00
|
|
|
char *a = va;
|
|
|
|
char *b = vb;
|
2002-02-18 16:25:19 +00:00
|
|
|
cp
|
2002-02-20 17:15:33 +00:00
|
|
|
for(m = masklen, i = 0; m >= 8; m -= 8, i++)
|
2002-02-18 16:25:19 +00:00
|
|
|
if((result = a[i] - b[i]))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(m)
|
2002-04-05 09:11:38 +00:00
|
|
|
return (a[i] & (0x100 - (1 << (8 - m)))) - (b[i] & (0x100 - (1 << (8 - m))));
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-08 12:57:10 +00:00
|
|
|
void mask(void *va, int masklen, int len)
|
2002-02-18 16:25:19 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-06-08 12:57:10 +00:00
|
|
|
char *a = va;
|
2002-02-18 16:25:19 +00:00
|
|
|
cp
|
|
|
|
i = masklen / 8;
|
|
|
|
masklen %= 8;
|
|
|
|
|
|
|
|
if(masklen)
|
2002-04-05 09:11:38 +00:00
|
|
|
a[i++] &= (0x100 - (1 << masklen));
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
for(; i < len; i++)
|
|
|
|
a[i] = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-08 12:57:10 +00:00
|
|
|
void maskcpy(void *va, void *vb, int masklen, int len)
|
2002-02-18 16:25:19 +00:00
|
|
|
{
|
|
|
|
int i, m;
|
2002-06-08 12:57:10 +00:00
|
|
|
char *a = va;
|
|
|
|
char *b = vb;
|
2002-02-18 16:25:19 +00:00
|
|
|
cp
|
2002-02-20 17:15:33 +00:00
|
|
|
for(m = masklen, i = 0; m >= 8; m -= 8, i++)
|
2002-02-18 16:25:19 +00:00
|
|
|
a[i] = b[i];
|
|
|
|
|
|
|
|
if(m)
|
|
|
|
{
|
2002-04-05 09:11:38 +00:00
|
|
|
a[i] = b[i] & (0x100 - (1 << m));
|
2002-02-18 16:25:19 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; i < len; i++)
|
|
|
|
a[i] = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-08 12:57:10 +00:00
|
|
|
int maskcheck(void *va, int masklen, int len)
|
2002-02-18 16:25:19 +00:00
|
|
|
{
|
|
|
|
int i;
|
2002-06-08 12:57:10 +00:00
|
|
|
char *a = va;
|
2002-02-18 16:25:19 +00:00
|
|
|
cp
|
|
|
|
i = masklen / 8;
|
|
|
|
masklen %= 8;
|
2002-06-07 11:14:05 +00:00
|
|
|
|
|
|
|
if(a[i++] & (0xff >> masklen))
|
|
|
|
return -1;
|
|
|
|
|
2002-02-18 16:25:19 +00:00
|
|
|
for(; i < len; i++)
|
|
|
|
if(a[i] != 0)
|
2002-06-07 11:14:05 +00:00
|
|
|
return -2;
|
2002-02-18 16:25:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|