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.29 2002/03/17 15:59:29 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];
107 if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
109 syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
116 if((scopeid = strchr(address, '%')))
117 *scopeid = '\0'; /* Descope. */
120 *addrstr = xstrdup(address);
121 *portstr = xstrdup(port);
125 char *sockaddr2hostname(sockaddr_t *sa)
128 char address[NI_MAXHOST] = "unknown";
129 char port[NI_MAXSERV] = "unknown";
132 if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
134 syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
137 asprintf(&str, _("%s port %s"), address, port);
142 int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
146 result = a->sa.sa_family - b->sa.sa_family;
151 switch(a->sa.sa_family)
156 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
159 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
161 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
164 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
166 syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
174 void sockaddrunmap(sockaddr_t *sa)
176 if(IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
178 sa->in.sin_addr.s_addr = sa->in6.sin6_addr.s6_addr32[3];
179 sa->in.sin_family = AF_INET;
183 /* Subnet mask handling */
185 int maskcmp(char *a, char *b, int masklen, int len)
189 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
190 if((result = a[i] - b[i]))
194 return (a[i] & (0x100 - (m << 1))) - (b[i] & (0x100 - (m << 1)));
199 void mask(char *a, int masklen, int len)
207 a[i++] &= (0x100 - (masklen << 1));
213 void maskcpy(char *a, char *b, int masklen, int len)
217 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
222 a[i] = b[i] & (0x100 - (m << 1));
230 int maskcheck(char *a, int masklen, int len)
238 if(a[i++] & ~(0x100 - (masklen << 1)))