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.
17 void freehostport(struct hostportres *hp) {
22 freeaddrinfo(hp->addrinfo);
27 static int parsehostport(struct hostportres *hp, char *hostport, char *default_port) {
32 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
36 /* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
40 for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
42 debug(DBG_ERR, "no ] matching initial [");
48 for (; *p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
51 debug(DBG_ERR, "missing host/address");
55 hp->host = stringcopy(field, p - field);
58 if (*p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n') {
59 debug(DBG_ERR, "unexpected character after ]");
64 /* port number or service name is specified */;
66 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
68 debug(DBG_ERR, "syntax error, : but no following port");
71 hp->port = stringcopy(field, p - field);
73 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
77 struct hostportres *newhostport(char *hostport, char *default_port, uint8_t prefixok) {
78 struct hostportres *hp;
82 hp = malloc(sizeof(struct hostportres));
84 debug(DBG_ERR, "resolve_newhostport: malloc failed");
87 memset(hp, 0, sizeof(struct hostportres));
89 if (!parsehostport(hp, hostport, default_port))
92 if (hp->host && !strcmp(hp->host, "*")) {
97 slash = hostport ? strchr(hostport, '/') : NULL;
100 debug(DBG_WARN, "newhostport: prefix not allowed here", hp->host);
105 debug(DBG_WARN, "newhostport: prefix length must be specified after the / in %s", hp->host);
109 if (*s < '0' || *s > '9') {
110 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
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);
118 hp->prefixlen = plen;
128 int resolvehostport(struct hostportres *hp, int socktype, uint8_t passive) {
129 struct addrinfo hints, *res;
131 memset(&hints, 0, sizeof(hints));
132 hints.ai_socktype = socktype;
133 hints.ai_family = AF_UNSPEC;
135 hints.ai_flags = AI_PASSIVE;
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)");
143 for (res = hp->addrinfo; res; res = res->ai_next)
144 port_set(res->ai_addr, 0);
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)");
152 if (hp->prefixlen != 255) {
153 switch (hp->addrinfo->ai_family) {
155 if (hp->prefixlen > 32) {
156 debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
163 debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
172 freeaddrinfo(hp->addrinfo);
176 int addhostport(struct list **hostports, char **hostport, char *portdefault, uint8_t prefixok) {
177 struct hostportres *hp;
181 *hostports = list_create();
183 debug(DBG_ERR, "addhostport: malloc failed");
188 for (i = 0; hostport[i]; i++) {
189 hp = newhostport(hostport[i], portdefault, prefixok);
192 if (!list_push(*hostports, hp)) {
194 debug(DBG_ERR, "addhostport: malloc failed");
201 void freehostports(struct list *hostports) {
202 struct hostportres *hp;
204 while ((hp = (struct hostportres *)list_shift(hostports)))
206 list_destroy(hostports);
209 int resolvehostports(struct list *hostports, int socktype) {
210 struct list_node *entry;
211 struct hostportres *hp;
213 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
214 hp = (struct hostportres *)entry->data;
215 if (!hp->addrinfo && !resolvehostport(hp, socktype, 0))
221 struct addrinfo *resolvepassiveaddrinfo(char *hostport, char *default_port, int socktype) {
222 struct addrinfo *ai = NULL;
223 struct hostportres *hp = newhostport(hostport, default_port, 0);
224 if (hp && resolvehostport(hp, socktype, 1)) {
232 /* returns 1 if the len first bits are equal, else 0 */
233 static int prefixmatch(void *a1, void *a2, uint8_t len) {
234 static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
235 uint8_t r, l = len / 8;
236 if (l && memcmp(a1, a2, l))
241 return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
244 int addressmatches(struct list *hostports, struct sockaddr *addr) {
245 struct sockaddr_in6 *sa6 = NULL;
246 struct in_addr *a4 = NULL;
247 struct addrinfo *res;
248 struct list_node *entry;
249 struct hostportres *hp = NULL;
251 if (addr->sa_family == AF_INET6) {
252 sa6 = (struct sockaddr_in6 *)addr;
253 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
254 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
258 a4 = &((struct sockaddr_in *)addr)->sin_addr;
260 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
261 hp = (struct hostportres *)entry->data;
262 for (res = hp->addrinfo; res; res = res->ai_next)
263 if (hp->prefixlen == 255) {
264 if ((a4 && res->ai_family == AF_INET &&
265 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
266 (sa6 && res->ai_family == AF_INET6 &&
267 !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
270 if ((a4 && res->ai_family == AF_INET &&
271 prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
272 (sa6 && res->ai_family == AF_INET6 &&
273 prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen)))
280 int connecttcphostlist(struct list *hostports, struct addrinfo *src) {
282 struct list_node *entry;
283 struct hostportres *hp = NULL;
285 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
286 hp = (struct hostportres *)entry->data;
287 debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
288 if ((s = connecttcp(hp->addrinfo, src)) >= 0) {
289 debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
293 debug(DBG_ERR, "connecttcphostlist: failed");