1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file */
13
14 #include <inttypes.h>
15 #include <stdbool.h>
16
17 #include <isc/mem.h>
18 #include <isc/portset.h>
19 #include <isc/string.h>
20 #include <isc/types.h>
21 #include <isc/util.h>
22
23 #define ISC_PORTSET_BUFSIZE (65536 / (sizeof(uint32_t) * 8))
24
25 /*%
26 * Internal representation of portset. It's an array of 32-bit integers, each
27 * bit corresponding to a single port in the ascending order. For example,
28 * the second most significant bit of buf[0] corresponds to port 1.
29 */
30 struct isc_portset {
31 unsigned int nports; /*%< number of ports in the set */
32 uint32_t buf[ISC_PORTSET_BUFSIZE];
33 };
34
35 static inline bool
portset_isset(isc_portset_t * portset,in_port_t port)36 portset_isset(isc_portset_t *portset, in_port_t port) {
37 return ((portset->buf[port >> 5] & ((uint32_t)1 << (port & 31))) != 0);
38 }
39
40 static inline void
portset_add(isc_portset_t * portset,in_port_t port)41 portset_add(isc_portset_t *portset, in_port_t port) {
42 if (!portset_isset(portset, port)) {
43 portset->nports++;
44 portset->buf[port >> 5] |= ((uint32_t)1 << (port & 31));
45 }
46 }
47
48 static inline void
portset_remove(isc_portset_t * portset,in_port_t port)49 portset_remove(isc_portset_t *portset, in_port_t port) {
50 if (portset_isset(portset, port)) {
51 portset->nports--;
52 portset->buf[port >> 5] &= ~((uint32_t)1 << (port & 31));
53 }
54 }
55
56 isc_result_t
isc_portset_create(isc_mem_t * mctx,isc_portset_t ** portsetp)57 isc_portset_create(isc_mem_t *mctx, isc_portset_t **portsetp) {
58 isc_portset_t *portset;
59
60 REQUIRE(portsetp != NULL && *portsetp == NULL);
61
62 portset = isc_mem_get(mctx, sizeof(*portset));
63
64 /* Make the set 'empty' by default */
65 memset(portset, 0, sizeof(*portset));
66 *portsetp = portset;
67
68 return (ISC_R_SUCCESS);
69 }
70
71 void
isc_portset_destroy(isc_mem_t * mctx,isc_portset_t ** portsetp)72 isc_portset_destroy(isc_mem_t *mctx, isc_portset_t **portsetp) {
73 isc_portset_t *portset;
74
75 REQUIRE(portsetp != NULL);
76 portset = *portsetp;
77
78 isc_mem_put(mctx, portset, sizeof(*portset));
79 }
80
81 bool
isc_portset_isset(isc_portset_t * portset,in_port_t port)82 isc_portset_isset(isc_portset_t *portset, in_port_t port) {
83 REQUIRE(portset != NULL);
84
85 return (portset_isset(portset, port));
86 }
87
88 unsigned int
isc_portset_nports(isc_portset_t * portset)89 isc_portset_nports(isc_portset_t *portset) {
90 REQUIRE(portset != NULL);
91
92 return (portset->nports);
93 }
94
95 void
isc_portset_add(isc_portset_t * portset,in_port_t port)96 isc_portset_add(isc_portset_t *portset, in_port_t port) {
97 REQUIRE(portset != NULL);
98
99 portset_add(portset, port);
100 }
101
102 void
isc_portset_remove(isc_portset_t * portset,in_port_t port)103 isc_portset_remove(isc_portset_t *portset, in_port_t port) {
104 portset_remove(portset, port);
105 }
106
107 void
isc_portset_addrange(isc_portset_t * portset,in_port_t port_lo,in_port_t port_hi)108 isc_portset_addrange(isc_portset_t *portset, in_port_t port_lo,
109 in_port_t port_hi) {
110 in_port_t p;
111
112 REQUIRE(portset != NULL);
113 REQUIRE(port_lo <= port_hi);
114
115 p = port_lo;
116 do {
117 portset_add(portset, p);
118 } while (p++ < port_hi);
119 }
120
121 void
isc_portset_removerange(isc_portset_t * portset,in_port_t port_lo,in_port_t port_hi)122 isc_portset_removerange(isc_portset_t *portset, in_port_t port_lo,
123 in_port_t port_hi) {
124 in_port_t p;
125
126 REQUIRE(portset != NULL);
127 REQUIRE(port_lo <= port_hi);
128
129 p = port_lo;
130 do {
131 portset_remove(portset, p);
132 } while (p++ < port_hi);
133 }
134