a3622ebe192067a49b0b46162bb92af44b732242
[strongswan.git] / src / libstrongswan / networking / host.c
1 /*
2 * Copyright (C) 2006-2012 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 if (inet_pton(AF_INET6, string, &addr.v6.sin6_addr) != 1)
385 {
386 return NULL;
387 }
388 addr.v6.sin6_port = htons(port);
389 addr.v6.sin6_family = AF_INET6;
390 return host_create_from_sockaddr((sockaddr_t*)&addr);
391 case AF_INET:
392 if (strchr(string, ':'))
393 { /* do not try to convert v6 addresses for v4 family */
394 return NULL;
395 }
396 af_inet:
397 if (inet_pton(AF_INET, string, &addr.v4.sin_addr) != 1)
398 {
399 return NULL;
400 }
401 addr.v4.sin_port = htons(port);
402 addr.v4.sin_family = AF_INET;
403 return host_create_from_sockaddr((sockaddr_t*)&addr);
404 default:
405 return NULL;
406 }
407 }
408
409 /*
410 * Described in header.
411 */
412 host_t *host_create_from_string(char *string, u_int16_t port)
413 {
414 return host_create_from_string_and_family(string, AF_UNSPEC, port);
415 }
416
417 /*
418 * Described in header.
419 */
420 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
421 {
422 private_host_t *this = host_create_empty();
423
424 switch (sockaddr->sa_family)
425 {
426 case AF_INET:
427 {
428 memcpy(&this->address4, (struct sockaddr_in*)sockaddr,
429 sizeof(struct sockaddr_in));
430 this->socklen = sizeof(struct sockaddr_in);
431 update_sa_len(this);
432 return &this->public;
433 }
434 case AF_INET6:
435 {
436 memcpy(&this->address6, (struct sockaddr_in6*)sockaddr,
437 sizeof(struct sockaddr_in6));
438 this->socklen = sizeof(struct sockaddr_in6);
439 update_sa_len(this);
440 return &this->public;
441 }
442 default:
443 break;
444 }
445 free(this);
446 return NULL;
447 }
448
449 /*
450 * Described in header.
451 */
452 host_t *host_create_from_dns(char *string, int af, u_int16_t port)
453 {
454 host_t *this;
455
456 this = host_create_from_string_and_family(string, af, port);
457 if (!this)
458 {
459 this = lib->hosts->resolve(lib->hosts, string, af);
460 }
461 if (this)
462 {
463 this->set_port(this, port);
464 }
465 return this;
466 }
467
468 /*
469 * Described in header.
470 */
471 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
472 {
473 private_host_t *this;
474
475 switch (family)
476 {
477 case AF_INET:
478 if (address.len < IPV4_LEN)
479 {
480 return NULL;
481 }
482 address.len = IPV4_LEN;
483 break;
484 case AF_INET6:
485 if (address.len < IPV6_LEN)
486 {
487 return NULL;
488 }
489 address.len = IPV6_LEN;
490 break;
491 case AF_UNSPEC:
492 switch (address.len)
493 {
494 case IPV4_LEN:
495 family = AF_INET;
496 break;
497 case IPV6_LEN:
498 family = AF_INET6;
499 break;
500 default:
501 return NULL;
502 }
503 break;
504 default:
505 return NULL;
506 }
507 this = host_create_empty();
508 this->address.sa_family = family;
509 switch (family)
510 {
511 case AF_INET:
512 memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len);
513 this->address4.sin_port = htons(port);
514 this->socklen = sizeof(struct sockaddr_in);
515 break;
516 case AF_INET6:
517 memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len);
518 this->address6.sin6_port = htons(port);
519 this->socklen = sizeof(struct sockaddr_in6);
520 break;
521 }
522 update_sa_len(this);
523 return &this->public;
524 }
525
526 /*
527 * Described in header.
528 */
529 host_t *host_create_from_subnet(char *string, int *bits)
530 {
531 char *pos, buf[64];
532 host_t *net;
533
534 pos = strchr(string, '/');
535 if (pos)
536 {
537 if (pos - string >= sizeof(buf))
538 {
539 return NULL;
540 }
541 strncpy(buf, string, pos - string);
542 buf[pos - string] = '\0';
543 *bits = atoi(pos + 1);
544 return host_create_from_string(buf, 0);
545 }
546 net = host_create_from_string(string, 0);
547 if (net)
548 {
549 if (net->get_family(net) == AF_INET)
550 {
551 *bits = 32;
552 }
553 else
554 {
555 *bits = 128;
556 }
557 }
558 return net;
559 }
560
561 /*
562 * See header.
563 */
564 host_t *host_create_netmask(int family, int netbits)
565 {
566 private_host_t *this;
567 int bits, bytes, len = 0;
568 char *target;
569
570 switch (family)
571 {
572 case AF_INET:
573 if (netbits < 0 || netbits > 32)
574 {
575 return NULL;
576 }
577 this = host_create_empty();
578 this->socklen = sizeof(struct sockaddr_in);
579 target = (char*)&this->address4.sin_addr;
580 len = 4;
581 break;
582 case AF_INET6:
583 if (netbits < 0 || netbits > 128)
584 {
585 return NULL;
586 }
587 this = host_create_empty();
588 this->socklen = sizeof(struct sockaddr_in6);
589 target = (char*)&this->address6.sin6_addr;
590 len = 16;
591 break;
592 default:
593 return NULL;
594 }
595
596 memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
597 this->address.sa_family = family;
598 update_sa_len(this);
599
600 bytes = netbits / 8;
601 bits = 8 - (netbits & 0x07);
602
603 memset(target, 0xff, bytes);
604 if (bytes < len)
605 {
606 memset(target + bytes, 0x00, len - bytes);
607 target[bytes] = (u_int8_t)(0xff << bits);
608 }
609 return &this->public;
610 }
611
612 /*
613 * Described in header.
614 */
615 host_t *host_create_any(int family)
616 {
617 private_host_t *this = host_create_empty();
618
619 memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
620 this->address.sa_family = family;
621
622 switch (family)
623 {
624 case AF_INET:
625 {
626 this->socklen = sizeof(struct sockaddr_in);
627 update_sa_len(this);
628 return &(this->public);
629 }
630 case AF_INET6:
631 {
632 this->socklen = sizeof(struct sockaddr_in6);
633 update_sa_len(this);
634 return &this->public;
635 }
636 default:
637 break;
638 }
639 free(this);
640 return NULL;
641 }