chiark / gitweb /
lib/addr.c, etc.: Return plain addresses from `netaddress_resolve'.
[disorder] / lib / addr.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
cca89d7c 3 * Copyright (C) 2004, 2007, 2008, 2013 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
d6ea854a
RK
18/** @file lib/addr.c
19 * @brief Socket address support */
460b9539 20
05b75f8d 21#include "common.h"
460b9539 22
6735af5f
MW
23#include <stddef.h>
24
460b9539 25#include <sys/types.h>
cca89d7c
RK
26#if HAVE_SYS_SOCKET_H
27# include <sys/socket.h>
28#endif
29#if HAVE_NETINET_IN_H
30# include <netinet/in.h>
31#endif
32#if HAVE_ARPA_INET_H
33# include <arpa/inet.h>
34#endif
35#if HAVE_SYS_UN_H
36# include <sys/un.h>
37#endif
9e42afcd
RK
38#if HAVE_WS2TCPIP_H
39# include <Ws2tcpip.h>
40#endif
460b9539 41
42#include "log.h"
43#include "printf.h"
460b9539 44#include "addr.h"
6fba990c 45#include "mem.h"
76e72f65
RK
46#include "syscalls.h"
47#include "configuration.h"
48#include "vector.h"
460b9539 49
ec6f8488 50/** @brief Convert a pair of strings to an address
51 * @param a Pointer to string list
52 * @param pref Hints structure for getaddrinfo, or NULL
53 * @param namep Where to store address description, or NULL
54 * @return Address info structure or NULL on error
55 *
56 * This converts one or two strings into an address specification suitable
57 * for passing to socket(), bind() etc.
58 *
59 * If there is only one string then it is assumed to be the service
60 * name (port number). If there are two then the first is the host
61 * name and the second the service name.
62 *
63 * @p namep is used to return a description of the address suitable
64 * for use in log messages.
65 *
66 * If an error occurs a message is logged and a null pointer returned.
67 */
460b9539 68struct addrinfo *get_address(const struct stringlist *a,
69 const struct addrinfo *pref,
70 char **namep) {
71 struct addrinfo *res;
72 char *name;
73 int rc;
92db088e 74 char errbuf[1024];
e83d0967
RK
75
76 switch(a->n) {
77 case 1:
460b9539 78 byte_xasprintf(&name, "host * service %s", a->s[0]);
79 if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
92db088e
RK
80 disorder_error(0, "getaddrinfo %s: %s", a->s[0],
81 format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
460b9539 82 return 0;
83 }
e83d0967
RK
84 break;
85 case 2:
460b9539 86 byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
87 if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
2e9ba080 88 disorder_error(0, "getaddrinfo %s %s: %s",
92db088e
RK
89 a->s[0], a->s[1],
90 format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
460b9539 91 return 0;
92 }
e83d0967
RK
93 break;
94 default:
2e9ba080 95 disorder_error(0, "invalid network address specification (n=%d)", a->n);
e83d0967 96 return 0;
460b9539 97 }
e83d0967 98 if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
2e9ba080 99 disorder_error(0, "getaddrinfo didn't give us a suitable socket address");
460b9539 100 if(res)
101 freeaddrinfo(res);
102 return 0;
103 }
104 if(namep)
105 *namep = name;
106 return res;
107}
108
d7b6f0d1 109/** @brief Comparison function for address information
110 *
111 * Suitable for qsort().
112 */
460b9539 113int addrinfocmp(const struct addrinfo *a,
114 const struct addrinfo *b) {
460b9539 115 if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family;
116 if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype;
117 if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol;
80dc2c5f
RK
118 return sockaddrcmp(a->ai_addr, b->ai_addr);
119}
120
121/** @brief Comparison function for socket addresses
122 *
123 * Suitable for qsort().
124 */
125int sockaddrcmp(const struct sockaddr *a,
126 const struct sockaddr *b) {
127 const struct sockaddr_in *ina, *inb;
128 const struct sockaddr_in6 *in6a, *in6b;
129
130 if(a->sa_family != b->sa_family) return a->sa_family - b->sa_family;
131 switch(a->sa_family) {
460b9539 132 case PF_INET:
80dc2c5f
RK
133 ina = (const struct sockaddr_in *)a;
134 inb = (const struct sockaddr_in *)b;
460b9539 135 if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port;
136 return ina->sin_addr.s_addr - inb->sin_addr.s_addr;
137 break;
138 case PF_INET6:
80dc2c5f
RK
139 in6a = (const struct sockaddr_in6 *)a;
140 in6b = (const struct sockaddr_in6 *)b;
460b9539 141 if(in6a->sin6_port != in6b->sin6_port)
142 return in6a->sin6_port - in6b->sin6_port;
143 return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
144 sizeof (struct in6_addr));
145 default:
2e9ba080 146 disorder_fatal(0, "unsupported protocol family %d", a->sa_family);
460b9539 147 }
148}
6fba990c 149
0590cedc 150/** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
8d516888
RK
151static inline int multicast4(const struct sockaddr_in *sin4) {
152 return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr));
6fba990c
RK
153}
154
0590cedc 155/** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
6fba990c
RK
156static inline int multicast6(const struct sockaddr_in6 *sin6) {
157 return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr);
158}
159
160/** @brief Return true if @p sa represents a multicast address */
161int multicast(const struct sockaddr *sa) {
162 switch(sa->sa_family) {
163 case AF_INET:
164 return multicast4((const struct sockaddr_in *)sa);
165 case AF_INET6:
166 return multicast6((const struct sockaddr_in6 *)sa);
167 default:
168 return 0;
169 }
170}
171
0590cedc 172/** @brief Format an IPv4 address */
8d516888 173static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
6fba990c
RK
174 char buffer[1024], *r;
175
8d516888 176 if(sin4->sin_port)
6fba990c 177 byte_xasprintf(&r, "%s port %u",
9e42afcd 178 inet_ntop(sin4->sin_family, (void *)&sin4->sin_addr,
6fba990c 179 buffer, sizeof buffer),
8d516888 180 ntohs(sin4->sin_port));
6fba990c
RK
181 else
182 byte_xasprintf(&r, "%s",
9e42afcd 183 inet_ntop(sin4->sin_family, (void *)&sin4->sin_addr,
6fba990c
RK
184 buffer, sizeof buffer));
185 return r;
186}
187
0590cedc 188/** @brief Format an IPv6 address */
6fba990c
RK
189static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
190 char buffer[1024], *r;
191
192 if(sin6->sin6_port)
193 byte_xasprintf(&r, "%s port %u",
9e42afcd 194 inet_ntop(sin6->sin6_family, (void *)&sin6->sin6_addr,
6fba990c
RK
195 buffer, sizeof buffer),
196 ntohs(sin6->sin6_port));
197 else
198 byte_xasprintf(&r, "%s",
9e42afcd 199 inet_ntop(sin6->sin6_family, (void *)&sin6->sin6_addr,
6fba990c
RK
200 buffer, sizeof buffer));
201 return r;
202}
203
cca89d7c 204#if HAVE_SYS_UN_H
0590cedc 205/** @brief Format a UNIX socket address */
6fba990c
RK
206static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
207 return xstrdup(sun->sun_path);
208}
cca89d7c 209#endif
6fba990c 210
0590cedc
RK
211/** @brief Construct a text description a sockaddr
212 * @param sa Socket address
213 * @return Human-readable form of address
214 */
6fba990c
RK
215char *format_sockaddr(const struct sockaddr *sa) {
216 switch(sa->sa_family) {
217 case AF_INET:
218 return format_sockaddr4((const struct sockaddr_in *)sa);
219 case AF_INET6:
220 return format_sockaddr6((const struct sockaddr_in6 *)sa);
cca89d7c 221#if HAVE_SYS_UN_H
6fba990c
RK
222 case AF_UNIX:
223 return format_sockaddrun((const struct sockaddr_un *)sa);
cca89d7c 224#endif
6fba990c
RK
225 default:
226 return 0;
227 }
228}
229
76e72f65
RK
230/** @brief Parse the text form of a network address
231 * @param na Where to store result
232 * @param nvec Number of strings
233 * @param vec List of strings
234 * @return 0 on success, -1 on syntax error
235 */
236int netaddress_parse(struct netaddress *na,
237 int nvec,
238 char **vec) {
239 const char *port;
05b3f1f6
RK
240 long p;
241 int e;
76e72f65
RK
242
243 na->af = AF_UNSPEC;
244 if(nvec > 0 && vec[0][0] == '-') {
245 if(!strcmp(vec[0], "-4"))
246 na->af = AF_INET;
247 else if(!strcmp(vec[0], "-6"))
248 na->af = AF_INET6;
249 else if(!strcmp(vec[0], "-unix"))
250 na->af = AF_UNIX;
251 else if(!strcmp(vec[0], "-"))
252 na->af = AF_UNSPEC;
253 else
254 return -1;
255 --nvec;
256 ++vec;
257 }
258 if(nvec == 0)
259 return -1;
260 /* Possibilities are:
261 *
262 * /path/to/unix/socket an AF_UNIX socket
263 * * PORT any address, specific port
264 * PORT any address, specific port
265 * ADDRESS PORT specific address, specific port
266 */
267 if(vec[0][0] == '/' && na->af == AF_UNSPEC)
268 na->af = AF_UNIX;
269 if(na->af == AF_UNIX) {
270 if(nvec != 1)
271 return -1;
272 na->address = xstrdup(vec[0]);
273 na->port = -1; /* makes no sense */
274 } else {
275 switch(nvec) {
276 case 1:
277 na->address = NULL;
278 port = vec[0];
279 break;
280 case 2:
281 if(!strcmp(vec[0], "*"))
282 na->address = NULL;
283 else
284 na->address = xstrdup(vec[0]);
285 port = vec[1];
286 break;
287 default:
288 return -1;
289 }
290 if(port[strspn(port, "0123456789")])
291 return -1;
05b3f1f6 292 e = xstrtol(&p, port, NULL, 10);
76e72f65
RK
293
294 if(e)
295 return -1;
296 if(p > 65535)
297 return -1;
298 na->port = (int)p;
299 }
300 return 0;
301}
302
303/** @brief Format a @ref netaddress structure
304 * @param na Network address to format
305 * @param nvecp Where to put string count, or NULL
306 * @param vecp Where to put strings
307 *
308 * The formatted form is suitable for passing to netaddress_parse().
309 */
310void netaddress_format(const struct netaddress *na,
311 int *nvecp,
312 char ***vecp) {
313 struct vector v[1];
314
315 vector_init(v);
316 switch(na->af) {
317 case AF_UNSPEC: vector_append(v, xstrdup("-")); break;
318 case AF_INET: vector_append(v, xstrdup("-4")); break;
319 case AF_INET6: vector_append(v, xstrdup("-6")); break;
320 case AF_UNIX: vector_append(v, xstrdup("-unix")); break;
321 }
322 if(na->address)
323 vector_append(v, xstrdup(na->address));
324 else
325 vector_append(v, xstrdup("*"));
326 if(na->port != -1) {
327 char buffer[64];
328
3c95f40e 329 byte_snprintf(buffer, sizeof buffer, "%d", na->port);
76e72f65
RK
330 vector_append(v, xstrdup(buffer));
331 }
332 vector_terminate(v);
333 if(nvecp)
334 *nvecp = v->nvec;
335 if(vecp)
336 *vecp = v->vec;
337}
338
339/** @brief Resolve a network address
340 * @param na Address structure
341 * @param passive True if passive (bindable) address is desired
802bb596
MW
342 * @param type Socket type (e.g., @c SOCK_STREAM, @c SOCK_DGRAM)
343 * @param[out] raddr_out Vector of address/length pairs
344 * @param[out] nraddr_out Length of output vector
345 * @return Zero on success, -1 on error
81abb3f1 346 *
802bb596 347 * Call netaddress_free_resolved() to free the resolved addresses.
76e72f65 348 */
802bb596
MW
349int netaddress_resolve(const struct netaddress *na, int passive, int type,
350 struct resolved **raddr_out, size_t *nraddr_out) {
351 struct resolved *raddr;
352 size_t i, nraddr;
353 struct addrinfo *res, *ai, hints[1];
6735af5f 354 struct sockaddr_un *sun;
76e72f65
RK
355 char service[64];
356 int rc;
92db088e 357 char errbuf[1024];
76e72f65 358
6735af5f
MW
359#if HAVE_SYS_UN_H
360 if (na->af == AF_UNIX) {
361 /* `getaddrinfo' won't work, so we make our own one */
802bb596
MW
362 raddr = xmalloc(sizeof(*raddr));
363 raddr->len = offsetof(struct sockaddr_un, sun_path) +
6735af5f 364 strlen(na->address) + 1;
802bb596 365 sun = xmalloc_noptr(raddr->len);
6735af5f
MW
366 sun->sun_family = AF_UNIX;
367 strcpy(sun->sun_path, na->address);
802bb596
MW
368 raddr->sa = (struct sockaddr *)sun;
369 nraddr = 1;
6735af5f
MW
370 } else
371#endif
372 {
373 /* get the system to do the heavy lifting */
374 memset(hints, 0, sizeof hints);
375 hints->ai_family = na->af;
802bb596 376 hints->ai_socktype = type;
6735af5f
MW
377 hints->ai_flags = passive ? AI_PASSIVE : 0;
378 byte_snprintf(service, sizeof service, "%d", na->port);
379 rc = getaddrinfo(na->address, service, hints, &res);
380 if(rc) {
381 disorder_error(0, "getaddrinfo %s %d: %s",
382 na->address ? na->address : "*",
383 na->port,
384 format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
802bb596
MW
385 return -1;
386 }
387 /* copy the addresses into an output vector */
388 for(ai = res, nraddr = 0; ai; ai = ai->ai_next, nraddr++);
389 raddr = xmalloc(nraddr*sizeof(*raddr));
390 for(ai = res, i = 0; ai; ai = ai->ai_next, i++) {
391 raddr[i].sa = xmalloc_noptr(ai->ai_addrlen);
392 raddr[i].len = ai->ai_addrlen;
393 memcpy(raddr[i].sa, ai->ai_addr, ai->ai_addrlen);
6735af5f 394 }
802bb596 395 freeaddrinfo(res);
76e72f65 396 }
802bb596
MW
397 *raddr_out = raddr;
398 *nraddr_out = nraddr;
399 return 0;
76e72f65
RK
400}
401
802bb596
MW
402/** @brief Free a vector of resolved addresses */
403void netaddress_free_resolved(struct resolved *raddr, size_t nraddr)
404{
405 size_t i;
406
407 if (!raddr)
408 return;
409 for(i = 0; i < nraddr; i++)
410 xfree(raddr[i].sa);
411 xfree(raddr);
81abb3f1
MW
412}
413
460b9539 414/*
415Local Variables:
416c-basic-offset:2
417comment-column:40
418End:
419*/