/* netutl.c -- some supporting network utility code Copyright (C) 1998-2002 Ivo Timmermans 2000-2002 Guus Sliepen 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. $Id: netutl.c,v 1.12.4.26 2002/02/20 22:15:32 guus Exp $ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "errno.h" #include "conf.h" #include "net.h" #include "netutl.h" #include "system.h" int hostnames = 0; /* Turn a string into a struct addrinfo. Return NULL on failure. */ struct addrinfo *str2addrinfo(char *address, char *service, int socktype) { struct addrinfo hint, *ai; int err; cp memset(&hint, 0, sizeof(hint)); hint.ai_family = addressfamily; hint.ai_socktype = socktype; if((err = getaddrinfo(address, service, &hint, &ai))) { 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; } 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)) { syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err)); cp_trace(); raise(SIGFPE); exit(0); } result = *(sockaddr_t *)ai->ai_addr; freeaddrinfo(ai); cp return result; } void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr) { char address[NI_MAXHOST]; char port[NI_MAXSERV]; int err; cp if((err = getnameinfo(&sa->sa, SA_LEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV))) { syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err)); cp_trace(); raise(SIGFPE); exit(0); } *addrstr = xstrdup(address); *portstr = xstrdup(port); cp } char *sockaddr2hostname(sockaddr_t *sa) { char *str; char address[NI_MAXHOST] = "unknown"; char port[NI_MAXSERV] = "unknown"; int err; cp if((err = getnameinfo(&sa->sa, SA_LEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV)))) { syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err)); } asprintf(&str, _("%s port %s"), address, port); cp return str; } 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: return memcmp(&a->in, &b->in, sizeof(a->in)); case AF_INET6: return memcmp(&a->in6, &b->in6, sizeof(a->in6)); default: syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family); cp_trace(); raise(SIGFPE); exit(0); } cp } /* Subnet mask handling */ int maskcmp(char *a, char *b, int masklen, int len) { int i, m, result; cp for(m = masklen, i = 0; m >= 8; m -= 8, i++) if((result = a[i] - b[i])) return result; if(m) return (a[i] & (0x100 - (m << 1))) - (b[i] & (0x100 - (m << 1))); return 0; } void mask(char *a, int masklen, int len) { int i; cp i = masklen / 8; masklen %= 8; if(masklen) a[i++] &= (0x100 - (masklen << 1)); for(; i < len; i++) a[i] = 0; } void maskcpy(char *a, char *b, int masklen, int len) { int i, m; cp for(m = masklen, i = 0; m >= 8; m -= 8, i++) a[i] = b[i]; if(m) { a[i] = b[i] & (0x100 - (m << 1)); i++; } for(; i < len; i++) a[i] = 0; } int maskcheck(char *a, int masklen, int len) { int i; cp i = masklen / 8; masklen %= 8; if(masklen) if(a[i++] & ~(0x100 - (masklen << 1))) return -1; for(; i < len; i++) if(a[i] != 0) return -1; return 0; }