4 * Copyright (C) 2008 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: portset.c,v 1.4 2008/06/24 23:24:35 marka Exp */
26 #include <isc/portset.h>
27 #include <isc/string.h>
28 #include <isc/types.h>
31 #define ISC_PORTSET_BUFSIZE (65536 / (sizeof(isc_uint32_t) * 8))
34 * Internal representation of portset. It's an array of 32-bit integers, each
35 * bit corresponding to a single port in the ascending order. For example,
36 * the second most significant bit of buf[0] corresponds to port 1.
39 unsigned int nports
; /*%< number of ports in the set */
40 isc_uint32_t buf
[ISC_PORTSET_BUFSIZE
];
43 static inline isc_boolean_t
44 portset_isset(isc_portset_t
*portset
, in_port_t port
) {
45 return (ISC_TF((portset
->buf
[port
>> 5] & (1 << (port
& 31))) != 0));
49 portset_add(isc_portset_t
*portset
, in_port_t port
) {
50 if (!portset_isset(portset
, port
)) {
52 portset
->buf
[port
>> 5] |= (1 << (port
& 31));
57 portset_remove(isc_portset_t
*portset
, in_port_t port
) {
58 if (portset_isset(portset
, port
)) {
60 portset
->buf
[port
>> 5] &= ~(1 << (port
& 31));
65 isc_portset_create(isc_mem_t
*mctx
, isc_portset_t
**portsetp
) {
66 isc_portset_t
*portset
;
68 REQUIRE(portsetp
!= NULL
&& *portsetp
== NULL
);
70 portset
= isc_mem_get(mctx
, sizeof(*portset
));
72 return (ISC_R_NOMEMORY
);
74 /* Make the set 'empty' by default */
75 memset(portset
, 0, sizeof(*portset
));
78 return (ISC_R_SUCCESS
);
82 isc_portset_destroy(isc_mem_t
*mctx
, isc_portset_t
**portsetp
) {
83 isc_portset_t
*portset
;
85 REQUIRE(portsetp
!= NULL
);
88 isc_mem_put(mctx
, portset
, sizeof(*portset
));
92 isc_portset_isset(isc_portset_t
*portset
, in_port_t port
) {
93 REQUIRE(portset
!= NULL
);
95 return (portset_isset(portset
, port
));
99 isc_portset_nports(isc_portset_t
*portset
) {
100 REQUIRE(portset
!= NULL
);
102 return (portset
->nports
);
106 isc_portset_add(isc_portset_t
*portset
, in_port_t port
) {
107 REQUIRE(portset
!= NULL
);
109 portset_add(portset
, port
);
113 isc_portset_remove(isc_portset_t
*portset
, in_port_t port
) {
114 portset_remove(portset
, port
);
118 isc_portset_addrange(isc_portset_t
*portset
, in_port_t port_lo
,
123 REQUIRE(portset
!= NULL
);
124 REQUIRE(port_lo
<= port_hi
);
128 portset_add(portset
, p
);
129 } while (p
++ < port_hi
);
133 isc_portset_removerange(isc_portset_t
*portset
, in_port_t port_lo
,
138 REQUIRE(portset
!= NULL
);
139 REQUIRE(port_lo
<= port_hi
);
143 portset_remove(portset
, p
);
144 } while (p
++ < port_hi
);