Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a

8f0d505f9a feat: add ngc events
9b8216e70c refactor: Make event dispatch ordered by receive time.
814c12a6f4 cleanup: Add dynamically derived array sizes to the API.
226b23be12 cleanup: Add explicit array sizes to toxencryptsave.
ef33cb4de0 cleanup: Add Toxav alias for ToxAV.
1da723b34d cleanup: Make Tox_Options a typedef.
b148a2afff chore: Simplify msvc build using vcpkg.
5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API.
c9ca4007e3 refactor: Align group message sending with other send functions.
6c6c0b1b1b cleanup: Make setters take non-const `Tox *`.
a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`.
baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`.
79f55bd06a cleanup: Put the size of fixed arrays into the API types.
1e73698db2 cleanup: Add typedefs for public API int identifiers.
cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash.
32576656bb Make the comment capitalization uniform
aff4dda17c Spellcheck tox-bootstrapd
40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools.
ebafd51be7 chore: Use GPL license with https.
0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function.
5407384211 cleanup: Use github actions matrix to simplify CI.
82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time.
1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators.
6a90ddfe4e cleanup: Run clang-tidy on headers, as well.
bd930cc80a cleanup: Make TCP connection failures a warning instead of error.
fad6e4e173 cleanup: Make all .c files include the headers they need.
ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings.
REVERT: f1df709b87 feat: add ngc events
REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
This commit is contained in:
2024-01-09 16:39:05 +01:00
parent b2ae9530a4
commit 9ace11a0e2
152 changed files with 1542 additions and 1140 deletions

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2016-2024 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
@ -10,12 +10,12 @@
#include "command_line_arguments.h"
#include "global.h"
#include "log.h"
#include "../../../toxcore/ccompat.h"
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
@ -24,9 +24,9 @@
*/
static void print_help(void)
{
// 2 space ident
// make sure all lines fit into 80 columns
// make sure options are listed in alphabetical order
// 2 space indent
// Make sure all lines fit into 80 columns
// Make sure options are listed in alphabetical order
log_write(LOG_LEVEL_INFO,
"Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
"\n"
@ -39,7 +39,7 @@ static void print_help(void)
" (detach from the terminal) and won't use the PID file.\n"
" --help Print this help message.\n"
" --log-backend=BACKEND Specify which logging backend to use.\n"
" Valid BACKEND values (case sensetive):\n"
" Valid BACKEND values (case sensitive):\n"
" syslog Writes log messages to syslog.\n"
" Default option when no --log-backend is\n"
" specified.\n"
@ -47,13 +47,14 @@ static void print_help(void)
" --version Print version information.\n");
}
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground)
Cli_Status handle_command_line_arguments(
int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground)
{
if (argc < 2) {
log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
opterr = 0;
@ -89,7 +90,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
case 'h':
print_help();
exit(0);
return CLI_STATUS_DONE;
case 'l':
if (strcmp(optarg, "syslog") == 0) {
@ -101,24 +102,24 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
} else {
log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
break;
case 'v':
log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
exit(0);
return CLI_STATUS_DONE;
case '?':
log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
return CLI_STATUS_ERROR;
case ':':
log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
}
@ -129,6 +130,8 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
if (!cfg_file_path_set) {
log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
print_help();
exit(1);
return CLI_STATUS_ERROR;
}
return CLI_STATUS_OK;
}

View File

@ -12,6 +12,15 @@
#include "log.h"
typedef enum Cli_Status {
/** Continue the program. Command line processing completed. */
CLI_STATUS_OK,
/** Stop the program with success status. */
CLI_STATUS_DONE,
/** Stop the program with error status. */
CLI_STATUS_ERROR,
} Cli_Status;
/**
* Handles command line arguments, setting cfg_file_path and log_backend.
* Terminates the application if incorrect arguments are specified.
@ -22,7 +31,8 @@
* @param log_backend Sets to the provided by the user log backend option.
* @param run_in_foreground Sets to the provided by the user foreground option.
*/
void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground);
Cli_Status handle_command_line_arguments(
int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend,
bool *run_in_foreground);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2023 The TokTok team.
* Copyright © 2016-2024 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
@ -10,6 +10,7 @@
#include "config.h"
#include "config_defaults.h"
#include "global.h"
#include "log.h"
#include <stdio.h>
@ -18,6 +19,10 @@
#include <libconfig.h>
#include "../../../toxcore/DHT.h"
#include "../../../toxcore/ccompat.h"
#include "../../../toxcore/crypto_core.h"
#include "../../../toxcore/network.h"
#include "../../bootstrap_node_packets.h"
/**
@ -51,7 +56,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
log_write(LOG_LEVEL_INFO, "Port #%zu: %u\n", i, default_ports[i]);
}
// similar procedure to the one of reading config file below
// Similar procedure to the one of reading config file below
*tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t));
for (size_t i = 0; i < default_ports_count; ++i) {
@ -68,7 +73,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
++*tcp_relay_port_count;
}
// the loop above skips invalid ports, so we adjust the allocated memory size
// The loop above skips invalid ports, so we adjust the allocated memory size
if ((*tcp_relay_port_count) > 0) {
*tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
} else {
@ -98,7 +103,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
config_setting_t *elem = config_setting_get_elem(ports_array, i);
if (elem == nullptr) {
// it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be
// It's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be
log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i);
break;
}
@ -120,7 +125,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
++*tcp_relay_port_count;
}
// the loop above skips invalid ports, so we adjust the allocated memory size
// The loop above skips invalid ports, so we adjust the allocated memory size
if ((*tcp_relay_port_count) > 0) {
*tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
} else {
@ -260,7 +265,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
// show info about tcp ports only if tcp relay is enabled
// Show info about tcp ports only if tcp relay is enabled
if (*enable_tcp_relay) {
if (*tcp_relay_port_count == 0) {
log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n");
@ -407,7 +412,7 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
log_write(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key);
next:
// config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly
// config_setting_lookup_string() allocates string inside and doesn't allow us to free it directly
// though it's freed when the element is removed, so we free it right away in order to keep memory
// consumption minimal
config_setting_remove_elem(node_list, 0);

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2016-2024 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
@ -30,7 +30,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
* Bootstraps off nodes listed in the config file.
*
* @return 1 on success, some or no bootstrap nodes were added
* 0 on failure, a error accured while parsing config file.
* 0 on failure, an error occurred while parsing the config file.
*/
int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6);

View File

@ -3,6 +3,8 @@
* Copyright © 2015-2016 Tox project.
*/
#include <stdarg.h>
/*
* Tox DHT bootstrap daemon.
* Logging utility with support of multiple logging backends.

View File

@ -9,8 +9,11 @@
*/
#include "log_backend_stdout.h"
#include <stdarg.h>
#include <stdio.h>
#include "log.h"
static FILE *log_backend_stdout_level(LOG_LEVEL level)
{
switch (level) {

View File

@ -10,10 +10,12 @@
#include "log_backend_syslog.h"
#include "global.h"
#include "log.h"
#include "../../../toxcore/ccompat.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

View File

@ -23,17 +23,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// toxcore
#include "../../../toxcore/tox.h"
#include "../../../toxcore/DHT.h"
#include "../../../toxcore/LAN_discovery.h"
#include "../../../toxcore/TCP_server.h"
#include "../../../toxcore/announce.h"
#include "../../../toxcore/ccompat.h"
#include "../../../toxcore/crypto_core.h"
#include "../../../toxcore/forwarding.h"
#include "../../../toxcore/group_announce.h"
#include "../../../toxcore/group_onion_announce.h"
#include "../../../toxcore/logger.h"
#include "../../../toxcore/mem.h"
#include "../../../toxcore/mono_time.h"
#include "../../../toxcore/network.h"
#include "../../../toxcore/onion.h"
#include "../../../toxcore/onion_announce.h"
#include "../../../toxcore/util.h"
// misc
#include "../../bootstrap_node_packets.h"
@ -116,7 +123,7 @@ static void print_public_key(const uint8_t *public_key)
// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend
// Terminates the application if the daemonization fails.
static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path)
{
// Check if the PID file exists
FILE *pid_file = fopen(pid_file_path, "r");
@ -131,7 +138,7 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
if (pid_file == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
exit(1);
return CLI_STATUS_ERROR;
}
// Fork off from the parent process
@ -141,27 +148,27 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
fprintf(pid_file, "%d", pid);
fclose(pid_file);
log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid);
exit(0);
return CLI_STATUS_DONE;
} else {
fclose(pid_file);
}
if (pid < 0) {
log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Create a new SID for the child process
if (setsid() < 0) {
log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Change the current working directory
if ((chdir("/")) < 0) {
log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n");
exit(1);
return CLI_STATUS_ERROR;
}
// Go quiet
@ -170,6 +177,8 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
close(STDIN_FILENO);
close(STDERR_FILENO);
}
return CLI_STATUS_OK;
}
// Logs toxcore logger message using our logger facility
@ -212,11 +221,18 @@ int main(int argc, char *argv[])
char *cfg_file_path = nullptr;
bool run_in_foreground = false;
// choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal
// Choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal
LOG_BACKEND log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG;
log_open(log_backend);
handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground);
switch (handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground)) {
case CLI_STATUS_OK:
break;
case CLI_STATUS_DONE:
return 0;
case CLI_STATUS_ERROR:
return 1;
}
log_close();
log_open(log_backend);
@ -254,7 +270,14 @@ int main(int argc, char *argv[])
}
if (!run_in_foreground) {
daemonize(log_backend, pid_file_path);
switch (daemonize(log_backend, pid_file_path)) {
case CLI_STATUS_OK:
break;
case CLI_STATUS_DONE:
return 0;
case CLI_STATUS_ERROR:
return 1;
}
}
free(pid_file_path);