chiark / gitweb /
Cope with various header files being missing.
[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
460b9539 23#include <sys/types.h>
cca89d7c
RK
24#if HAVE_SYS_SOCKET_H
25# include <sys/socket.h>
26#endif
27#if HAVE_NETINET_IN_H
28# include <netinet/in.h>
29#endif
30#if HAVE_ARPA_INET_H
31# include <arpa/inet.h>
32#endif
33#if HAVE_SYS_UN_H
34# include <sys/un.h>
35#endif
460b9539 36
37#include "log.h"
38#include "printf.h"
460b9539 39#include "addr.h"
6fba990c 40#include "mem.h"
76e72f65
RK
41#include "syscalls.h"
42#include "configuration.h"
43#include "vector.h"
460b9539 44
ec6f8488 45/** @brief Convert a pair of strings to an address
46 * @param a Pointer to string list
47 * @param pref Hints structure for getaddrinfo, or NULL
48 * @param namep Where to store address description, or NULL
49 * @return Address info structure or NULL on error
50 *
51 * This converts one or two strings into an address specification suitable
52 * for passing to socket(), bind() etc.
53 *
54 * If there is only one string then it is assumed to be the service
55 * name (port number). If there are two then the first is the host
56 * name and the second the service name.
57 *
58 * @p namep is used to return a description of the address suitable
59 * for use in log messages.
60 *
61 * If an error occurs a message is logged and a null pointer returned.
62 */
460b9539 63struct addrinfo *get_address(const struct stringlist *a,
64 const struct addrinfo *pref,
65 char **namep) {
66 struct addrinfo *res;
67 char *name;
68 int rc;
e83d0967
RK
69
70 switch(a->n) {
71 case 1:
460b9539 72 byte_xasprintf(&name, "host * service %s", a->s[0]);
73 if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
2e9ba080 74 disorder_error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
460b9539 75 return 0;
76 }
e83d0967
RK
77 break;
78 case 2:
460b9539 79 byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
80 if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
2e9ba080
RK
81 disorder_error(0, "getaddrinfo %s %s: %s",
82 a->s[0], a->s[1], gai_strerror(rc));
460b9539 83 return 0;
84 }
e83d0967
RK
85 break;
86 default:
2e9ba080 87 disorder_error(0, "invalid network address specification (n=%d)", a->n);
e83d0967 88 return 0;
460b9539 89 }
e83d0967 90 if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
2e9ba080 91 disorder_error(0, "getaddrinfo didn't give us a suitable socket address");
460b9539 92 if(res)
93 freeaddrinfo(res);
94 return 0;
95 }
96 if(namep)
97 *namep = name;
98 return res;
99}
100
d7b6f0d1 101/** @brief Comparison function for address information
102 *
103 * Suitable for qsort().
104 */
460b9539 105int addrinfocmp(const struct addrinfo *a,
106 const struct addrinfo *b) {
460b9539 107 if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family;
108 if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype;
109 if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol;
80dc2c5f
RK
110 return sockaddrcmp(a->ai_addr, b->ai_addr);
111}
112
113/** @brief Comparison function for socket addresses
114 *
115 * Suitable for qsort().
116 */
117int sockaddrcmp(const struct sockaddr *a,
118 const struct sockaddr *b) {
119 const struct sockaddr_in *ina, *inb;
120 const struct sockaddr_in6 *in6a, *in6b;
121
122 if(a->sa_family != b->sa_family) return a->sa_family - b->sa_family;
123 switch(a->sa_family) {
460b9539 124 case PF_INET:
80dc2c5f
RK
125 ina = (const struct sockaddr_in *)a;
126 inb = (const struct sockaddr_in *)b;
460b9539 127 if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port;
128 return ina->sin_addr.s_addr - inb->sin_addr.s_addr;
129 break;
130 case PF_INET6:
80dc2c5f
RK
131 in6a = (const struct sockaddr_in6 *)a;
132 in6b = (const struct sockaddr_in6 *)b;
460b9539 133 if(in6a->sin6_port != in6b->sin6_port)
134 return in6a->sin6_port - in6b->sin6_port;
135 return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
136 sizeof (struct in6_addr));
137 default:
2e9ba080 138 disorder_fatal(0, "unsupported protocol family %d", a->sa_family);
460b9539 139 }
140}
6fba990c 141
0590cedc 142/** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
8d516888
RK
143static inline int multicast4(const struct sockaddr_in *sin4) {
144 return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr));
6fba990c
RK
145}
146
0590cedc 147/** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
6fba990c
RK
148static inline int multicast6(const struct sockaddr_in6 *sin6) {
149 return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr);
150}
151
152/** @brief Return true if @p sa represents a multicast address */
153int multicast(const struct sockaddr *sa) {
154 switch(sa->sa_family) {
155 case AF_INET:
156 return multicast4((const struct sockaddr_in *)sa);
157 case AF_INET6:
158 return multicast6((const struct sockaddr_in6 *)sa);
159 default:
160 return 0;
161 }
162}
163
0590cedc 164/** @brief Format an IPv4 address */
8d516888 165static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
6fba990c
RK
166 char buffer[1024], *r;
167
8d516888 168 if(sin4->sin_port)
6fba990c 169 byte_xasprintf(&r, "%s port %u",
8d516888 170 inet_ntop(sin4->sin_family, &sin4->sin_addr,
6fba990c 171 buffer, sizeof buffer),
8d516888 172 ntohs(sin4->sin_port));
6fba990c
RK
173 else
174 byte_xasprintf(&r, "%s",
8d516888 175 inet_ntop(sin4->sin_family, &sin4->sin_addr,
6fba990c
RK
176 buffer, sizeof buffer));
177 return r;
178}
179
0590cedc 180/** @brief Format an IPv6 address */
6fba990c
RK
181static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
182 char buffer[1024], *r;
183
184 if(sin6->sin6_port)
185 byte_xasprintf(&r, "%s port %u",
186 inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
187 buffer, sizeof buffer),
188 ntohs(sin6->sin6_port));
189 else
190 byte_xasprintf(&r, "%s",
191 inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
192 buffer, sizeof buffer));
193 return r;
194}
195
cca89d7c 196#if HAVE_SYS_UN_H
0590cedc 197/** @brief Format a UNIX socket address */
6fba990c
RK
198static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
199 return xstrdup(sun->sun_path);
200}
cca89d7c 201#endif
6fba990c 202
0590cedc
RK
203/** @brief Construct a text description a sockaddr
204 * @param sa Socket address
205 * @return Human-readable form of address
206 */
6fba990c
RK
207char *format_sockaddr(const struct sockaddr *sa) {
208 switch(sa->sa_family) {
209 case AF_INET:
210 return format_sockaddr4((const struct sockaddr_in *)sa);
211 case AF_INET6:
212 return format_sockaddr6((const struct sockaddr_in6 *)sa);
cca89d7c 213#if HAVE_SYS_UN_H
6fba990c
RK
214 case AF_UNIX:
215 return format_sockaddrun((const struct sockaddr_un *)sa);
cca89d7c 216#endif
6fba990c
RK
217 default:
218 return 0;
219 }
220}
221
76e72f65
RK
222/** @brief Parse the text form of a network address
223 * @param na Where to store result
224 * @param nvec Number of strings
225 * @param vec List of strings
226 * @return 0 on success, -1 on syntax error
227 */
228int netaddress_parse(struct netaddress *na,
229 int nvec,
230 char **vec) {
231 const char *port;
232
233 na->af = AF_UNSPEC;
234 if(nvec > 0 && vec[0][0] == '-') {
235 if(!strcmp(vec[0], "-4"))
236 na->af = AF_INET;
237 else if(!strcmp(vec[0], "-6"))
238 na->af = AF_INET6;
239 else if(!strcmp(vec[0], "-unix"))
240 na->af = AF_UNIX;
241 else if(!strcmp(vec[0], "-"))
242 na->af = AF_UNSPEC;
243 else
244 return -1;
245 --nvec;
246 ++vec;
247 }
248 if(nvec == 0)
249 return -1;
250 /* Possibilities are:
251 *
252 * /path/to/unix/socket an AF_UNIX socket
253 * * PORT any address, specific port
254 * PORT any address, specific port
255 * ADDRESS PORT specific address, specific port
256 */
257 if(vec[0][0] == '/' && na->af == AF_UNSPEC)
258 na->af = AF_UNIX;
259 if(na->af == AF_UNIX) {
260 if(nvec != 1)
261 return -1;
262 na->address = xstrdup(vec[0]);
263 na->port = -1; /* makes no sense */
264 } else {
265 switch(nvec) {
266 case 1:
267 na->address = NULL;
268 port = vec[0];
269 break;
270 case 2:
271 if(!strcmp(vec[0], "*"))
272 na->address = NULL;
273 else
274 na->address = xstrdup(vec[0]);
275 port = vec[1];
276 break;
277 default:
278 return -1;
279 }
280 if(port[strspn(port, "0123456789")])
281 return -1;
282 long p;
283 int e = xstrtol(&p, port, NULL, 10);
284
285 if(e)
286 return -1;
287 if(p > 65535)
288 return -1;
289 na->port = (int)p;
290 }
291 return 0;
292}
293
294/** @brief Format a @ref netaddress structure
295 * @param na Network address to format
296 * @param nvecp Where to put string count, or NULL
297 * @param vecp Where to put strings
298 *
299 * The formatted form is suitable for passing to netaddress_parse().
300 */
301void netaddress_format(const struct netaddress *na,
302 int *nvecp,
303 char ***vecp) {
304 struct vector v[1];
305
306 vector_init(v);
307 switch(na->af) {
308 case AF_UNSPEC: vector_append(v, xstrdup("-")); break;
309 case AF_INET: vector_append(v, xstrdup("-4")); break;
310 case AF_INET6: vector_append(v, xstrdup("-6")); break;
311 case AF_UNIX: vector_append(v, xstrdup("-unix")); break;
312 }
313 if(na->address)
314 vector_append(v, xstrdup(na->address));
315 else
316 vector_append(v, xstrdup("*"));
317 if(na->port != -1) {
318 char buffer[64];
319
320 snprintf(buffer, sizeof buffer, "%d", na->port);
321 vector_append(v, xstrdup(buffer));
322 }
323 vector_terminate(v);
324 if(nvecp)
325 *nvecp = v->nvec;
326 if(vecp)
327 *vecp = v->vec;
328}
329
330/** @brief Resolve a network address
331 * @param na Address structure
332 * @param passive True if passive (bindable) address is desired
333 * @param protocol Protocol number desired (e.g. @c IPPROTO_TCP)
334 * @return List of suitable addresses or NULL
335 */
336struct addrinfo *netaddress_resolve(const struct netaddress *na,
337 int passive,
338 int protocol) {
339 struct addrinfo *res, hints[1];
340 char service[64];
341 int rc;
342
343 memset(hints, 0, sizeof hints);
344 hints->ai_family = na->af;
345 hints->ai_protocol = protocol;
346 hints->ai_flags = passive ? AI_PASSIVE : 0;
347 snprintf(service, sizeof service, "%d", na->port);
348 rc = getaddrinfo(na->address, service, hints, &res);
349 if(rc) {
2e9ba080
RK
350 disorder_error(0, "getaddrinfo %s %d: %s",
351 na->address ? na->address : "*",
352 na->port, gai_strerror(rc));
76e72f65
RK
353 return NULL;
354 }
355 return res;
356}
357
460b9539 358/*
359Local Variables:
360c-basic-offset:2
361comment-column:40
362End:
363*/