2 * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
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.
9 /* Code contributions from:
11 * Simon Leinen <simon.leinen@switch.ch>
18 #include <netinet/in.h>
24 void freehostport(struct hostportres *hp) {
29 freeaddrinfo(hp->addrinfo);
34 static int parsehostport(struct hostportres *hp, char *hostport, char *default_port) {
39 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
43 /* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
47 for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
49 debug(DBG_ERR, "no ] matching initial [");
55 for (; *p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
58 debug(DBG_ERR, "missing host/address");
62 hp->host = stringcopy(field, p - field);
65 if (*p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n') {
66 debug(DBG_ERR, "unexpected character after ]");
71 /* port number or service name is specified */;
73 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
75 debug(DBG_ERR, "syntax error, : but no following port");
78 hp->port = stringcopy(field, p - field);
80 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
84 struct hostportres *newhostport(char *hostport, char *default_port, uint8_t prefixok) {
85 struct hostportres *hp;
89 hp = malloc(sizeof(struct hostportres));
91 debug(DBG_ERR, "resolve_newhostport: malloc failed");
94 memset(hp, 0, sizeof(struct hostportres));
96 if (!parsehostport(hp, hostport, default_port))
99 if (hp->host && !strcmp(hp->host, "*")) {
104 slash = hostport ? strchr(hostport, '/') : NULL;
107 debug(DBG_WARN, "newhostport: prefix not allowed here", hp->host);
112 debug(DBG_WARN, "newhostport: prefix length must be specified after the / in %s", hp->host);
116 if (*s < '0' || *s > '9') {
117 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
120 plen = atoi(slash + 1);
121 if (plen < 0 || plen > 128) {
122 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
125 hp->prefixlen = plen;
135 int resolvehostport(struct hostportres *hp, int socktype, uint8_t passive) {
136 struct addrinfo hints, *res;
138 memset(&hints, 0, sizeof(hints));
139 hints.ai_socktype = socktype;
140 hints.ai_family = AF_UNSPEC;
142 hints.ai_flags = AI_PASSIVE;
144 if (!hp->host && !hp->port) {
145 /* getaddrinfo() doesn't like host and port to be NULL */
146 if (getaddrinfo(hp->host, "1812" /* can be anything */, &hints, &hp->addrinfo)) {
147 debug(DBG_WARN, "resolvehostport: can't resolve (null) port (null)");
150 for (res = hp->addrinfo; res; res = res->ai_next)
151 port_set(res->ai_addr, 0);
153 if (hp->prefixlen != 255)
154 hints.ai_flags |= AI_NUMERICHOST;
155 if (getaddrinfo(hp->host, hp->port, &hints, &hp->addrinfo)) {
156 debug(DBG_WARN, "resolvehostport: can't resolve %s port %s", hp->host ? hp->host : "(null)", hp->port ? hp->port : "(null)");
159 if (hp->prefixlen != 255) {
160 switch (hp->addrinfo->ai_family) {
162 if (hp->prefixlen > 32) {
163 debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
170 debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
179 freeaddrinfo(hp->addrinfo);
183 int addhostport(struct list **hostports, char **hostport, char *portdefault, uint8_t prefixok) {
184 struct hostportres *hp;
188 *hostports = list_create();
190 debug(DBG_ERR, "addhostport: malloc failed");
195 for (i = 0; hostport[i]; i++) {
196 hp = newhostport(hostport[i], portdefault, prefixok);
199 if (!list_push(*hostports, hp)) {
201 debug(DBG_ERR, "addhostport: malloc failed");
208 void freehostports(struct list *hostports) {
209 struct hostportres *hp;
211 while ((hp = (struct hostportres *)list_shift(hostports)))
213 list_destroy(hostports);
216 int resolvehostports(struct list *hostports, int socktype) {
217 struct list_node *entry;
218 struct hostportres *hp;
220 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
221 hp = (struct hostportres *)entry->data;
222 if (!hp->addrinfo && !resolvehostport(hp, socktype, 0))
228 struct addrinfo *resolvepassiveaddrinfo(char *hostport, char *default_port, int socktype) {
229 struct addrinfo *ai = NULL;
230 struct hostportres *hp = newhostport(hostport, default_port, 0);
231 if (hp && resolvehostport(hp, socktype, 1)) {
239 /* returns 1 if the len first bits are equal, else 0 */
240 static int prefixmatch(void *a1, void *a2, uint8_t len) {
241 static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
242 uint8_t r, l = len / 8;
243 if (l && memcmp(a1, a2, l))
248 return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
251 int addressmatches(struct list *hostports, struct sockaddr *addr, uint8_t checkport) {
252 struct sockaddr_in6 *sa6 = NULL;
253 struct in_addr *a4 = NULL;
254 struct addrinfo *res;
255 struct list_node *entry;
256 struct hostportres *hp = NULL;
258 if (addr->sa_family == AF_INET6) {
259 sa6 = (struct sockaddr_in6 *)addr;
260 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
261 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
265 a4 = &((struct sockaddr_in *)addr)->sin_addr;
267 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
268 hp = (struct hostportres *)entry->data;
269 for (res = hp->addrinfo; res; res = res->ai_next)
270 if (hp->prefixlen == 255) {
271 if ((a4 && res->ai_family == AF_INET &&
272 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4) &&
273 (!checkport || ((struct sockaddr_in *)res->ai_addr)->sin_port ==
274 ((struct sockaddr_in *)addr)->sin_port)) ||
275 (sa6 && res->ai_family == AF_INET6 &&
276 !memcmp(&sa6->sin6_addr,
277 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16) &&
278 (!checkport || ((struct sockaddr_in6 *)res->ai_addr)->sin6_port ==
279 ((struct sockaddr_in6 *)addr)->sin6_port)))
282 if ((a4 && res->ai_family == AF_INET &&
283 prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
284 (sa6 && res->ai_family == AF_INET6 &&
285 prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen)))
292 int connecttcphostlist(struct list *hostports, struct addrinfo *src) {
294 struct list_node *entry;
295 struct hostportres *hp = NULL;
297 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
298 hp = (struct hostportres *)entry->data;
299 debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
300 if ((s = connecttcp(hp->addrinfo, src, list_count(hostports) > 1 ? 5 : 30)) >= 0) {
301 debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
305 debug(DBG_ERR, "connecttcphostlist: failed");
309 /* Local Variables: */
310 /* c-file-style: "stroustrup" */