Skip to content

Commit

Permalink
Replace DEFAULT_TCP_RELAY_PORTS_COUNT with a compile-time calculation
Browse files Browse the repository at this point in the history
That way we don't have to rely on a human to remember to keep
DEFAULT_TCP_RELAY_PORTS_COUNT in sync with DEFAULT_TCP_RELAY_PORTS.
  • Loading branch information
nurupo committed Dec 5, 2023
1 parent 63fb294 commit 26d41fc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8bf35cb22ca6d5d85f5fe8993a6ce9424d2048f9bd6a57ab45dc52a6c8444fb3 /usr/local/bin/tox-bootstrapd
e0e1c3f1ba835f76faca9383b606ed1a5b778bd79caae9a1e3bb4bf3bfaed19a /usr/local/bin/tox-bootstrapd
20 changes: 13 additions & 7 deletions other/bootstrap_daemon/src/config.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2016-2023 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/

Expand Down Expand Up @@ -39,22 +39,28 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);

uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS};
uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS};

for (int i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; ++i) {
log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]);
// Check to avoid calling malloc(0) later on
// NOLINTNEXTLINE, clang-tidy: error: suspicious comparison of 'sizeof(expr)' to a constant [bugprone-sizeof-expression,-warnings-as-errors]
static_assert(sizeof(default_ports) > 0, "At least one default TCP relay port should be provided");

const size_t default_ports_count = sizeof(default_ports)/sizeof(*default_ports);

for (size_t i = 0; i < default_ports_count; ++i) {
log_write(LOG_LEVEL_INFO, "Port #%zu: %u\n", i, default_ports[i]);
}

// similar procedure to the one of reading config file below
*tcp_relay_ports = (uint16_t *)malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t));
*tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t));

for (int i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; ++i) {
for (size_t i = 0; i < default_ports_count; ++i) {

(*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];

if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
log_write(LOG_LEVEL_WARNING, "Port #%zu: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue;
}
Expand Down
5 changes: 2 additions & 3 deletions other/bootstrap_daemon/src/config_defaults.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2016-2023 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/

Expand All @@ -19,8 +19,7 @@
#define DEFAULT_ENABLE_IPV4_FALLBACK 1 // 1 - true, 0 - false
#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
#define DEFAULT_ENABLE_TCP_RELAY 1 // 1 - true, 0 - false
#define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports. make sure to adjust DEFAULT_TCP_RELAY_PORTS_COUNT accordingly
#define DEFAULT_TCP_RELAY_PORTS_COUNT 3
#define DEFAULT_TCP_RELAY_PORTS 443, 3389, 33445 // comma-separated list of ports
#define DEFAULT_ENABLE_MOTD 1 // 1 - true, 0 - false
#define DEFAULT_MOTD DAEMON_NAME

Expand Down

0 comments on commit 26d41fc

Please sign in to comment.