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