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