minor fixes to hostport stuff
[libradsec.git] / hostport.c
1 /*
2  * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <netdb.h>
12 #include "debug.h"
13 #include "util.h"
14 #include "list.h"
15 #include "hostport.h"
16
17 void freehostport(struct hostportres *hp) {
18     if (hp) {
19         free(hp->host);
20         free(hp->port);
21         if (hp->addrinfo)
22             freeaddrinfo(hp->addrinfo);
23         free(hp);
24     }
25 }
26
27 static int parsehostport(struct hostportres *hp, char *hostport, char *default_port) {
28     char *p, *field;
29     int ipv6 = 0;
30
31     if (!hostport) {
32         hp->port = default_port ? stringcopy(default_port, 0) : NULL;
33         return 1;
34     }
35     p = hostport;
36     /* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
37     if (*p == '[') {
38         p++;
39         field = p;
40         for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
41         if (*p != ']') {
42             debug(DBG_ERR, "no ] matching initial [");
43             return 0;
44         }
45         ipv6 = 1;
46     } else {
47         field = p;
48         for (; *p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
49     }
50     if (field == p) {
51         debug(DBG_ERR, "missing host/address");
52         return 0;
53     }
54
55     hp->host = stringcopy(field, p - field);
56     if (ipv6) {
57         p++;
58         if (*p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n') {
59             debug(DBG_ERR, "unexpected character after ]");
60             return 0;
61         }
62     }
63     if (*p == ':') {
64             /* port number or service name is specified */;
65             field = ++p;
66             for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
67             if (field == p) {
68                 debug(DBG_ERR, "syntax error, : but no following port");
69                 return 0;
70             }
71             hp->port = stringcopy(field, p - field);
72     } else
73         hp->port = default_port ? stringcopy(default_port, 0) : NULL;
74     return 1;
75 }
76     
77 struct hostportres *newhostport(char *hostport, char *default_port, uint8_t prefixok) {
78     struct hostportres *hp;
79     char *slash, *s;
80     int plen;
81     
82     hp = malloc(sizeof(struct hostportres));
83     if (!hp) {
84         debug(DBG_ERR, "resolve_newhostport: malloc failed");
85         goto errexit;
86     }
87     memset(hp, 0, sizeof(struct hostportres));
88
89     if (!parsehostport(hp, hostport, default_port))
90         goto errexit;
91
92     if (hp->host && !strcmp(hp->host, "*")) {
93         free(hp->host);
94         hp->host = NULL;
95     }
96
97     slash = hp->host ? strchr(hp->host, '/') : NULL;
98     if (slash) {
99         if (!prefixok) {
100             debug(DBG_WARN, "newhostport: prefix not allowed here", hp->host);
101             goto errexit;
102         }
103         s = slash + 1;
104         if (!*s) {
105             debug(DBG_WARN, "newhostport: prefix length must be specified after the / in %s", hp->host);
106             goto errexit;
107         }
108         for (; *s; s++)
109             if (*s < '0' || *s > '9') {
110                 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
111                 goto errexit;
112             }
113         plen = atoi(slash + 1);
114         if (plen < 0 || plen > 128) {
115             debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
116             goto errexit;
117         }
118         hp->prefixlen = plen;
119         *slash = '\0';
120     } else
121         hp->prefixlen = 255;
122     return hp;
123
124  errexit:
125     freehostport(hp);
126     return NULL;
127 }
128
129 int resolvehostport(struct hostportres *hp, int socktype, uint8_t passive) {
130     struct addrinfo hints, *res;
131
132     memset(&hints, 0, sizeof(hints));
133     hints.ai_socktype = socktype;
134     hints.ai_family = AF_UNSPEC;
135     if (passive)
136         hints.ai_flags = AI_PASSIVE;
137     
138     if (!hp->host && !hp->port) {
139         /* getaddrinfo() doesn't like host and port to be NULL */
140         if (getaddrinfo(hp->host, "1812" /* can be anything */, &hints, &hp->addrinfo)) {
141             debug(DBG_WARN, "resolvehostport: can't resolve (null) port (null)");
142             goto errexit;
143         }
144         for (res = hp->addrinfo; res; res = res->ai_next)
145             port_set(res->ai_addr, 0);
146     } else {
147         if (hp->prefixlen != 255)
148             hints.ai_flags |= AI_NUMERICHOST;
149         if (getaddrinfo(hp->host, hp->port, &hints, &hp->addrinfo)) {
150             debug(DBG_WARN, "resolvehostport: can't resolve %s port %s", hp->host ? hp->host : "(null)", hp->port ? hp->port : "(null)");
151             goto errexit;
152         }
153         if (hp->prefixlen != 255) {
154             switch (hp->addrinfo->ai_family) {
155             case AF_INET:
156                 if (hp->prefixlen > 32) {
157                     debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
158                     goto errexit;
159                 }
160                 break;
161             case AF_INET6:
162                 break;
163             default:
164                 debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
165                 goto errexit;
166             }
167         }
168     }
169     return 1;
170
171  errexit:
172     if (hp->addrinfo)
173         freeaddrinfo(hp->addrinfo);
174     return 0;
175 }         
176
177 int addhostport(struct list **hostports, char *hostport, char *portdefault, uint8_t prefixok) {
178     struct hostportres *hp;
179
180     hp = newhostport(hostport, portdefault, prefixok);
181     if (!hp)
182         return 0;
183     if (!*hostports)
184         *hostports = list_create();
185     if (!*hostports || !list_push(*hostports, hp)) {
186         freehostport(hp);
187         debug(DBG_ERR, "addhostport: malloc failed");
188         return 0;
189     }
190     return 1;
191 }
192
193 void freehostports(struct list *hostports) {
194     struct hostportres *hp;
195
196     while ((hp = (struct hostportres *)list_shift(hostports)))
197         freehostport(hp);
198     list_destroy(hostports);
199 }
200
201 int resolvehostports(struct list *hostports, int socktype) {
202     struct list_node *entry;
203     struct hostportres *hp;
204     
205     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
206         hp = (struct hostportres *)entry->data;
207         if (!hp->addrinfo && !resolvehostport(hp, socktype, 0))
208             return 0;
209     }
210     return 1;
211 }
212
213 struct addrinfo *resolvepassiveaddrinfo(char *hostport, char *default_port, int socktype) {
214     struct addrinfo *ai = NULL;
215     struct hostportres *hp = newhostport(hostport, default_port, 0);
216     if (hp && resolvehostport(hp, socktype, 1)) {
217         ai = hp->addrinfo;
218         hp->addrinfo = NULL;
219     }
220     freehostport(hp);
221     return ai;
222 }
223
224 /* returns 1 if the len first bits are equal, else 0 */
225 static int prefixmatch(void *a1, void *a2, uint8_t len) {
226     static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
227     uint8_t r, l = len / 8;
228     if (l && memcmp(a1, a2, l))
229         return 0;
230     r = len % 8;
231     if (!r)
232         return 1;
233     return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
234 }
235
236 int addressmatches(struct list *hostports, struct sockaddr *addr) {
237     struct sockaddr_in6 *sa6 = NULL;
238     struct in_addr *a4 = NULL;
239     struct addrinfo *res;
240     struct list_node *entry;
241     struct hostportres *hp = NULL;
242     
243     if (addr->sa_family == AF_INET6) {
244         sa6 = (struct sockaddr_in6 *)addr;
245         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
246             a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
247             sa6 = NULL;
248         }
249     } else
250         a4 = &((struct sockaddr_in *)addr)->sin_addr;
251
252     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
253         hp = (struct hostportres *)entry->data;
254         for (res = hp->addrinfo; res; res = res->ai_next)
255             if (hp->prefixlen == 255) {
256                 if ((a4 && res->ai_family == AF_INET &&
257                      !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
258                     (sa6 && res->ai_family == AF_INET6 &&
259                      !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
260                     return 1;
261             } else {
262                 if ((a4 && res->ai_family == AF_INET &&
263                      prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
264                     (sa6 && res->ai_family == AF_INET6 &&
265                      prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen)))
266                     return 1;
267             }
268     }
269     return 0;
270 }
271
272 int connecttcphostlist(struct list *hostports,  struct addrinfo *src) {
273     int s;
274     struct list_node *entry;
275     struct hostportres *hp = NULL;
276
277     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
278         hp = (struct hostportres *)entry->data;
279         debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
280         if ((s = connecttcp(hp->addrinfo, src)) >= 0) {
281             debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
282             return s;
283         }
284     }
285     debug(DBG_ERR, "connecttcphostlist: failed");
286     return -1;
287 }