do not recalculate netbits for true subnets
[strongswan.git] / src / libstrongswan / selectors / traffic_selector.c
1 /*
2 * Copyright (C) 2007-2009 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <arpa/inet.h>
19 #include <string.h>
20 #include <netdb.h>
21 #include <stdio.h>
22
23 #include "traffic_selector.h"
24
25 #include <utils/linked_list.h>
26 #include <utils/identification.h>
27
28 #define NON_SUBNET_ADDRESS_RANGE 255
29
30 ENUM(ts_type_name, TS_IPV4_ADDR_RANGE, TS_IPV6_ADDR_RANGE,
31 "TS_IPV4_ADDR_RANGE",
32 "TS_IPV6_ADDR_RANGE",
33 );
34
35 typedef struct private_traffic_selector_t private_traffic_selector_t;
36
37 /**
38 * Private data of an traffic_selector_t object
39 */
40 struct private_traffic_selector_t {
41
42 /**
43 * Public part
44 */
45 traffic_selector_t public;
46
47 /**
48 * Type of address
49 */
50 ts_type_t type;
51
52 /**
53 * IP protocol (UDP, TCP, ICMP, ...)
54 */
55 u_int8_t protocol;
56
57 /**
58 * narrow this traffic selector to hosts external ip
59 * if set, from and to have no meaning until set_address() is called
60 */
61 bool dynamic;
62
63 /**
64 * subnet size in CIDR notation, 255 means a non-subnet address range
65 */
66 u_int8_t netbits;
67
68 /**
69 * begin of address range, network order
70 */
71 union {
72 /** dummy char for common address manipulation */
73 char from[0];
74 /** IPv4 address */
75 u_int32_t from4[1];
76 /** IPv6 address */
77 u_int32_t from6[4];
78 };
79
80 /**
81 * end of address range, network order
82 */
83 union {
84 /** dummy char for common address manipulation */
85 char to[0];
86 /** IPv4 address */
87 u_int32_t to4[1];
88 /** IPv6 address */
89 u_int32_t to6[4];
90 };
91
92 /**
93 * begin of port range
94 */
95 u_int16_t from_port;
96
97 /**
98 * end of port range
99 */
100 u_int16_t to_port;
101 };
102
103 /**
104 * calculate the "to"-address for the "from" address and a subnet size
105 */
106 static void calc_range(private_traffic_selector_t *this, u_int8_t netbits)
107 {
108 size_t len;
109 int bytes, bits;
110 u_int8_t mask;
111
112 this->netbits = netbits;
113
114 len = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
115 bytes = (netbits + 7)/8;
116 bits = (bytes * 8) - netbits;
117 mask = bits ? (1 << bits) - 1 : 0;
118
119 memcpy(this->to, this->from, bytes);
120 memset(this->from + bytes, 0x00, len - bytes);
121 memset(this->to + bytes, 0xff, len - bytes);
122 this->from[bytes-1] &= ~mask;
123 this->to[bytes-1] |= mask;
124 }
125
126 /**
127 * calculate the subnet size from the "to" and "from" addresses
128 */
129 static u_int8_t calc_netbits(private_traffic_selector_t *this)
130 {
131 int byte, bit;
132 u_int8_t netbits;
133 size_t size = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
134 bool prefix = TRUE;
135
136 /* a perfect match results in a single address with a /32 or /128 netmask */
137 netbits = (size * 8);
138 this->netbits = netbits;
139
140 /* go through all bits of the addresses, beginning in the front.
141 * as long as they are equal, the subnet gets larger
142 */
143 for (byte = 0; byte < size; byte++)
144 {
145 for (bit = 7; bit >= 0; bit--)
146 {
147 u_int8_t bitmask = 1 << bit;
148
149 if (prefix)
150 {
151 if ((bitmask & this->from[byte]) != (bitmask & this->to[byte]))
152 {
153 /* store the common prefix which might be a true subnet */
154 netbits = (7 - bit) + (byte * 8);
155 this->netbits = netbits;
156 prefix = FALSE;
157 }
158 }
159 else
160 {
161 if ((bitmask & this->from[byte]) || !(bitmask & this->to[byte]))
162 {
163 this->netbits = NON_SUBNET_ADDRESS_RANGE;
164 return netbits; /* return a pseudo subnet */
165
166 }
167 }
168 }
169 }
170 return netbits; /* return a true subnet */
171 }
172
173 /**
174 * internal generic constructor
175 */
176 static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port);
177
178 /**
179 * Described in header.
180 */
181 int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
182 const void *const *args)
183 {
184 private_traffic_selector_t *this = *((private_traffic_selector_t**)(args[0]));
185 linked_list_t *list = *((linked_list_t**)(args[0]));
186 iterator_t *iterator;
187 char from_str[INET6_ADDRSTRLEN] = "";
188 char to_str[INET6_ADDRSTRLEN] = "";
189 char *serv_proto = NULL;
190 bool has_proto;
191 bool has_ports;
192 size_t written = 0;
193 u_int32_t from[4], to[4];
194
195 if (this == NULL)
196 {
197 return print_in_hook(dst, len, "(null)");
198 }
199
200 if (spec->hash)
201 {
202 iterator = list->create_iterator(list, TRUE);
203 while (iterator->iterate(iterator, (void**)&this))
204 {
205 /* call recursivly */
206 written += print_in_hook(dst, len, "%R ", this);
207 }
208 iterator->destroy(iterator);
209 return written;
210 }
211
212 memset(from, 0, sizeof(from));
213 memset(to, 0xFF, sizeof(to));
214 if (this->dynamic &&
215 memeq(this->from, from, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16) &&
216 memeq(this->to, to, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16))
217 {
218 written += print_in_hook(dst, len, "dynamic");
219 }
220 else
221 {
222 if (this->type == TS_IPV4_ADDR_RANGE)
223 {
224 inet_ntop(AF_INET, &this->from4, from_str, sizeof(from_str));
225 }
226 else
227 {
228 inet_ntop(AF_INET6, &this->from6, from_str, sizeof(from_str));
229 }
230 if (this->netbits == NON_SUBNET_ADDRESS_RANGE)
231 {
232 if (this->type == TS_IPV4_ADDR_RANGE)
233 {
234 inet_ntop(AF_INET, &this->to4, to_str, sizeof(to_str));
235 }
236 else
237 {
238 inet_ntop(AF_INET6, &this->to6, to_str, sizeof(to_str));
239 }
240 written += print_in_hook(dst, len, "%s..%s", from_str, to_str);
241 }
242 else
243 {
244 written += print_in_hook(dst, len, "%s/%d", from_str, this->netbits);
245 }
246 }
247
248 /* check if we have protocol and/or port selectors */
249 has_proto = this->protocol != 0;
250 has_ports = !(this->from_port == 0 && this->to_port == 0xFFFF);
251
252 if (!has_proto && !has_ports)
253 {
254 return written;
255 }
256
257 written += print_in_hook(dst, len, "[");
258
259 /* build protocol string */
260 if (has_proto)
261 {
262 struct protoent *proto = getprotobynumber(this->protocol);
263
264 if (proto)
265 {
266 written += print_in_hook(dst, len, "%s", proto->p_name);
267 serv_proto = proto->p_name;
268 }
269 else
270 {
271 written += print_in_hook(dst, len, "%d", this->protocol);
272 }
273 }
274
275 if (has_proto && has_ports)
276 {
277 written += print_in_hook(dst, len, "/");
278 }
279
280 /* build port string */
281 if (has_ports)
282 {
283 if (this->from_port == this->to_port)
284 {
285 struct servent *serv = getservbyport(htons(this->from_port), serv_proto);
286
287 if (serv)
288 {
289 written += print_in_hook(dst, len, "%s", serv->s_name);
290 }
291 else
292 {
293 written += print_in_hook(dst, len, "%d", this->from_port);
294 }
295 }
296 else
297 {
298 written += print_in_hook(dst, len, "%d-%d", this->from_port, this->to_port);
299 }
300 }
301
302 written += print_in_hook(dst, len, "]");
303
304 return written;
305 }
306
307 /**
308 * implements traffic_selector_t.get_subset
309 */
310 static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_traffic_selector_t *other)
311 {
312 if (this->type == other->type && (this->protocol == other->protocol ||
313 this->protocol == 0 || other->protocol == 0))
314 {
315 u_int16_t from_port, to_port;
316 u_char *from, *to;
317 u_int8_t protocol;
318 size_t size;
319 private_traffic_selector_t *new_ts;
320
321 /* calculate the maximum port range allowed for both */
322 from_port = max(this->from_port, other->from_port);
323 to_port = min(this->to_port, other->to_port);
324 if (from_port > to_port)
325 {
326 return NULL;
327 }
328 /* select protocol, which is not zero */
329 protocol = max(this->protocol, other->protocol);
330
331 switch (this->type)
332 {
333 case TS_IPV4_ADDR_RANGE:
334 size = sizeof(this->from4);
335 break;
336 case TS_IPV6_ADDR_RANGE:
337 size = sizeof(this->from6);
338 break;
339 default:
340 return NULL;
341 }
342
343 /* get higher from-address */
344 if (memcmp(this->from, other->from, size) > 0)
345 {
346 from = this->from;
347 }
348 else
349 {
350 from = other->from;
351 }
352 /* get lower to-address */
353 if (memcmp(this->to, other->to, size) > 0)
354 {
355 to = other->to;
356 }
357 else
358 {
359 to = this->to;
360 }
361 /* if "from" > "to", we don't have a match */
362 if (memcmp(from, to, size) > 0)
363 {
364 return NULL;
365 }
366
367 /* we have a match in protocol, port, and address: return it... */
368 new_ts = traffic_selector_create(protocol, this->type, from_port, to_port);
369 new_ts->dynamic = this->dynamic || other->dynamic;
370 memcpy(new_ts->from, from, size);
371 memcpy(new_ts->to, to, size);
372 calc_netbits(new_ts);
373 return &new_ts->public;
374 }
375 return NULL;
376 }
377
378 /**
379 * implements traffic_selector_t.equals
380 */
381 static bool equals(private_traffic_selector_t *this, private_traffic_selector_t *other)
382 {
383 if (this->type != other->type)
384 {
385 return FALSE;
386 }
387 if (!(this->from_port == other->from_port &&
388 this->to_port == other->to_port &&
389 this->protocol == other->protocol))
390 {
391 return FALSE;
392 }
393 switch (this->type)
394 {
395 case TS_IPV4_ADDR_RANGE:
396 if (memeq(this->from4, other->from4, sizeof(this->from4)))
397 {
398 return TRUE;
399 }
400 break;
401 case TS_IPV6_ADDR_RANGE:
402 if (memeq(this->from6, other->from6, sizeof(this->from6)))
403 {
404 return TRUE;
405 }
406 break;
407 default:
408 break;
409 }
410 return FALSE;
411 }
412
413 /**
414 * Implements traffic_selector_t.get_from_address.
415 */
416 static chunk_t get_from_address(private_traffic_selector_t *this)
417 {
418 switch (this->type)
419 {
420 case TS_IPV4_ADDR_RANGE:
421 return chunk_create(this->from, sizeof(this->from4));
422 case TS_IPV6_ADDR_RANGE:
423 return chunk_create(this->from, sizeof(this->from6));
424 default:
425 return chunk_empty;
426 }
427 }
428
429 /**
430 * Implements traffic_selector_t.get_to_address.
431 */
432 static chunk_t get_to_address(private_traffic_selector_t *this)
433 {
434 switch (this->type)
435 {
436 case TS_IPV4_ADDR_RANGE:
437 return chunk_create(this->to, sizeof(this->to4));
438 case TS_IPV6_ADDR_RANGE:
439 return chunk_create(this->to, sizeof(this->to6));
440 default:
441 return chunk_empty;
442 }
443 }
444
445 /**
446 * Implements traffic_selector_t.get_from_port.
447 */
448 static u_int16_t get_from_port(private_traffic_selector_t *this)
449 {
450 return this->from_port;
451 }
452
453 /**
454 * Implements traffic_selector_t.get_to_port.
455 */
456 static u_int16_t get_to_port(private_traffic_selector_t *this)
457 {
458 return this->to_port;
459 }
460
461 /**
462 * Implements traffic_selector_t.get_type.
463 */
464 static ts_type_t get_type(private_traffic_selector_t *this)
465 {
466 return this->type;
467 }
468
469 /**
470 * Implements traffic_selector_t.get_protocol.
471 */
472 static u_int8_t get_protocol(private_traffic_selector_t *this)
473 {
474 return this->protocol;
475 }
476
477 /**
478 * Implements traffic_selector_t.is_host.
479 */
480 static bool is_host(private_traffic_selector_t *this, host_t *host)
481 {
482 if (host)
483 {
484 chunk_t addr;
485 int family = host->get_family(host);
486
487 if ((family == AF_INET && this->type == TS_IPV4_ADDR_RANGE) ||
488 (family == AF_INET6 && this->type == TS_IPV6_ADDR_RANGE))
489 {
490 addr = host->get_address(host);
491 if (memeq(addr.ptr, this->from, addr.len) &&
492 memeq(addr.ptr, this->to, addr.len))
493 {
494 return TRUE;
495 }
496 }
497 }
498 else
499 {
500 size_t length = (this->type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
501
502 if (this->dynamic)
503 {
504 return TRUE;
505 }
506
507 if (memeq(this->from, this->to, length))
508 {
509 return TRUE;
510 }
511 }
512 return FALSE;
513 }
514
515 /**
516 * Implementation of traffic_selector_t.is_dynamic
517 */
518 static bool is_dynamic(private_traffic_selector_t *this)
519 {
520 return this->dynamic;
521 }
522
523 /**
524 * Implements traffic_selector_t.set_address.
525 */
526 static void set_address(private_traffic_selector_t *this, host_t *host)
527 {
528 if (this->dynamic)
529 {
530 this->type = host->get_family(host) == AF_INET ?
531 TS_IPV4_ADDR_RANGE : TS_IPV6_ADDR_RANGE;
532
533 if (host->is_anyaddr(host))
534 {
535 memset(this->from6, 0x00, sizeof(this->from6));
536 memset(this->to6, 0xFF, sizeof(this->to6));
537 this->netbits = 0;
538 }
539 else
540 {
541 chunk_t from = host->get_address(host);
542 memcpy(this->from, from.ptr, from.len);
543 memcpy(this->to, from.ptr, from.len);
544 this->netbits = from.len * 8;
545 }
546 }
547 }
548
549 /**
550 * Implements traffic_selector_t.is_contained_in.
551 */
552 static bool is_contained_in(private_traffic_selector_t *this,
553 private_traffic_selector_t *other)
554 {
555 private_traffic_selector_t *subset;
556 bool contained_in = FALSE;
557
558 subset = (private_traffic_selector_t*)get_subset(this, other);
559
560 if (subset)
561 {
562 if (equals(subset, this))
563 {
564 contained_in = TRUE;
565 }
566 free(subset);
567 }
568 return contained_in;
569 }
570
571 /**
572 * Implements traffic_selector_t.includes.
573 */
574 static bool includes(private_traffic_selector_t *this, host_t *host)
575 {
576 chunk_t addr;
577 int family = host->get_family(host);
578
579 if ((family == AF_INET && this->type == TS_IPV4_ADDR_RANGE) ||
580 (family == AF_INET6 && this->type == TS_IPV6_ADDR_RANGE))
581 {
582 addr = host->get_address(host);
583
584 return memcmp(this->from, addr.ptr, addr.len) <= 0 &&
585 memcmp(this->to, addr.ptr, addr.len) >= 0;
586 }
587
588 return FALSE;
589 }
590
591 /**
592 * Implements traffic_selector_t.to_subnet.
593 */
594 static void to_subnet(private_traffic_selector_t *this, host_t **net, u_int8_t *mask)
595 {
596 /* there is no way to do this cleanly, as the address range may
597 * be anything else but a subnet. We use from_addr as subnet
598 * and try to calculate a usable subnet mask.
599 */
600 int family, byte;
601 u_int16_t port = 0;
602 chunk_t net_chunk;
603
604 *mask = (this->netbits == NON_SUBNET_ADDRESS_RANGE) ? calc_netbits(this)
605 : this->netbits;
606
607 switch (this->type)
608 {
609 case TS_IPV4_ADDR_RANGE:
610 family = AF_INET;
611 net_chunk.len = sizeof(this->from4);
612 break;
613 case TS_IPV6_ADDR_RANGE:
614 family = AF_INET6;
615 net_chunk.len = sizeof(this->from6);
616 break;
617 default:
618 /* unreachable */
619 return;
620 }
621
622 net_chunk.ptr = malloc(net_chunk.len);
623 memcpy(net_chunk.ptr, this->from, net_chunk.len);
624
625 for (byte = net_chunk.len - 1; byte >= (*mask / 8); --byte)
626 {
627 int shift = (byte + 1) * 8 - *mask;
628 net_chunk.ptr[byte] = net_chunk.ptr[byte] & (0xFF << shift);
629 }
630
631 if (this->to_port == this->from_port)
632 {
633 port = this->to_port;
634 }
635
636 *net = host_create_from_chunk(family, net_chunk, port);
637 chunk_free(&net_chunk);
638 }
639
640 /**
641 * Implements traffic_selector_t.clone.
642 */
643 static traffic_selector_t *clone_(private_traffic_selector_t *this)
644 {
645 private_traffic_selector_t *clone;
646
647 clone = traffic_selector_create(this->protocol, this->type,
648 this->from_port, this->to_port);
649 clone->netbits = this->netbits;
650 clone->dynamic = this->dynamic;
651
652 switch (clone->type)
653 {
654 case TS_IPV4_ADDR_RANGE:
655 memcpy(clone->from4, this->from4, sizeof(this->from4));
656 memcpy(clone->to4, this->to4, sizeof(this->to4));
657 return &clone->public;
658 case TS_IPV6_ADDR_RANGE:
659 memcpy(clone->from6, this->from6, sizeof(this->from6));
660 memcpy(clone->to6, this->to6, sizeof(this->to6));
661 return &clone->public;
662 default:
663 /* unreachable */
664 return &clone->public;
665 }
666 }
667
668 /**
669 * Implements traffic_selector_t.destroy.
670 */
671 static void destroy(private_traffic_selector_t *this)
672 {
673 free(this);
674 }
675
676 /*
677 * see header
678 */
679 traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol,
680 ts_type_t type,
681 chunk_t from, u_int16_t from_port,
682 chunk_t to, u_int16_t to_port)
683 {
684 private_traffic_selector_t *this = traffic_selector_create(protocol, type,
685 from_port, to_port);
686
687 switch (type)
688 {
689 case TS_IPV4_ADDR_RANGE:
690 if (from.len != 4 || to.len != 4)
691 {
692 free(this);
693 return NULL;
694 }
695 memcpy(this->from4, from.ptr, from.len);
696 memcpy(this->to4, to.ptr, to.len);
697 break;
698 case TS_IPV6_ADDR_RANGE:
699 if (from.len != 16 || to.len != 16)
700 {
701 free(this);
702 return NULL;
703 }
704 memcpy(this->from6, from.ptr, from.len);
705 memcpy(this->to6, to.ptr, to.len);
706 break;
707 default:
708 free(this);
709 return NULL;
710 }
711 calc_netbits(this);
712 return (&this->public);
713 }
714
715 /*
716 * see header
717 */
718 traffic_selector_t *traffic_selector_create_from_rfc3779_format(ts_type_t type,
719 chunk_t from, chunk_t to)
720 {
721 size_t len;
722 private_traffic_selector_t *this = traffic_selector_create(0, type, 0, 65535);
723
724 switch (type)
725 {
726 case TS_IPV4_ADDR_RANGE:
727 len = 4;
728 break;
729 case TS_IPV6_ADDR_RANGE:
730 len = 16;
731 break;
732 default:
733 free(this);
734 return NULL;
735 }
736 memset(this->from, 0x00, len);
737 memset(this->to , 0xff, len);
738
739 if (from.len > 1)
740 {
741 memcpy(this->from, from.ptr+1, from.len-1);
742 }
743 if (to.len > 1)
744 {
745 u_int8_t mask = to.ptr[0] ? (1 << to.ptr[0]) - 1 : 0;
746
747 memcpy(this->to, to.ptr+1, to.len-1);
748 this->to[to.len-2] |= mask;
749 }
750 this->netbits = chunk_equals(from, to) ? (from.len-1)*8 - from.ptr[0]
751 : NON_SUBNET_ADDRESS_RANGE;
752 return (&this->public);
753 }
754
755 /*
756 * see header
757 */
758 traffic_selector_t *traffic_selector_create_from_subnet(host_t *net,
759 u_int8_t netbits, u_int8_t protocol, u_int16_t port)
760 {
761 private_traffic_selector_t *this = traffic_selector_create(protocol, 0, 0, 65535);
762
763 switch (net->get_family(net))
764 {
765 case AF_INET:
766 {
767 chunk_t from;
768
769 this->type = TS_IPV4_ADDR_RANGE;
770 from = net->get_address(net);
771 memcpy(this->from, from.ptr, from.len);
772 if (this->from4[0] == 0)
773 {
774 /* use /0 for 0.0.0.0 */
775 this->to4[0] = ~0;
776 this->netbits = 0;
777 }
778 else
779 {
780 calc_range(this, netbits);
781 }
782 break;
783 }
784 case AF_INET6:
785 {
786 chunk_t from;
787
788 this->type = TS_IPV6_ADDR_RANGE;
789 from = net->get_address(net);
790 memcpy(this->from, from.ptr, from.len);
791 if (this->from6[0] == 0 && this->from6[1] == 0 &&
792 this->from6[2] == 0 && this->from6[3] == 0)
793 {
794 /* use /0 for ::0 */
795 this->to6[0] = ~0;
796 this->to6[1] = ~0;
797 this->to6[2] = ~0;
798 this->to6[3] = ~0;
799 this->netbits = 0;
800 }
801 else
802 {
803 calc_range(this, netbits);
804 }
805 break;
806 }
807 default:
808 {
809 net->destroy(net);
810 free(this);
811 return NULL;
812 }
813 }
814 if (port)
815 {
816 this->from_port = port;
817 this->to_port = port;
818 }
819 net->destroy(net);
820 return (&this->public);
821 }
822
823 /*
824 * see header
825 */
826 traffic_selector_t *traffic_selector_create_from_string(
827 u_int8_t protocol, ts_type_t type,
828 char *from_addr, u_int16_t from_port,
829 char *to_addr, u_int16_t to_port)
830 {
831 private_traffic_selector_t *this = traffic_selector_create(protocol, type,
832 from_port, to_port);
833
834 switch (type)
835 {
836 case TS_IPV4_ADDR_RANGE:
837 if (inet_pton(AF_INET, from_addr, (struct in_addr*)this->from4) < 0)
838 {
839 free(this);
840 return NULL;
841 }
842 if (inet_pton(AF_INET, to_addr, (struct in_addr*)this->to4) < 0)
843 {
844 free(this);
845 return NULL;
846 }
847 break;
848 case TS_IPV6_ADDR_RANGE:
849 if (inet_pton(AF_INET6, from_addr, (struct in6_addr*)this->from6) < 0)
850 {
851 free(this);
852 return NULL;
853 }
854 if (inet_pton(AF_INET6, to_addr, (struct in6_addr*)this->to6) < 0)
855 {
856 free(this);
857 return NULL;
858 }
859 break;
860 }
861 calc_netbits(this);
862 return (&this->public);
863 }
864
865 /*
866 * see header
867 */
868 traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
869 u_int16_t from_port, u_int16_t to_port)
870 {
871 private_traffic_selector_t *this = traffic_selector_create(
872 protocol, TS_IPV4_ADDR_RANGE, from_port, to_port);
873
874 memset(this->from6, 0, sizeof(this->from6));
875 memset(this->to6, 0xFF, sizeof(this->to6));
876 this->netbits = 0;
877 this->dynamic = TRUE;
878
879 return &this->public;
880 }
881
882 /*
883 * see declaration
884 */
885 static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol,
886 ts_type_t type, u_int16_t from_port, u_int16_t to_port)
887 {
888 private_traffic_selector_t *this = malloc_thing(private_traffic_selector_t);
889
890 /* public functions */
891 this->public.get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset;
892 this->public.equals = (bool(*)(traffic_selector_t*,traffic_selector_t*))equals;
893 this->public.get_from_address = (chunk_t(*)(traffic_selector_t*))get_from_address;
894 this->public.get_to_address = (chunk_t(*)(traffic_selector_t*))get_to_address;
895 this->public.get_from_port = (u_int16_t(*)(traffic_selector_t*))get_from_port;
896 this->public.get_to_port = (u_int16_t(*)(traffic_selector_t*))get_to_port;
897 this->public.get_type = (ts_type_t(*)(traffic_selector_t*))get_type;
898 this->public.get_protocol = (u_int8_t(*)(traffic_selector_t*))get_protocol;
899 this->public.is_host = (bool(*)(traffic_selector_t*,host_t*))is_host;
900 this->public.is_dynamic = (bool(*)(traffic_selector_t*))is_dynamic;
901 this->public.is_contained_in = (bool(*)(traffic_selector_t*,traffic_selector_t*))is_contained_in;
902 this->public.includes = (bool(*)(traffic_selector_t*,host_t*))includes;
903 this->public.set_address = (void(*)(traffic_selector_t*,host_t*))set_address;
904 this->public.to_subnet = (void(*)(traffic_selector_t*,host_t**,u_int8_t*))to_subnet;
905 this->public.clone = (traffic_selector_t*(*)(traffic_selector_t*))clone_;
906 this->public.destroy = (void(*)(traffic_selector_t*))destroy;
907
908 this->from_port = from_port;
909 this->to_port = to_port;
910 this->protocol = protocol;
911 this->type = type;
912 this->dynamic = FALSE;
913
914 return this;
915 }
916