host: print %#H format specifiers not as %any, but with the port
[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.get_differences
278 */
279 static host_diff_t get_differences(host_t *this, host_t *other)
280 {
281 host_diff_t ret = HOST_DIFF_NONE;
282
283 if (!this->ip_equals(this, other))
284 {
285 ret |= HOST_DIFF_ADDR;
286 }
287
288 if (this->get_port(this) != other->get_port(other))
289 {
290 ret |= HOST_DIFF_PORT;
291 }
292
293 return ret;
294 }
295
296 /**
297 * Implements host_t.equals
298 */
299 static bool equals(private_host_t *this, private_host_t *other)
300 {
301 if (!ip_equals(this, other))
302 {
303 return FALSE;
304 }
305
306 switch (this->address.sa_family)
307 {
308 case AF_INET:
309 {
310 return (this->address4.sin_port == other->address4.sin_port);
311 }
312 case AF_INET6:
313 {
314 return (this->address6.sin6_port == other->address6.sin6_port);
315 }
316 default:
317 break;
318 }
319 return FALSE;
320 }
321
322 METHOD(host_t, destroy, void,
323 private_host_t *this)
324 {
325 free(this);
326 }
327
328 /**
329 * Creates an empty host_t object
330 */
331 static private_host_t *host_create_empty(void)
332 {
333 private_host_t *this;
334
335 INIT(this,
336 .public = {
337 .get_sockaddr = _get_sockaddr,
338 .get_sockaddr_len = _get_sockaddr_len,
339 .clone = _clone_,
340 .get_family = _get_family,
341 .get_address = _get_address,
342 .get_port = _get_port,
343 .set_port = _set_port,
344 .get_differences = get_differences,
345 .ip_equals = (bool (*)(host_t *,host_t *))ip_equals,
346 .equals = (bool (*)(host_t *,host_t *)) equals,
347 .is_anyaddr = _is_anyaddr,
348 .destroy = _destroy,
349 },
350 );
351
352 return this;
353 }
354
355 /*
356 * Create a %any host with port
357 */
358 static host_t *host_create_any_port(int family, u_int16_t port)
359 {
360 host_t *this;
361
362 this = host_create_any(family);
363 this->set_port(this, port);
364 return this;
365 }
366
367 /*
368 * Described in header.
369 */
370 host_t *host_create_from_string_and_family(char *string, int family,
371 u_int16_t port)
372 {
373 union {
374 struct sockaddr_in v4;
375 struct sockaddr_in6 v6;
376 } addr;
377
378 if (streq(string, "%any"))
379 {
380 return host_create_any_port(family ? family : AF_INET, port);
381 }
382 if (family == AF_UNSPEC || family == AF_INET)
383 {
384 if (streq(string, "%any4") || streq(string, "0.0.0.0"))
385 {
386 return host_create_any_port(AF_INET, port);
387 }
388 }
389 if (family == AF_UNSPEC || family == AF_INET6)
390 {
391 if (streq(string, "%any6") || streq(string, "::"))
392 {
393 return host_create_any_port(AF_INET6, port);
394 }
395 }
396 switch (family)
397 {
398 case AF_UNSPEC:
399 if (strchr(string, '.'))
400 {
401 goto af_inet;
402 }
403 /* FALL */
404 case AF_INET6:
405 if (inet_pton(AF_INET6, string, &addr.v6.sin6_addr) != 1)
406 {
407 return NULL;
408 }
409 addr.v6.sin6_port = htons(port);
410 addr.v6.sin6_family = AF_INET6;
411 return host_create_from_sockaddr((sockaddr_t*)&addr);
412 case AF_INET:
413 if (strchr(string, ':'))
414 { /* do not try to convert v6 addresses for v4 family */
415 return NULL;
416 }
417 af_inet:
418 if (inet_pton(AF_INET, string, &addr.v4.sin_addr) != 1)
419 {
420 return NULL;
421 }
422 addr.v4.sin_port = htons(port);
423 addr.v4.sin_family = AF_INET;
424 return host_create_from_sockaddr((sockaddr_t*)&addr);
425 default:
426 return NULL;
427 }
428 }
429
430 /*
431 * Described in header.
432 */
433 host_t *host_create_from_string(char *string, u_int16_t port)
434 {
435 return host_create_from_string_and_family(string, AF_UNSPEC, port);
436 }
437
438 /*
439 * Described in header.
440 */
441 host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
442 {
443 private_host_t *this = host_create_empty();
444
445 switch (sockaddr->sa_family)
446 {
447 case AF_INET:
448 {
449 memcpy(&this->address4, (struct sockaddr_in*)sockaddr,
450 sizeof(struct sockaddr_in));
451 this->socklen = sizeof(struct sockaddr_in);
452 update_sa_len(this);
453 return &this->public;
454 }
455 case AF_INET6:
456 {
457 memcpy(&this->address6, (struct sockaddr_in6*)sockaddr,
458 sizeof(struct sockaddr_in6));
459 this->socklen = sizeof(struct sockaddr_in6);
460 update_sa_len(this);
461 return &this->public;
462 }
463 default:
464 break;
465 }
466 free(this);
467 return NULL;
468 }
469
470 /*
471 * Described in header.
472 */
473 host_t *host_create_from_dns(char *string, int af, u_int16_t port)
474 {
475 host_t *this;
476
477 this = host_create_from_string_and_family(string, af, port);
478 if (!this)
479 {
480 this = lib->hosts->resolve(lib->hosts, string, af);
481 }
482 if (this)
483 {
484 this->set_port(this, port);
485 }
486 return this;
487 }
488
489 /*
490 * Described in header.
491 */
492 host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
493 {
494 private_host_t *this;
495
496 switch (family)
497 {
498 case AF_INET:
499 if (address.len < IPV4_LEN)
500 {
501 return NULL;
502 }
503 address.len = IPV4_LEN;
504 break;
505 case AF_INET6:
506 if (address.len < IPV6_LEN)
507 {
508 return NULL;
509 }
510 address.len = IPV6_LEN;
511 break;
512 case AF_UNSPEC:
513 switch (address.len)
514 {
515 case IPV4_LEN:
516 family = AF_INET;
517 break;
518 case IPV6_LEN:
519 family = AF_INET6;
520 break;
521 default:
522 return NULL;
523 }
524 break;
525 default:
526 return NULL;
527 }
528 this = host_create_empty();
529 this->address.sa_family = family;
530 switch (family)
531 {
532 case AF_INET:
533 memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len);
534 this->address4.sin_port = htons(port);
535 this->socklen = sizeof(struct sockaddr_in);
536 break;
537 case AF_INET6:
538 memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len);
539 this->address6.sin6_port = htons(port);
540 this->socklen = sizeof(struct sockaddr_in6);
541 break;
542 }
543 update_sa_len(this);
544 return &this->public;
545 }
546
547 /*
548 * Described in header.
549 */
550 host_t *host_create_from_subnet(char *string, int *bits)
551 {
552 char *pos, buf[64];
553 host_t *net;
554
555 pos = strchr(string, '/');
556 if (pos)
557 {
558 if (pos - string >= sizeof(buf))
559 {
560 return NULL;
561 }
562 strncpy(buf, string, pos - string);
563 buf[pos - string] = '\0';
564 *bits = atoi(pos + 1);
565 return host_create_from_string(buf, 0);
566 }
567 net = host_create_from_string(string, 0);
568 if (net)
569 {
570 if (net->get_family(net) == AF_INET)
571 {
572 *bits = 32;
573 }
574 else
575 {
576 *bits = 128;
577 }
578 }
579 return net;
580 }
581
582 /*
583 * Described in header.
584 */
585 host_t *host_create_any(int family)
586 {
587 private_host_t *this = host_create_empty();
588
589 memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
590 this->address.sa_family = family;
591
592 switch (family)
593 {
594 case AF_INET:
595 {
596 this->socklen = sizeof(struct sockaddr_in);
597 update_sa_len(this);
598 return &(this->public);
599 }
600 case AF_INET6:
601 {
602 this->socklen = sizeof(struct sockaddr_in6);
603 update_sa_len(this);
604 return &this->public;
605 }
606 default:
607 break;
608 }
609 free(this);
610 return NULL;
611 }