2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: netutl.c,v 1.12.4.26 2002/02/20 22:15:32 guus Exp $
27 #include <netinet/in.h>
32 #include <sys/socket.h>
34 #include <arpa/inet.h>
49 Turn a string into a struct addrinfo.
50 Return NULL on failure.
52 struct addrinfo *str2addrinfo(char *address, char *service, int socktype)
54 struct addrinfo hint, *ai;
57 memset(&hint, 0, sizeof(hint));
59 hint.ai_family = addressfamily;
60 hint.ai_socktype = socktype;
62 if((err = getaddrinfo(address, service, &hint, &ai)))
64 if(debug_lvl >= DEBUG_ERROR)
65 syslog(LOG_WARNING, _("Error looking up %s port %s: %s\n"), address, service, gai_strerror(err));
74 sockaddr_t str2sockaddr(char *address, char *port)
76 struct addrinfo hint, *ai;
80 memset(&hint, 0, sizeof(hint));
82 hint.ai_family = AF_UNSPEC;
83 hint.ai_flags = AI_NUMERICHOST;
84 hint.ai_socktype = SOCK_STREAM;
86 if((err = getaddrinfo(address, port, &hint, &ai) || !ai))
88 syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err));
94 result = *(sockaddr_t *)ai->ai_addr;
100 void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr)
102 char address[NI_MAXHOST];
103 char port[NI_MAXSERV];
106 if((err = getnameinfo(&sa->sa, SA_LEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
108 syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
114 *addrstr = xstrdup(address);
115 *portstr = xstrdup(port);
119 char *sockaddr2hostname(sockaddr_t *sa)
122 char address[NI_MAXHOST] = "unknown";
123 char port[NI_MAXSERV] = "unknown";
126 if((err = getnameinfo(&sa->sa, SA_LEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
128 syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
131 asprintf(&str, _("%s port %s"), address, port);
136 int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
140 result = a->sa.sa_family - b->sa.sa_family;
145 switch(a->sa.sa_family)
150 return memcmp(&a->in, &b->in, sizeof(a->in));
152 return memcmp(&a->in6, &b->in6, sizeof(a->in6));
154 syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
162 /* Subnet mask handling */
164 int maskcmp(char *a, char *b, int masklen, int len)
168 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
169 if((result = a[i] - b[i]))
173 return (a[i] & (0x100 - (m << 1))) - (b[i] & (0x100 - (m << 1)));
178 void mask(char *a, int masklen, int len)
186 a[i++] &= (0x100 - (masklen << 1));
192 void maskcpy(char *a, char *b, int masklen, int len)
196 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
201 a[i] = b[i] & (0x100 - (m << 1));
209 int maskcheck(char *a, int masklen, int len)
217 if(a[i++] & ~(0x100 - (masklen << 1)))