chiark / gitweb /
tests for cache.c
[disorder] / lib / addr.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2007 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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/addr.c Socket address support */
21
22 #include <config.h>
23 #include "types.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <arpa/inet.h>
32 #include <sys/un.h>
33
34 #include "log.h"
35 #include "printf.h"
36 #include "configuration.h"
37 #include "addr.h"
38 #include "mem.h"
39
40 /** @brief Convert a pair of strings to an address
41  * @param a Pointer to string list
42  * @param pref Hints structure for getaddrinfo, or NULL
43  * @param namep Where to store address description, or NULL
44  * @return Address info structure or NULL on error
45  *
46  * This converts one or two strings into an address specification suitable
47  * for passing to socket(), bind() etc.
48  *
49  * If there is only one string then it is assumed to be the service
50  * name (port number).  If there are two then the first is the host
51  * name and the second the service name.
52  *
53  * @p namep is used to return a description of the address suitable
54  * for use in log messages.
55  *
56  * If an error occurs a message is logged and a null pointer returned.
57  */
58 struct addrinfo *get_address(const struct stringlist *a,
59                              const struct addrinfo *pref,
60                              char **namep) {
61   struct addrinfo *res;
62   char *name;
63   int rc;
64
65   switch(a->n) {  
66   case 1:
67     byte_xasprintf(&name, "host * service %s", a->s[0]);
68     if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
69       error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
70       return 0;
71     }
72     break;
73   case 2:
74     byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
75     if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
76       error(0, "getaddrinfo %s %s: %s", a->s[0], a->s[1], gai_strerror(rc));
77       return 0;
78     }
79     break;
80   default:
81     error(0, "invalid network address specification (n=%d)", a->n);
82     return 0;
83   }
84   if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
85     error(0, "getaddrinfo didn't give us a suitable socket address");
86     if(res)
87       freeaddrinfo(res);
88     return 0;
89   }
90   if(namep)
91     *namep = name;
92   return res;
93 }
94
95 /** @brief Comparison function for address information
96  *
97  * Suitable for qsort().
98  */
99 int addrinfocmp(const struct addrinfo *a,
100                 const struct addrinfo *b) {
101   const struct sockaddr_in *ina, *inb;
102   const struct sockaddr_in6 *in6a, *in6b;
103   
104   if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family;
105   if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype;
106   if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol;
107   switch(a->ai_protocol) {
108   case PF_INET:
109     ina = (const struct sockaddr_in *)a->ai_addr;
110     inb = (const struct sockaddr_in *)b->ai_addr;
111     if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port;
112     return ina->sin_addr.s_addr - inb->sin_addr.s_addr;
113     break;
114   case PF_INET6:
115     in6a = (const struct sockaddr_in6 *)a->ai_addr;
116     in6b = (const struct sockaddr_in6 *)b->ai_addr;
117     if(in6a->sin6_port != in6b->sin6_port)
118       return in6a->sin6_port - in6b->sin6_port;
119     return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
120                   sizeof (struct in6_addr));
121   default:
122     error(0, "unsupported protocol family %d", a->ai_protocol);
123     return memcmp(a->ai_addr, b->ai_addr, a->ai_addrlen); /* kludge */
124   }
125 }
126
127 static inline int multicast4(const struct sockaddr_in *sin) {
128   return IN_MULTICAST(ntohl(sin->sin_addr.s_addr));
129 }
130
131 static inline int multicast6(const struct sockaddr_in6 *sin6) {
132   return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr);
133 }
134
135 /** @brief Return true if @p sa represents a multicast address */
136 int multicast(const struct sockaddr *sa) {
137   switch(sa->sa_family) {
138   case AF_INET:
139     return multicast4((const struct sockaddr_in *)sa);
140   case AF_INET6:
141     return multicast6((const struct sockaddr_in6 *)sa);
142   default:
143     return 0;
144   }
145 }
146
147 static inline char *format_sockaddr4(const struct sockaddr_in *sin) {
148   char buffer[1024], *r;
149
150   if(sin->sin_port)
151     byte_xasprintf(&r, "%s port %u",
152                    inet_ntop(sin->sin_family, &sin->sin_addr,
153                              buffer, sizeof buffer),
154                    ntohs(sin->sin_port));
155   else
156     byte_xasprintf(&r, "%s",
157                    inet_ntop(sin->sin_family, &sin->sin_addr,
158                              buffer, sizeof buffer));
159   return r;
160 }
161
162 static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
163   char buffer[1024], *r;
164
165   if(sin6->sin6_port)
166     byte_xasprintf(&r, "%s port %u",
167                    inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
168                              buffer, sizeof buffer),
169                    ntohs(sin6->sin6_port));
170   else
171     byte_xasprintf(&r, "%s",
172                    inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
173                              buffer, sizeof buffer));
174   return r;
175 }
176
177 static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
178   return xstrdup(sun->sun_path);
179 }
180     
181 /** @brief Construct a text description a sockaddr */
182 char *format_sockaddr(const struct sockaddr *sa) {
183   switch(sa->sa_family) {
184   case AF_INET:
185     return format_sockaddr4((const struct sockaddr_in *)sa);
186   case AF_INET6:
187     return format_sockaddr6((const struct sockaddr_in6 *)sa);
188   case AF_UNIX:
189     return format_sockaddrun((const struct sockaddr_un *)sa);
190   default:
191     return 0;
192   }
193 }
194
195 /*
196 Local Variables:
197 c-basic-offset:2
198 comment-column:40
199 End:
200 */