2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>
4 2000-2003 Guus Sliepen <guus@sliepen.eu.org>
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.54 2003/08/22 15:03:59 guus Exp $
31 bool hostnames = false;
34 Turn a string into a struct addrinfo.
35 Return NULL on failure.
37 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype)
39 struct addrinfo *ai, hint = {0};
44 hint.ai_family = addressfamily;
45 hint.ai_socktype = socktype;
47 err = getaddrinfo(address, service, &hint, &ai);
50 logger(LOG_WARNING, _("Error looking up %s port %s: %s"), address,
51 service, gai_strerror(err));
58 sockaddr_t str2sockaddr(const char *address, const char *port)
60 struct addrinfo *ai, hint = {0};
66 hint.ai_family = AF_UNSPEC;
67 hint.ai_flags = AI_NUMERICHOST;
68 hint.ai_socktype = SOCK_STREAM;
70 err = getaddrinfo(address, port, &hint, &ai);
74 logger(LOG_DEBUG, "Unknown type address %s port %s", address, port);
75 result.sa.sa_family = AF_UNKNOWN;
76 result.unknown.address = xstrdup(address);
77 result.unknown.port = xstrdup(port);
81 result = *(sockaddr_t *) ai->ai_addr;
87 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr)
89 char address[NI_MAXHOST];
90 char port[NI_MAXSERV];
96 if(sa->sa.sa_family == AF_UNKNOWN) {
97 *addrstr = xstrdup(sa->unknown.address);
98 *portstr = xstrdup(sa->unknown.port);
102 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
105 logger(LOG_ERR, _("Error while translating addresses: %s"),
112 scopeid = strchr(address, '%');
115 *scopeid = '\0'; /* Descope. */
117 *addrstr = xstrdup(address);
118 *portstr = xstrdup(port);
121 char *sockaddr2hostname(const sockaddr_t *sa)
124 char address[NI_MAXHOST] = "unknown";
125 char port[NI_MAXSERV] = "unknown";
130 if(sa->sa.sa_family == AF_UNKNOWN) {
131 asprintf(&str, _("%s port %s"), sa->unknown.address, sa->unknown.port);
135 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
136 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
138 logger(LOG_ERR, _("Error while looking up hostname: %s"),
142 asprintf(&str, _("%s port %s"), address, port);
147 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b)
153 result = a->sa.sa_family - b->sa.sa_family;
158 switch (a->sa.sa_family) {
163 result = strcmp(a->unknown.address, b->unknown.address);
168 return strcmp(a->unknown.port, b->unknown.port);
171 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
176 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
179 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
184 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
187 logger(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"),
195 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
198 if(b->sa.sa_family != AF_UNKNOWN) {
201 a->unknown.family = AF_UNKNOWN;
202 a->unknown.address = xstrdup(b->unknown.address);
203 a->unknown.port = xstrdup(b->unknown.port);
207 void sockaddrfree(sockaddr_t *a) {
210 if(a->sa.sa_family == AF_UNKNOWN) {
211 free(a->unknown.address);
212 free(a->unknown.port);
216 void sockaddrunmap(sockaddr_t *sa)
220 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
221 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
222 sa->in.sin_family = AF_INET;
226 /* Subnet mask handling */
228 int maskcmp(const void *va, const void *vb, int masklen, int len)
236 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
237 result = a[i] - b[i];
243 return (a[i] & (0x100 - (1 << (8 - m)))) -
244 (b[i] & (0x100 - (1 << (8 - m))));
249 void mask(void *va, int masklen, int len)
260 a[i++] &= (0x100 - (1 << masklen));
266 void maskcpy(void *va, const void *vb, int masklen, int len)
274 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
278 a[i] = b[i] & (0x100 - (1 << m));
286 bool maskcheck(const void *va, int masklen, int len)
296 if(masklen && a[i++] & (0xff >> masklen))