Merge commit '227425b90e9a671118026689dd30967e127a1090' as 'external/toxcore/c-toxcore'

This commit is contained in:
2023-07-25 11:53:09 +02:00
467 changed files with 116591 additions and 0 deletions

View File

@ -0,0 +1,34 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//tools:no_undefined.bzl", "cc_library")
package(features = ["layering_check"])
cc_library(
name = "bootstrap_node_packets",
srcs = ["bootstrap_node_packets.c"],
hdrs = ["bootstrap_node_packets.h"],
visibility = ["//c-toxcore/other/bootstrap_daemon:__pkg__"],
deps = ["//c-toxcore/toxcore:network"],
)
cc_binary(
name = "DHT_bootstrap",
testonly = 1,
srcs = ["DHT_bootstrap.c"],
deps = [
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:LAN_discovery",
"//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:TCP_server",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:friend_requests",
"//c-toxcore/toxcore:group_onion_announce",
"//c-toxcore/toxcore:logger",
"//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:util",
],
)

View File

@ -0,0 +1,254 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2013 Tox project.
*/
/*
* DHT bootstrap
*
* A simple DHT boostrap node for tox.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../toxcore/DHT.h"
#include "../toxcore/LAN_discovery.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/friend_requests.h"
#include "../toxcore/group_onion_announce.h"
#include "../toxcore/logger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"
#define TCP_RELAY_ENABLED
#ifdef TCP_RELAY_ENABLED
#include "../toxcore/TCP_server.h"
#endif
#include "../testing/misc_tools.h"
#ifdef DHT_NODE_EXTRA_PACKETS
#include "./bootstrap_node_packets.h"
#define DHT_VERSION_NUMBER 1
#define DHT_MOTD "This is a test motd"
#endif
#define PORT 33445
static void manage_keys(DHT *dht)
{
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
uint8_t keys[KEYS_SIZE];
FILE *keys_file = fopen("key", "rb");
if (keys_file != nullptr) {
/* If file was opened successfully -- load keys,
otherwise save new keys */
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (read_size != KEYS_SIZE) {
printf("Error while reading the key file\nExiting.\n");
exit(1);
}
dht_set_self_public_key(dht, keys);
dht_set_self_secret_key(dht, keys + CRYPTO_PUBLIC_KEY_SIZE);
printf("Keys loaded successfully.\n");
} else {
memcpy(keys, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht_get_self_secret_key(dht), CRYPTO_SECRET_KEY_SIZE);
keys_file = fopen("key", "wb");
if (keys_file == nullptr) {
printf("Error opening key file in write mode.\nKeys will not be saved.\n");
return;
}
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
printf("Error while writing the key file.\nExiting.\n");
exit(1);
}
printf("Keys saved successfully.\n");
}
fclose(keys_file);
}
static void print_log(void *context, Logger_Level level, const char *file, int line,
const char *func, const char *message, void *userdata)
{
const char *strlevel;
switch (level) {
case LOGGER_LEVEL_TRACE:
strlevel = "TRACE";
break;
case LOGGER_LEVEL_DEBUG:
strlevel = "DEBUG";
break;
case LOGGER_LEVEL_INFO:
strlevel = "INFO";
break;
case LOGGER_LEVEL_WARNING:
strlevel = "WARNING";
break;
case LOGGER_LEVEL_ERROR:
strlevel = "ERROR";
break;
default:
strlevel = "<unknown>";
break;
}
fprintf(stderr, "[%s] %s:%d(%s) %s\n", strlevel, file, line, func, message);
}
int main(int argc, char *argv[])
{
if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
exit(0);
}
/* let user override default by cmdline */
bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0) {
exit(1);
}
/* Initialize networking -
Bind to ip 0.0.0.0 / [::] : PORT */
IP ip;
ip_init(&ip, ipv6enabled);
Logger *logger = logger_new();
if (MIN_LOGGER_LEVEL == LOGGER_LEVEL_TRACE || MIN_LOGGER_LEVEL == LOGGER_LEVEL_DEBUG) {
logger_callback_log(logger, print_log, nullptr, nullptr);
}
const Random *rng = system_random();
Mono_Time *mono_time = mono_time_new(nullptr, nullptr);
const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
const Network *ns = system_network();
DHT *dht = new_dht(logger, rng, ns, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true);
Onion *onion = new_onion(logger, mono_time, rng, dht);
Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht);
GC_Announces_List *gc_announces_list = new_gca_list();
Onion_Announce *onion_a = new_onion_announce(logger, rng, mono_time, dht);
#ifdef DHT_NODE_EXTRA_PACKETS
bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
#endif
if (!(onion && forwarding && onion_a)) {
printf("Something failed to initialize.\n");
exit(1);
}
gca_onion_init(gc_announces_list, onion_a);
perror("Initialization");
manage_keys(dht);
printf("Public key: ");
#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
TCP_Server *tcp_s = new_TCP_server(logger, rng, ns, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);
if (tcp_s == nullptr) {
printf("TCP server failed to initialize.\n");
exit(1);
}
#endif
const char *const public_id_filename = "PUBLIC_ID.txt";
FILE *file = fopen(public_id_filename, "w");
if (file == nullptr) {
printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
exit(1);
}
for (uint32_t i = 0; i < 32; ++i) {
const uint8_t *const self_public_key = dht_get_self_public_key(dht);
printf("%02X", self_public_key[i]);
fprintf(file, "%02X", self_public_key[i]);
}
fclose(file);
printf("\n");
printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht))));
if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n");
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
if (port_conv <= 0 || port_conv > UINT16_MAX) {
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
exit(1);
}
const uint16_t port = net_htons((uint16_t)port_conv);
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
int res = dht_bootstrap_from_address(dht, argv[argvoffset + 1],
ipv6enabled, port, bootstrap_key);
free(bootstrap_key);
if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
exit(1);
}
}
int is_waiting_for_dht_connection = 1;
uint64_t last_LANdiscovery = 0;
const Broadcast_Info *broadcast = lan_discovery_init(ns);
while (1) {
mono_time_update(mono_time);
if (is_waiting_for_dht_connection && dht_isconnected(dht)) {
printf("Connected to other bootstrap node successfully.\n");
is_waiting_for_dht_connection = 0;
}
do_dht(dht);
if (mono_time_is_timeout(mono_time, last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
lan_discovery_send(dht_get_net(dht), broadcast, dht_get_self_public_key(dht), net_htons(PORT));
last_LANdiscovery = mono_time_get(mono_time);
}
#ifdef TCP_RELAY_ENABLED
do_TCP_server(tcp_s, mono_time);
#endif
networking_poll(dht_get_net(dht), nullptr);
c_sleep(1);
}
}

View File

@ -0,0 +1,3 @@
As maintaining 2 separate lists of the same information seemed redundant, this list has been phased out.
For a current DHT node list please visit https://wiki.tox.chat/doku.php?id=users:nodes

View File

@ -0,0 +1,27 @@
if BUILD_DHT_BOOTSTRAP
bin_PROGRAMS += DHT_bootstrap
DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \
../toxcore/DHT.h \
../toxcore/friend_requests.h \
../other/bootstrap_node_packets.h \
../other/bootstrap_node_packets.c
DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \
$(LIBSODIUM_CFLAGS) \
$(NACL_CFLAGS)
DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \
$(NACL_LDFLAGS) \
libmisc_tools.la \
libtoxcore.la \
$(LIBSODIUM_LIBS) \
$(NACL_OBJECTS) \
$(NACL_LIBS) \
$(WINSOCK2_LIBS)
endif
EXTRA_DIST += $(top_srcdir)/other/DHTnodes \
$(top_srcdir)/other/tox.png

View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import subprocess
import sys
from typing import Tuple
ALLOWLIST: Tuple[str, ...] = (
# system headers
"pthread.h",
"stdarg.h",
"stdbool.h",
"stddef.h",
"stdint.h",
"time.h", # time_t used in Messenger.h TODO(iphydf): maybe don't?
# toxav stuff, maybe not worth abstracting away
"opus.h",
"vpx/vp8cx.h",
"vpx/vp8dx.h",
"vpx/vpx_decoder.h",
"vpx/vpx_encoder.h",
"vpx/vpx_image.h",
)
out = (subprocess.run(
[
"grep", "-R", "^#include <.*>", "other", "toxav", "toxcore",
"toxencryptsave"
],
check=True,
capture_output=True,
).stdout.decode("utf-8").rstrip())
errors = 0
for line in out.split("\n"):
# other/fun can do what it wants.
if "/fun/" in line:
continue
filename, include = line.split(":", 1)
# We only check headers.
if not filename.endswith(".h"):
continue
# ccompat.h can include some things we don't really want elsewhere.
allowlist = ALLOWLIST
if filename == "toxcore/ccompat.h":
allowlist += ("assert.h", "alloca.h", "malloc.h", "stdlib.h")
header = include[include.index("<") + 1:include.index(">")]
if header not in allowlist:
source = filename[:-2] + ".c"
print(
f"{filename}: includes system header <{header}>, which is not allowed in .h files"
)
print(
" " * len(filename) +
f" consider including it in {source} and exporting an abstraction, instead"
)
errors += 1
if errors:
sys.exit(1)

View File

@ -0,0 +1,18 @@
#!/bin/sh
# Make sure that logger levels in toxcore/logger.h, CMakeLists.txt and
# configure.ac stay in sync.
set -ex
TMP_DIR="$(mktemp -d)"
# ^\s+LOGGER_LEVEL_(\w+),?$
sed -n 's/^\s\+LOGGER_LEVEL_\(\w\+\),\?$/\1/p' toxcore/logger.h > "${TMP_DIR}/logger.h"
# ^.*\$\{MIN_LOGGER_LEVEL\}" STREQUAL "(\w+)".*$
sed -n 's/^.*\${MIN_LOGGER_LEVEL}\" STREQUAL \"\(\w\+\)\".*$/\1/p' CMakeLists.txt > "${TMP_DIR}/CMakeLists.txt"
# ^.*LOGGER_LEVEL_(\w+).*$
sed -n 's/^.*LOGGER_LEVEL_\(\w\+\).*$/\1/p' configure.ac > "${TMP_DIR}/configure.ac"
diff -u "${TMP_DIR}/CMakeLists.txt" "${TMP_DIR}/logger.h"
diff -u "${TMP_DIR}/configure.ac" "${TMP_DIR}/logger.h"

View File

@ -0,0 +1,103 @@
#!/bin/bash
CPPFLAGS="-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE"
CPPFLAGS+=("-DCMP_NO_FLOAT=1")
CPPFLAGS+=("-isystem" "/usr/include/opus")
CPPFLAGS+=("-Iauto_tests")
CPPFLAGS+=("-Iother")
CPPFLAGS+=("-Iother/bootstrap_daemon/src")
CPPFLAGS+=("-Iother/fun")
CPPFLAGS+=("-Itesting")
CPPFLAGS+=("-Itesting/fuzzing")
CPPFLAGS+=("-Itoxcore")
CPPFLAGS+=("-Itoxcore/events")
CPPFLAGS+=("-Itoxav")
CPPFLAGS+=("-Itoxencryptsave")
CPPFLAGS+=("-Ithird_party/cmp")
LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgtest")
LDFLAGS+=("-fuse-ld=gold")
LDFLAGS+=("-Wl,--detect-odr-violations")
LDFLAGS+=("-Wl,--warn-common")
LDFLAGS+=("-Wl,--warn-execstack")
LDFLAGS+=("-Wl,-z,noexecstack")
LDFLAGS+=("-Wl,-z,now")
put() {
if [ "$SKIP_LINES" = "" ]; then
echo "#line 1 \"$1\"" >>amalgamation.cc
fi
cat "$1" >>amalgamation.cc
}
putmain() {
NS=$(echo "${1//[^a-zA-Z0-9_]/_}" | sed -e 's/^__*//')
echo "namespace $NS {" >>amalgamation.cc
if [ "$SKIP_LINES" = "" ]; then
echo "#line 1 \"$1\"" >>amalgamation.cc
fi
sed -e 's/^int main(/static &/' "$1" >>amalgamation.cc
echo "} // namespace $NS" >>amalgamation.cc
}
callmain() {
NS=$(echo "${1//[^a-zA-Z0-9_]/_}" | sed -e 's/^__*//')
echo " call($NS::main, argc, argv);" >>amalgamation.cc
}
: >amalgamation.cc
# Include all C and C++ code
FIND_QUERY="find . '-(' -name '*.c' -or -name '*.cc' '-)'"
# Excludes
FIND_QUERY="$FIND_QUERY -and -not -wholename './_build/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './other/docker/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './super_donators/*'"
FIND_QUERY="$FIND_QUERY -and -not -name amalgamation.cc"
FIND_QUERY="$FIND_QUERY -and -not -name av_test.c"
FIND_QUERY="$FIND_QUERY -and -not -name cracker.c"
FIND_QUERY="$FIND_QUERY -and -not -name version_test.c"
FIND_QUERY="$FIND_QUERY -and -not -name '*_fuzz_test.cc'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './testing/fuzzing/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './third_party/cmp/examples/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './third_party/cmp/test/*'"
if [ "$SKIP_GTEST" == 1 ]; then
FIND_QUERY="$FIND_QUERY -and -not -name '*_test.cc'"
fi
readarray -t FILES <<<"$(eval "$FIND_QUERY")"
(for i in "${FILES[@]}"; do
grep -o '#include <[^>]*>' "$i" |
grep -E -v '<win|<ws|<iphlp|<libc|<mach/|<crypto_|<randombytes|<u.h>|<sys/filio|<stropts.h>|<linux'
done) | sort -u >>amalgamation.cc
put auto_tests/check_compat.h
echo 'namespace {' >>amalgamation.cc
for i in "${FILES[@]}"; do
if ! grep -q '^int main(' "$i"; then
put "$i"
fi
done
for i in "${FILES[@]}"; do
if grep -q '^int main(' "$i"; then
putmain "$i"
fi
done
echo "static void call(int m(), int argc, char **argv) { m(); }" >>amalgamation.cc
echo "static void call(int m(int, char **), int argc, char **argv) { m(argc, argv); }" >>amalgamation.cc
echo "static void call(int m(int, const char *const *), int argc, char **argv) { m(argc, argv); }" >>amalgamation.cc
echo '} // namespace' >>amalgamation.cc
echo "int main(int argc, char **argv) {" >>amalgamation.cc
for i in "${FILES[@]}"; do
if grep -q '^int main(' "$i"; then
callmain "$i"
fi
done
echo " return 0;" >>amalgamation.cc
echo "}" >>amalgamation.cc

View File

@ -0,0 +1,38 @@
#!/bin/bash
. other/analysis/gen-file.sh
set -e
run() {
echo "Running Clang compiler in variant '$*'"
clang++ -o /dev/null amalgamation.cc \
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-Werror \
-Weverything \
-Wno-alloca \
-Wno-c++98-compat-pedantic \
-Wno-c99-extensions \
-Wno-conversion \
-Wno-covered-switch-default \
-Wno-disabled-macro-expansion \
-Wno-documentation-deprecated-sync \
-Wno-documentation-unknown-command \
-Wno-global-constructors \
-Wno-missing-braces \
-Wno-missing-field-initializers \
-Wno-missing-noreturn \
-Wno-old-style-cast \
-Wno-padded \
-Wno-sign-compare \
-Wno-tautological-pointer-compare \
-Wno-unreachable-code-return \
-Wno-unused-parameter \
-Wno-used-but-marked-unused \
-Wno-source-uses-openmp
}
. other/analysis/variants.sh

View File

@ -0,0 +1,15 @@
#!/bin/bash
. other/analysis/gen-file.sh
set -e
run() {
echo "Running Clang static analyzer in variant '$*'"
clang++ --analyze amalgamation.cc \
"${CPPFLAGS[@]}" \
"$@" \
-std=c++11
}
. other/analysis/variants.sh

View File

@ -0,0 +1,87 @@
#!/bin/bash
CHECKS="*"
# __attribute__((nonnull)) causes this warning on defensive null checks.
CHECKS="$CHECKS,-clang-diagnostic-pointer-bool-conversion"
CHECKS="$CHECKS,-clang-diagnostic-tautological-pointer-compare"
# TODO(iphydf): We might want some of these. For the ones we don't want, add a
# comment explaining why not.
CHECKS="$CHECKS,-altera-unroll-loops"
CHECKS="$CHECKS,-android-cloexec-accept"
CHECKS="$CHECKS,-android-cloexec-fopen"
CHECKS="$CHECKS,-bugprone-not-null-terminated-result"
CHECKS="$CHECKS,-bugprone-reserved-identifier"
CHECKS="$CHECKS,-bugprone-sizeof-expression"
CHECKS="$CHECKS,-cert-dcl37-c"
CHECKS="$CHECKS,-cert-dcl51-cpp"
CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers"
CHECKS="$CHECKS,-cppcoreguidelines-init-variables"
CHECKS="$CHECKS,-hicpp-multiway-paths-covered"
CHECKS="$CHECKS,-hicpp-signed-bitwise"
CHECKS="$CHECKS,-llvm-else-after-return"
CHECKS="$CHECKS,-llvmlibc-restrict-system-libc-headers"
CHECKS="$CHECKS,-misc-redundant-expression"
CHECKS="$CHECKS,-misc-unused-parameters"
CHECKS="$CHECKS,-readability-else-after-return"
CHECKS="$CHECKS,-readability-function-cognitive-complexity"
CHECKS="$CHECKS,-readability-inconsistent-declaration-parameter-name"
CHECKS="$CHECKS,-readability-magic-numbers"
CHECKS="$CHECKS,-readability-redundant-control-flow"
# TODO(iphydf): Maybe fix these?
CHECKS="$CHECKS,-altera-id-dependent-backward-branch"
CHECKS="$CHECKS,-altera-struct-pack-align"
CHECKS="$CHECKS,-bugprone-branch-clone"
CHECKS="$CHECKS,-bugprone-easily-swappable-parameters"
CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result"
CHECKS="$CHECKS,-bugprone-integer-division"
CHECKS="$CHECKS,-bugprone-narrowing-conversions"
CHECKS="$CHECKS,-clang-analyzer-core.NonNullParamChecker"
CHECKS="$CHECKS,-clang-analyzer-core.NullDereference"
CHECKS="$CHECKS,-clang-analyzer-optin.portability.UnixAPI"
CHECKS="$CHECKS,-clang-analyzer-unix.Malloc"
CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized"
CHECKS="$CHECKS,-concurrency-mt-unsafe"
CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables"
CHECKS="$CHECKS,-cppcoreguidelines-narrowing-conversions"
CHECKS="$CHECKS,-google-readability-casting"
CHECKS="$CHECKS,-misc-no-recursion"
ERRORS="*"
# TODO(iphydf): Fix these.
ERRORS="$ERRORS,-bugprone-macro-parentheses"
ERRORS="$ERRORS,-bugprone-posix-return"
ERRORS="$ERRORS,-bugprone-signed-char-misuse"
ERRORS="$ERRORS,-cert-err34-c"
ERRORS="$ERRORS,-cert-str34-c"
ERRORS="$ERRORS,-hicpp-uppercase-literal-suffix"
ERRORS="$ERRORS,-readability-suspicious-call-argument"
ERRORS="$ERRORS,-readability-uppercase-literal-suffix"
set -eux
run() {
echo "Running clang-tidy in variant '$*'"
EXTRA_ARGS=("$@")
for i in "${!EXTRA_ARGS[@]}"; do
EXTRA_ARGS[$i]="--extra-arg=${EXTRA_ARGS[$i]}"
done
clang-tidy-12 \
-p=_build \
--extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
"${EXTRA_ARGS[@]}" \
--checks="$CHECKS" \
--warnings-as-errors="$ERRORS" \
--use-color \
other/bootstrap_daemon/src/*.c \
other/*.c \
toxav/*.c \
toxcore/*.c \
toxencryptsave/*.c
}
. other/analysis/variants.sh

View File

@ -0,0 +1,46 @@
#!/bin/bash
SKIP_GTEST=1
. other/analysis/gen-file.sh
set -e
CPPCHECK=("--enable=all")
CPPCHECK+=("--inconclusive")
CPPCHECK+=("--error-exitcode=1")
# Used for VLA.
CPPCHECK+=("--suppress=allocaCalled")
# False positives in switch statements.
CPPCHECK+=("--suppress=knownConditionTrueFalse")
# Cppcheck does not need standard library headers to get proper results.
CPPCHECK+=("--suppress=missingIncludeSystem")
# TODO(iphydf): Maybe fix?
CPPCHECK+=("--suppress=signConversion")
# TODO(iphydf): Fixed in the toxav refactor PR.
CPPCHECK+=("--suppress=redundantAssignment")
# We have some redundant nullptr checks in assertions
CPPCHECK+=("--suppress=nullPointerRedundantCheck")
# Triggers a false warning in group.c
CPPCHECK+=("--suppress=AssignmentAddressToInteger")
# TODO(sudden6): This triggers a false positive, check again later to enable it
CPPCHECK+=("--suppress=arrayIndexOutOfBoundsCond")
# We're a library. This only works on whole programs.
CPPCHECK_C=("--suppress=unusedFunction")
# False positive in auto_tests.
CPPCHECK_CXX+=("--suppress=shadowArgument")
CPPCHECK_CXX+=("--suppress=shadowFunction")
# False positive for callback functions
CPPCHECK_CXX+=("--suppress=constParameter")
# Used in Messenger.c for a static_assert(...)
CPPCHECK_CXX+=("--suppress=sizeofFunctionCall")
run() {
echo "Running cppcheck in variant '$*'"
cppcheck "${CPPCHECK[@]}" "${CPPCHECK_C[@]}" tox*/*.[ch] tox*/*/*.[ch] "${CPPFLAGS[@]}" "$@"
cppcheck "${CPPCHECK[@]}" "${CPPCHECK_CXX[@]}" amalgamation.cc "${CPPFLAGS[@]}" "$@"
}
. other/analysis/variants.sh

View File

@ -0,0 +1,11 @@
#!/bin/sh
FILTER="-whitespace"
FILTER="$FILTER,-build/include_subdir"
FILTER="$FILTER,-readability/casting"
FILTER="$FILTER,-runtime/arrays"
FILTER="$FILTER,-runtime/printf"
FILTER="$FILTER,-runtime/int"
FILTER="$FILTER,-build/header_guard"
cpplint --filter="$FILTER" toxav/*.[ch] toxcore/*.[ch] toxencryptsave/*.[ch]

View File

@ -0,0 +1,71 @@
#!/bin/bash
. other/analysis/gen-file.sh
set -e
run() {
echo "Running GCC in variant '$*'"
# TODO(iphydf): Get rid of all VLAs, then enable -fstack-protector -Wstack-protector
g++ -O3 -o /dev/null amalgamation.cc \
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-fdiagnostics-color=always \
-Wall \
-Wextra \
-Werror \
-Wno-error=type-limits \
-Wno-aggressive-loop-optimizations \
-Wno-float-conversion \
-Wno-format-signedness \
-Wno-missing-field-initializers \
-Wno-nonnull-compare \
-Wno-padded \
-Wno-sign-compare \
-Wno-sign-conversion \
-Wno-switch-default \
-Wno-unused-parameter \
-Wstrict-aliasing=0 \
-Wstrict-overflow=1 \
\
-Wmissing-declarations \
-Wbool-compare \
-Wcast-align \
-Wcast-qual \
-Wchar-subscripts \
-Wdouble-promotion \
-Wduplicated-cond \
-Wempty-body \
-Wenum-compare \
-Wfloat-equal \
-Wformat=2 \
-Wframe-address \
-Wframe-larger-than=9000 \
-Wignored-attributes \
-Wignored-qualifiers \
-Winit-self \
-Winline \
-Wlarger-than=530000 \
-Wmaybe-uninitialized \
-Wmemset-transposed-args \
-Wmisleading-indentation \
-Wnonnull \
-Wnull-dereference \
-Wodr \
-Wredundant-decls \
-Wreturn-type \
-Wshadow \
-Wsuggest-attribute=format \
-Wundef \
-Wunsafe-loop-optimizations \
-Wunused-label \
-Wunused-local-typedefs \
-Wunused-value \
-Wunused-but-set-parameter \
-Wunused-but-set-variable \
-fopenmp
}
. other/analysis/variants.sh

View File

@ -0,0 +1,25 @@
#!/bin/sh
# --bufferoverrun \
# --pulse \
read -r -d '' SCRIPT <<'EOF'
infer \
--report-console-limit 100 \
--jobs 8 \
--biabduction \
--loop-hoisting \
--quandary \
--racerd \
--starvation \
--uninit \
-- clang++ -fsyntax-only \
$(pkg-config --cflags libconfig libsodium opus vpx) \
/work/other/bootstrap_daemon/src/*.c \
/work/other/bootstrap_node_packets.c \
/work/toxav/*.c \
/work/toxcore/*.c \
/work/toxencryptsave/*.c
EOF
docker run --rm -it -v "$PWD:/work" toxchat/infer bash -c "$SCRIPT"

View File

@ -0,0 +1,3 @@
#!/bin/bash
run

View File

@ -0,0 +1,32 @@
This directory can house various tools and utilities.
# How to use astyle
## Manually
### For all files
Run from ``toxcore`` directory:
```bash
astyle --options=./other/astyle/astylerc ./toxcore/*.c ./toxcore/*.h ./testing/*.c ./toxav/*.c ./toxav/*.h ./other/*.c ./other/bootstrap_daemon/*.c ./toxencryptsave/*.c ./toxencryptsave/*.h ./auto_tests/*.c
```
### For selected file
Run from ``toxcore`` directory, e.g. for [``tox.h``](/toxcore/tox.h) file:
```bash
astyle --options=./other/astyle/astylerc ./toxcore/tox.h
```
## Automatically, as pre-commit hook (*NIX only)
Copy [``astylerc``](/other/astyle/astylerc) to ``toxcore/.git/hooks``
# Why
``astylerc`` - this file can be used in the pre-commit hook to try its best at making the code conform to the coding style of toxcore.
Furthermore, it is being used to format ``tox.h`` after using ``apidsl`` to generate it.

View File

@ -0,0 +1,26 @@
# Bracket Style Options
--style=kr
# Tab Options
--indent=spaces=4
# Indentation Options
--indent-switches
# Padding Options
--pad-header
--break-blocks
--pad-oper
--unpad-paren
--align-pointer=name
--align-reference=name
# Formatting Options
--add-brackets
--convert-tabs
--max-code-length=120
# Other Options
--preserve-date
--formatted
--lineend=linux

View File

@ -0,0 +1,46 @@
#!/bin/bash
set -ex
SOURCE_DIR="$1"
ASTYLE="$2"
# Go to the source root.
if [ -z "$SOURCE_DIR" ]; then
SOURCE_DIR=.
fi
cd "$SOURCE_DIR"
if [ -z "$ASTYLE" ] || ! which "$ASTYLE"; then
ASTYLE=astyle
fi
if ! which "$ASTYLE"; then
# If we couldn't find or install an astyle binary, don't do anything.
echo "Could not find an astyle binary; please install astyle."
exit 1
fi
readarray -t CC_SOURCES <<<"$(find . '(' -name '*.cc' ')')"
CC_SOURCES+=(toxcore/crypto_core.c)
CC_SOURCES+=(toxcore/ping_array.c)
for bin in clang-format-11 clang-format-7 clang-format-6.0 clang-format-5.0 clang-format; do
if which "$bin"; then
"$bin" -i -style='{BasedOnStyle: Google, ColumnLimit: 100}' "${CC_SOURCES[@]}"
break
fi
done
FIND="find ."
FIND="$FIND '(' -name '*.[ch]' ')'"
FIND="$FIND -and -not -name '*.api.h'"
FIND="$FIND -and -not -wholename './super_donators/*'"
FIND="$FIND -and -not -wholename './third_party/*'"
FIND="$FIND -and -not -wholename './toxencryptsave/crypto_pwhash*'"
readarray -t C_SOURCES <<<"$(eval "$FIND")"
"$ASTYLE" -n --options=other/astyle/astylerc "${C_SOURCES[@]}"
git diff --color=always --exit-code

View File

@ -0,0 +1,17 @@
#!/usr/bin/env sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
for file in `git diff-index --diff-filter=ACMR --name-only HEAD`; do
if [[ $file == *.c || $file == *.h ]]
then
echo $file
`which astyle` $file --options=tools/astylerc
git add $file
fi
done

View File

@ -0,0 +1,26 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
package(features = ["layering_check"])
cc_binary(
name = "bootstrap_daemon",
srcs = glob([
"src/*.c",
"src/*.h",
]),
deps = [
"//c-toxcore/other:bootstrap_node_packets",
"//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:LAN_discovery",
"//c-toxcore/toxcore:TCP_server",
"//c-toxcore/toxcore:announce",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:group_onion_announce",
"//c-toxcore/toxcore:logger",
"//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:util",
"@libconfig",
],
)

View File

@ -0,0 +1,24 @@
# Unity build broken because of _XOPEN_SOURCE macro
set_source_files_properties(
src/tox-bootstrapd.c
PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)
add_executable(tox-bootstrapd
src/command_line_arguments.c
src/command_line_arguments.h
src/config.c
src/config.h
src/config_defaults.h
src/global.h
src/log.c
src/log.h
src/log_backend_stdout.c
src/log_backend_stdout.h
src/log_backend_syslog.c
src/log_backend_syslog.h
src/tox-bootstrapd.c
../bootstrap_node_packets.c
../bootstrap_node_packets.h)
target_link_modules(tox-bootstrapd toxcore ${LIBCONFIG_LIBRARIES})
install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin)
install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")

View File

@ -0,0 +1,308 @@
# Instructions
- [For `systemd` users](#for-systemd-users)
- [Setting up](#setting-up)
- [Updating](#updating)
- [Troubleshooting](#troubleshooting)
- [For `SysVinit` users](#for-sysvinit-users)
- [Setting up](#setting-up-1)
- [Updating](#updating-1)
- [Troubleshooting](#troubleshooting-1)
- [For `Docker` users](#for-docker-users)
- [Setting up](#setting-up-2)
- [Updating](#updating-2)
- [Troubleshooting](#troubleshooting-2)
These instructions are primarily tested on Debian Linux, Wheezy for SysVinit and Jessie for systemd, but they should work on other POSIX-compliant systems too.
## For `systemd` users
### Setting up
For security reasons we run the daemon under its own user.
Create a new user by executing the following:
```sh
sudo useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment "Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
```
Restrict access to home directory:
```sh
sudo chmod 700 /var/lib/tox-bootstrapd
```
Copy `tox-bootstrapd.conf` file to where `ExecStart=` from `tox-bootstrapd.service` points to. By default it's `/etc/tox-bootstrapd.conf`.
```sh
sudo cp tox-bootstrapd.conf /etc/tox-bootstrapd.conf
```
Go over everything in the copied `tox-bootstrapd.conf` file. Set options you want and add actual working nodes to the `bootstrap_nodes` list, instead of the example ones, if you want your node to connect to the Tox network. Make sure `pid_file_path` matches `PIDFile=` from `tox-bootstrapd.service`.
Copy `tox-bootstrapd.service` to `/etc/systemd/system/`:
```sh
sudo cp tox-bootstrapd.service /etc/systemd/system/
```
You must uncomment the next line in tox-bootstrapd.service, if you want to use port number < 1024:
```
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
```
and, possibly, install `libcap2-bin` or `libcap2` package, depending of your distribution.
Reload systemd units definitions, enable service for automatic start (if needed), start it and verify it's running:
```sh
sudo systemctl daemon-reload
sudo systemctl enable tox-bootstrapd.service
sudo systemctl start tox-bootstrapd.service
sudo systemctl status tox-bootstrapd.service
```
Get your public key and check that the daemon initialized correctly:
```sh
sudo grep "tox-bootstrapd" /var/log/syslog
```
### Updating
You want to make sure that the daemon uses the newest toxcore, as there might have been some changes done to the DHT, so it's advised to update the daemon at least once every month.
To update the daemon first stop it:
```sh
sudo systemctl stop tox-bootstrapd.service
```
Then update your toxcore git repository, rebuild the toxcore and the daemon and make sure to install them.
Check if `tox-bootstrapd.service` in toxcore git repository was modified since the last time you copied it, as you might need to update it too.
Reload `tox-bootstrapd.service` if you have updated modified it:
```sh
sudo systemctl daemon-reload
```
After all of this is done, simply start the daemon back again:
```sh
sudo systemctl start tox-bootstrapd.service
```
### Troubleshooting
- Check daemon's status:
```sh
sudo systemctl status tox-bootstrapd.service
```
- Check the log for errors:
```sh
sudo grep "tox-bootstrapd" /var/log/syslog
# or
sudo journalctl --pager-end
# or
sudo journalctl -f _SYSTEMD_UNIT=tox-bootstrapd.service
```
- Make sure tox-bootstrapd user has write permission for keys and pid files.
- Make sure tox-bootstrapd has read permission for the config file.
- Make sure tox-bootstrapd location matches its path in tox-bootstrapd.service file.
## For `SysVinit` users
### Setting up
For security reasons we run the daemon under its own user.
Create a new user by executing the following:
```sh
sudo useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment "Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
```
Restrict access to home directory:
```sh
sudo chmod 700 /var/lib/tox-bootstrapd
```
Copy `tox-bootstrapd.conf` file to where `CFGFILE` variable from `tox-bootstrapd.sh` points to. By default it's `/etc/tox-bootstrapd.conf`.
```sh
sudo cp tox-bootstrapd.conf /etc/tox-bootstrapd.conf
```
Go over everything in the copied `tox-bootstrapd.conf` file. Set options you want and add actual working nodes to the `bootstrap_nodes` list, instead of the example ones, if you want your node to connect to the Tox network. Make sure `pid_file_path` matches `PIDFILE` from `tox-bootstrapd.sh`.
Look at the variable declarations in the beginning of `tox-bootstrapd.sh` init script to see if you need to change anything for it to work on your system. The default values must be fine for most users and we assume that you use those next.
If you have configured the daemon to use any port numbers that are lower than 1024, you need to execute the command below, as by default non-privileged users cannot open ports <1024. The change persists through reboot:
```sh
sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/tox-bootstrapd
```
Copy `tox-bootstrapd.sh` init script to `/etc/init.d/tox-bootstrapd` (note the disappearance of ".sh" ending):
```sh
sudo cp tox-bootstrapd.sh /etc/init.d/tox-bootstrapd
```
Set permissions for the init system to run the script:
```sh
sudo chmod 755 /etc/init.d/tox-bootstrapd
```
Make the init system aware of the script, start the daemon and verify it's running:
```sh
sudo update-rc.d tox-bootstrapd defaults
sudo service tox-bootstrapd start
sudo service tox-bootstrapd status
```
Get your public key and check that the daemon initialized correctly:
```sh
sudo grep "tox-bootstrapd" /var/log/syslog
```
### Updating
You want to make sure that the daemon uses the newest toxcore, as there might have been some changes done to the DHT, so it's advised to update the daemon at least once every month.
To update the daemon first stop it:
```sh
sudo service tox-bootstrapd stop
```
Then update your toxcore git repository, rebuild the toxcore and the daemon and make sure to install them.
Check if `tox-bootstrapd.sh` in toxcore git repository was modified since the last time you copied it, as you might need to update it too.
After all of this is done, simply start the daemon back again:
```sh
sudo service tox-bootstrapd start
```
### Troubleshooting
- Check daemon's status:
```sh
sudo service tox-bootstrapd status
```
- Check the log for errors:
```sh
sudo grep "tox-bootstrapd" /var/log/syslog
```
- Check that variables in the beginning of `/etc/init.d/tox-bootstrapd` are valid.
- Make sure tox-bootstrapd user has write permission for keys and pid files.
- Make sure tox-bootstrapd has read permission for the config file.
- Make sure tox-bootstrapd location matches its path in the `/etc/init.d/tox-bootstrapd` init script.
## For `Docker` users:
### Setting up
If you are familiar with Docker and would rather run the daemon in a Docker container, you may download the latest official docker image. To download the latest image run:
```sh
docker pull toxchat/bootstrap-node:latest
docker run --rm -it --entrypoint=sha256sum toxchat/bootstrap-node:latest /usr/local/bin/tox-bootstrapd
```
This will print the SHA256 checksum of the latest binary, which should agree with the SHA256 checksum in the Dockerfile.
If you want to build the bootstrap node from source, check out the latest release:
```sh
git checkout $(git tag --list | grep -P '^v(\d+).(\d+).(\d+)$' | \
sed 's/v/v /g' | sed 's/\./ /g' | \
sort -snk4,4 | sort -snk3,3 | sort -snk2,2 | tail -n 1 | \
sed 's/v /v/g' | sed 's/ /\./g')
```
and run the following from the top level c-toxcore directory:
```sh
tar c $(git ls-files) | docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node -
sudo useradd \
--home-dir /var/lib/tox-bootstrapd \
--create-home \
--system \
--shell /sbin/nologin \
--comment "Account to run Tox's DHT bootstrap daemon" \
--user-group tox-bootstrapd
sudo chmod 700 /var/lib/tox-bootstrapd
docker run -d --name tox-bootstrapd --restart always \
--user "$(id -u tox-bootstrapd):$(id -g tox-bootstrapd)" \
-v /var/lib/tox-bootstrapd/:/var/lib/tox-bootstrapd/ \
--ulimit nofile=32768:32768 \
-p 443:443 \
-p 3389:3389 \
-p 33445:33445 \
-p 33445:33445/udp \
toxchat/bootstrap-node
```
We create a new user and protect its home directory in order to mount it in the Docker image, so that the kyepair the daemon uses would be stored on the host system, which makes it less likely that you would loose the keypair while playing with or updating the Docker container.
You can check logs for your public key or any errors:
```sh
docker logs tox-bootstrapd
```
Note that the Docker container runs a script which pulls a list of bootstrap nodes off https://nodes.tox.chat/ and adds them in the config file.
### Updating
You want to make sure that the daemon uses the newest toxcore, as there might have been some changes done to the DHT, so it's advised to update the daemon at least once every month.
To update the daemon, all you need is to erase current container with its image:
```sh
docker stop tox-bootstrapd
docker rm tox-bootstrapd
docker rmi toxchat/bootstrap-node
```
Then rebuild and run the image again:
```sh
tar c $(git ls-files) | docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node -
docker run -d --name tox-bootstrapd --restart always \
--user "$(id -u tox-bootstrapd):$(id -g tox-bootstrapd)" \
-v /var/lib/tox-bootstrapd/:/var/lib/tox-bootstrapd/ \
--ulimit nofile=32768:32768 \
-p 443:443 \
-p 3389:3389 \
-p 33445:33445 \
-p 33445:33445/udp \
toxchat/bootstrap-node
```
### Troubleshooting
- Check if the container is running:
```sh
docker ps -a
```
- Check the log for errors:
```sh
docker logs tox-bootstrapd
```

View File

@ -0,0 +1,27 @@
# tox-bootstrapd completion -*- shell-script -*-
_tox_bootstrapd()
{
local cur prev words cword split
_init_completion -s || return
case $prev in
--config)
_filedir
return
;;
--log-backend)
COMPREPLY=($(compgen -W 'stdout syslog' -- "$cur"))
return
;;
esac
$split && return
if type _bashcomp_try_faketty &>/dev/null; then
COMPREPLY=($(compgen -W '$(_parse_help _bashcomp_try_faketty "$1" --help)' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
fi
} && complete -F _tox_bootstrapd tox-bootstrapd
# ex: filetype=sh

View File

@ -0,0 +1,90 @@
###########################################################
# Builder image: we compile the code here (static build)
FROM alpine:3.15.0 AS build
RUN ["apk", "--no-cache", "add",\
"build-base",\
"cmake",\
"linux-headers",\
"libconfig-dev",\
"libconfig-static",\
"libsodium-dev",\
"libsodium-static",\
"ninja",\
"python3"\
]
WORKDIR /src/c-toxcore
# Very selectively add files to the image, because we may have random stuff
# lying around. In particular, we don't need to rebuild the docker image when
# toxav changes or the Dockerfile changes down from the build.
COPY cmake cmake
COPY other/bootstrap_daemon/bash-completion other/bootstrap_daemon/bash-completion
COPY other/bootstrap_daemon/src other/bootstrap_daemon/src
COPY other/bootstrap_node_packets.[ch] other/
COPY other/DHT_bootstrap.c other/
COPY other/pkgconfig other/pkgconfig
COPY other/rpm other/rpm
COPY testing/misc_tools.[ch] testing/
COPY toxcore toxcore
COPY toxencryptsave toxencryptsave
COPY third_party third_party
COPY CMakeLists.txt so.version ./
COPY other/bootstrap_daemon/CMakeLists.txt other/bootstrap_daemon/CMakeLists.txt
COPY testing/CMakeLists.txt testing/CMakeLists.txt
RUN cmake -B_build -H. \
-GNinja \
-DCMAKE_C_FLAGS="-DTCP_SERVER_USE_EPOLL -fstack-protector-all -fisolate-erroneous-paths-attribute" \
-DCMAKE_UNITY_BUILD=ON \
-DCMAKE_BUILD_TYPE=Release \
-DFULLY_STATIC=ON \
-DMIN_LOGGER_LEVEL=DEBUG \
-DBUILD_TOXAV=OFF \
-DBOOTSTRAP_DAEMON=ON && \
cmake --build _build --target install
# Verify checksum from dev-built binary, so we can be sure Docker Hub doesn't
# mess with your binaries.
COPY other/bootstrap_daemon/docker/tox-bootstrapd.sha256 other/bootstrap_daemon/docker/
RUN sha256sum /usr/local/bin/tox-bootstrapd && \
sha256sum -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256
# Remove all the example bootstrap nodes from the config file.
COPY other/bootstrap_daemon/tox-bootstrapd.conf other/bootstrap_daemon/
# hadolint ignore=SC2086,SC2154
RUN ["sed", "-i", "/^bootstrap_nodes = /,$d", "other/bootstrap_daemon/tox-bootstrapd.conf"]
# Add bootstrap nodes from https://nodes.tox.chat/.
COPY other/bootstrap_daemon/docker/get-nodes.py other/bootstrap_daemon/docker/
RUN ["other/bootstrap_daemon/docker/get-nodes.py", "other/bootstrap_daemon/tox-bootstrapd.conf"]
###########################################################
# Final image build: this is what runs the bootstrap node
FROM debian:bullseye-slim
COPY --from=build /usr/local/bin/tox-bootstrapd /usr/local/bin/
COPY --from=build /src/c-toxcore/other/bootstrap_daemon/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
RUN useradd --home-dir /var/lib/tox-bootstrapd --create-home \
--system --shell /sbin/nologin \
--comment "Account to run the Tox DHT bootstrap daemon" \
--user-group tox-bootstrapd && \
chmod 644 /etc/tox-bootstrapd.conf && \
chmod 700 /var/lib/tox-bootstrapd
WORKDIR /var/lib/tox-bootstrapd
USER tox-bootstrapd
# Smoke-test: make sure the binary actually starts up.
# hadolint ignore=DL4006
RUN script /usr/local/bin/tox-bootstrapd --help | grep "Usage"
ENTRYPOINT ["/usr/local/bin/tox-bootstrapd",\
"--config", "/etc/tox-bootstrapd.conf",\
"--log-backend", "stdout",\
"--foreground"\
]
EXPOSE 443/tcp 3389/tcp 33445/tcp 33445/udp

View File

@ -0,0 +1,55 @@
#!/usr/bin/env python3
"""
Copyright (c) 2016 by nurupo <nurupo.contributions@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
# Gets a list of nodes from https://nodes.tox.chat/json and prints them out
# in the format of tox-bootstrapd config file.
import json
import sys
import urllib.request
from typing import Dict
response = urllib.request.urlopen("https://nodes.tox.chat/json")
raw_json = response.read().decode("ascii", "ignore")
nodes = json.loads(raw_json)["nodes"]
def node_to_string(node: Dict[str, str]) -> str:
node_output = " { // " + node["maintainer"] + "\n"
node_output += ' public_key = "' + node["public_key"] + '"\n'
node_output += " port = " + str(node["port"]) + "\n"
node_output += ' address = "'
if len(node["ipv4"]) > 4:
return node_output + node["ipv4"] + '"\n }'
if len(node["ipv6"]) > 4:
return node_output + node["ipv6"] + '"\n }'
raise Exception("no IP address found for node " + json.dumps(node))
output = "bootstrap_nodes = (\n" + ",\n".join(map(node_to_string,
nodes)) + "\n)"
if len(sys.argv) > 1:
with open(sys.argv[1], "a") as fh:
fh.write(output + "\n")
print("Wrote %d nodes to %s" % (len(nodes), sys.argv[1]))
else:
print(output)

View File

@ -0,0 +1 @@
4f5b47978dc26aed78719526f862a44693f821db12f5ff6d70b338d67fb6f784 /usr/local/bin/tox-bootstrapd

View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -eux
docker_build() {
docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node .
}
# Run Docker build once. If it succeeds, we're good.
if docker_build; then
exit 0
fi
# We're not good. Run it again, but now capture the output.
OUTPUT=$(docker_build || true 2>&1)
if echo "$OUTPUT" | grep '/usr/local/bin/tox-bootstrapd: FAILED'; then
# This is a checksum warning, so we need to update it.
IMAGE=$(echo "$OUTPUT" | grep '^ ---> [0-9a-f]*$' | grep -o '[0-9a-f]*$' | tail -n1)
docker run --rm "$IMAGE" sha256sum /usr/local/bin/tox-bootstrapd >other/bootstrap_daemon/docker/tox-bootstrapd.sha256
fi
# Run once last time to complete the build.
docker_build

View File

@ -0,0 +1,47 @@
if BUILD_DHT_BOOTSTRAP_DAEMON
bin_PROGRAMS += tox-bootstrapd
tox_bootstrapd_SOURCES = \
../other/bootstrap_daemon/src/command_line_arguments.c \
../other/bootstrap_daemon/src/command_line_arguments.h \
../other/bootstrap_daemon/src/config.c \
../other/bootstrap_daemon/src/config.h \
../other/bootstrap_daemon/src/config_defaults.h \
../other/bootstrap_daemon/src/global.h \
../other/bootstrap_daemon/src/log.c \
../other/bootstrap_daemon/src/log.h \
../other/bootstrap_daemon/src/log_backend_stdout.c \
../other/bootstrap_daemon/src/log_backend_stdout.h \
../other/bootstrap_daemon/src/log_backend_syslog.c \
../other/bootstrap_daemon/src/log_backend_syslog.h \
../other/bootstrap_daemon/src/tox-bootstrapd.c \
../other/bootstrap_daemon/src/global.h \
../other/bootstrap_node_packets.c \
../other/bootstrap_node_packets.h
tox_bootstrapd_CFLAGS = \
-I$(top_srcdir)/other/bootstrap_daemon \
$(LIBSODIUM_CFLAGS) \
$(NACL_CFLAGS) \
$(LIBCONFIG_CFLAGS)
tox_bootstrapd_LDADD = \
$(LIBSODIUM_LDFLAGS) \
$(NACL_LDFLAGS) \
libtoxcore.la \
$(LIBCONFIG_LIBS) \
$(LIBSODIUM_LIBS) \
$(NACL_OBJECTS) \
$(NACL_LIBS)
bashcompdir = $(datarootdir)/bash-completion/completions
dist_bashcomp_DATA = $(top_builddir)/other/bootstrap_daemon/bash-completion/completions/tox-bootstrapd
EXTRA_DIST += \
$(top_srcdir)/other/bootstrap_daemon/tox-bootstrapd.conf \
$(top_srcdir)/other/bootstrap_daemon/tox-bootstrapd.service \
$(top_srcdir)/other/bootstrap_daemon/tox-bootstrapd.sh
endif

View File

@ -0,0 +1,134 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Command line argument handling.
*/
#include "command_line_arguments.h"
#include "global.h"
#include "../../../toxcore/ccompat.h"
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
/**
* Prints --help message
*/
static void print_help(void)
{
// 2 space ident
// 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"
"Options:\n"
" --config=FILE_PATH Specify path to the config file.\n"
" This is a required option.\n"
" Set FILE_PATH to a path to an empty file in order to\n"
" use default settings.\n"
" --foreground Run the daemon in foreground. The daemon won't fork\n"
" (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"
" syslog Writes log messages to syslog.\n"
" Default option when no --log-backend is\n"
" specified.\n"
" stdout Writes log messages to stdout/stderr.\n"
" --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)
{
if (argc < 2) {
log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
print_help();
exit(1);
}
opterr = 0;
static const struct option long_options[] = {
{"config", required_argument, nullptr, 'c'}, // required option
{"foreground", no_argument, nullptr, 'f'},
{"help", no_argument, nullptr, 'h'},
{"log-backend", required_argument, nullptr, 'l'}, // optional, defaults to syslog
{"version", no_argument, nullptr, 'v'},
{nullptr, 0, nullptr, 0 }
};
bool cfg_file_path_set = false;
bool log_backend_set = false;
*run_in_foreground = false;
int opt;
while ((opt = getopt_long(argc, argv, ":", long_options, nullptr)) != -1) {
switch (opt) {
case 'c':
*cfg_file_path = optarg;
cfg_file_path_set = true;
break;
case 'f':
*run_in_foreground = true;
break;
case 'h':
print_help();
exit(0);
case 'l':
if (strcmp(optarg, "syslog") == 0) {
*log_backend = LOG_BACKEND_SYSLOG;
log_backend_set = true;
} else if (strcmp(optarg, "stdout") == 0) {
*log_backend = LOG_BACKEND_STDOUT;
log_backend_set = true;
} else {
log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
print_help();
exit(1);
}
break;
case 'v':
log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
exit(0);
case '?':
log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
case ':':
log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
print_help();
exit(1);
}
}
if (!log_backend_set) {
*log_backend = LOG_BACKEND_SYSLOG;
}
if (!cfg_file_path_set) {
log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
print_help();
exit(1);
}
}

View File

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Command line argument handling.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H
#include "log.h"
/**
* Handles command line arguments, setting cfg_file_path and log_backend.
* Terminates the application if incorrect arguments are specified.
*
* @param argc Argc passed into main().
* @param argv Argv passed into main().
* @param cfg_file_path Sets to the provided by the user config file path.
* @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);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H

View File

@ -0,0 +1,414 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Functionality related to dealing with the config file.
*/
#include "config.h"
#include "config_defaults.h"
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libconfig.h>
#include "../../bootstrap_node_packets.h"
/**
* Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array.
*
* Supposed to be called from get_general_config only.
*
* Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports`.
*/
static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count)
{
const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports";
*tcp_relay_port_count = 0;
config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS);
if (ports_array == nullptr) {
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};
for (int i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; ++i) {
log_write(LOG_LEVEL_INFO, "Port #%d: %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));
for (int i = 0; i < DEFAULT_TCP_RELAY_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,
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue;
}
++*tcp_relay_port_count;
}
// 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 {
free(*tcp_relay_ports);
*tcp_relay_ports = nullptr;
}
return;
}
if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
log_write(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
NAME_TCP_RELAY_PORTS);
return;
}
int config_port_count = config_setting_length(ports_array);
if (config_port_count == 0) {
log_write(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
return;
}
*tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t));
for (int i = 0; i < config_port_count; ++i) {
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
log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i);
break;
}
if (config_setting_is_number(elem) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i);
continue;
}
(*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
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,
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue;
}
++*tcp_relay_port_count;
}
// 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 {
free(*tcp_relay_ports);
*tcp_relay_ports = nullptr;
}
}
int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
int *enable_ipv6, int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay,
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_motd, char **motd)
{
config_t cfg;
const char *NAME_PORT = "port";
const char *NAME_PID_FILE_PATH = "pid_file_path";
const char *NAME_KEYS_FILE_PATH = "keys_file_path";
const char *NAME_ENABLE_IPV6 = "enable_ipv6";
const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback";
const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
const char *NAME_ENABLE_TCP_RELAY = "enable_tcp_relay";
const char *NAME_ENABLE_MOTD = "enable_motd";
const char *NAME_MOTD = "motd";
config_init(&cfg);
// Read the file. If there is an error, report it and exit.
if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return 0;
}
// Get port
if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
*port = DEFAULT_PORT;
}
// Get PID file location
const char *tmp_pid_file;
if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
tmp_pid_file = DEFAULT_PID_FILE_PATH;
}
const size_t pid_file_path_len = strlen(tmp_pid_file) + 1;
*pid_file_path = (char *)malloc(pid_file_path_len);
memcpy(*pid_file_path, tmp_pid_file, pid_file_path_len);
// Get keys file location
const char *tmp_keys_file;
if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
}
const size_t keys_file_path_len = strlen(tmp_keys_file) + 1;
*keys_file_path = (char *)malloc(keys_file_path_len);
memcpy(*keys_file_path, tmp_keys_file, keys_file_path_len);
// Get IPv6 option
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
*enable_ipv6 = DEFAULT_ENABLE_IPV6;
}
// Get IPv4 fallback option
if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
*enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
}
// Get LAN discovery option
if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
*enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
}
// Get TCP relay option
if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
*enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
}
if (*enable_tcp_relay) {
parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count);
} else {
*tcp_relay_port_count = 0;
}
// Get MOTD option
if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
DEFAULT_ENABLE_MOTD ? "true" : "false");
*enable_motd = DEFAULT_ENABLE_MOTD;
}
if (*enable_motd) {
// Get MOTD
const char *tmp_motd;
if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD);
log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
tmp_motd = DEFAULT_MOTD;
}
size_t tmp_motd_length = strlen(tmp_motd) + 1;
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
*motd = (char *)malloc(motd_length);
snprintf(*motd, motd_length, "%s", tmp_motd);
}
config_destroy(&cfg);
log_write(LOG_LEVEL_INFO, "Successfully read:\n");
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
log_write(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port);
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false");
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
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
if (*enable_tcp_relay) {
if (*tcp_relay_port_count == 0) {
log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n");
} else {
log_write(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count);
for (int i = 0; i < *tcp_relay_port_count; ++i) {
log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
}
}
}
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
if (*enable_motd) {
log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd);
}
return 1;
}
/**
*
* Converts a hex string with even number of characters into binary.
*
* Important: You are responsible for freeing the return value.
*
* @return binary on success,
* NULL on failure.
*/
static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string)
{
if (strlen(hex_string) % 2 != 0) {
return nullptr;
}
size_t len = strlen(hex_string) / 2;
uint8_t *ret = (uint8_t *)malloc(len);
const char *pos = hex_string;
for (size_t i = 0; i < len; ++i, pos += 2) {
unsigned int val;
sscanf(pos, "%02x", &val);
ret[i] = val;
}
return ret;
}
int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
{
const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes";
const char *NAME_PUBLIC_KEY = "public_key";
const char *NAME_PORT = "port";
const char *NAME_ADDRESS = "address";
config_t cfg;
config_init(&cfg);
if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return 0;
}
config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
if (node_list == nullptr) {
log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n",
NAME_BOOTSTRAP_NODES);
config_destroy(&cfg);
return 1;
}
if (config_setting_length(node_list) == 0) {
log_write(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n");
config_destroy(&cfg);
return 1;
}
int bs_port;
const char *bs_address;
const char *bs_public_key;
config_setting_t *node;
int i = 0;
while (config_setting_length(node_list)) {
int address_resolved;
uint8_t *bs_public_key_bin;
node = config_setting_get_elem(node_list, 0);
if (node == nullptr) {
config_destroy(&cfg);
return 0;
}
// Check that all settings are present
if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i,
NAME_PUBLIC_KEY);
goto next;
}
if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT);
goto next;
}
if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS);
goto next;
}
// Process settings
if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
bs_public_key);
goto next;
}
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i,
NAME_PORT,
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
goto next;
}
bs_public_key_bin = bootstrap_hex_string_to_bin(bs_public_key);
address_resolved = dht_bootstrap_from_address(dht, bs_address, enable_ipv6, net_htons(bs_port),
bs_public_key_bin);
free(bs_public_key_bin);
if (!address_resolved) {
log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address);
goto next;
}
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
// 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);
++i;
}
config_destroy(&cfg);
return 1;
}

View File

@ -0,0 +1,37 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Functionality related to dealing with the config file.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_H
#include "../../../toxcore/DHT.h"
/**
* Gets general config options from the config file.
*
* Important: You are responsible for freeing `pid_file_path` and `keys_file_path`
* also, iff `tcp_relay_ports_count` > 0, then you are responsible for freeing `tcp_relay_ports`
* and also `motd` iff `enable_motd` is set.
*
* @return 1 on success,
* 0 on failure, doesn't modify any data pointed by arguments.
*/
int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
int *enable_ipv6, int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay,
uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_motd, char **motd);
/**
* 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.
*/
int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_H

View File

@ -0,0 +1,27 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Default config options for when they are missing in the config file.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H
#include "global.h"
#define DEFAULT_PID_FILE_PATH "tox-bootstrapd.pid"
#define DEFAULT_KEYS_FILE_PATH "tox-bootstrapd.keys"
#define DEFAULT_PORT 33445
#define DEFAULT_ENABLE_IPV6 1 // 1 - true, 0 - false
#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_ENABLE_MOTD 1 // 1 - true, 0 - false
#define DEFAULT_MOTD DAEMON_NAME
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_CONFIG_DEFAULTS_H

View File

@ -0,0 +1,44 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Globally used defines.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_GLOBAL_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_GLOBAL_H
#include "../../../toxcore/tox.h"
#define DAEMON_NAME "tox-bootstrapd"
#define DAEMON_VERSION_MAJOR TOX_VERSION_MAJOR
#define DAEMON_VERSION_MINOR TOX_VERSION_MINOR
#define DAEMON_VERSION_PATCH TOX_VERSION_PATCH
// Make sure versions are within the limit
#define VERSION_IS_OK(NUM) ( NUM >= 0 && NUM <= 999 )
#if !VERSION_IS_OK(DAEMON_VERSION_MAJOR) || !VERSION_IS_OK(DAEMON_VERSION_MINOR) || !VERSION_IS_OK(DAEMON_VERSION_PATCH)
#error "At least one of major, minor or patch parts of the version is out of bounds of [0, 999]. Current version: " DAEMON_VERSION_MAJOR "." DAEMON_VERSION_MINOR "." DAEMON_VERSION_PATCH
#endif
#undef VERSION_IS_OK
// New version scheme of 1AAABBBCCC, where A B and C are major, minor and patch
// versions of toxcore. The leading 1 is there just to keep the leading zeros,
// so that it would be easier to read the version when printed as a number.
// The version is in a visual decimal format rather than in any other format,
// because the original version was using a similar format, it was using
// YYYYMMDDVV date-based format for the version, with VV being an incremental
// counter in case more than one version was released at that day. Due to this
// some tools started showing the version to users as a plain number, rather
// than some binary format that needs to be parsed before being shown to users
// so we decided to keep this display format compatibility and adopted this
// weird scheme with a leading 1.
#define DAEMON_VERSION_NUMBER (1000000000UL + DAEMON_VERSION_MAJOR*1000000UL + DAEMON_VERSION_MINOR*1000UL + DAEMON_VERSION_PATCH*1UL)
#define MIN_ALLOWED_PORT 1
#define MAX_ALLOWED_PORT 65535
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_GLOBAL_H

View File

@ -0,0 +1,82 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Logging utility with support of multiple logging backends.
*/
#include "log.h"
#include "log_backend_stdout.h"
#include "log_backend_syslog.h"
#define INVALID_BACKEND ((LOG_BACKEND)-1u)
static LOG_BACKEND current_backend = INVALID_BACKEND;
bool log_open(LOG_BACKEND backend)
{
if (current_backend != INVALID_BACKEND) {
return false;
}
current_backend = backend;
switch (current_backend) {
case LOG_BACKEND_STDOUT:
// nothing to do here
break;
case LOG_BACKEND_SYSLOG:
log_backend_syslog_open();
break;
}
return true;
}
bool log_close(void)
{
if (current_backend == INVALID_BACKEND) {
return false;
}
switch (current_backend) {
case LOG_BACKEND_STDOUT:
// nothing to do here
break;
case LOG_BACKEND_SYSLOG:
log_backend_syslog_close();
break;
}
current_backend = INVALID_BACKEND;
return true;
}
bool log_write(LOG_LEVEL level, const char *format, ...)
{
if (current_backend == INVALID_BACKEND) {
return false;
}
va_list args;
va_start(args, format);
switch (current_backend) {
case LOG_BACKEND_STDOUT:
log_backend_stdout_write(level, format, args);
break;
case LOG_BACKEND_SYSLOG:
log_backend_syslog_write(level, format, args);
break;
}
va_end(args);
return true;
}

View File

@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Logging utility with support of multiple logging backends.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H
#include <stdbool.h>
#include "../../../toxcore/ccompat.h"
typedef enum LOG_BACKEND {
LOG_BACKEND_STDOUT,
LOG_BACKEND_SYSLOG
} LOG_BACKEND;
typedef enum LOG_LEVEL {
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR
} LOG_LEVEL;
/**
* Initializes logger.
* @param backend Specifies which backend to use.
* @return true on success, false if log is already opened.
*/
bool log_open(LOG_BACKEND backend);
/**
* Releases all used resources by the logger.
* @return true on success, false if log is already closed.
*/
bool log_close(void);
/**
* Writes a message to the log.
* @param level Log level to use.
* @param format printf-like format string.
* @param ... Zero or more arguments, similar to printf function.
* @return true on success, false if log is closed.
*/
bool log_write(LOG_LEVEL level, const char *format, ...) GNU_PRINTF(2, 3);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H

View File

@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Stdout logging backend.
*/
#include "log_backend_stdout.h"
#include <stdio.h>
static FILE *log_backend_stdout_level(LOG_LEVEL level)
{
switch (level) {
case LOG_LEVEL_INFO:
return stdout;
case LOG_LEVEL_WARNING: // intentional fallthrough
case LOG_LEVEL_ERROR:
return stderr;
}
return stdout;
}
void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args)
{
vfprintf(log_backend_stdout_level(level), format, args);
fflush(log_backend_stdout_level(level));
}

View File

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Stdout logging backend.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H
#include "log.h"
#include <stdarg.h>
void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H

View File

@ -0,0 +1,66 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Syslog logging backend.
*/
#include "log_backend_syslog.h"
#include "global.h"
#include "../../../toxcore/ccompat.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
void log_backend_syslog_open(void)
{
openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
}
void log_backend_syslog_close(void)
{
closelog();
}
static int log_backend_syslog_level(LOG_LEVEL level)
{
switch (level) {
case LOG_LEVEL_INFO:
return LOG_INFO;
case LOG_LEVEL_WARNING:
return LOG_WARNING;
case LOG_LEVEL_ERROR:
return LOG_ERR;
}
return LOG_INFO;
}
void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args)
{
va_list args2;
va_copy(args2, args);
int size = vsnprintf(nullptr, 0, format, args2);
va_end(args2);
assert(size >= 0);
if (size < 0) {
return;
}
char *buf = (char *)malloc(size + 1);
vsnprintf(buf, size + 1, format, args);
syslog(log_backend_syslog_level(level), "%s", buf);
free(buf);
}

View File

@ -0,0 +1,21 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Syslog logging backend.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H
#define C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H
#include "log.h"
#include <stdarg.h>
void log_backend_syslog_open(void);
void log_backend_syslog_close(void);
void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H

View File

@ -0,0 +1,624 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2014-2016 Tox project.
*/
/*
* Tox DHT bootstrap daemon.
* Main file.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
// system provided
#include <signal.h> // system header, rather than C, because we need it for POSIX sigaction(2)
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
// C
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// toxcore
#include "../../../toxcore/tox.h"
#include "../../../toxcore/LAN_discovery.h"
#include "../../../toxcore/TCP_server.h"
#include "../../../toxcore/announce.h"
#include "../../../toxcore/group_onion_announce.h"
#include "../../../toxcore/logger.h"
#include "../../../toxcore/mono_time.h"
#include "../../../toxcore/onion_announce.h"
#include "../../../toxcore/util.h"
// misc
#include "../../bootstrap_node_packets.h"
#include "command_line_arguments.h"
#include "config.h"
#include "global.h"
#include "log.h"
static void sleep_milliseconds(uint32_t ms)
{
struct timespec req;
req.tv_sec = ms / 1000;
req.tv_nsec = (long)ms % 1000 * 1000 * 1000;
nanosleep(&req, nullptr);
}
// Uses the already existing key or creates one if it didn't exist
//
// returns 1 on success
// 0 on failure - no keys were read or stored
static int manage_keys(DHT *dht, char *keys_file_path)
{
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
uint8_t keys[KEYS_SIZE];
FILE *keys_file;
// Check if file exits, proceed to open and load keys
keys_file = fopen(keys_file_path, "rb");
if (keys_file != nullptr) {
const size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (read_size != KEYS_SIZE) {
fclose(keys_file);
return 0;
}
dht_set_self_public_key(dht, keys);
dht_set_self_secret_key(dht, keys + CRYPTO_PUBLIC_KEY_SIZE);
} else {
// Otherwise save new keys
memcpy(keys, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht_get_self_secret_key(dht), CRYPTO_SECRET_KEY_SIZE);
keys_file = fopen(keys_file_path, "wb");
if (!keys_file) {
return 0;
}
const size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (write_size != KEYS_SIZE) {
fclose(keys_file);
return 0;
}
}
fclose(keys_file);
return 1;
}
// Prints public key
static void print_public_key(const uint8_t *public_key)
{
char buffer[2 * CRYPTO_PUBLIC_KEY_SIZE + 1];
int index = 0;
for (size_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; i++) {
index += snprintf(buffer + index, sizeof(buffer) - index, "%02X", public_key[i]);
}
log_write(LOG_LEVEL_INFO, "Public Key: %s\n", buffer);
}
// 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)
{
// Check if the PID file exists
FILE *pid_file;
if ((pid_file = fopen(pid_file_path, "r"))) {
log_write(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
fclose(pid_file);
}
// Open the PID file for writing
pid_file = fopen(pid_file_path, "a+");
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);
}
// Fork off from the parent process
const pid_t pid = fork();
if (pid > 0) {
fprintf(pid_file, "%d", pid);
fclose(pid_file);
log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid);
exit(0);
} else {
fclose(pid_file);
}
if (pid < 0) {
log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n");
exit(1);
}
// Create a new SID for the child process
if (setsid() < 0) {
log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n");
exit(1);
}
// Change the current working directory
if ((chdir("/")) < 0) {
log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n");
exit(1);
}
// Go quiet
if (log_backend != LOG_BACKEND_STDOUT) {
close(STDOUT_FILENO);
close(STDIN_FILENO);
close(STDERR_FILENO);
}
}
// Logs toxcore logger message using our logger facility
static void toxcore_logger_callback(void *context, Logger_Level level, const char *file, int line,
const char *func, const char *message, void *userdata)
{
LOG_LEVEL log_level;
switch (level) {
case LOGGER_LEVEL_TRACE:
log_level = LOG_LEVEL_INFO;
break;
case LOGGER_LEVEL_DEBUG:
log_level = LOG_LEVEL_INFO;
break;
case LOGGER_LEVEL_INFO:
log_level = LOG_LEVEL_INFO;
break;
case LOGGER_LEVEL_WARNING:
log_level = LOG_LEVEL_WARNING;
break;
case LOGGER_LEVEL_ERROR:
log_level = LOG_LEVEL_ERROR;
break;
default:
log_level = LOG_LEVEL_INFO;
break;
}
log_write(log_level, "%s:%d(%s) %s\n", file, line, func, message);
}
static volatile sig_atomic_t caught_signal = 0;
static void handle_signal(int signum)
{
caught_signal = signum;
}
int main(int argc, char *argv[])
{
umask(077);
char *cfg_file_path = nullptr;
LOG_BACKEND log_backend;
bool run_in_foreground;
// choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal
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);
log_close();
log_open(log_backend);
log_write(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER);
char *pid_file_path = nullptr;
char *keys_file_path = nullptr;
int start_port;
int enable_ipv6;
int enable_ipv4_fallback;
int enable_lan_discovery;
int enable_tcp_relay;
uint16_t *tcp_relay_ports = nullptr;
int tcp_relay_port_count;
int enable_motd;
char *motd = nullptr;
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback,
&enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
log_write(LOG_LEVEL_INFO, "General config read successfully\n");
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
return 1;
}
if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT,
MAX_ALLOWED_PORT);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
free(pid_file_path);
return 1;
}
if (!run_in_foreground) {
daemonize(log_backend, pid_file_path);
}
free(pid_file_path);
IP ip;
ip_init(&ip, enable_ipv6);
Logger *logger = logger_new();
if (MIN_LOGGER_LEVEL == LOGGER_LEVEL_TRACE || MIN_LOGGER_LEVEL == LOGGER_LEVEL_DEBUG) {
logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr);
}
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
const Network *ns = system_network();
Networking_Core *net = new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr);
if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) {
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr);
if (net == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n");
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
}
Mono_Time *const mono_time = mono_time_new(nullptr, nullptr);
if (mono_time == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize monotonic timer. Exiting.\n");
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
mono_time_update(mono_time);
const Random *rng = system_random();
DHT *const dht = new_dht(logger, rng, ns, mono_time, net, true, enable_lan_discovery);
if (dht == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht);
if (forwarding == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n");
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
Announcements *announce = new_announcements(logger, rng, mono_time, forwarding);
if (announce == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize DHT announcements. Exiting.\n");
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
GC_Announces_List *group_announce = new_gca_list();
if (group_announce == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize group announces. Exiting.\n");
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
Onion *onion = new_onion(logger, mono_time, rng, dht);
if (!onion) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n");
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
Onion_Announce *onion_a = new_onion_announce(logger, rng, mono_time, dht);
if (!onion_a) {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion Announce. Exiting.\n");
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
gca_onion_init(group_announce, onion_a);
if (enable_motd) {
if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
free(motd);
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd);
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(motd);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
}
if (manage_keys(dht, keys_file_path)) {
log_write(LOG_LEVEL_INFO, "Keys are managed successfully.\n");
free(keys_file_path);
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path);
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(tcp_relay_ports);
free(keys_file_path);
return 1;
}
TCP_Server *tcp_server = nullptr;
if (enable_tcp_relay) {
if (tcp_relay_port_count == 0) {
log_write(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n");
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_onion(onion);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
free(tcp_relay_ports);
return 1;
}
tcp_server = new_TCP_server(logger, rng, ns, enable_ipv6, tcp_relay_port_count, tcp_relay_ports,
dht_get_self_secret_key(dht), onion, forwarding);
free(tcp_relay_ports);
if (tcp_server != nullptr) {
log_write(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n");
struct rlimit limit;
const rlim_t rlim_suggested = 32768;
const rlim_t rlim_min = 4096;
assert(rlim_suggested >= rlim_min);
if (!getrlimit(RLIMIT_NOFILE, &limit)) {
if (limit.rlim_cur < limit.rlim_max) {
// Some systems have a hard limit of over 1000000 open file descriptors, so let's cap it at something reasonable
// so that we don't set it to an unreasonably high number.
limit.rlim_cur = limit.rlim_max > rlim_suggested ? rlim_suggested : limit.rlim_max;
setrlimit(RLIMIT_NOFILE, &limit);
}
}
if (!getrlimit(RLIMIT_NOFILE, &limit) && limit.rlim_cur < rlim_min) {
log_write(LOG_LEVEL_WARNING,
"Current limit on the number of files this process can open (%ju) is rather low for the proper functioning of the TCP server. "
"Consider raising the limit to at least %ju or the recommended %ju. "
"Continuing using the current limit (%ju).\n",
(uintmax_t)limit.rlim_cur, (uintmax_t)rlim_min, (uintmax_t)rlim_suggested, (uintmax_t)limit.rlim_cur);
}
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n");
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
return 1;
}
}
if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
log_write(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n");
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path);
kill_TCP_server(tcp_server);
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
return 1;
}
print_public_key(dht_get_self_public_key(dht));
uint64_t last_LANdiscovery = 0;
const uint16_t net_htons_port = net_htons(start_port);
int waiting_for_dht_connection = 1;
Broadcast_Info *broadcast = nullptr;
if (enable_lan_discovery) {
broadcast = lan_discovery_init(ns);
log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n");
}
struct sigaction sa;
sa.sa_handler = handle_signal;
// Try to restart interrupted system calls if they are restartable
sa.sa_flags = SA_RESTART;
// Prevent the signal handler from being called again before it returns
sigfillset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, nullptr)) {
log_write(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGINT. Continuing without the signal handler set.\n");
}
if (sigaction(SIGTERM, &sa, nullptr)) {
log_write(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGTERM. Continuing without the signal handler set.\n");
}
while (!caught_signal) {
mono_time_update(mono_time);
do_dht(dht);
if (enable_lan_discovery && mono_time_is_timeout(mono_time, last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) {
lan_discovery_send(dht_get_net(dht), broadcast, dht_get_self_public_key(dht), net_htons_port);
last_LANdiscovery = mono_time_get(mono_time);
}
if (enable_tcp_relay) {
do_TCP_server(tcp_server, mono_time);
}
networking_poll(dht_get_net(dht), nullptr);
if (waiting_for_dht_connection && dht_isconnected(dht)) {
log_write(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n");
waiting_for_dht_connection = 0;
}
sleep_milliseconds(30);
}
switch (caught_signal) {
case SIGINT:
log_write(LOG_LEVEL_INFO, "Received SIGINT (%d) signal. Exiting.\n", SIGINT);
break;
case SIGTERM:
log_write(LOG_LEVEL_INFO, "Received SIGTERM (%d) signal. Exiting.\n", SIGTERM);
break;
default:
log_write(LOG_LEVEL_INFO, "Received (%d) signal. Exiting.\n", caught_signal);
}
lan_discovery_kill(broadcast);
kill_TCP_server(tcp_server);
kill_onion_announce(onion_a);
kill_gca(group_announce);
kill_onion(onion);
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
mono_time_free(mono_time);
kill_networking(net);
logger_kill(logger);
return 0;
}

View File

@ -0,0 +1,65 @@
// Tox DHT bootstrap daemon configuration file.
// Listening port (UDP).
port = 33445
// A key file is like a password, so keep it where no one can read it.
// If there is no key file, a new one will be generated.
// The daemon should have permission to read/write it.
keys_file_path = "/var/lib/tox-bootstrapd/keys"
// The PID file written to by the daemon.
// Make sure that the user that daemon runs as has permissions to write to the
// PID file.
pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid"
// Enable IPv6.
enable_ipv6 = true
// Fallback to IPv4 in case IPv6 fails.
enable_ipv4_fallback = true
// Automatically bootstrap with nodes on local area network.
enable_lan_discovery = true
enable_tcp_relay = true
// While Tox uses 33445 port by default, 443 (https) and 3389 (rdp) ports are very
// common among nodes, so it's encouraged to keep them in place.
tcp_relay_ports = [443, 3389, 33445]
// Reply to MOTD (Message Of The Day) requests.
enable_motd = true
// Just a message that is sent when someone requests MOTD.
// Put anything you want, but note that it will be trimmed to fit into 255 bytes.
motd = "tox-bootstrapd"
// Any number of nodes the daemon will bootstrap itself off.
//
// Remember to replace the provided example with your own node list.
// There is a maintained list of bootstrap nodes on Tox's wiki, if you need it
// (https://wiki.tox.chat/doku.php?id=users:nodes).
//
// You may leave the list empty or remove "bootstrap_nodes" completely,
// in both cases this will be interpreted as if you don't want to bootstrap
// from anyone.
//
// address = any IPv4 or IPv6 address and also any US-ASCII domain name.
bootstrap_nodes = (
{ // Example Node 1 (IPv4)
address = "127.0.0.1"
port = 33445
public_key = "728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854"
},
{ // Example Node 2 (IPv6)
address = "::1/128"
port = 33445
public_key = "3E78BACF0F84235B30054B54898F56793E1DEF8BD46B1038B9D822E8460FAB67"
},
{ // Example Node 3 (US-ASCII domain name)
address = "example.org"
port = 33445
public_key = "8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858"
}
)

View File

@ -0,0 +1,20 @@
[Unit]
Description=Tox DHT Bootstrap Daemon
After=network.target
[Service]
Type=forking
RuntimeDirectory=tox-bootstrapd
RuntimeDirectoryMode=750
PIDFile=/var/run/tox-bootstrapd/tox-bootstrapd.pid
WorkingDirectory=/var/lib/tox-bootstrapd
ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf
User=tox-bootstrapd
Group=tox-bootstrapd
# TCP Server needs to be able to have lots of TCP sockets open.
LimitNOFILE=32768
# Uncomment to allow binding to ports < 1024, e.g. 443 (HTTPS) for TCP Server
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,147 @@
#! /bin/bash
### BEGIN INIT INFO
# Provides: tox-bootstrapd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts the Tox DHT bootstrapping server daemon
# Description: Starts the Tox DHT bootstrapping server daemon
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Tox DHT bootstrap daemon"
NAME=tox-bootstrapd
DAEMON=/usr/local/bin/$NAME
CFGFILE=/etc/$NAME.conf
DAEMON_ARGS="--config $CFGFILE"
PIDDIR=/var/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
USER=tox-bootstrapd
GROUP=tox-bootstrapd
# Set ulimit -n based on number of fds available.
# This check is borrowed from Debian's tor package, with a few modifications.
if [ -r /proc/sys/fs/file-max ]; then
system_max=$(cat /proc/sys/fs/file-max)
if [ "$system_max" -gt "80000" ] ; then
MAX_FILEDESCRIPTORS=32768
elif [ "$system_max" -gt "40000" ] ; then
MAX_FILEDESCRIPTORS=16384
elif [ "$system_max" -gt "20000" ] ; then
MAX_FILEDESCRIPTORS=8192
elif [ "$system_max" -gt "10000" ] ; then
MAX_FILEDESCRIPTORS=4096
else
MAX_FILEDESCRIPTORS=1024
cat << EOF
Warning: Your system has very few file descriptors available in total.
Maybe you should try raising that by adding 'fs.file-max=100000' to your
/etc/sysctl.conf file. Feel free to pick any number that you deem appropriate.
Then run 'sysctl -p'. See /proc/sys/fs/file-max for the current value, and
file-nr in the same directory for how many of those are used at the moment.
EOF
fi
else
MAX_FILEDESCRIPTORS=32768
fi
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
if [ ! -d $PIDDIR ]
then
mkdir $PIDDIR
fi
chown $USER:$GROUP $PIDDIR
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test --chuid $USER > /dev/null || return 1
# TCP Server needs to be able to have lots of TCP sockets open.
ulimit -n $MAX_FILEDESCRIPTORS
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER -- $DAEMON_ARGS || return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry 5 --pidfile $PIDFILE --name $NAME --chuid $USER
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
exit 0

View File

@ -0,0 +1,29 @@
# Stage 1 - Compile websockify.
FROM toxchat/bootstrap-node:latest AS tox
FROM golang:1.17-alpine AS websockify
COPY websockify /work/websockify
RUN cd /work/websockify && go mod download github.com/gorilla/websocket && go install
# Stage 2 - Create the run-time image with bootstrap daemon and websockify.
FROM alpine:latest
RUN addgroup -S tox && adduser -SDH -G tox tox
COPY --from=websockify /go/bin/websockify /usr/local/bin/
COPY --from=tox /usr/local /usr/local/
COPY --from=tox /etc/tox-bootstrapd.conf /etc/
COPY entrypoint.sh /
RUN mkdir -p /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/ \
&& chown tox:tox /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/
# Public Key: 122837CCDD474DD1183A83152164D51427044B3EAAA52ED683F6BA602568673B
COPY keys /var/lib/tox-bootstrapd/
USER tox
# Use this to generate a keys file:
#RUN /usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout \
# && sleep 1
WORKDIR /web
CMD ["/entrypoint.sh"]

View File

@ -0,0 +1,6 @@
#!/bin/sh
set -eux
/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout
/usr/local/bin/websockify -l "0.0.0.0:$PORT" -t 127.0.0.1:33445

View File

@ -0,0 +1 @@
(7<><37>GM<47>:<3A>!d<>'K><3E><><><D683>`%hg;<3B>G<EFBFBD><47>Y<>:<3A>Zy5<79><35>S<EFBFBD>$<24>|Cڠ<43>Ǹ<EFBFBD>2<10> <09>t

View File

@ -0,0 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
package(features = ["-layering_check"])
go_library(
name = "go_default_library",
srcs = ["websockify.go"],
importpath = "github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify",
visibility = ["//visibility:private"],
deps = ["@com_github_gorilla_websocket//:go_default_library"],
)
go_binary(
name = "websockify",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,7 @@
module github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify
go 1.17
require (
github.com/gorilla/websocket master
)

View File

@ -0,0 +1,111 @@
// A Go version WebSocket to TCP socket proxy
//
// This is a heavily modified version of this file:
// https://github.com/novnc/websockify-other/blob/master/golang/websockify.go
//
// Changes include:
// - Fix infinite loop on error.
// - Proper logging.
// - Proper error handling in general.
// - Support both websocket and regular GET requests on /.
//
// Copyright 2022 The TokTok team.
// Copyright 2021 Michael.liu.
// See LICENSE for licensing conditions.
package main
import (
"encoding/hex"
"flag"
"log"
"net"
"net/http"
"github.com/gorilla/websocket"
)
var (
sourceAddr = flag.String("l", "127.0.0.1:8080", "http service address")
targetAddr = flag.String("t", "127.0.0.1:5900", "tcp service address")
)
var upgrader = websocket.Upgrader{
// Should be enough to fit any Tox TCP packets.
ReadBufferSize: 2048,
WriteBufferSize: 2048,
Subprotocols: []string{"binary"},
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func forwardTCP(wsconn *websocket.Conn, conn net.Conn) {
var tcpbuffer [2048]byte
defer wsconn.Close()
defer conn.Close()
for {
n, err := conn.Read(tcpbuffer[0:])
if err != nil {
log.Println("TCP READ :", err)
break
}
log.Println("TCP READ :", n, hex.EncodeToString(tcpbuffer[0:n]))
if err := wsconn.WriteMessage(websocket.BinaryMessage, tcpbuffer[0:n]); err != nil {
log.Println("WS WRITE :", err)
break
}
log.Println("WS WRITE :", n)
}
}
func forwardWeb(wsconn *websocket.Conn, conn net.Conn) {
defer wsconn.Close()
defer conn.Close()
for {
_, buffer, err := wsconn.ReadMessage()
if err != nil {
log.Println("WS READ :", err)
break
}
log.Println("WS READ :", len(buffer), hex.EncodeToString(buffer))
m, err := conn.Write(buffer)
if err != nil {
log.Println("TCP WRITE:", err)
break
}
log.Println("TCP WRITE:", m)
}
}
func serveWs(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("upgrade:", err)
return
}
vnc, err := net.Dial("tcp", *targetAddr)
if err != nil {
log.Println("dial:", err)
return
}
go forwardTCP(ws, vnc)
go forwardWeb(ws, vnc)
}
func main() {
flag.Parse()
log.Println("Starting up websockify endpoint")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Upgrade") == "websocket" {
serveWs(w, r)
} else {
http.ServeFile(w, r, r.URL.Path[1:])
}
})
log.Fatal(http.ListenAndServe(*sourceAddr, nil))
}

View File

@ -0,0 +1,58 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2013 Tox project.
*/
/*
* Special bootstrap node only packets.
*
* Include it in your bootstrap node and use: bootstrap_set_callbacks() to enable.
*/
#include "bootstrap_node_packets.h"
#include <string.h>
#define INFO_REQUEST_PACKET_LENGTH 78
static uint32_t bootstrap_version;
static uint8_t bootstrap_motd[MAX_MOTD_LENGTH];
static uint16_t bootstrap_motd_length;
/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH
* with the first byte being BOOTSTRAP_INFO_PACKET_ID
*/
static int handle_info_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
void *userdata)
{
if (length != INFO_REQUEST_PACKET_LENGTH) {
return 1;
}
const Networking_Core *nc = (const Networking_Core *)object;
uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
data[0] = BOOTSTRAP_INFO_PACKET_ID;
memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length);
if (sendpacket(nc, source, data, len) == len) {
return 0;
}
return 1;
}
int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, const uint8_t *motd, uint16_t motd_length)
{
if (motd_length > MAX_MOTD_LENGTH) {
return -1;
}
bootstrap_version = net_htonl(version);
memcpy(bootstrap_motd, motd, motd_length);
bootstrap_motd_length = motd_length;
networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net);
return 0;
}

View File

@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2015 Tox project.
*/
/*
* Special bootstrap node only packets.
*
* Include it in your bootstrap node and use: bootstrap_set_callbacks() to enable.
*/
#ifndef C_TOXCORE_OTHER_BOOTSTRAP_NODE_PACKETS_H
#define C_TOXCORE_OTHER_BOOTSTRAP_NODE_PACKETS_H
#include "../toxcore/network.h"
#define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */
int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, const uint8_t *motd, uint16_t motd_length);
#endif // C_TOXCORE_OTHER_BOOTSTRAP_NODE_PACKETS_H

View File

@ -0,0 +1,8 @@
################################################
# autotools-linux
FROM toxchat/nacl:latest
# Copy the sources and run the build.
COPY --chown=builder:builder . /home/builder/c-toxcore/
WORKDIR /home/builder/c-toxcore
RUN CC=gcc .github/scripts/autotools-linux

View File

@ -0,0 +1,3 @@
#!/bin/sh
docker build -t toxchat/c-toxcore:autotools -f other/docker/autotools/Dockerfile .

View File

@ -0,0 +1,6 @@
FROM toxchat/haskell:hs-cimple AS cimple
FROM ubuntu:20.04
COPY --from=cimple /bin/cimplefmt /bin/
WORKDIR /work
ENTRYPOINT ["/bin/cimplefmt"]

View File

@ -0,0 +1,8 @@
#!/bin/sh
if [ "$1" = "-u" ]; then
shift
docker pull toxchat/haskell:hs-cimple
docker build -t toxchat/cimplefmt -f other/docker/cimplefmt/Dockerfile .
fi
docker run --rm -v "$PWD:/work" toxchat/cimplefmt "$@"

View File

@ -0,0 +1,30 @@
################################################
# cmake-asan
FROM ubuntu:20.04
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
clang \
cmake \
libconfig-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
llvm-dev \
ninja-build \
pkg-config \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY entrypoint.sh /
RUN ["chmod", "755", "/entrypoint.sh"]
WORKDIR /home/builder
RUN groupadd -r -g 1000 builder \
&& useradd --no-log-init -r -g builder -u 1000 builder \
&& chown builder:builder /home/builder
USER builder
ENV CC=clang CXX=clang++
ENTRYPOINT ["/entrypoint.sh"]

View File

@ -0,0 +1,9 @@
#!/bin/sh
set -eu
SANITIZER="${1:-asan}"
cp -a /c-toxcore .
cd c-toxcore
.circleci/cmake-"$SANITIZER"

View File

@ -0,0 +1,6 @@
#!/bin/sh
SANITIZER="${1:-asan}"
docker build -t toxchat/c-toxcore:circleci other/docker/circleci
docker run --rm -it -v "$PWD:/c-toxcore" toxchat/c-toxcore:circleci "$SANITIZER"

View File

@ -0,0 +1,78 @@
FROM toxchat/c-toxcore:sources AS src
FROM ubuntu:20.04 AS build
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
clang \
cmake \
gcc \
git \
golang \
libconfig-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
llvm-dev \
make \
ninja-build \
pkg-config \
python3-pip \
python3-pygments \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& pip3 install --no-cache-dir gcovr
ENV CC=clang \
CXX=clang++ \
PYTHONUNBUFFERED=1
SHELL ["/bin/bash", "-c"]
WORKDIR /work
COPY --from=src /src/ /work/
RUN source .github/scripts/flags-coverage.sh \
&& go get github.com/things-go/go-socks5 \
&& go build other/proxy/proxy_server.go \
&& cmake -B_build -H. -GNinja \
-DCMAKE_C_FLAGS="$C_FLAGS" \
-DCMAKE_CXX_FLAGS="$CXX_FLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \
-DCMAKE_UNITY_BUILD=ON \
-DENABLE_SHARED=OFF \
-DMIN_LOGGER_LEVEL=TRACE \
-DMUST_BUILD_TOXAV=ON \
-DNON_HERMETIC_TESTS=ON \
-DSTRICT_ABI=ON \
-DAUTOTEST=ON \
-DPROXY_TEST=ON \
-DUSE_IPV6=OFF \
&& cmake --build _build --parallel 8 --target install
WORKDIR /work/_build
RUN /work/proxy_server \
& ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6
WORKDIR /work/mallocfail
RUN ["git", "clone", "--depth=1", "https://github.com/ralight/mallocfail", "/work/mallocfail"]
COPY run_mallocfail /usr/local/bin/
COPY syscall_funcs.c src/
RUN gcc -fPIC -shared -O2 -g3 -Wall -Ideps/uthash -Ideps/sha3 deps/*/*.c src/*.c -o mallocfail.so -ldl -lbacktrace \
&& install mallocfail.so /usr/local/lib/mallocfail.so
WORKDIR /work/_build
RUN ["run_mallocfail", "--ctest=2", "--jobs=8"]
RUN ["gcovr", \
"--sort-percentage", \
"--gcov-executable=llvm-cov gcov", \
"--html-details=html/", \
"--root=..", \
"--exclude=CMakeFiles/", \
"--exclude=_deps/", \
"--exclude=(.+/)?auto_tests/", \
"--exclude=.+_test.cc?$", \
"--exclude=(.+/)?other/", \
"--exclude=(.+/)?testing/"]
FROM nginx:alpine
COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html
COPY --from=build /work/_build/html/ /usr/share/nginx/html/

View File

@ -0,0 +1,3 @@
// Dummy, actual one is here: https://github.com/ralight/mallocfail/blob/master/src/mallocfail.h
int should_malloc_fail(void);

View File

@ -0,0 +1,7 @@
#!/bin/sh
set -eux
docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile .
docker build -t toxchat/c-toxcore:coverage other/docker/coverage
docker run --rm -it -p "28192:80" toxchat/c-toxcore:coverage

View File

@ -0,0 +1,166 @@
#!/usr/bin/env python3
"""Run a test repeatedly with mallocfail.
Usage: run_mallocfail [--ctest=<cost>] [<exe>...]
This runs the programs with mallocfail until there are no more additional
stack hashes for mallocfail to try out.
Passing "--ctest" additionally runs all the tests with a cost lower than the
given flag value. Cost is measured in seconds of runtime.
You need to build mallocfail (https://github.com/ralight/mallocfail) and install
it to /usr/local/lib/mallocfail. Change _MALLOCFAIL_SO below if you want it
elsewhere.
"""
import glob
import multiprocessing
import os
import shutil
import subprocess
import sys
import tempfile
import time
from typing import Dict
from typing import List
from typing import NoReturn
from typing import Optional
from typing import Tuple
_PRIMER = "./unit_util_test"
_MALLOCFAIL_SO = "/usr/local/lib/mallocfail.so"
_HASHES = "mallocfail_hashes"
_HASHES_PREV = "mallocfail_hashes.prev"
_TIMEOUT = 3.0
_ENV = {
"LD_PRELOAD": _MALLOCFAIL_SO,
"UBSAN_OPTIONS": "color=always,print_stacktrace=1,exitcode=11",
}
def run_mallocfail(tmpdir: str, timeout: float, exe: str,
iteration: int) -> bool:
"""Run a program with mallocfail."""
print(f"\x1b[1;33mmallocfail '{exe}' run #{iteration}\x1b[0m")
hashes = os.path.join(tmpdir, _HASHES)
hashes_prev = os.path.join(tmpdir, _HASHES_PREV)
if os.path.exists(hashes):
shutil.copy(hashes, hashes_prev)
try:
proc = subprocess.run([exe], timeout=timeout, env=_ENV, cwd=tmpdir)
except subprocess.TimeoutExpired:
print(f"\x1b[1;34mProgram {exe} timed out\x1b[0m")
return True
finally:
assert os.path.exists(hashes)
if os.path.exists(hashes_prev):
with open(hashes_prev, "r") as prev:
with open(hashes, "r") as cur:
if prev.read() == cur.read():
# Done: no new stack hashes.
return False
if proc.returncode in (0, 1):
# Process exited cleanly (success or failure).
pass
elif proc.returncode == -6:
# Assertion failed.
pass
elif proc.returncode == -14:
print(f"\x1b[0;34mProgram '{exe}' timed out\x1b[0m")
else:
print(
f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly\x1b[0m"
)
raise Exception("Aborting test")
return True
def ctest_costs() -> Dict[str, float]:
with open("Testing/Temporary/CTestCostData.txt", "r") as fh:
costs = {}
for line in fh.readlines():
if line.startswith("---"):
break
prog, _, cost = line.rstrip().split(" ")
costs[prog] = float(cost)
return costs
def find_prog(name: str) -> Tuple[Optional[str], ...]:
def attempt(path: str) -> Optional[str]:
if os.path.exists(path):
return path
return None
return (attempt(f"./unit_{name}_test"), attempt(f"./auto_{name}_test"))
def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]:
flags: Dict[str, str] = {}
exes: List[str] = []
for arg in args:
if arg.startswith("--"):
flag, value = arg.split("=", 1)
flags[flag] = value
else:
exes.append(arg)
return flags, exes
def loop_mallocfail(tmpdir: str, timeout: float, exe: str) -> None:
i = 1
while run_mallocfail(tmpdir, timeout, exe, i):
i += 1
def isolated_mallocfail(timeout: int, exe: str) -> None:
with tempfile.TemporaryDirectory(prefix="mallocfail") as tmpdir:
print(f"\x1b[1;33mRunning for {exe} in isolated path {tmpdir}\x1b[0m")
shutil.copy(exe, os.path.join(tmpdir, exe))
shutil.copy(_HASHES, os.path.join(tmpdir, _HASHES))
loop_mallocfail(tmpdir, timeout, exe)
def main(args: List[str]) -> None:
"""Run a program repeatedly under mallocfail."""
if len(args) == 1:
print(f"Usage: {args[0]} <exe>")
sys.exit(1)
flags, exes = parse_flags(args[1:])
timeout = _TIMEOUT
if "--ctest" in flags:
costs = ctest_costs()
max_cost = float(flags["--ctest"])
timeout = max(max_cost + 1, timeout)
exes.extend(prog for test in costs.keys() for prog in find_prog(test)
if costs[test] <= max_cost and prog)
if "--jobs" in flags:
jobs = int(flags["--jobs"])
else:
jobs = 1
# Start by running util_test, which allocates no memory of its own, just
# to prime the mallocfail hashes so it doesn't make global initialisers
# such as llvm_gcov_init fail.
if os.path.exists(_PRIMER):
print(f"\x1b[1;33mPriming hashes with unit_util_test\x1b[0m")
loop_mallocfail(".", timeout, _PRIMER)
print(f"\x1b[1;33m--------------------------------\x1b[0m")
print(f"\x1b[1;33mStarting mallocfail for {len(exes)} programs:\x1b[0m")
print(f"\x1b[1;33m{exes}\x1b[0m")
print(f"\x1b[1;33m--------------------------------\x1b[0m")
time.sleep(1)
with multiprocessing.Pool(jobs) as p:
done = tuple(
p.starmap(isolated_mallocfail, ((timeout, exe) for exe in exes)))
print(f"\x1b[1;32mCompleted {len(done)} programs\x1b[0m")
if __name__ == "__main__":
main(sys.argv)

View File

@ -0,0 +1,149 @@
#define _GNU_SOURCE
#include "mallocfail.h"
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
static int (*libc_ioctl)(int fd, unsigned long request, ...);
static int (*libc_bind)(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
static int (*libc_getsockopt)(int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
static int (*libc_setsockopt)(int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
static ssize_t (*libc_recv)(int sockfd, void *buf, size_t len, int flags);
static ssize_t (*libc_recvfrom)(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
static ssize_t (*libc_send)(int sockfd, const void *buf, size_t len, int flags);
static ssize_t(*libc_sendto)(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
static int (*libc_socket)(int domain, int type, int protocol);
static int (*libc_listen)(int sockfd, int backlog);
__attribute__((__constructor__))
static void init(void)
{
libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
libc_bind = dlsym(RTLD_NEXT, "bind");
libc_getsockopt = dlsym(RTLD_NEXT, "getsockopt");
libc_setsockopt = dlsym(RTLD_NEXT, "setsockopt");
libc_recv = dlsym(RTLD_NEXT, "recv");
libc_recvfrom = dlsym(RTLD_NEXT, "recvfrom");
libc_send = dlsym(RTLD_NEXT, "send");
libc_sendto = dlsym(RTLD_NEXT, "sendto");
libc_socket = dlsym(RTLD_NEXT, "socket");
libc_listen = dlsym(RTLD_NEXT, "listen");
}
int ioctl(int fd, unsigned long request, ...)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
va_list ap;
va_start(ap, request);
const int ret = libc_ioctl(fd, SIOCGIFCONF, va_arg(ap, void *));
va_end(ap);
return ret;
}
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen)
{
// Unlike all others, if bind should fail once, it should fail always, because in toxcore we try
// many ports before giving up. If this only fails once, we'll never reach the code path where
// we give up.
static int should_fail = -1;
if (should_fail == -1) {
should_fail = should_malloc_fail();
}
if (should_fail) {
errno = ENOMEM;
return -1;
}
return libc_bind(sockfd, addr, addrlen);
}
int getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_getsockopt(sockfd, level, optname, optval, optlen);
}
int setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_setsockopt(sockfd, level, optname, optval, optlen);
}
ssize_t recv(int sockfd, void *buf, size_t len, int flags)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_recv(sockfd, buf, len, flags);
}
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
}
ssize_t send(int sockfd, const void *buf, size_t len, int flags)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_send(sockfd, buf, len, flags);
}
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
}
int socket(int domain, int type, int protocol)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_socket(domain, type, protocol);
}
int listen(int sockfd, int backlog)
{
if (should_malloc_fail()) {
errno = ENOMEM;
return -1;
}
return libc_listen(sockfd, backlog);
}

View File

@ -0,0 +1,18 @@
FROM alpine:latest AS build
ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
LC_CTYPE=en_US.UTF-8 \
LC_ALL=en_US.UTF-8
RUN apk add --no-cache doxygen git graphviz \
&& git clone --depth=1 https://github.com/jothepro/doxygen-awesome-css.git /work/doxygen-awesome-css
WORKDIR /work
COPY . /work/
RUN cat docs/Doxyfile > Doxyfile \
&& echo "WARN_AS_ERROR = YES" >> Doxyfile \
&& sed -i -e 's/^non_null([^)]*) *//;s/^nullable([^)]*) *//' $(find . -name "*.[ch]") \
&& doxygen Doxyfile
FROM nginx:alpine
COPY --from=build /work/_docs/html/ /usr/share/nginx/html/

View File

@ -0,0 +1,6 @@
#!/bin/sh
set -eux
docker build -t toxchat/c-toxcore:docs -f other/docker/doxygen/Dockerfile .
docker run --rm -it -p "28192:80" toxchat/c-toxcore:docs

View File

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(toxcore)

View File

@ -0,0 +1,48 @@
FROM toxchat/c-toxcore:sources AS src
FROM ubuntu:18.04
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
bison \
ccache \
cmake \
flex \
git \
gperf \
libncurses-dev \
ninja-build \
python \
python-cryptography \
python-future \
python-pip \
python-pyparsing \
python-serial \
python-setuptools \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ENV ESP32_TARBALL=xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0 \
IDF_PATH="/root/esp/esp-idf" \
PATH="/root/esp/esp-idf/tools:/root/esp/xtensa-esp32-elf/bin:$PATH"
WORKDIR /root/esp
RUN wget -q https://dl.espressif.com/dl/$ESP32_TARBALL.tar.gz \
&& tar zxf $ESP32_TARBALL.tar.gz \
&& rm -f $ESP32_TARBALL.tar.gz \
&& git clone -b v3.3 --recursive --depth=1 --shallow-submodules https://github.com/espressif/esp-idf
# Build a hello world first, so the OS and libsodium etc. are compiled.
WORKDIR /root/esp/toxcore
COPY other/docker/esp32/CMakeLists.txt /root/esp/toxcore/
COPY other/docker/esp32/hello/ /root/esp/toxcore/main/
RUN idf.py build
# Then copy over the actual toxcore sources and build those.
COPY --from=src /src/third_party/cmp/ /root/esp/toxcore/main/third_party/cmp/
COPY --from=src /src/toxencryptsave/defines.h /root/esp/toxcore/main/toxencryptsave/
COPY --from=src /src/toxcore/ /root/esp/toxcore/main/toxcore/
COPY other/docker/esp32/toxcore/CMakeLists.txt /root/esp/toxcore/main/
COPY other/docker/esp32/toxcore/toxcore_main.cc /root/esp/toxcore/main/other/docker/esp32/main/
RUN idf.py build
RUN ls -lh build/toxcore.bin \
&& shasum build/toxcore.bin

View File

@ -0,0 +1,4 @@
set(COMPONENT_SRCS "hello_main.c")
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -0,0 +1,6 @@
#include <stdio.h>
void app_main(void)
{
printf("Hello world!\n");
}

View File

@ -0,0 +1,4 @@
#!/bin/sh
docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile .
docker build -t toxchat/c-toxcore:esp32 -f other/docker/esp32/Dockerfile .

View File

@ -0,0 +1,12 @@
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-field-initializers")
file(GLOB toxcore_SRCS "toxcore/*.[ch]" "toxcore/*/*.[ch]")
set(COMPONENT_SRCS
${toxcore_SRCS}
other/docker/esp32/main/toxcore_main.cc
third_party/cmp/cmp.c
third_party/cmp/cmp.h
toxencryptsave/defines.h)
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include "../../../../toxcore/ccompat.h"
#include "../../../../toxcore/tox.h"
#include "../../../../toxcore/tox_events.h"
extern "C" void app_main(void)
{
printf("Hello Tox!\n");
Tox *tox = tox_new(nullptr, nullptr);
tox_events_free(tox_events_iterate(tox, true, nullptr));
tox_kill(tox);
}

View File

@ -0,0 +1 @@
!/Makefile

View File

@ -0,0 +1,20 @@
FROM ubuntu:20.04
RUN apt-get update && apt-get install --no-install-recommends -y \
ca-certificates \
cppcheck \
libopus-dev \
libsodium-dev \
libvpx-dev \
make \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY toxav/ /src/workspace/c-toxcore/toxav/
COPY toxcore/ /src/workspace/c-toxcore/toxcore/
COPY toxencryptsave/ /src/workspace/c-toxcore/toxencryptsave/
COPY third_party/cmp/cmp.h /src/workspace/c-toxcore/third_party/cmp/cmp.h
COPY other/docker/misra/Makefile /src/workspace/
WORKDIR /src/workspace
RUN ["make"]

View File

@ -0,0 +1,166 @@
# See the following PDF for descriptions of each of the rules:
# http://my.ldrasoftware.co.uk/repository/miscellaneous/Misra-c_2012_compliance.pdf
# There should be no unused parameters in functions.
#
# Reason: callbacks often have unused parameters. Marking them explicitly isn't
# very helpful. A better diagnostic should be able to identify functions never
# used as callbacks and warn about unused parameters in those.
SUPPRESSIONS = 2.7
# The character sequences /* and // shall not be used within a comment.
#
# Reason: "//" appears in code examples and "http://" inside comments.
SUPPRESSIONS += 3.1
# Identifiers declared in the same scope and name space shall be distinct.
# Identifier not unique within 31 characters.
#
# Reason: Compilers we use allow longer identifier names.
SUPPRESSIONS += 5.2
# Macro identifiers shall be distinct.
# Identifier matches macro name in 31 chars.
#
# Reason: Compilers we use allow longer identifier names.
SUPPRESSIONS += 5.4
# The lowercase character 'l' shall not be used in a literal suffix.
#
# Reason: False positive. We don't use 'l', but this flags 'ulOutBufLen'.
SUPPRESSIONS += 7.3
# Operands shall not be of an inappropriate essential type.
#
# Reason: This diagnoses (1 << n) and wants us to use (1u << n). That's fair,
# but this diagnostic is impossible to fix for ((1u << n) >> m).
SUPPRESSIONS += 10.1
# Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category.
#
# Reason: This warns about ((unsigned)n == 0) and other constant comparisons.
SUPPRESSIONS += 10.4
# The value of a composite expression shall not be cast to a different essential type category or a wider essential type.
#
# TODO(iphydf): investigate.
SUPPRESSIONS += 10.8
# A conversion should not be performed from pointer to void into pointer to object.
#
# Reason: this is needed for generic callbacks to make any sense.
SUPPRESSIONS += 11.5
# The precedence of operators within expressions should be made explicit.
#
# Reason: this asks us to add a lot of extra parentheses that don't really help
# readability. We expect people to know basic precedence. GCC has a better
# diagnostic requiring parentheses around less common operators.
SUPPRESSIONS += 12.1
# The comma operator should not be used.
#
# Reason: We don't use the comma operator (cimple doesn't even parse it). This is
# all false positives.
SUPPRESSIONS += 12.3
# Evaluation of constant expressions should not lead to unsigned integer wrap-around.
#
# Reason: False positives on UINT64_MAX.
SUPPRESSIONS += 12.4
# A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator.
#
# TODO(iphydf): maybe fix?
SUPPRESSIONS += 13.3
# The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type.
#
# Reason: We actually follow this rule, but cppcheck's implementation is flawed and has false positives.
SUPPRESSIONS += 14.4
# The goto statement should not be used.
#
# TODO(iphydf): Get rid of goto.
SUPPRESSIONS += 15.1
# A function should have a single point of exit at the end.
#
# Reason: This doesn't make code much clearer. Sometimes this is useful for
# putting all the cleanup code in one spot, but often an early return improves
# readability.
SUPPRESSIONS += 15.5
# All if . . else if constructs shall be terminated with an else statement.
#
# TODO(iphydf): Why is this a good idea?
SUPPRESSIONS += 15.7
# An unconditional break statement shall terminate every switch-clause.
#
# Reason: This conflicts with "break unused after abort()". MISRA doesn't allow
# abort(), but we use it, so this rule must be disabled, too.
SUPPRESSIONS += 16.3
# Every switch statement shall have a default label.
#
# Reason: C compilers have better diagnostics for this (-Wswitch variants).
SUPPRESSIONS += 16.4
# The features of <stdarg.h> shall not be used.
#
# Reason: We use it for logging.
SUPPRESSIONS += 17.1
# Functions shall not call themselves, either directly or indirectly.
#
# Reason: Cimple is better at this diagnostic, recognising indirect recursion
# through callbacks.
SUPPRESSIONS += 17.2
# The value returned by a function having non-void return type shall be used.
#
# TODO(iphydf): Investigate.
SUPPRESSIONS += 17.7
# A function parameter should not be modified.
#
# TODO(iphydf): maybe fix?
SUPPRESSIONS += 17.8
# The +, -, += and -= operators should not be applied to an expression of pointer type.
# Use of pointer arithmetic.
#
# TODO(iphydf): Someday we won't be using pointer arithmetic.
SUPPRESSIONS += 18.4
# Flexible array members shall not be declared.
#
# TODO(iphydf): Fix.
SUPPRESSIONS += 18.7
# Variable-length array types shall not be used.
#
# TODO(iphydf): Fix.
SUPPRESSIONS += 18.8
# The union keyword should not be used.
#
# TODO(iphydf): Maybe we need a good linter to check that unions are used safely.
SUPPRESSIONS += 19.2
# #undef should not be used.
#
# Reason: We believe it should be used when #define is used in block scope.
SUPPRESSIONS += 20.5
# The # and ## preprocessor operators should not be used.
#
# TODO(iphydf): Remove suppression when VLAs are gone. This is only used in
# the SIZEOF_VLA macro.
SUPPRESSIONS += 20.10
# #define and #undef shall not be used on a reserved identifier or reserved macro name.
#
# Reason: Needed for feature test macros like _DEFAULT_SOURCE.
SUPPRESSIONS += 21.1
# The memory allocation and deallocation functions of <stdlib.h> shall not be used.
#
# Reason: We use malloc/free. Making our own allocators doesn't make the code
# safer.
SUPPRESSIONS += 21.3
# The Standard Library input/output routines shall not be used.
#
# Reason: Used in logging.
SUPPRESSIONS += 21.6
# The Standard Library termination functions of <stdlib.h> shall not be used.
# Use of abort, exit, etc.
#
# Reason: Used in LOGGER_FATAL.
SUPPRESSIONS += 21.8
# The Standard Library functions bsearch and qsort of <stdlib.h> shall not be used.
#
# TODO(iphydf): Why not use qsort?
SUPPRESSIONS += 21.9
# The Standard Library time and date routines shall not be used.
#
# TODO(iphydf): Probably stop using time().
SUPPRESSIONS += 21.10
CPPFLAGS := -DCMP_NO_FLOAT=1 -DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE
SOURCES := $(shell find /src/workspace/c-toxcore -name "*.c")
analyse: $(DUMPS:.dump=.diag)
cppcheck --error-exitcode=1 -j8 --addon=misra --suppress=doubleFree $(patsubst %,--suppress=misra-c2012-%,$(SUPPRESSIONS)) $(CPPFLAGS) $(SOURCES)

View File

@ -0,0 +1,3 @@
#!/bin/sh
docker build -f other/docker/misra/Dockerfile .

View File

@ -0,0 +1,15 @@
FROM alpine:3.14.0
RUN ["apk", "add", "--no-cache", \
"bash", \
"gcc", \
"linux-headers", \
"musl-dev", \
"libsodium-dev", \
"libvpx-dev", \
"opus-dev", \
"perf"]
WORKDIR /work
COPY other/docker/perf/entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -eux
TEST=${1:-conference_test}
OUTPUT="/work/c-toxcore/test.perf"
readarray -t FLAGS <<<"$(pkg-config --cflags --libs libsodium opus vpx | sed -e 's/ /\n/g')"
readarray -t SRCS <<<"$(find /work/c-toxcore/tox* -name "*.c")"
gcc -pthread -g \
-o "/work/$TEST" -O3 -fno-omit-frame-pointer \
"${SRCS[@]}" \
/work/c-toxcore/auto_tests/auto_test_support.c \
/work/c-toxcore/testing/misc_tools.c \
"/work/c-toxcore/auto_tests/$TEST.c" \
-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
"${FLAGS[@]}"
time perf record -g --call-graph dwarf --freq=max "/work/$TEST" /work/c-toxcore/auto_tests/
perf report | head -n50
perf script -F +pid >"$OUTPUT"
chown 1000:100 "$OUTPUT"

View File

@ -0,0 +1,9 @@
#!/bin/sh
set -eux
docker build -t toxchat/c-toxcore:perf -f other/docker/perf/Dockerfile .
docker run --privileged --rm -it \
-v "$PWD:/work/c-toxcore" \
toxchat/c-toxcore:perf \
"$@"

View File

@ -0,0 +1,17 @@
FROM scratch
# Roughly in order of change frequency.
COPY third_party/ /src/third_party/
COPY .github/scripts/flags*.sh /src/.github/scripts/
COPY other/proxy/ /src/other/proxy/
COPY cmake/ /src/cmake/
COPY other/bootstrap_daemon/ /src/other/bootstrap_daemon/
COPY other/pkgconfig/ /src/other/pkgconfig/
COPY other/rpm/ /src/other/rpm/
COPY other/*.[ch] /src/other/
COPY CMakeLists.txt so.version /src/
COPY toxencryptsave/ /src/toxencryptsave/
COPY testing/ /src/testing/
COPY toxav/ /src/toxav/
COPY toxcore/ /src/toxcore/
COPY auto_tests/ /src/auto_tests/

View File

@ -0,0 +1,47 @@
FROM ubuntu:20.04
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
libc-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
pkg-config \
tcc \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /work
COPY auto_tests/ /work/auto_tests/
COPY testing/ /work/testing/
COPY toxav/ /work/toxav/
COPY toxcore/ /work/toxcore/
COPY toxencryptsave/ /work/toxencryptsave/
RUN tcc \
-Dinline=static \
-o send_message_test \
-Wall -Werror \
-bench -g \
auto_tests/auto_test_support.c \
auto_tests/send_message_test.c \
testing/misc_tools.c \
toxav/*.c \
toxcore/*.c \
toxcore/*/*.c \
toxencryptsave/*.c \
third_party/cmp/*.c \
$(pkg-config --cflags --libs libsodium opus vpx)
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
COPY other/make_single_file /work/other/
RUN \
other/make_single_file \
auto_tests/auto_test_support.c \
auto_tests/send_message_test.c \
testing/misc_tools.c | \
tcc - \
-o send_message_test \
-Wall -Werror \
-bench -g \
$(pkg-config --cflags --libs libsodium opus vpx)

View File

@ -0,0 +1,5 @@
#!/bin/sh
set -eux
docker build -t toxchat/c-toxcore:tcc -f other/docker/tcc/Dockerfile .

View File

@ -0,0 +1,21 @@
FROM toxchat/haskell:hs-tokstyle AS tokstyle
FROM ubuntu:20.04
RUN apt-get update && apt-get install --no-install-recommends -y \
ca-certificates \
clang \
git \
libopus-dev \
libsodium-dev \
libvpx-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=tokstyle /bin/check-c /bin/
RUN ["git", "clone", "--depth=1", "https://github.com/TokTok/hs-tokstyle", "/src/workspace/hs-tokstyle"]
COPY toxav/ /src/workspace/c-toxcore/toxav/
COPY toxcore/ /src/workspace/c-toxcore/toxcore/
COPY toxencryptsave/ /src/workspace/c-toxcore/toxencryptsave/
COPY third_party/cmp/cmp.h /src/workspace/c-toxcore/third_party/cmp/cmp.h
RUN /bin/check-c $(find /src/workspace/c-toxcore -name "*.c")

View File

@ -0,0 +1,3 @@
#!/bin/sh
docker build -f other/docker/tokstyle/Dockerfile .

View File

@ -0,0 +1,35 @@
FROM debian:bullseye-slim
# Build-time environment variables
ARG VERSION_MSGPACK=4.0.0 \
VERSION_SODIUM=1.0.18 \
VERSION_OPUS=1.3.1 \
VERSION_VPX=1.11.0 \
\
SUPPORT_TEST=false \
SUPPORT_ARCH_i686=true \
SUPPORT_ARCH_x86_64=true \
CROSS_COMPILE=true
# Make those available when running the container
ENV SUPPORT_TEST=${SUPPORT_TEST} \
SUPPORT_ARCH_i686=${SUPPORT_ARCH_i686} \
SUPPORT_ARCH_x86_64=${SUPPORT_ARCH_x86_64} \
CROSS_COMPILE=${CROSS_COMPILE}
WORKDIR /work
COPY get_packages.sh .
RUN ./get_packages.sh
COPY build_dependencies.sh .
RUN ./build_dependencies.sh
COPY build_toxcore.sh .
ENV ENABLE_TEST=false \
ALLOW_TEST_FAILURE=false \
ENABLE_ARCH_i686=true \
ENABLE_ARCH_x86_64=true \
EXTRA_CMAKE_FLAGS="-DTEST_TIMEOUT_SECONDS=90"
ENTRYPOINT ["bash", "./build_toxcore.sh"]

View File

@ -0,0 +1,97 @@
#!/usr/bin/env bash
# disable on Cygwin otherwise some builds fail
if [ "$CROSS_COMPILE" = "true" ]; then
set -e -x
fi
#=== Cross-Compile Dependencies ===
build() {
ARCH=${1}
echo "Building for $ARCH architecture"
# set some things
WINDOWS_TOOLCHAIN=$ARCH-w64-mingw32
# prefix that we will copy to the user
PREFIX_DIR="/root/prefix/$ARCH"
rm -rf "$PREFIX_DIR"
mkdir -p "$PREFIX_DIR"
export MAKEFLAGS=j"$(nproc)"
export CFLAGS=-O3
CURL_OPTIONS=(-L --connect-timeout 10)
cd /tmp
rm -rf /tmp/*
echo "
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_C_COMPILER $WINDOWS_TOOLCHAIN-gcc)
SET(CMAKE_CXX_COMPILER $WINDOWS_TOOLCHAIN-g++)
SET(CMAKE_RC_COMPILER $WINDOWS_TOOLCHAIN-windres)
SET(CMAKE_FIND_ROOT_PATH /usr/$WINDOWS_TOOLCHAIN $DEP_PREFIX_DIR)
" >windows_toolchain.cmake
echo
echo "=== Building Sodium $VERSION_SODIUM $ARCH ==="
curl "${CURL_OPTIONS[@]}" -O "https://download.libsodium.org/libsodium/releases/libsodium-$VERSION_SODIUM.tar.gz"
tar -xf "libsodium-$VERSION_SODIUM.tar.gz"
cd "libsodium-$VERSION_SODIUM"
./configure --host="$WINDOWS_TOOLCHAIN" --prefix="$PREFIX_DIR" --disable-shared --enable-static
make
make install
cd ..
echo
echo "=== Building Opus $VERSION_OPUS $ARCH ==="
curl "${CURL_OPTIONS[@]}" -O "https://archive.mozilla.org/pub/opus/opus-$VERSION_OPUS.tar.gz"
tar -xf "opus-$VERSION_OPUS.tar.gz"
cd "opus-$VERSION_OPUS"
./configure --host="$WINDOWS_TOOLCHAIN" --prefix="$PREFIX_DIR" --disable-extra-programs --disable-doc --disable-shared --enable-static
make
make install
cd ..
echo
echo "=== Building VPX $VERSION_VPX $ARCH ==="
LIB_VPX_TARGET=""
if [ "$ARCH" = "i686" ]; then
LIB_VPX_TARGET=x86-win32-gcc
LIB_VPX_CFLAGS=""
else
LIB_VPX_TARGET=x86_64-win64-gcc
# There is a bug in gcc that breaks avx512 on 64-bit Windows https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
# VPX fails to build due to it.
# This is a workaround as suggested in https://stackoverflow.com/questions/43152633
LIB_VPX_CFLAGS="-fno-asynchronous-unwind-tables"
fi
curl "${CURL_OPTIONS[@]}" "https://github.com/webmproject/libvpx/archive/v$VERSION_VPX.tar.gz" -o "libvpx-$VERSION_VPX.tar.gz"
tar -xf "libvpx-$VERSION_VPX.tar.gz"
cd "libvpx-$VERSION_VPX"
CFLAGS="$LIB_VPX_CFLAGS" CROSS="$WINDOWS_TOOLCHAIN"- ./configure --target="$LIB_VPX_TARGET" --prefix="$PREFIX_DIR" --disable-examples --disable-unit-tests --disable-shared --enable-static
make
make install
cd ..
rm -rf /tmp/*
}
if [ "$SUPPORT_ARCH_i686" = "true" ]; then
build i686
fi
if [ "$SUPPORT_ARCH_x86_64" = "true" ]; then
build x86_64
fi
tree /root
echo
echo "Built dependencies successfully!"
echo

View File

@ -0,0 +1,189 @@
#!/usr/bin/env bash
set -e -x
#=== Cross-Compile Toxcore ===
build() {
ARCH=${1}
echo "Building for $ARCH architecture"
# set some things
WINDOWS_TOOLCHAIN=$ARCH-w64-mingw32
# toxcore dependencies that we will copy to the user for static build of toxcore (e.g. vpx, opus, sodium)
DEP_PREFIX_DIR="/root/prefix/$ARCH"
# where to put the result of this particular build
RESULT_PREFIX_DIR="/prefix/$ARCH"
rm -rf "$RESULT_PREFIX_DIR"
mkdir -p "$RESULT_PREFIX_DIR"
rm -rf /tmp/*
# where to install static/shared toxcores before deciding whether they should be copied over to the user
STATIC_TOXCORE_PREFIX_DIR="/tmp/static_prefix"
SHARED_TOXCORE_PREFIX_DIR="/tmp/shared_prefix"
mkdir -p "$STATIC_TOXCORE_PREFIX_DIR" "$SHARED_TOXCORE_PREFIX_DIR"
export MAKEFLAGS=j"$(nproc)"
export CFLAGS=-O3
echo
echo "=== Building toxcore $ARCH ==="
export PKG_CONFIG_PATH="$DEP_PREFIX_DIR/lib/pkgconfig"
if [ "$CROSS_COMPILE" = "true" ]; then
TOXCORE_DIR="/toxcore"
else
# get Toxcore root
cd "$(cd "$(dirname -- "$0")" >/dev/null 2>&1 && pwd)"
cd ../../../
TOXCORE_DIR="$PWD"
fi
cp -a "$TOXCORE_DIR" /tmp/toxcore
cd /tmp/toxcore/build
echo "
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_C_COMPILER $WINDOWS_TOOLCHAIN-gcc)
SET(CMAKE_CXX_COMPILER $WINDOWS_TOOLCHAIN-g++)
SET(CMAKE_RC_COMPILER $WINDOWS_TOOLCHAIN-windres)
SET(CMAKE_FIND_ROOT_PATH /usr/$WINDOWS_TOOLCHAIN $DEP_PREFIX_DIR)
" >windows_toolchain.cmake
if [ "$ENABLE_TEST" = "true" ]; then
echo "SET(CROSSCOMPILING_EMULATOR /usr/bin/wine)" >>windows_toolchain.cmake
fi
# Silly way to bypass a shellharden check
read -ra EXTRA_CMAKE_FLAGS_ARRAY <<<"$EXTRA_CMAKE_FLAGS"
cmake -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake \
-DCMAKE_INSTALL_PREFIX="$STATIC_TOXCORE_PREFIX_DIR" \
-DENABLE_SHARED=OFF \
-DENABLE_STATIC=ON \
-DCMAKE_C_FLAGS="$CMAKE_C_FLAGS" \
-DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="$CMAKE_EXE_LINKER_FLAGS -fstack-protector" \
-DCMAKE_SHARED_LINKER_FLAGS="$CMAKE_SHARED_LINKER_FLAGS" \
"${EXTRA_CMAKE_FLAGS_ARRAY[@]}" \
-S ..
cmake --build . --target install -- -j"$(nproc)"
if [ "$ENABLE_TEST" = "true" ]; then
rm -rf /root/.wine
# setup wine
if [ "$ARCH" = "i686" ]; then
export WINEARCH=win32
else
export WINEARCH=win64
fi
winecfg
export CTEST_OUTPUT_ON_FAILURE=1
# add libgcc_s_sjlj-1.dll libwinpthread-1.dll into PATH env var of wine
export WINEPATH="$(
cd /usr/lib/gcc/"$WINDOWS_TOOLCHAIN"/*posix/
winepath -w "$PWD"
)"\;"$(winepath -w /usr/"$WINDOWS_TOOLCHAIN"/lib/)"
if [ "$ALLOW_TEST_FAILURE" = "true" ]; then
set +e
fi
cmake --build . --target test -- ARGS="-j50"
if [ "$ALLOW_TEST_FAILURE" = "true" ]; then
set -e
fi
fi
# move static dependencies
cp -a "$STATIC_TOXCORE_PREFIX_DIR"/* "$RESULT_PREFIX_DIR"
cp -a "$DEP_PREFIX_DIR"/* "$RESULT_PREFIX_DIR"
# make libtox.dll
cd "$SHARED_TOXCORE_PREFIX_DIR"
for archive in "$STATIC_TOXCORE_PREFIX_DIR"/lib/libtox*.a; do
"$WINDOWS_TOOLCHAIN"-ar xv "$archive"
done
if [ "$CROSS_COMPILE" = "true" ]; then
LIBWINPTHREAD="/usr/$WINDOWS_TOOLCHAIN/lib/libwinpthread.a"
else
LIBWINPTHREAD="/usr/$WINDOWS_TOOLCHAIN/sys-root/mingw/lib/libwinpthread.a"
fi
"$WINDOWS_TOOLCHAIN"-gcc -Wl,--export-all-symbols \
-Wl,--out-implib=libtox.dll.a \
-shared \
-o libtox.dll \
*.obj \
"$STATIC_TOXCORE_PREFIX_DIR"/lib/*.a \
"$DEP_PREFIX_DIR"/lib/*.a \
"$LIBWINPTHREAD" \
-liphlpapi \
-lws2_32 \
-static-libgcc \
-lssp
cp libtox.dll.a "$RESULT_PREFIX_DIR"/lib
mkdir -p "$RESULT_PREFIX_DIR"/bin
cp libtox.dll "$RESULT_PREFIX_DIR"/bin
rm -rf /tmp/*
# remove everything from include directory except tox headers
mv "$RESULT_PREFIX_DIR"/include/tox "$RESULT_PREFIX_DIR"/tox
rm -rf "$RESULT_PREFIX_DIR"/include/*
mv "$RESULT_PREFIX_DIR"/tox "$RESULT_PREFIX_DIR"/include/tox
sed -i "s|^prefix=.*|prefix=$RESULT_PREFIX_DIR|g" "$RESULT_PREFIX_DIR"/lib/pkgconfig/*.pc
sed -i "s|^libdir=.*|libdir=$RESULT_PREFIX_DIR/lib|g" "$RESULT_PREFIX_DIR"/lib/*.la
}
#=== Test Supported vs. Enabled ===
if [ "$ENABLE_ARCH_i686" != "true" ] && [ "$ENABLE_ARCH_x86_64" != "true" ]; then
echo "Error: No architecture specified. Set either ENABLE_ARCH_i686 or ENABLE_ARCH_x86_64 or both."
exit 1
fi
if [ "$ENABLE_ARCH_i686" = "true" ] && [ "$SUPPORT_ARCH_i686" != "true" ]; then
echo "Error: Can't build for i686 architecture because the image was created without SUPPORT_ARCH_i686 set"
exit 1
fi
if [ "$ENABLE_ARCH_x86_64" = "true" ] && [ "$SUPPORT_ARCH_x86_64" != "true" ]; then
echo "Error: Can't build for x86_64 architecture because the image was created without SUPPORT_ARCH_x86_64 set"
exit 1
fi
if [ "$ENABLE_TEST" = "true" ] && [ "$SUPPORT_TEST" != "true" ]; then
echo "Error: Can't build with tests because the image was created without SUPPORT_TEST set"
exit 1
fi
#=== Build ===
if [ "$ENABLE_ARCH_i686" = "true" ]; then
build i686
fi
if [ "$ENABLE_ARCH_x86_64" = "true" ]; then
build x86_64
fi
tree -h /prefix
echo
echo "Built toxcore successfully!"
echo
# since we are building as root
if [ "$CROSS_COMPILE" = "true" ]; then
chmod 777 /prefix -R
fi

View File

@ -0,0 +1,54 @@
#!/usr/bin/env sh
set -e -x
#=== Install Packages ===
apt-get update
# Arch-independent packages required for building toxcore's dependencies and toxcore itself
apt-get install -y \
autoconf \
automake \
ca-certificates \
cmake \
curl \
libtool \
libc-dev \
make \
pkg-config \
tree \
yasm
# Arch-dependent packages required for building toxcore's dependencies and toxcore itself
if [ "$SUPPORT_ARCH_i686" = "true" ]; then
apt-get install -y \
g++-mingw-w64-i686 \
gcc-mingw-w64-i686
fi
if [ "$SUPPORT_ARCH_x86_64" = "true" ]; then
apt-get install -y \
g++-mingw-w64-x86-64 \
gcc-mingw-w64-x86-64
fi
# Packages needed for running toxcore tests
if [ "$SUPPORT_TEST" = "true" ]; then
apt-get install -y \
texinfo
dpkg --add-architecture i386
apt-get update
apt-get install -y \
wine \
wine32 \
wine64
fi
# Clean up to reduce image size
apt-get clean
rm -rf \
/var/lib/apt/lists/* \
/tmp/* \
/var/tmp/*

View File

@ -0,0 +1,84 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND="noninteractive"
# Install dependencies.
RUN apt-get update && apt-get install --no-install-recommends -y \
autoconf \
automake \
ca-certificates \
cmake \
curl \
git \
libtool \
make \
ninja-build \
pkg-config \
python3 \
unzip \
wget \
xz-utils \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /work/emsdk
RUN git clone --depth=1 https://github.com/emscripten-core/emsdk /work/emsdk \
&& ./emsdk install 3.1.3 \
&& ./emsdk activate 3.1.3
# Build libsodium.
RUN . "/work/emsdk/emsdk_env.sh" \
&& git clone --depth=1 --branch=1.0.18 https://github.com/jedisct1/libsodium /work/libsodium \
&& cd /work/libsodium \
&& autoreconf -fi \
&& emconfigure ./configure --disable-shared \
--without-pthreads \
--disable-ssp --disable-asm --disable-pie \
&& emmake make install -j8
# Build an unused libsodium binding first so emcc caches all the system
# libraries. This makes rebuilds of toxcore below much faster.
RUN . "/work/emsdk/emsdk_env.sh" \
&& mkdir -p /work/wasm \
&& emcc -O3 -flto \
--closure=1 \
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
-s EXPORT_NAME=libtoxcore \
-s IGNORE_MISSING_MAIN=1 \
-s MAIN_MODULE=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s STRICT=1 \
-s WEBSOCKET_URL=wss:// \
/usr/local/lib/libsodium.a \
-o /work/wasm/libsodium.js
# Build c-toxcore.
COPY . /work/c-toxcore
RUN . "/work/emsdk/emsdk_env.sh" \
&& cd /work/c-toxcore \
&& emcmake cmake -B_build -H. -GNinja \
-DCMAKE_INSTALL_PREFIX:PATH="/usr/local" \
-DCMAKE_C_FLAGS="-O3 -flto -fPIC" \
-DBUILD_TOXAV=OFF \
-DENABLE_SHARED=OFF \
-DBOOTSTRAP_DAEMON=OFF \
-DMIN_LOGGER_LEVEL=DEBUG \
&& emmake cmake --build _build --parallel 8 --target install
# Build wasm bindings.
RUN . "/work/emsdk/emsdk_env.sh" \
&& mkdir -p /work/wasm \
&& emcc -O3 -flto \
--closure=1 \
-s ALLOW_UNIMPLEMENTED_SYSCALLS=1 \
-s EXPORT_NAME=libtoxcore \
-s IGNORE_MISSING_MAIN=1 \
-s MAIN_MODULE=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s STRICT=1 \
-s WEBSOCKET_URL=wss:// \
/usr/local/lib/libsodium.a \
/usr/local/lib/libtoxcore.a \
-o /work/wasm/libtoxcore.js

View File

@ -0,0 +1,3 @@
#!/bin/sh
docker build -t toxchat/toxcore-js -f other/emscripten/Dockerfile .

View File

@ -0,0 +1 @@
out/

View File

@ -0,0 +1,779 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2023 The TokTok team.
// this file can be used to generate event.c files
// requires c++17
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <variant>
#include <map>
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
return s;
}
std::string str_toupper(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::toupper(c); });
return s;
}
std::string output_folder = "out";
struct EventTypeTrivial {
std::string type; // eg uint32_t
std::string name; // eg friend_number
};
struct EventTypeByteRange {
// type is always uint8_t * for data AND uint32_t for length
std::string name_data; // eg data
std::string name_length; // eg data_length
std::string name_length_cb; // eg length // TODO: merge with normal?
};
// TODO: EventTypeByteArray
using EventType = std::variant<EventTypeTrivial, EventTypeByteRange>;
// helper type for the visitor #4
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
// explicit deduction guide (not needed as of C++20)
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
std::string bin_pack_name_from_type(const std::string& type) {
if (type == "uint64_t") {
return "bin_pack_u64";
} else if (type == "uint32_t") {
return "bin_pack_u32";
} else if (type == "uint16_t") {
return "bin_pack_u16";
} else if (type == "uint8_t") {
return "bin_pack_u8";
} else if (type == "bool") {
return "bin_pack_bool";
// only unpack is special TODO(Green-Sky): should we change that?
//} else if (type == "Tox_User_Status") {
//return "tox_pack_user_status";
//} else if (type == "Tox_Conference_Type") {
//return "tox_pack_conference_type";
} else {
//std::cerr << "unknown type " << type << "\n";
//exit(1);
// assume enum -> u32
return "bin_pack_u32";
}
}
std::string bin_unpack_name_from_type(const std::string& type) {
if (type == "uint64_t") {
return "bin_unpack_u64";
} else if (type == "uint32_t") {
return "bin_unpack_u32";
} else if (type == "uint16_t") {
return "bin_unpack_u16";
} else if (type == "uint8_t") {
return "bin_unpack_u8";
} else if (type == "bool") {
return "bin_unpack_bool";
} else if (type == "Tox_User_Status") {
return "tox_unpack_user_status";
} else if (type == "Tox_Conference_Type") {
return "tox_unpack_conference_type";
} else if (type == "Tox_Message_Type") {
return "tox_unpack_message_type";
} else if (type == "Tox_File_Control") {
return "tox_unpack_file_control";
} else if (type == "Tox_Connection") {
return "tox_unpack_connection";
} else if (type == "Tox_Group_Privacy_State") {
return "tox_unpack_group_privacy_state";
} else if (type == "Tox_Group_Voice_State") {
return "tox_unpack_group_voice_state";
} else if (type == "Tox_Group_Topic_Lock") {
return "tox_unpack_group_topic_lock";
} else if (type == "Tox_Group_Join_Fail") {
return "tox_unpack_group_join_fail";
} else if (type == "Tox_Group_Mod_Event") {
return "tox_unpack_group_mod_event";
} else if (type == "Tox_Group_Exit_Type") {
return "tox_unpack_group_exit_type";
} else {
std::cerr << "unknown type " << type << "\n";
exit(1);
// assume enum -> u32
return "bin_unpack_u32";
}
}
void generate_event_impl(const std::string& event_name, std::vector<EventType> event_types) {
const std::string event_name_l = str_tolower(event_name);
std::string file_name = output_folder + "/" + event_name_l + ".c";
std::ofstream f(file_name);
if (!f.good()) {
std::cerr << "error opening file to write " << file_name << "\n";
exit(1);
}
f << R"(/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************
*
* :: struct and accessors
*
*****************************************************/
)";
// gen struct
f << "struct Tox_Event_" << event_name << " {\n";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " " << t.type << " " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << " " << "uint8_t" << " *" << t.name_data << ";\n";
f << " " << "uint32_t" << " " << t.name_length << ";\n";
}
},
t
);
}
f << "};\n\n";
// gen contruct
f << "non_null()\n";
f << "static void tox_event_" << event_name_l << "_construct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n";
// TODO: initialize all members properly
// TODO: check if _NONE is universal
// str_toupper(
f << " *" << event_name_l << " = (Tox_Event_" << event_name << ") {\n 0\n };\n}\n";
// gen destruct
f << "non_null()\n";
f << "static void tox_event_" << event_name_l << "_destruct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n";
size_t data_count = 0;
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial&) {},
[&](const EventTypeByteRange& t) {
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
data_count++;
}
},
t
);
}
if (data_count == 0) {
f << " return;\n";
}
f << "}\n\n";
// gen setters and getters
for (const auto& t : event_types) {
// setter
f << "non_null()\n";
f << "static " << (t.index() == 0 ? "void" : "bool") << " tox_event_" << event_name_l << "_set_";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << t.name;
},
[&](const EventTypeByteRange& t) {
f << t.name_data;
}
},
t
);
f << "(Tox_Event_" << event_name << " *" << event_name_l << ",\n";
f << " ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << t.type << " " << t.name << ")\n";
},
[&](const EventTypeByteRange& t) {
f << "const uint8_t *" << t.name_data << ", uint32_t " << t.name_length << ")\n";
}
},
t
);
f << "{\n assert(" << event_name_l << " != nullptr);\n";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " " << event_name_l << "->" << t.name << " = " << t.name << ";\n";
},
[&](const EventTypeByteRange& t) {
f << "\n if (" << event_name_l << "->" << t.name_data << " != nullptr) {\n";
f << " free(" << event_name_l << "->" << t.name_data << ");\n";
f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n";
f << " " << event_name_l << "->" << t.name_length << " = 0;\n";
f << " }\n\n";
f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n";
f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n";
f << " return false;\n }\n\n";
f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n";
f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n";
f << " return true;\n";
}
},
t
);
f << "}\n";
// getter
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
//f << "non_null()\n"; // TODO: is this missing in the original?
f << t.type << " tox_event_" << event_name_l << "_get_" << t.name;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name << ";\n}\n\n";
},
[&](const EventTypeByteRange& t) {
//f << "non_null()\n"; // TODO: is this missing in the original?
f << "size_t tox_event_" << event_name_l << "_get_" << t.name_length;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_length << ";\n}\n";
f << "const uint8_t *tox_event_" << event_name_l << "_get_" << t.name_data;
f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n";
f << "{\n assert(" << event_name_l << " != nullptr);\n";
f << " return " << event_name_l << "->" << t.name_data << ";\n}\n\n";
}
},
t
);
}
// pack
f << "non_null()\n";
f << "static bool tox_event_" << event_name_l << "_pack(\n";
f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n";
f << " assert(event != nullptr);\n";
f << " return bin_pack_array(bp, 2)\n";
f << " && bin_pack_u32(bp, TOX_EVENT_" << str_toupper(event_name) << ")\n";
if (event_types.size() > 1) {
f << " && bin_pack_array(bp, " << event_types.size() << ")";
}
for (const auto& t : event_types) {
f << "\n && ";
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_pack_name_from_type(t.type);
f << "(bp, event->" << t.name << ")";
},
[&](const EventTypeByteRange& t) {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
}
},
t
);
}
f << ";\n}\n\n";
// unpack
f << "non_null()\n";
f << "static bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n";
f << " assert(event != nullptr);\n";
if (event_types.size() > 1) {
f << " if (!bin_unpack_array_fixed(bu, " << event_types.size() << ")) {\n return false;\n }\n\n";
}
bool first = true;
for (const auto& t : event_types) {
if (first) {
f << " return ";
} else {
f << "\n && ";
}
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_unpack_name_from_type(t.type);
f << "(bu, &event->" << t.name << ")";
},
[&](const EventTypeByteRange& t) {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
}
},
t
);
first = false;
}
f << ";\n}\n\n";
f << R"(
/*****************************************************
*
* :: add/clear/get
*
*****************************************************/
)";
// add
f << "non_null()\n";
f << "static Tox_Event_" << event_name << " *tox_events_add_" << event_name_l << "(Tox_Events *events)\n{\n";
f << " if (events->" << event_name_l << "_size == UINT32_MAX) {\n";
f << " return nullptr;\n }\n\n";
f << " if (events->" << event_name_l << "_size == events->" << event_name_l << "_capacity) {\n";
f << " const uint32_t new_" << event_name_l << "_capacity = events->" << event_name_l << "_capacity * 2 + 1;\n";
f << " Tox_Event_" << event_name << " *new_" << event_name_l << " = (Tox_Event_" << event_name << " *)\n";
f << " realloc(\n";
f << " events->" << event_name_l << ",\n";
f << " new_" << event_name_l << "_capacity * sizeof(Tox_Event_" << event_name << "));\n\n";
f << " if (new_" << event_name_l << " == nullptr) {\n return nullptr;\n }\n\n";
f << " events->" << event_name_l << " = new_" << event_name_l << ";\n";
f << " events->" << event_name_l << "_capacity = new_" << event_name_l << "_capacity;\n";
f << " }\n\n";
f << " Tox_Event_" << event_name << " *const " << event_name_l << " =\n";
f << " &events->" << event_name_l << "[events->" << event_name_l << "_size];\n";
f << " tox_event_" << event_name_l << "_construct(" << event_name_l << ");\n";
f << " ++events->" << event_name_l << "_size;\n";
f << " return " << event_name_l << ";\n}\n\n";
// clear
f << "void tox_events_clear_" << event_name_l << "(Tox_Events *events)\n{\n";
f << " if (events == nullptr) {\n return;\n }\n\n";
f << " for (uint32_t i = 0; i < events->" << event_name_l << "_size; ++i) {\n";
f << " tox_event_" << event_name_l << "_destruct(&events->" << event_name_l << "[i]);\n }\n\n";
f << " free(events->" << event_name_l << ");\n";
f << " events->" << event_name_l << " = nullptr;\n";
f << " events->" << event_name_l << "_size = 0;\n";
f << " events->" << event_name_l << "_capacity = 0;\n";
f << "}\n\n";
// get size
f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n";
f << " if (events == nullptr) {\n return 0;\n }\n\n";
f << " return events->" << event_name_l << "_size;\n}\n\n";
// get
f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n";
f << " assert(index < events->" << event_name_l << "_size);\n";
f << " assert(events->" << event_name_l << " != nullptr);\n";
f << " return &events->" << event_name_l << "[index];\n}\n\n";
// aux pack
f << "bool tox_events_pack_" << event_name_l << "(const Tox_Events *events, Bin_Pack *bp)\n{\n";
f << " const uint32_t size = tox_events_get_" << event_name_l << "_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (!tox_event_" << event_name_l << "_pack(tox_events_get_" << event_name_l << "(events, i), bp)) {\n";
f << " return false;\n }\n }\n";
f << " return true;\n}\n\n";
// aux unpack
f << "bool tox_events_unpack_" << event_name_l << "(Tox_Events *events, Bin_Unpack *bu)\n{\n";
f << " Tox_Event_" << event_name << " *event = tox_events_add_" << event_name_l << "(events);\n\n";
f << " if (event == nullptr) {\n return false;\n }\n\n";
f << " return tox_event_" << event_name_l << "_unpack(event, bu);\n}\n\n";
f << R"(
/*****************************************************
*
* :: event handler
*
*****************************************************/
)";
f << "void tox_events_handle_" << event_name_l << "(Tox *tox";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << ", " << t.type << " " << t.name;
},
[&](const EventTypeByteRange& t) {
f << ", const uint8_t *" << t.name_data << ", size_t " << t.name_length_cb;
}
},
t
);
}
f << ",\n void *user_data)\n{\n";
f << " Tox_Events_State *state = tox_events_alloc(user_data);\n";
f << " assert(state != nullptr);\n\n";
f << " if (state->events == nullptr) {\n return;\n }\n\n";
f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_events_add_" << event_name_l << "(state->events);\n\n";
f << " if (" << event_name_l << " == nullptr) {\n";
f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n return;\n }\n\n";
for (const auto& t : event_types) {
std::visit(
overloaded{
[&](const EventTypeTrivial& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name << "(" << event_name_l << ", " << t.name << ");\n";
},
[&](const EventTypeByteRange& t) {
f << " tox_event_" << event_name_l << "_set_" << t.name_data << "(" << event_name_l << ", " << t.name_data << ", " << t.name_length_cb << ");\n";
}
},
t
);
}
f << "}\n";
}
// c++ generate_event_c.cpp -std=c++17 && ./a.out Friend_Lossy_Packet && diff --color ../../toxcore/events/friend_lossy_packet.c out/friend_lossy_packet.c
int main(int argc, char** argv) {
// TODO: json?
std::map<std::string, std::vector<EventType>> event_defs {
{
"Conference_Connected",
{
EventTypeTrivial{"uint32_t", "conference_number"},
}
},
{
"Conference_Invite",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Conference_Type", "type"},
EventTypeByteRange{"cookie", "cookie_length", "length"}, // the later two are idealy the same
}
},
{
"Conference_Message",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
}
},
{
"Conference_Peer_List_Changed",
{
EventTypeTrivial{"uint32_t", "conference_number"},
}
},
{
"Conference_Peer_Name",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same
}
},
{
"Conference_Title",
{
EventTypeTrivial{"uint32_t", "conference_number"},
EventTypeTrivial{"uint32_t", "peer_number"},
EventTypeByteRange{"title", "title_length", "length"}, // the later two are idealy the same
}
},
{
"File_Chunk_Request",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint64_t", "position"},
EventTypeTrivial{"uint16_t", "length"},
}
},
{
"File_Recv",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint32_t", "kind"},
EventTypeTrivial{"uint64_t", "file_size"},
EventTypeByteRange{"filename", "filename_length", "length"}, // the later two are idealy the same
}
},
{
"File_Recv_Chunk",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"uint64_t", "position"},
EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same
}
},
{
"File_Recv_Control",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "file_number"},
EventTypeTrivial{"Tox_File_Control", "control"},
}
},
{
"Friend_Connection_Status",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Connection", "connection_status"},
}
},
{
"Friend_Lossless_Packet",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same
}
},
{
"Friend_Lossy_Packet",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same
}
},
{
"Friend_Message",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
}
},
{
"Friend_Name",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same
}
},
{
"Friend_Read_Receipt",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"uint32_t", "message_id"},
}
},
#if 0
{
"Friend_Request",
{
//EventTypeTrivial{"uint32_t", "friend_number"}, // public_key ByteArray
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
}
},
#endif
{
"Friend_Status",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"Tox_User_Status", "status"},
}
},
{
"Friend_Status_Message",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
}
},
{
"Friend_Typing",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeTrivial{"bool", "typing"},
}
},
{
"Self_Connection_Status",
{
EventTypeTrivial{"Tox_Connection", "connection_status"},
}
},
{
"Group_Peer_Name",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Peer_Status",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_User_Status", "status"},
}
},
{
"Group_Topic",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"topic", "topic_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Privacy_State",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Privacy_State", "privacy_state"},
}
},
{
"Group_Voice_State",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Voice_State", "voice_state"},
}
},
{
"Group_Topic_Lock",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Topic_Lock", "topic_lock"},
}
},
{
"Group_Peer_Limit",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_limit"},
}
},
{
"Group_Password",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeByteRange{"password", "password_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Message",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
EventTypeTrivial{"uint32_t", "message_id"},
}
},
{
"Group_Private_Message",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Message_Type", "type"},
EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Custom_Packet",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Custom_Private_Packet",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same
}
},
{
"Group_Invite",
{
EventTypeTrivial{"uint32_t", "friend_number"},
EventTypeByteRange{"invite_data", "invite_data_length", "length"}, // the later two are idealy the same
EventTypeByteRange{"group_name", "group_name_length", "group_name_length"}, // they are :)
}
},
{
"Group_Peer_Join",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
}
},
{
"Group_Peer_Exit",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "peer_id"},
EventTypeTrivial{"Tox_Group_Exit_Type", "exit_type"},
EventTypeByteRange{"name", "name_length", "name_length"}, // they are :)
EventTypeByteRange{"part_message", "part_message_length", "part_message_length"}, // they are :)
}
},
{
"Group_Self_Join",
{
EventTypeTrivial{"uint32_t", "group_number"},
}
},
{
"Group_Join_Fail",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"Tox_Group_Join_Fail", "fail_type"},
}
},
{
"Group_Moderation",
{
EventTypeTrivial{"uint32_t", "group_number"},
EventTypeTrivial{"uint32_t", "source_peer_id"},
EventTypeTrivial{"uint32_t", "target_peer_id"},
EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"},
}
},
};
if (argc < 2) {
for (const auto& [event, event_types] : event_defs) {
generate_event_impl(event, event_types);
}
} else {
if (event_defs.count(argv[1])) {
generate_event_impl(argv[1], event_defs[argv[1]]);
} else {
std::cerr << "error: unknown event " << argv[1] << "\n";
return 1;
}
}
return 0;
}

View File

@ -0,0 +1,101 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
package(features = ["layering_check"])
#cc_binary(
# name = "cracker",
# testonly = 1,
# srcs = ["cracker.c"],
# copts = ["-fopenmp"],
# linkopts = ["-fopenmp"],
# deps = [
# "//c-toxcore/toxcore:ccompat",
# "@libsodium",
# ],
#)
cc_binary(
name = "cracker_simple",
testonly = 1,
srcs = ["cracker_simple.c"],
deps = [
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:ccompat",
"@libsodium",
],
)
cc_binary(
name = "create_minimal_savedata",
testonly = 1,
srcs = [
"create_common.h",
"create_minimal_savedata.c",
],
deps = [
"//c-toxcore/toxcore:ccompat",
"@libsodium",
],
)
cc_binary(
name = "create_savedata",
testonly = 1,
srcs = [
"create_common.h",
"create_savedata.c",
],
deps = [
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:tox",
"@libsodium",
],
)
cc_binary(
name = "create_bootstrap_keys",
testonly = 1,
srcs = [
"create_bootstrap_keys.c",
"create_common.h",
],
deps = [
"//c-toxcore/toxcore:ccompat",
"@libsodium",
],
)
cc_binary(
name = "sign",
testonly = 1,
srcs = ["sign.c"],
copts = ["-w"],
deps = [
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:ccompat",
"@libsodium",
],
)
cc_binary(
name = "strkey",
testonly = 1,
srcs = ["strkey.c"],
copts = ["-w"],
deps = [
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:tox",
"@libsodium",
],
)
cc_binary(
name = "save-generator",
testonly = 1,
srcs = ["save-generator.c"],
deps = [
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:tox",
],
)

View File

@ -0,0 +1,30 @@
add_executable(save-generator save-generator.c)
target_link_modules(save-generator toxcore misc_tools)
add_executable(strkey strkey.c)
target_link_modules(strkey toxcore ${LIBSODIUM_LIBRARIES})
add_executable(create_bootstrap_keys create_bootstrap_keys.c)
target_link_modules(create_bootstrap_keys ${LIBSODIUM_LIBRARIES})
add_executable(create_minimal_savedata create_minimal_savedata.c)
target_link_modules(create_minimal_savedata ${LIBSODIUM_LIBRARIES})
add_executable(create_savedata create_savedata.c)
target_link_modules(create_savedata toxcore ${LIBSODIUM_LIBRARIES})
add_executable(sign sign.c)
target_link_modules(sign ${LIBSODIUM_LIBRARIES} misc_tools)
add_executable(cracker_simple cracker_simple.c)
target_link_modules(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools)
# MSVC doesn't support OpenMP
if(NOT MSVC)
find_package(OpenMP)
if(OpenMP_C_FOUND)
add_executable(cracker cracker.c)
target_link_modules(cracker ${LIBSODIUM_LIBRARIES})
target_link_libraries(cracker OpenMP::OpenMP_C)
endif()
endif()

View File

@ -0,0 +1,122 @@
#!/usr/bin/env python3
"""
Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import socket
import sys
if sys.version_info[0] == 2:
print("This script requires Python 3+ in order to run.")
sys.exit(1)
def print_help(prog: str) -> None:
"""Print program usage to stdout."""
print(f"Usage: {prog} <ipv4|ipv6> <ip/hostname> <port>")
print(f" Example: {prog} ipv4 192.210.149.121 33445")
print(f" Example: {prog} ipv4 23.226.230.47 33445")
print(f" Example: {prog} ipv4 node.tox.biribiri.org 33445")
print(f" Example: {prog} ipv4 cerberus.zodiaclabs.org 33445")
print(f" Example: {prog} ipv6 2604:180:1::3ded:b280 33445")
print("")
print("Return values:")
print(" 0 - received info reply from a node")
print(" 1 - incorrect command line arguments")
print(" 2 - didn't receive any reply from a node")
print(" 3 - received a malformed/unexpected reply")
# https://github.com/irungentoo/toxcore/blob/4940c4c62b6014d1f0586aa6aca7bf6e4ecfcf29/toxcore/network.h#L128
INFO_PACKET_ID = b"\xF0"
# https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L28
INFO_REQUEST_PACKET_LENGTH = 78
# first byte is INFO_REQUEST_ID, other bytes don't matter as long as reqest's
# length matches INFO_REQUEST_LENGTH
INFO_REQUEST_PACKET = INFO_PACKET_ID + (
b"0" * (INFO_REQUEST_PACKET_LENGTH - len(INFO_PACKET_ID)))
PACKET_ID_LENGTH = len(INFO_PACKET_ID)
# https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L44
VERSION_LENGTH = 4
# https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L26
MAX_MOTD_LENGTH = 256
MAX_INFO_RESPONSE_PACKET_LENGTH = PACKET_ID_LENGTH + VERSION_LENGTH + MAX_MOTD_LENGTH
SOCK_TIMEOUT_SECONDS = 1.0
def main(prog: str, protocol: str, host: str, port: int) -> None:
"""Call the bootstrap node info RPC and output the response."""
if protocol == "ipv4":
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif protocol == "ipv6":
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
else:
print("Invalid first argument")
print_help(prog)
sys.exit(1)
sock.sendto(INFO_REQUEST_PACKET, (host, port))
sock.settimeout(SOCK_TIMEOUT_SECONDS)
try:
data, _ = sock.recvfrom(MAX_INFO_RESPONSE_PACKET_LENGTH)
except socket.timeout:
print("The DHT bootstrap node didn't reply in " +
str(SOCK_TIMEOUT_SECONDS) + " sec.")
print("The likely reason for that is that the DHT bootstrap node "
"is either offline or has no info set.")
sys.exit(2)
packet_id = data[:PACKET_ID_LENGTH]
if packet_id != INFO_PACKET_ID:
print("Bad response, first byte should be {info_packet_id!r}"
" but got {packet_id!r}({data!r})".format(
info_packet_id=INFO_PACKET_ID,
packet_id=packet_id,
data=data,
))
print("Are you sure that you are pointing the script at a Tox "
"DHT bootstrap node and that the script is up to date?")
sys.exit(3)
version = int.from_bytes(data[PACKET_ID_LENGTH:PACKET_ID_LENGTH +
VERSION_LENGTH],
byteorder="big")
motd = data[PACKET_ID_LENGTH + VERSION_LENGTH:].decode("utf-8")
print("Version: " + str(version))
print("MOTD: " + motd)
sys.exit(0)
if __name__ == "__main__":
if len(sys.argv) != 4:
print_help(sys.argv[0])
sys.exit(1)
main(
prog=sys.argv[0],
protocol=sys.argv[1],
host=sys.argv[2],
port=int(sys.argv[3]),
)

View File

@ -0,0 +1,255 @@
/* Public key cracker.
*
* Can be used to find public keys starting with specific hex (ABCD) for example.
*
* NOTE: There's probably a way to make this faster.
*
* Usage: ./cracker ABCDEF
*
* Will try to find a public key starting with: ABCDEF
*/
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
/* Sodium includes*/
#include <sodium/crypto_scalarmult_curve25519.h>
#include <sodium/randombytes.h>
/* NULL compatibility macro */
#include "../../toxcore/ccompat.h"
#define KEY_LEN 32
// Maximum number of bytes this program can crack in one run
#define MAX_CRACK_BYTES 8
// Maximum length of hex encoded prefix
#define MAX_HEX_PREFIX_LEN (MAX_CRACK_BYTES * 2)
#if defined(_OPENMP)
#include <omp.h>
#define NUM_THREADS() ((unsigned) omp_get_max_threads())
#else
#define NUM_THREADS() (1U)
#endif
static void print_key(const uint8_t *client_id)
{
for (uint32_t j = 0; j < 32; ++j) {
printf("%02X", client_id[j]);
}
}
/// bytes needs to be at least (hex_len+1)/2 long
static size_t hex_string_to_bin(const char *hex_string, size_t hex_len, uint8_t *bytes)
{
size_t i;
const char *pos = hex_string;
// make even
for (i = 0; i < hex_len / 2; ++i, pos += 2) {
uint8_t val;
if (sscanf(pos, "%02hhx", &val) != 1) {
return 0;
}
bytes[i] = val;
}
if (i * 2 < hex_len) {
uint8_t val;
if (sscanf(pos, "%hhx", &val) != 1) {
return 0;
}
bytes[i] = (uint8_t)(val << 4);
++i;
}
return i;
}
static size_t match_hex_prefix(const uint8_t *key, const uint8_t *prefix, size_t prefix_len)
{
size_t same = 0;
uint8_t diff = 0;
size_t i;
for (i = 0; i < prefix_len / 2; ++i) {
diff = key[i] ^ prefix[i];
// First check high nibble
if ((diff & 0xF0) == 0) {
++same;
}
// Then low nibble
if (diff == 0) {
++same;
} else {
break;
}
}
// check last high nibble
if ((prefix_len % 2) && diff == 0) {
diff = key[i] ^ prefix[i];
// First check high nibble
if ((diff & 0xF0) == 0) {
++same;
}
}
return same;
}
static void cracker_core(uint64_t range_start, uint64_t range_end, uint64_t range_offs, uint64_t priv_key_shadow[4],
uint32_t *longest_match, uint8_t hex_prefix[MAX_CRACK_BYTES], size_t prefix_chars_len)
{
#pragma omp parallel for firstprivate(priv_key_shadow) shared(longest_match, range_start, range_end, range_offs, hex_prefix, prefix_chars_len) schedule(static) default(none)
for (uint64_t batch = range_start; batch < range_end; ++batch) {
uint8_t *priv_key = (uint8_t *) priv_key_shadow;
/*
* We can't use the first and last bytes because they are masked in
* curve25519. Offset by 16 bytes to get better alignment.
*/
uint64_t *counter = priv_key_shadow + 2;
/*
* Add to `counter` instead of assign here, to preservere more randomness on short runs
* There can be an intentional overflow in `batch + range_offs`
*/
*counter += batch + range_offs;
uint8_t pub_key[KEY_LEN] = {0};
crypto_scalarmult_curve25519_base(pub_key, priv_key);
const unsigned matching = (unsigned) match_hex_prefix(pub_key, hex_prefix, prefix_chars_len);
// Global compare and update
uint32_t l_longest_match;
#pragma omp atomic read
l_longest_match = *longest_match;
if (matching > l_longest_match) {
#pragma omp atomic write
*longest_match = matching;
#pragma omp critical
{
printf("%u chars matching: \n", matching);
printf("Public key: ");
print_key(pub_key);
printf("\nSecret key: ");
print_key(priv_key);
printf("\n");
}
}
}
}
static void print_stats(double seconds_passed, double keys_tried)
{
printf("Runtime: %10lus, Keys tried %e/%e, Calculating %e keys/s\n",
(unsigned long) seconds_passed, keys_tried, (double) UINT64_MAX, keys_tried / seconds_passed);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: ./cracker public_key(or beginning of one in hex format)\n");
return 0;
}
const size_t prefix_chars_len = strlen(argv[1]);
/*
* If you can afford the hardware to crack longer prefixes, you can probably
* afford to rewrite this program.
*/
if (prefix_chars_len > MAX_HEX_PREFIX_LEN) {
printf("Finding a key with more than 16 hex chars as prefix is not supported\n");
return 1;
}
uint8_t hex_prefix[MAX_CRACK_BYTES] = {0};
const size_t prefix_len = hex_string_to_bin(argv[1], prefix_chars_len, hex_prefix);
if (prefix_len == 0) {
printf("Invalid hex key specified\n");
return 1;
}
printf("Searching for key with prefix: %s\n", argv[1]);
time_t start_time = time(nullptr);
// Declare private key bytes as uint64_t[4] so we can lower the alignment without problems
uint64_t priv_key_shadow[KEY_LEN / 8];
uint8_t *priv_key = (uint8_t *) priv_key_shadow;
// Put randomness into the key
randombytes(priv_key, KEY_LEN);
uint32_t longest_match = 0;
// Finishes a batch every ~10s on my PC
const uint64_t batch_size = (UINT64_C(1) << 18) * NUM_THREADS();
// calculate remaining batch that doesn't fit the main loop
const uint64_t rem_batch_size = UINT64_MAX % batch_size;
const uint64_t rem_start = UINT64_MAX - rem_batch_size - 1;
cracker_core(rem_start, UINT64_MAX, 1, priv_key_shadow, &longest_match, hex_prefix, prefix_chars_len);
double seconds_passed = difftime(time(nullptr), start_time);
double old_seconds_passed = seconds_passed;
// Reduce time to first stats output
print_stats(seconds_passed, rem_batch_size + 1);
if (longest_match >= prefix_chars_len) {
printf("Found matching prefix, exiting...\n");
return 0;
}
for (uint64_t tries = 0; tries < rem_start; tries += batch_size) {
cracker_core(tries, tries + batch_size, 0, priv_key_shadow, &longest_match, hex_prefix, prefix_chars_len);
seconds_passed = difftime(time(nullptr), start_time);
// Use double type to avoid overflow in addition, we don't need precision here anyway
double keys_tried = ((double) tries) + rem_batch_size + 1;
if (longest_match >= prefix_chars_len) {
print_stats(seconds_passed, keys_tried);
printf("Found matching prefix, exiting...\n");
return 0;
}
// Rate limit output
if (seconds_passed - old_seconds_passed > 5.0) {
old_seconds_passed = seconds_passed;
print_stats(seconds_passed, keys_tried);
fflush(stdout);
}
}
printf("Congrats future person who successfully searched a key space of 2^64\n");
uint64_t *counter = priv_key_shadow + 2;
*counter = 0;
printf("Didn't find anything from:\n");
print_key(priv_key);
printf("\nto:\n");
*counter = UINT64_MAX;
print_key(priv_key);
printf("\n");
return 2;
}

View File

@ -0,0 +1,84 @@
/* Public key cracker.
*
* Can be used to find public keys starting with specific hex (ABCD) for example.
*
* NOTE: There's probably a way to make this faster.
*
* Usage: ./cracker ABCDEF
*
* Will try to find a public key starting with: ABCDEF
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Sodium includes*/
#include <sodium/crypto_scalarmult_curve25519.h>
#include <sodium/randombytes.h>
#include "../../testing/misc_tools.h"
#include "../../toxcore/ccompat.h"
// Secret key and public key length
#define KEY_LEN 32
static void print_key(const uint8_t *client_id)
{
for (int j = 0; j < KEY_LEN; ++j) {
printf("%02X", client_id[j]);
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: ./cracker public_key(or beginning of one in hex format)\n");
return 0;
}
long long unsigned int num_tries = 0;
size_t len = strlen(argv[1]) / 2;
unsigned char *key = hex_string_to_bin(argv[1]);
uint8_t pub_key[KEY_LEN], priv_key[KEY_LEN], c_key[KEY_LEN];
if (len > KEY_LEN) {
printf("%zu characters given, truncating to: %d\n", len * 2, KEY_LEN * 2);
len = KEY_LEN;
}
memcpy(c_key, key, len);
free(key);
randombytes(priv_key, KEY_LEN);
while (1) {
crypto_scalarmult_curve25519_base(pub_key, priv_key);
if (memcmp(c_key, pub_key, len) == 0) {
break;
}
/*
* We can't use the first and last bytes because they are masked in
* curve25519. Using them would generate duplicate keys.
*/
for (int i = (KEY_LEN - 1); i > 1; --i) {
priv_key[i - 1] += 1;
if (priv_key[i - 1] != 0) {
break;
}
}
++num_tries;
}
printf("Public key:\n");
print_key(pub_key);
printf("\nPrivate key:\n");
print_key(priv_key);
printf("\n %llu keys tried\n", num_tries);
return 0;
}

View File

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
*
* Creates bootstrap keys data for a given secret key, if provided, or a random key otherwise.
* The data is written to stderr, human-readable key info is written to stdout.
*
* Build: gcc -o create_bootstrap_keys create_bootstrap_key.c -lsodium -std=c99
*
* Usage: ./create_bootstrap_key [secret-key] 2>data
*/
#include <stdio.h>
#include "create_common.h"
int main(const int argc, const char *const argv[])
{
init_sodium();
unsigned char public_key[crypto_box_PUBLICKEYBYTES];
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
handle_args(argc, argv, "Creates bootstrap keys data", public_key, secret_key);
print_keys(public_key, secret_key);
fwrite(public_key, sizeof(public_key), 1, stderr);
fwrite(secret_key, sizeof(secret_key), 1, stderr);
return 0;
}

View File

@ -0,0 +1,89 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
*
* Shared functions of create_*.c programs.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h>
#include "../../toxcore/ccompat.h"
static void init_sodium(void)
{
if (sodium_init() < 0) {
printf("Error: Failed to initialize sodium.\n");
exit(1);
}
}
static int tox_strcasecmp(const char *s1, const char *s2)
{
while (true) {
const int c1 = tolower(*(s1++));
const int c2 = tolower(*(s2++));
if (c1 == '\0' || c2 == '\0' || c1 != c2) {
return c1 - c2;
}
}
}
static void print_usage(const char *const argv0, const char *const does_what)
{
printf("%s for a given secret key, if provided, or a random key otherwise.\n", does_what);
printf("The data is written to stderr, human-readable key info is written to stdout.\n");
printf("\nUsage: %s [secret-key] 2>data\n", argv0);
}
static void handle_args(const int argc, const char *const argv[], const char *const does_what,
unsigned char *const public_key, unsigned char *const secret_key)
{
if (argc == 2 && (!tox_strcasecmp(argv[1], "-h") || !tox_strcasecmp(argv[1], "--help"))) {
print_usage(argv[0], does_what);
exit(0);
}
if (argc == 1) {
crypto_box_keypair(public_key, secret_key);
} else if (argc == 2 && strlen(argv[1]) == crypto_box_SECRETKEYBYTES * 2) {
size_t bin_len = 0;
if (sodium_hex2bin(secret_key, crypto_box_SECRETKEYBYTES, argv[1], crypto_box_SECRETKEYBYTES * 2, nullptr, &bin_len,
nullptr) != 0 || bin_len != crypto_box_SECRETKEYBYTES) {
printf("Error: Secret key must be a hex string.\n");
exit(1);
}
crypto_scalarmult_base(public_key, secret_key);
} else if (argc == 2) {
printf("Error: Secret key must be a %u character hex string.\n", crypto_box_SECRETKEYBYTES * 2);
exit(1);
} else {
print_usage(argv[0], does_what);
exit(1);
}
}
static void bin2hex_toupper(char *const hex, const size_t hex_maxlen, const unsigned char *const bin,
const size_t bin_len)
{
sodium_bin2hex(hex, hex_maxlen, bin, bin_len);
for (size_t i = 0; i < hex_maxlen; i ++) {
hex[i] = (char)toupper(hex[i]);
}
}
static void print_keys(const unsigned char *const public_key, const unsigned char *const secret_key)
{
char public_key_str[crypto_box_PUBLICKEYBYTES * 2 + 1];
char secret_key_str[crypto_box_SECRETKEYBYTES * 2 + 1];
bin2hex_toupper(public_key_str, sizeof(public_key_str), public_key, crypto_box_PUBLICKEYBYTES);
bin2hex_toupper(secret_key_str, sizeof(secret_key_str), secret_key, crypto_box_SECRETKEYBYTES);
fprintf(stdout, "Public key: %s\n", public_key_str);
fprintf(stdout, "Secret key: %s\n", secret_key_str);
}

Some files were not shown because too many files have changed in this diff Show More