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.36 2002/06/05 00:25:54 guus Exp $
27 #include <netinet/in.h>
35 #include <sys/socket.h>
37 #include <arpa/inet.h>
52 Turn a string into a struct addrinfo.
53 Return NULL on failure.
55 struct addrinfo *str2addrinfo(char *address, char *service, int socktype)
57 struct addrinfo hint, *ai;
60 memset(&hint, 0, sizeof(hint));
62 hint.ai_family = addressfamily;
63 hint.ai_socktype = socktype;
65 if((err = getaddrinfo(address, service, &hint, &ai)))
67 if(debug_lvl >= DEBUG_ERROR)
68 syslog(LOG_WARNING, _("Error looking up %s port %s: %s\n"), address, service, gai_strerror(err));
77 sockaddr_t str2sockaddr(char *address, char *port)
79 struct addrinfo hint, *ai;
83 memset(&hint, 0, sizeof(hint));
85 hint.ai_family = AF_UNSPEC;
86 hint.ai_flags = AI_NUMERICHOST;
87 hint.ai_socktype = SOCK_STREAM;
89 if((err = getaddrinfo(address, port, &hint, &ai) || !ai))
91 syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err));
97 result = *(sockaddr_t *)ai->ai_addr;
103 void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr)
105 char address[NI_MAXHOST];
106 char port[NI_MAXSERV];
110 if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV)))
112 syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err));
119 if((scopeid = strchr(address, '%')))
120 *scopeid = '\0'; /* Descope. */
123 *addrstr = xstrdup(address);
124 *portstr = xstrdup(port);
128 char *sockaddr2hostname(sockaddr_t *sa)
131 char address[NI_MAXHOST] = "unknown";
132 char port[NI_MAXSERV] = "unknown";
135 if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
137 syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
140 asprintf(&str, _("%s port %s"), address, port);
145 int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
149 result = a->sa.sa_family - b->sa.sa_family;
154 switch(a->sa.sa_family)
159 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
162 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
164 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
167 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
169 syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
177 void sockaddrunmap(sockaddr_t *sa)
179 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
181 sa->in.sin_addr.s_addr = ((uint32_t *)&sa->in6.sin6_addr)[3];
182 sa->in.sin_family = AF_INET;
186 /* Subnet mask handling */
188 int maskcmp(char *a, char *b, int masklen, int len)
192 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
193 if((result = a[i] - b[i]))
197 return (a[i] & (0x100 - (1 << (8 - m)))) - (b[i] & (0x100 - (1 << (8 - m))));
202 void mask(char *a, int masklen, int len)
210 a[i++] &= (0x100 - (1 << masklen));
216 void maskcpy(char *a, char *b, int masklen, int len)
220 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
225 a[i] = b[i] & (0x100 - (1 << m));
233 int maskcheck(char *a, int masklen, int len)
241 if(a[i++] & (char)~(0x100 - (1 << masklen)))