host: Ignore spaces around - when parsing ranges
[strongswan.git] / src / libstrongswan / networking / host.c
1 /*
2 * Copyright (C) 2006-2014 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "host.h"
20
21 #include <utils/debug.h>
22 #include <library.h>
23
24 #define IPV4_LEN 4
25 #define IPV6_LEN 16
26
27 typedef struct private_host_t private_host_t;
28
29 /**
30 * Private Data of a host object.
31 */
32 struct private_host_t {
33 /**
34 * Public data
35 */
36 host_t public;
37
38 /**
39 * low-lewel structure, which stores the address
40 */
41 union {
42 /** generic type */
43 struct sockaddr address;
44 /** maximum sockaddr size */
45 struct sockaddr_storage address_max;
46 /** IPv4 address */
47 struct sockaddr_in address4;
48 /** IPv6 address */
49 struct sockaddr_in6 address6;
50 };
51 /**
52 * length of address structure
53 */
54 socklen_t socklen;
55 };
56
57 /**
58 * Update the sockaddr internal sa_len option, if available
59 */
60 static inline void update_sa_len(private_host_t *this)
61 {
62 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
63 this->address.sa_len = this->socklen;
64 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
65 }
66
67 METHOD(host_t, get_sockaddr, sockaddr_t*,
68 private_host_t *this)
69 {
70 return &(this->address);
71 }
72
73 METHOD(host_t, get_sockaddr_len, socklen_t*,
74 private_host_t *this)
75 {
76 return &(this->socklen);
77 }
78
79 METHOD(host_t, is_anyaddr, bool,
80 private_host_t *this)
81 {
82 static const u_int8_t zeroes[IPV6_LEN];
83
84 switch (this->address.sa_family)
85 {
86 case AF_INET:
87 {
88 return memeq(zeroes, &(this->address4.sin_addr.s_addr), IPV4_LEN);
89 }
90 case AF_INET6:
91 {
92 return memeq(zeroes, &(this->address6.sin6_addr.s6_addr), IPV6_LEN);
93 }
94 default:
95 {
96 return FALSE;
97 }
98 }
99 }
100
101 /**
102 * Described in header.
103 */
104 int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
105 const void *const *args)
106 {
107 private_host_t *this = *((private_host_t**)(args[0]));
108 char buffer[INET6_ADDRSTRLEN + 16];
109
110 if (this == NULL)
111 {
112 snprintf(buffer, sizeof(buffer), "(null)");
113 }
114 else if (is_anyaddr(this) && !spec->plus && !spec->hash)
115 {
116 snprintf(buffer, sizeof(buffer), "%%any%s",
117 this->address.sa_family == AF_INET6 ? "6" : "");
118 }
119 else
120 {
121 void *address;
122 u_int16_t port;
123 int len;
124
125 address = &this->address6.sin6_addr;
126 port = this->address6.sin6_port;
127
128 switch (this->address.sa_family)
129 {
130 case AF_INET:
131 address = &this->address4.sin_addr;
132 port = this->address4.sin_port;
133 /* fall */
134 case AF_INET6:
135
136 if (inet_ntop(this->address.sa_family, address,
137 buffer, sizeof(buffer)) == NULL)
138 {
139 snprintf(buffer, sizeof(buffer),
140 "(address conversion failed)");
141 }
142 else if (spec->hash)
143 {
144 len = strlen(buffer);
145 snprintf(buffer + len, sizeof(buffer) - len,
146 "[%d]", ntohs(port));
147 }
148 break;
149 default:
150 snprintf(buffer, sizeof(buffer), "(family not supported)");
151 break;
152 }
153 }
154 if (spec->minus)
155 {
156 return print_in_hook(data, "%-*s", spec->width, buffer);
157 }
158 return print_in_hook(data, "%*s", spec->width, buffer);
159 }
160
161 METHOD(host_t, get_address, chunk_t,
162 private_host_t *this)
163 {
164 chunk_t address = chunk_empty;
165
166 switch (this->address.sa_family)
167 {
168 case AF_INET:
169 {
170 address.ptr = (char*)&(this->address4.sin_addr.s_addr);
171 address.len = IPV4_LEN;
172 return address;
173 }
174 case AF_INET6:
175 {
176 address.ptr = (char*)&(this->address6.sin6_addr.s6_addr);
177 address.len = IPV6_LEN;
178 return address;
179 }
180 default:
181 {
182 /* return empty chunk */
183 return address;
184 }
185 }
186 }
187
188 METHOD(host_t, get_family, int,
189 private_host_t *this)
190 {
191 return this->address.sa_family;
192 }
193
194 METHOD(host_t, get_port, u_int16_t,
195 private_host_t *this)
196 {
197 switch (this->address.sa_family)
198 {
199 case AF_INET:
200 {
201 return ntohs(this->address4.sin_port);
202 }
203 case AF_INET6:
204 {
205 return ntohs(this->address6.sin6_port);
206 }
207 default:
208 {
209 return 0;
210 }
211 }
212 }
213
214 METHOD(host_t, set_port, void,
215 private_host_t *this, u_int16_t port)
216 {
217 switch (this->address.sa_family)
218 {
219 case AF_INET:
220 {
221 this->address4.sin_port = htons(port);
222 break;
223 }
224 case AF_INET6:
225 {
226 this->address6.sin6_port = htons(port);
227 break;
228 }
229 default:
230 {
231 break;
232 }
233 }
234 }
235
236 METHOD(host_t, clone_, host_t*,
237 private_host_t *this)
238 {
239 private_host_t *new;
240
241 new = malloc_thing(private_host_t);
242 memcpy(new, this, sizeof(private_host_t));
243
244 return &new->public;
245 }
246
247 /**
248 * Implements host_t.ip_equals
249 */
250 static bool ip_equals(private_host_t *this, private_host_t *other)
251 {
252 if (this->address.sa_family != other->address.sa_family)
253 {
254 /* 0.0.0.0 and 0::0 are equal */
255 return (is_anyaddr(this) && is_anyaddr(other));
256 }
257
258 switch (this->address.sa_family)
259 {
260 case AF_INET:
261 {
262 return memeq(&this->address4.sin_addr, &other->address4.sin_addr,
263 sizeof(this->address4.sin_addr));
264 }
265 case AF_INET6:
266 {
267 return memeq(&this->address6.sin6_addr, &other->address6.sin6_addr,
268 sizeof(this->address6.sin6_addr));
269 }
270 default:
271 break;
272 }
273 return FALSE;
274 }
275
276 /**
277 * Implements host_t.equals
278 */
279 static bool equals(private_host_t *this, private_host_t *other)
280 {
281 if (!ip_equals(this, other))
282 {
283 return FALSE;
284 }
285
286 switch (this->address.sa_family)
287 {
288 case AF_INET:
289 {
290 return (this->address4.sin_port == other->address4.sin_port);
291 }
292 case AF_INET6:
293 {
294 return (this->address6.sin6_port == other->address6.sin6_port);
295 }
296 default:
297 break;
298 }
299 return FALSE;
300 }
301
302 METHOD(host_t, destroy, void,
303 private_host_t *this)
304 {
305 free(this);
306 }
307
308 /**
309 * Creates an empty host_t object
310 */
311 static private_host_t *host_create_empty(void)
312 {
313 private_host_t *this;
314
315 INIT(this,
316 .public = {
317 .get_sockaddr = _get_sockaddr,
318 .get_sockaddr_len = _get_sockaddr_len,
319 .clone = _clone_,
320 .get_family = _get_family,
321 .get_address = _get_address,
322 .get_port = _get_port,
323 .set_port = _set_port,
324 .ip_equals = (bool (*)(host_t *,host_t *))ip_equals,
325 .equals = (bool (*)(host_t *,host_t *)) equals,
326 .is_anyaddr = _is_anyaddr,
327 .destroy = _destroy,
328 },
329 );
330
331 return this;
332 }
333
334 /*
335 * Create a %any host with port
336 */
337 static host_t *host_create_any_port(int family, u_int16_t port)
338 {
339 host_t *this;
340
341 this = host_create_any(family);
342 this->set_port(this, port);
343 return this;
344 }
345
346 /*
347 * Described in header.
348 */
349 host_t *host_create_from_string_and_family(char *string, int family,
350 u_int16_t port)
351 {
352 union {
353 struct sockaddr_in v4;
354 struct sockaddr_in6 v6;
355 } addr;
356
357 if (streq(string, "%any"))
358 {
359 return host_create_any_port(family ? family : AF_INET, port);
360 }
361 if (family == AF_UNSPEC || family == AF_INET)
362 {
363 if (streq(string, "%any4") || streq(string, "0.0.0.0"))
364 {
365 return host_create_any_port(AF_INET, port);
366 }
367 }
368 if (family == AF_UNSPEC || family == AF_INET6)
369 {
370 if (streq(string, "%any6") || streq(string, "::"))
371 {
372 return host_create_any_port(AF_INET6, port);
373 }
374 }
375 switch (family)
376 {
377 case AF_UNSPEC:
378 if (strchr(string, '.'))
379 {
380 goto af_inet;
381 }
382 /* FALL */
383 case AF_INET6:
384 memset(&addr.v6, 0, sizeof(addr.v6));
385 if (inet_pton(AF_INET6, string, &addr.v6.sin6_addr) != 1)
386 {
387 return NULL;
388 }
389 addr.v6.sin6_port = htons(port);
390 addr.v6.sin6_family = AF_INET6;
391 return host_create_from_sockaddr((sockaddr_t*)&addr);
392 case AF_INET:
393 if (strchr(string, ':'))
394 { /* do not try to convert v6 addresses for v4 family */
395 return NULL;
396 }
397 af_inet:
398 memset(&addr.v4, 0, sizeof(addr.v4));
399 if (inet_pton(AF_INET, string, &addr.v4.sin_addr) != 1)
400 {
401 return NULL;
402 }
403 addr.v4.sin_port = htons(port);
404 addr.v4.sin_family = AF_INET;
405 return host_create_from_sockaddr((sockaddr_t*)&addr);
406 default:
407 return NULL;
408 }
409 }
410
411 /*
412 * Described in header.
413 */
414 host_t *host_create_from_string(char *string, u_int16_t port)
415 {
416 return host_create_from_string_and_family(string, AF_UNSPEC, port);
417 }
418
419 /*
420 * Described in header.
421 */
422 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
423 {
424 private_host_t *this = host_create_empty();
425
426 switch (sockaddr->sa_family)
427 {
428 case AF_INET:
429 {
430 memcpy(&this->address4, (struct sockaddr_in*)sockaddr,
431 sizeof(struct sockaddr_in));
432 this->socklen = sizeof(struct sockaddr_in);
433 update_sa_len(this);
434 return &this->public;
435 }
436 case AF_INET6:
437 {
438 memcpy(&this->address6, (struct sockaddr_in6*)sockaddr,
439 sizeof(struct sockaddr_in6));
440 this->socklen = sizeof(struct sockaddr_in6);
441 update_sa_len(this);
442 return &this->public;
443 }
444 default:
445 break;
446 }
447 free(this);
448 return NULL;
449 }
450
451 /*
452 * Described in header.
453 */
454 host_t *host_create_from_dns(char *string, int af, u_int16_t port)
455 {
456 host_t *this;
457
458 this = host_create_from_string_and_family(string, af, port);
459 if (!this)
460 {
461 this = lib->hosts->resolve(lib->hosts, string, af);
462 }
463 if (this)
464 {
465 this->set_port(this, port);
466 }
467 return this;
468 }
469
470 /*
471 * Described in header.
472 */
473 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
474 {
475 private_host_t *this;
476
477 switch (family)
478 {
479 case AF_INET:
480 if (address.len < IPV4_LEN)
481 {
482 return NULL;
483 }
484 address.len = IPV4_LEN;
485 break;
486 case AF_INET6:
487 if (address.len < IPV6_LEN)
488 {
489 return NULL;
490 }
491 address.len = IPV6_LEN;
492 break;
493 case AF_UNSPEC:
494 switch (address.len)
495 {
496 case IPV4_LEN:
497 family = AF_INET;
498 break;
499 case IPV6_LEN:
500 family = AF_INET6;
501 break;
502 default:
503 return NULL;
504 }
505 break;
506 default:
507 return NULL;
508 }
509 this = host_create_empty();
510 this->address.sa_family = family;
511 switch (family)
512 {
513 case AF_INET:
514 memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len);
515 this->address4.sin_port = htons(port);
516 this->socklen = sizeof(struct sockaddr_in);
517 break;
518 case AF_INET6:
519 memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len);
520 this->address6.sin6_port = htons(port);
521 this->socklen = sizeof(struct sockaddr_in6);
522 break;
523 }
524 update_sa_len(this);
525 return &this->public;
526 }
527
528 /*
529 * Described in header.
530 */
531 bool host_create_from_range(char *string, host_t **from, host_t **to)
532 {
533 char *sep, *pos;
534
535 sep = strchr(string, '-');
536 if (!sep)
537 {
538 return FALSE;
539 }
540 for (pos = sep+1; *pos && *pos == ' '; pos++)
541 {
542 /* trim spaces before to address*/
543 }
544 *to = host_create_from_string(pos, 0);
545 if (!*to)
546 {
547 return FALSE;
548 }
549 for (pos = sep-1; pos > string && *pos == ' '; pos--)
550 {
551 /* trim spaces behind from address */
552 }
553 pos = strndup(string, pos - string + 1);
554 *from = host_create_from_string_and_family(pos, (*to)->get_family(*to), 0);
555 free(pos);
556 if (!*from)
557 {
558 (*to)->destroy(*to);
559 return FALSE;
560 }
561 return TRUE;
562 }
563
564 /*
565 * Described in header.
566 */
567 host_t *host_create_from_subnet(char *string, int *bits)
568 {
569 char *pos, buf[64];
570 host_t *net;
571
572 pos = strchr(string, '/');
573 if (pos)
574 {
575 if (pos - string >= sizeof(buf))
576 {
577 return NULL;
578 }
579 strncpy(buf, string, pos - string);
580 buf[pos - string] = '\0';
581 *bits = atoi(pos + 1);
582 return host_create_from_string(buf, 0);
583 }
584 net = host_create_from_string(string, 0);
585 if (net)
586 {
587 if (net->get_family(net) == AF_INET)
588 {
589 *bits = 32;
590 }
591 else
592 {
593 *bits = 128;
594 }
595 }
596 return net;
597 }
598
599 /*
600 * See header.
601 */
602 host_t *host_create_netmask(int family, int netbits)
603 {
604 private_host_t *this;
605 int bits, bytes, len = 0;
606 char *target;
607
608 switch (family)
609 {
610 case AF_INET:
611 if (netbits < 0 || netbits > 32)
612 {
613 return NULL;
614 }
615 this = host_create_empty();
616 this->socklen = sizeof(struct sockaddr_in);
617 target = (char*)&this->address4.sin_addr;
618 len = 4;
619 break;
620 case AF_INET6:
621 if (netbits < 0 || netbits > 128)
622 {
623 return NULL;
624 }
625 this = host_create_empty();
626 this->socklen = sizeof(struct sockaddr_in6);
627 target = (char*)&this->address6.sin6_addr;
628 len = 16;
629 break;
630 default:
631 return NULL;
632 }
633
634 memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
635 this->address.sa_family = family;
636 update_sa_len(this);
637
638 bytes = netbits / 8;
639 bits = 8 - (netbits & 0x07);
640
641 memset(target, 0xff, bytes);
642 if (bytes < len)
643 {
644 memset(target + bytes, 0x00, len - bytes);
645 target[bytes] = (u_int8_t)(0xff << bits);
646 }
647 return &this->public;
648 }
649
650 /*
651 * Described in header.
652 */
653 host_t *host_create_any(int family)
654 {
655 private_host_t *this = host_create_empty();
656
657 memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
658 this->address.sa_family = family;
659
660 switch (family)
661 {
662 case AF_INET:
663 {
664 this->socklen = sizeof(struct sockaddr_in);
665 update_sa_len(this);
666 return &(this->public);
667 }
668 case AF_INET6:
669 {
670 this->socklen = sizeof(struct sockaddr_in6);
671 update_sa_len(this);
672 return &this->public;
673 }
674 default:
675 break;
676 }
677 free(this);
678 return NULL;
679 }