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.39 2002/06/08 13:46:43 guus Exp $
27 #include <netinet/in.h>
30 #ifdef HAVE_INTTYPES_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));
118 if((scopeid = strchr(address, '%')))
119 *scopeid = '\0'; /* Descope. */
121 *addrstr = xstrdup(address);
122 *portstr = xstrdup(port);
126 char *sockaddr2hostname(sockaddr_t *sa)
129 char address[NI_MAXHOST] = "unknown";
130 char port[NI_MAXSERV] = "unknown";
133 if((err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV))))
135 syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err));
138 asprintf(&str, _("%s port %s"), address, port);
143 int sockaddrcmp(sockaddr_t *a, sockaddr_t *b)
147 result = a->sa.sa_family - b->sa.sa_family;
152 switch(a->sa.sa_family)
157 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
160 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
162 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
165 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
167 syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family);
175 void sockaddrunmap(sockaddr_t *sa)
177 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
179 sa->in.sin_addr.s_addr = ((uint32_t *)&sa->in6.sin6_addr)[3];
180 sa->in.sin_family = AF_INET;
184 /* Subnet mask handling */
186 int maskcmp(void *va, void *vb, 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(void *va, int masklen, int len)
211 a[i++] &= (0x100 - (1 << masklen));
217 void maskcpy(void *va, void *vb, int masklen, int len)
223 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
228 a[i] = b[i] & (0x100 - (1 << m));
236 int maskcheck(void *va, int masklen, int len)
244 if(a[i++] & (0xff >> masklen))