2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/lib/isc/portset.c
Evan Hunt 10accd6260 clean up uses of ISC_R_NOMEMORY
the isc_mem allocation functions can no longer fail; as a result,
ISC_R_NOMEMORY is now rarely used: only when an external library
such as libjson-c or libfstrm could return NULL. (even in
these cases, arguably we should assert rather than returning
ISC_R_NOMEMORY.)

code and comments that mentioned ISC_R_NOMEMORY have been
cleaned up, and the following functions have been changed to
type void, since (in most cases) the only value they could
return was ISC_R_SUCCESS:

- dns_dns64_create()
- dns_dyndb_create()
- dns_ipkeylist_resize()
- dns_kasp_create()
- dns_kasp_key_create()
- dns_keystore_create()
- dns_order_create()
- dns_order_add()
- dns_peerlist_new()
- dns_tkeyctx_create()
- dns_view_create()
- dns_zone_setorigin()
- dns_zone_setfile()
- dns_zone_setstream()
- dns_zone_getdbtype()
- dns_zone_setjournal()
- dns_zone_setkeydirectory()
- isc_lex_openstream()
- isc_portset_create()
- isc_symtab_create()

(the exception is dns_view_create(), which could have returned
other error codes in the event of a crypto library failure when
calling isc_file_sanitize(), but that should be a RUNTIME_CHECK
anyway.)
2025-01-23 15:54:57 -08:00

132 lines
2.9 KiB
C

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
#include <inttypes.h>
#include <stdbool.h>
#include <isc/mem.h>
#include <isc/portset.h>
#include <isc/string.h>
#include <isc/types.h>
#include <isc/util.h>
#define ISC_PORTSET_BUFSIZE (65536 / (sizeof(uint32_t) * 8))
/*%
* Internal representation of portset. It's an array of 32-bit integers, each
* bit corresponding to a single port in the ascending order. For example,
* the second most significant bit of buf[0] corresponds to port 1.
*/
struct isc_portset {
unsigned int nports; /*%< number of ports in the set */
uint32_t buf[ISC_PORTSET_BUFSIZE];
};
static bool
portset_isset(isc_portset_t *portset, in_port_t port) {
return (portset->buf[port >> 5] & ((uint32_t)1 << (port & 31))) != 0;
}
static void
portset_add(isc_portset_t *portset, in_port_t port) {
if (!portset_isset(portset, port)) {
portset->nports++;
portset->buf[port >> 5] |= ((uint32_t)1 << (port & 31));
}
}
static void
portset_remove(isc_portset_t *portset, in_port_t port) {
if (portset_isset(portset, port)) {
portset->nports--;
portset->buf[port >> 5] &= ~((uint32_t)1 << (port & 31));
}
}
void
isc_portset_create(isc_mem_t *mctx, isc_portset_t **portsetp) {
isc_portset_t *portset;
REQUIRE(portsetp != NULL && *portsetp == NULL);
portset = isc_mem_get(mctx, sizeof(*portset));
*portset = (isc_portset_t){ 0 };
*portsetp = portset;
}
void
isc_portset_destroy(isc_mem_t *mctx, isc_portset_t **portsetp) {
isc_portset_t *portset;
REQUIRE(portsetp != NULL);
portset = *portsetp;
isc_mem_put(mctx, portset, sizeof(*portset));
}
bool
isc_portset_isset(isc_portset_t *portset, in_port_t port) {
REQUIRE(portset != NULL);
return portset_isset(portset, port);
}
unsigned int
isc_portset_nports(isc_portset_t *portset) {
REQUIRE(portset != NULL);
return portset->nports;
}
void
isc_portset_add(isc_portset_t *portset, in_port_t port) {
REQUIRE(portset != NULL);
portset_add(portset, port);
}
void
isc_portset_remove(isc_portset_t *portset, in_port_t port) {
portset_remove(portset, port);
}
void
isc_portset_addrange(isc_portset_t *portset, in_port_t port_lo,
in_port_t port_hi) {
in_port_t p;
REQUIRE(portset != NULL);
REQUIRE(port_lo <= port_hi);
p = port_lo;
do {
portset_add(portset, p);
} while (p++ < port_hi);
}
void
isc_portset_removerange(isc_portset_t *portset, in_port_t port_lo,
in_port_t port_hi) {
in_port_t p;
REQUIRE(portset != NULL);
REQUIRE(port_lo <= port_hi);
p = port_lo;
do {
portset_remove(portset, p);
} while (p++ < port_hi);
}