2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2007, 2008, 2013 Richard Kettlewell
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 3 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
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @brief Socket address support */
25 #include <sys/types.h>
27 # include <sys/socket.h>
30 # include <netinet/in.h>
33 # include <arpa/inet.h>
39 # include <Ws2tcpip.h>
47 #include "configuration.h"
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
56 * This converts one or two strings into an address specification suitable
57 * for passing to socket(), bind() etc.
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.
63 * @p namep is used to return a description of the address suitable
64 * for use in log messages.
66 * If an error occurs a message is logged and a null pointer returned.
68 struct addrinfo
*get_address(const struct stringlist
*a
,
69 const struct addrinfo
*pref
,
78 byte_xasprintf(&name
, "host * service %s", a
->s
[0]);
79 if((rc
= getaddrinfo(0, a
->s
[0], pref
, &res
))) {
80 disorder_error(0, "getaddrinfo %s: %s", a
->s
[0],
81 format_error(ec_getaddrinfo
, rc
, errbuf
, sizeof errbuf
));
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
))) {
88 disorder_error(0, "getaddrinfo %s %s: %s",
90 format_error(ec_getaddrinfo
, rc
, errbuf
, sizeof errbuf
));
95 disorder_error(0, "invalid network address specification (n=%d)", a
->n
);
98 if(!res
|| (pref
&& res
->ai_socktype
!= pref
->ai_socktype
)) {
99 disorder_error(0, "getaddrinfo didn't give us a suitable socket address");
109 /** @brief Comparison function for address information
111 * Suitable for qsort().
113 int addrinfocmp(const struct addrinfo
*a
,
114 const struct addrinfo
*b
) {
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
;
118 return sockaddrcmp(a
->ai_addr
, b
->ai_addr
);
121 /** @brief Comparison function for socket addresses
123 * Suitable for qsort().
125 int sockaddrcmp(const struct sockaddr
*a
,
126 const struct sockaddr
*b
) {
127 const struct sockaddr_in
*ina
, *inb
;
128 const struct sockaddr_in6
*in6a
, *in6b
;
130 if(a
->sa_family
!= b
->sa_family
) return a
->sa_family
- b
->sa_family
;
131 switch(a
->sa_family
) {
133 ina
= (const struct sockaddr_in
*)a
;
134 inb
= (const struct sockaddr_in
*)b
;
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
;
139 in6a
= (const struct sockaddr_in6
*)a
;
140 in6b
= (const struct sockaddr_in6
*)b
;
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
));
146 disorder_fatal(0, "unsupported protocol family %d", a
->sa_family
);
150 /** @brief Return nonzero if @p sin4 is an IPv4 multicast address */
151 static inline int multicast4(const struct sockaddr_in
*sin4
) {
152 return IN_MULTICAST(ntohl(sin4
->sin_addr
.s_addr
));
155 /** @brief Return nonzero if @p sin6 is an IPv6 multicast address */
156 static inline int multicast6(const struct sockaddr_in6
*sin6
) {
157 return IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
);
160 /** @brief Return true if @p sa represents a multicast address */
161 int multicast(const struct sockaddr
*sa
) {
162 switch(sa
->sa_family
) {
164 return multicast4((const struct sockaddr_in
*)sa
);
166 return multicast6((const struct sockaddr_in6
*)sa
);
172 /** @brief Format an IPv4 address */
173 static inline char *format_sockaddr4(const struct sockaddr_in
*sin4
) {
174 char buffer
[1024], *r
;
177 byte_xasprintf(&r
, "%s port %u",
178 inet_ntop(sin4
->sin_family
, (void *)&sin4
->sin_addr
,
179 buffer
, sizeof buffer
),
180 ntohs(sin4
->sin_port
));
182 byte_xasprintf(&r
, "%s",
183 inet_ntop(sin4
->sin_family
, (void *)&sin4
->sin_addr
,
184 buffer
, sizeof buffer
));
188 /** @brief Format an IPv6 address */
189 static inline char *format_sockaddr6(const struct sockaddr_in6
*sin6
) {
190 char buffer
[1024], *r
;
193 byte_xasprintf(&r
, "%s port %u",
194 inet_ntop(sin6
->sin6_family
, (void *)&sin6
->sin6_addr
,
195 buffer
, sizeof buffer
),
196 ntohs(sin6
->sin6_port
));
198 byte_xasprintf(&r
, "%s",
199 inet_ntop(sin6
->sin6_family
, (void *)&sin6
->sin6_addr
,
200 buffer
, sizeof buffer
));
205 /** @brief Format a UNIX socket address */
206 static inline char *format_sockaddrun(const struct sockaddr_un
*sun
) {
207 return xstrdup(sun
->sun_path
);
211 /** @brief Construct a text description a sockaddr
212 * @param sa Socket address
213 * @return Human-readable form of address
215 char *format_sockaddr(const struct sockaddr
*sa
) {
216 switch(sa
->sa_family
) {
218 return format_sockaddr4((const struct sockaddr_in
*)sa
);
220 return format_sockaddr6((const struct sockaddr_in6
*)sa
);
223 return format_sockaddrun((const struct sockaddr_un
*)sa
);
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
236 int netaddress_parse(struct netaddress
*na
,
244 if(nvec
> 0 && vec
[0][0] == '-') {
245 if(!strcmp(vec
[0], "-4"))
247 else if(!strcmp(vec
[0], "-6"))
249 else if(!strcmp(vec
[0], "-unix"))
251 else if(!strcmp(vec
[0], "-"))
260 /* Possibilities are:
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
267 if(vec
[0][0] == '/' && na
->af
== AF_UNSPEC
)
269 if(na
->af
== AF_UNIX
) {
272 na
->address
= xstrdup(vec
[0]);
273 na
->port
= -1; /* makes no sense */
281 if(!strcmp(vec
[0], "*"))
284 na
->address
= xstrdup(vec
[0]);
290 if(port
[strspn(port
, "0123456789")])
292 e
= xstrtol(&p
, port
, NULL
, 10);
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
308 * The formatted form is suitable for passing to netaddress_parse().
310 void netaddress_format(const struct netaddress
*na
,
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;
323 vector_append(v
, xstrdup(na
->address
));
325 vector_append(v
, xstrdup("*"));
329 byte_snprintf(buffer
, sizeof buffer
, "%d", na
->port
);
330 vector_append(v
, xstrdup(buffer
));
339 /** @brief Resolve a network address
340 * @param na Address structure
341 * @param passive True if passive (bindable) address is desired
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
347 * Call netaddress_free_resolved() to free the resolved addresses.
349 int netaddress_resolve(const struct netaddress
*na
, int passive
, int type
,
350 struct resolved
**raddr_out
, size_t *nraddr_out
) {
351 struct resolved
*raddr
;
353 struct addrinfo
*res
, *ai
, hints
[1];
354 struct sockaddr_un
*sun
;
360 if (na
->af
== AF_UNIX
) {
361 /* `getaddrinfo' won't work, so we make our own one */
362 raddr
= xmalloc(sizeof(*raddr
));
363 raddr
->len
= offsetof(struct sockaddr_un
, sun_path
) +
364 strlen(na
->address
) + 1;
365 sun
= xmalloc_noptr(raddr
->len
);
366 sun
->sun_family
= AF_UNIX
;
367 strcpy(sun
->sun_path
, na
->address
);
368 raddr
->sa
= (struct sockaddr
*)sun
;
373 /* get the system to do the heavy lifting */
374 memset(hints
, 0, sizeof hints
);
375 hints
->ai_family
= na
->af
;
376 hints
->ai_socktype
= type
;
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
);
381 disorder_error(0, "getaddrinfo %s %d: %s",
382 na
->address ? na
->address
: "*",
384 format_error(ec_getaddrinfo
, rc
, errbuf
, sizeof errbuf
));
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
);
398 *nraddr_out
= nraddr
;
402 /** @brief Free a vector of resolved addresses */
403 void netaddress_free_resolved(struct resolved
*raddr
, size_t nraddr
)
409 for(i
= 0; i
< nraddr
; i
++)