1 /* Copyright (c) 2006-2010, UNINETT AS
2 * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
5 /* Code contributions from:
7 * Simon Leinen <simon.leinen@switch.ch>
14 #include <netinet/in.h>
20 void freehostport(struct hostportres *hp) {
25 freeaddrinfo(hp->addrinfo);
30 static int parsehostport(struct hostportres *hp, char *hostport, char *default_port) {
35 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
39 /* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
43 for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
45 debug(DBG_ERR, "no ] matching initial [");
51 for (; *p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
54 debug(DBG_ERR, "missing host/address");
58 hp->host = stringcopy(field, p - field);
61 if (*p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n') {
62 debug(DBG_ERR, "unexpected character after ]");
67 /* port number or service name is specified */;
69 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
71 debug(DBG_ERR, "syntax error, : but no following port");
74 hp->port = stringcopy(field, p - field);
76 hp->port = default_port ? stringcopy(default_port, 0) : NULL;
80 struct hostportres *newhostport(char *hostport, char *default_port, uint8_t prefixok) {
81 struct hostportres *hp;
85 hp = malloc(sizeof(struct hostportres));
87 debug(DBG_ERR, "resolve_newhostport: malloc failed");
90 memset(hp, 0, sizeof(struct hostportres));
92 if (!parsehostport(hp, hostport, default_port))
95 if (hp->host && !strcmp(hp->host, "*")) {
100 slash = hostport ? strchr(hostport, '/') : NULL;
103 debug(DBG_WARN, "newhostport: prefix not allowed here", hp->host);
108 debug(DBG_WARN, "newhostport: prefix length must be specified after the / in %s", hp->host);
112 if (*s < '0' || *s > '9') {
113 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
116 plen = atoi(slash + 1);
117 if (plen < 0 || plen > 128) {
118 debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
121 hp->prefixlen = plen;
131 int resolvehostport(struct hostportres *hp, int af, int socktype, uint8_t passive) {
132 struct addrinfo hints, *res;
134 memset(&hints, 0, sizeof(hints));
135 hints.ai_socktype = socktype;
136 hints.ai_family = af;
138 hints.ai_flags = AI_PASSIVE;
140 if (!hp->host && !hp->port) {
141 /* getaddrinfo() doesn't like host and port to be NULL */
142 if (getaddrinfo(hp->host, "1812" /* can be anything */, &hints, &hp->addrinfo)) {
143 debug(DBG_WARN, "resolvehostport: can't resolve (null) port (null)");
146 for (res = hp->addrinfo; res; res = res->ai_next)
147 port_set(res->ai_addr, 0);
149 if (hp->prefixlen != 255)
150 hints.ai_flags |= AI_NUMERICHOST;
151 if (getaddrinfo(hp->host, hp->port, &hints, &hp->addrinfo)) {
152 debug(DBG_WARN, "resolvehostport: can't resolve %s port %s", hp->host ? hp->host : "(null)", hp->port ? hp->port : "(null)");
155 if (hp->prefixlen != 255) {
156 switch (hp->addrinfo->ai_family) {
158 if (hp->prefixlen > 32) {
159 debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
166 debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
171 debug(DBG_DBG, "%s: %s -> %s", __func__, hp->host, addr2string(hp->addrinfo->ai_addr));
176 freeaddrinfo(hp->addrinfo);
180 int addhostport(struct list **hostports, char **hostport, char *portdefault, uint8_t prefixok) {
181 struct hostportres *hp;
185 *hostports = list_create();
187 debug(DBG_ERR, "addhostport: malloc failed");
192 for (i = 0; hostport[i]; i++) {
193 hp = newhostport(hostport[i], portdefault, prefixok);
196 if (!list_push(*hostports, hp)) {
198 debug(DBG_ERR, "addhostport: malloc failed");
205 void freehostports(struct list *hostports) {
206 struct hostportres *hp;
208 while ((hp = (struct hostportres *)list_shift(hostports)))
210 list_destroy(hostports);
213 int resolvehostports(struct list *hostports, int af, int socktype) {
214 struct list_node *entry;
215 struct hostportres *hp;
217 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
218 hp = (struct hostportres *)entry->data;
219 if (!hp->addrinfo && !resolvehostport(hp, af, socktype, 0))
225 struct addrinfo *resolvepassiveaddrinfo(char *hostport, int af, char *default_port, int socktype) {
226 struct addrinfo *ai = NULL;
227 struct hostportres *hp = newhostport(hostport, default_port, 0);
228 if (hp && resolvehostport(hp, af, socktype, 1)) {
236 /* returns 1 if the len first bits are equal, else 0 */
237 static int prefixmatch(void *a1, void *a2, uint8_t len) {
238 static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
239 uint8_t r, l = len / 8;
240 if (l && memcmp(a1, a2, l))
245 return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
248 int addressmatches(struct list *hostports, struct sockaddr *addr, uint8_t checkport) {
249 struct sockaddr_in6 *sa6 = NULL;
250 struct in_addr *a4 = NULL;
251 struct addrinfo *res;
252 struct list_node *entry;
253 struct hostportres *hp = NULL;
255 if (addr->sa_family == AF_INET6) {
256 sa6 = (struct sockaddr_in6 *)addr;
257 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
258 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
262 a4 = &((struct sockaddr_in *)addr)->sin_addr;
264 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
265 hp = (struct hostportres *)entry->data;
266 for (res = hp->addrinfo; res; res = res->ai_next)
267 if (hp->prefixlen == 255) {
268 if ((a4 && res->ai_family == AF_INET &&
269 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4) &&
270 (!checkport || ((struct sockaddr_in *)res->ai_addr)->sin_port ==
271 ((struct sockaddr_in *)addr)->sin_port)) ||
272 (sa6 && res->ai_family == AF_INET6 &&
273 !memcmp(&sa6->sin6_addr,
274 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16) &&
275 (!checkport || ((struct sockaddr_in6 *)res->ai_addr)->sin6_port ==
276 ((struct sockaddr_in6 *)addr)->sin6_port)))
279 if ((a4 && res->ai_family == AF_INET &&
280 prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
281 (sa6 && res->ai_family == AF_INET6 &&
282 prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen)))
289 int connecttcphostlist(struct list *hostports, struct addrinfo *src) {
291 struct list_node *entry;
292 struct hostportres *hp = NULL;
294 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
295 hp = (struct hostportres *)entry->data;
296 debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
297 if ((s = connecttcp(hp->addrinfo, src, list_count(hostports) > 1 ? 5 : 30)) >= 0) {
298 debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
302 debug(DBG_ERR, "connecttcphostlist: failed");
306 /* Local Variables: */
307 /* c-file-style: "stroustrup" */