small fix to allow IPv6 prefixes
[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 != ' ' && *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 != ' ' && *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 = hostport ? strchr(hostport, '/') : 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     } else
120         hp->prefixlen = 255;
121     return hp;
122
123  errexit:
124     freehostport(hp);
125     return NULL;
126 }
127
128 int resolvehostport(struct hostportres *hp, int socktype, uint8_t passive) {
129     struct addrinfo hints, *res;
130
131     memset(&hints, 0, sizeof(hints));
132     hints.ai_socktype = socktype;
133     hints.ai_family = AF_UNSPEC;
134     if (passive)
135         hints.ai_flags = AI_PASSIVE;
136     
137     if (!hp->host && !hp->port) {
138         /* getaddrinfo() doesn't like host and port to be NULL */
139         if (getaddrinfo(hp->host, "1812" /* can be anything */, &hints, &hp->addrinfo)) {
140             debug(DBG_WARN, "resolvehostport: can't resolve (null) port (null)");
141             goto errexit;
142         }
143         for (res = hp->addrinfo; res; res = res->ai_next)
144             port_set(res->ai_addr, 0);
145     } else {
146         if (hp->prefixlen != 255)
147             hints.ai_flags |= AI_NUMERICHOST;
148         if (getaddrinfo(hp->host, hp->port, &hints, &hp->addrinfo)) {
149             debug(DBG_WARN, "resolvehostport: can't resolve %s port %s", hp->host ? hp->host : "(null)", hp->port ? hp->port : "(null)");
150             goto errexit;
151         }
152         if (hp->prefixlen != 255) {
153             switch (hp->addrinfo->ai_family) {
154             case AF_INET:
155                 if (hp->prefixlen > 32) {
156                     debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
157                     goto errexit;
158                 }
159                 break;
160             case AF_INET6:
161                 break;
162             default:
163                 debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
164                 goto errexit;
165             }
166         }
167     }
168     return 1;
169
170  errexit:
171     if (hp->addrinfo)
172         freeaddrinfo(hp->addrinfo);
173     return 0;
174 }         
175
176 int addhostport(struct list **hostports, char *hostport, char *portdefault, uint8_t prefixok) {
177     struct hostportres *hp;
178
179     hp = newhostport(hostport, portdefault, prefixok);
180     if (!hp)
181         return 0;
182     if (!*hostports)
183         *hostports = list_create();
184     if (!*hostports || !list_push(*hostports, hp)) {
185         freehostport(hp);
186         debug(DBG_ERR, "addhostport: malloc failed");
187         return 0;
188     }
189     return 1;
190 }
191
192 void freehostports(struct list *hostports) {
193     struct hostportres *hp;
194
195     while ((hp = (struct hostportres *)list_shift(hostports)))
196         freehostport(hp);
197     list_destroy(hostports);
198 }
199
200 int resolvehostports(struct list *hostports, int socktype) {
201     struct list_node *entry;
202     struct hostportres *hp;
203     
204     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
205         hp = (struct hostportres *)entry->data;
206         if (!hp->addrinfo && !resolvehostport(hp, socktype, 0))
207             return 0;
208     }
209     return 1;
210 }
211
212 struct addrinfo *resolvepassiveaddrinfo(char *hostport, char *default_port, int socktype) {
213     struct addrinfo *ai = NULL;
214     struct hostportres *hp = newhostport(hostport, default_port, 0);
215     if (hp && resolvehostport(hp, socktype, 1)) {
216         ai = hp->addrinfo;
217         hp->addrinfo = NULL;
218     }
219     freehostport(hp);
220     return ai;
221 }
222
223 /* returns 1 if the len first bits are equal, else 0 */
224 static int prefixmatch(void *a1, void *a2, uint8_t len) {
225     static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
226     uint8_t r, l = len / 8;
227     if (l && memcmp(a1, a2, l))
228         return 0;
229     r = len % 8;
230     if (!r)
231         return 1;
232     return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
233 }
234
235 int addressmatches(struct list *hostports, struct sockaddr *addr) {
236     struct sockaddr_in6 *sa6 = NULL;
237     struct in_addr *a4 = NULL;
238     struct addrinfo *res;
239     struct list_node *entry;
240     struct hostportres *hp = NULL;
241     
242     if (addr->sa_family == AF_INET6) {
243         sa6 = (struct sockaddr_in6 *)addr;
244         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
245             a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
246             sa6 = NULL;
247         }
248     } else
249         a4 = &((struct sockaddr_in *)addr)->sin_addr;
250
251     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
252         hp = (struct hostportres *)entry->data;
253         for (res = hp->addrinfo; res; res = res->ai_next)
254             if (hp->prefixlen == 255) {
255                 if ((a4 && res->ai_family == AF_INET &&
256                      !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
257                     (sa6 && res->ai_family == AF_INET6 &&
258                      !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
259                     return 1;
260             } else {
261                 if ((a4 && res->ai_family == AF_INET &&
262                      prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
263                     (sa6 && res->ai_family == AF_INET6 &&
264                      prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen)))
265                     return 1;
266             }
267     }
268     return 0;
269 }
270
271 int connecttcphostlist(struct list *hostports,  struct addrinfo *src) {
272     int s;
273     struct list_node *entry;
274     struct hostportres *hp = NULL;
275
276     for (entry = list_first(hostports); entry; entry = list_next(entry)) {
277         hp = (struct hostportres *)entry->data;
278         debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
279         if ((s = connecttcp(hp->addrinfo, src)) >= 0) {
280             debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
281             return s;
282         }
283     }
284     debug(DBG_ERR, "connecttcphostlist: failed");
285     return -1;
286 }