2 netutl.c -- some supporting network utility code
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 bool hostnames = false;
31 Turn a string into a struct addrinfo.
32 Return NULL on failure.
34 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
35 struct addrinfo *ai, hint = {0};
38 hint.ai_family = addressfamily;
39 hint.ai_socktype = socktype;
41 err = getaddrinfo(address, service, &hint, &ai);
44 logger(DEBUG_ALWAYS, LOG_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
51 sockaddr_t str2sockaddr(const char *address, const char *port) {
52 struct addrinfo *ai, hint = {0};
53 sockaddr_t result = {{0}};
56 hint.ai_family = AF_UNSPEC;
57 hint.ai_flags = AI_NUMERICHOST;
58 hint.ai_socktype = SOCK_STREAM;
60 err = getaddrinfo(address, port, &hint, &ai);
63 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Unknown type address %s port %s", address, port);
64 result.sa.sa_family = AF_UNKNOWN;
65 result.unknown.address = xstrdup(address);
66 result.unknown.port = xstrdup(port);
70 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
76 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
77 char address[NI_MAXHOST];
78 char port[NI_MAXSERV];
82 if(sa->sa.sa_family == AF_UNKNOWN) {
84 *addrstr = xstrdup(sa->unknown.address);
86 *portstr = xstrdup(sa->unknown.port);
90 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port, NI_NUMERICHOST | NI_NUMERICSERV);
93 logger(DEBUG_ALWAYS, LOG_ERR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
97 scopeid = strchr(address, '%');
100 *scopeid = '\0'; /* Descope. */
103 *addrstr = xstrdup(address);
105 *portstr = xstrdup(port);
108 char *sockaddr2hostname(const sockaddr_t *sa) {
110 char address[NI_MAXHOST] = "unknown";
111 char port[NI_MAXSERV] = "unknown";
114 if(sa->sa.sa_family == AF_UNKNOWN) {
115 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
119 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port,
120 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
122 logger(DEBUG_ALWAYS, LOG_ERR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
125 xasprintf(&str, "%s port %s", address, port);
130 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
133 result = a->sa.sa_family - b->sa.sa_family;
138 switch (a->sa.sa_family) {
143 return strcmp(a->unknown.address, b->unknown.address);
146 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
149 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
152 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
158 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
161 result = a->sa.sa_family - b->sa.sa_family;
166 switch (a->sa.sa_family) {
171 result = strcmp(a->unknown.address, b->unknown.address);
176 return strcmp(a->unknown.port, b->unknown.port);
179 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof a->in.sin_addr);
184 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof a->in.sin_port);
187 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof a->in6.sin6_addr);
192 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof a->in6.sin6_port);
195 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
201 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
202 if(b->sa.sa_family != AF_UNKNOWN) {
205 a->unknown.family = AF_UNKNOWN;
206 a->unknown.address = xstrdup(b->unknown.address);
207 a->unknown.port = xstrdup(b->unknown.port);
211 void sockaddrfree(sockaddr_t *a) {
212 if(a->sa.sa_family == AF_UNKNOWN) {
213 free(a->unknown.address);
214 free(a->unknown.port);
218 void sockaddrunmap(sockaddr_t *sa) {
219 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
220 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
221 sa->in.sin_family = AF_INET;