2 netutl.c -- some supporting network utility code
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2013 Guus Sliepen <guus@tinc-vpn.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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 bool hostnames = false;
30 uint16_t service_to_port(const char *service) {
31 struct addrinfo *ai = str2addrinfo("localhost", service, SOCK_STREAM);
33 if(!ai || !ai->ai_addr) {
38 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
41 switch(sa.sa.sa_family) {
43 return ntohs(sa.in.sin_port);
46 return ntohs(sa.in6.sin6_port);
49 logger(DEBUG_ALWAYS, LOG_WARNING, "Unknown address family %d for service %s.", sa.sa.sa_family, service);
55 Turn a string into a struct addrinfo.
56 Return NULL on failure.
58 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
59 struct addrinfo *ai, hint = {0};
62 hint.ai_family = addressfamily;
63 hint.ai_socktype = socktype;
65 #if HAVE_DECL_RES_INIT
68 err = getaddrinfo(address, service, &hint, &ai);
71 logger(DEBUG_ALWAYS, LOG_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
78 sockaddr_t str2sockaddr(const char *address, const char *port) {
79 struct addrinfo *ai, hint = {0};
80 sockaddr_t result = {0};
83 hint.ai_family = AF_UNSPEC;
84 hint.ai_flags = AI_NUMERICHOST;
85 hint.ai_socktype = SOCK_STREAM;
87 err = getaddrinfo(address, port, &hint, &ai);
90 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Unknown type address %s port %s", address, port);
91 result.sa.sa_family = AF_UNKNOWN;
92 result.unknown.address = xstrdup(address);
93 result.unknown.port = xstrdup(port);
97 memcpy(&result, ai->ai_addr, ai->ai_addrlen);
103 void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
104 char address[NI_MAXHOST];
105 char port[NI_MAXSERV];
109 if(sa->sa.sa_family == AF_UNSPEC) {
111 *addrstr = xstrdup("unspec");
115 *portstr = xstrdup("unspec");
119 } else if(sa->sa.sa_family == AF_UNKNOWN) {
121 *addrstr = xstrdup(sa->unknown.address);
125 *portstr = xstrdup(sa->unknown.port);
131 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
134 logger(DEBUG_ALWAYS, LOG_ERR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
138 scopeid = strchr(address, '%');
141 *scopeid = '\0'; /* Descope. */
145 *addrstr = xstrdup(address);
149 *portstr = xstrdup(port);
153 char *sockaddr2hostname(const sockaddr_t *sa) {
155 char address[NI_MAXHOST] = "unknown";
156 char port[NI_MAXSERV] = "unknown";
159 if(sa->sa.sa_family == AF_UNSPEC) {
160 xasprintf(&str, "unspec port unspec");
162 } else if(sa->sa.sa_family == AF_UNKNOWN) {
163 xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
167 err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port),
168 hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
174 xasprintf(&str, "%s port %s", address, port);
179 int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
182 result = a->sa.sa_family - b->sa.sa_family;
188 switch(a->sa.sa_family) {
193 return strcmp(a->unknown.address, b->unknown.address);
196 return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
199 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
202 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
208 int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
211 result = a->sa.sa_family - b->sa.sa_family;
217 switch(a->sa.sa_family) {
222 result = strcmp(a->unknown.address, b->unknown.address);
228 return strcmp(a->unknown.port, b->unknown.port);
231 result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
237 return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a->in.sin_port));
240 result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
246 return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a->in6.sin6_port));
249 logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
255 void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
256 if(b->sa.sa_family != AF_UNKNOWN) {
259 a->unknown.family = AF_UNKNOWN;
260 a->unknown.address = xstrdup(b->unknown.address);
261 a->unknown.port = xstrdup(b->unknown.port);
265 void sockaddrfree(sockaddr_t *a) {
266 if(a->sa.sa_family == AF_UNKNOWN) {
267 free(a->unknown.address);
268 free(a->unknown.port);
272 void sockaddrunmap(sockaddr_t *sa) {
273 if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
274 sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
275 sa->in.sin_family = AF_INET;
279 void sockaddr_setport(sockaddr_t *sa, const char *port) {
280 uint16_t portnum = htons(atoi(port));
286 switch(sa->sa.sa_family) {
288 sa->in.sin_port = portnum;
292 sa->in6.sin6_port = portnum;
296 free(sa->unknown.port);
297 sa->unknown.port = xstrdup(port);
304 uint16_t get_bound_port(int sockfd) {
306 socklen_t salen = sizeof(sa);
308 if(getsockname(sockfd, (struct sockaddr *) &sa, &salen)) {
312 if(sa.sa.sa_family == AF_INET) {
313 return ntohs(sa.in.sin_port);
314 } else if(sa.sa.sa_family == AF_INET6) {
315 return ntohs(sa.in6.sin6_port);