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