From 9ace11a0e2843cbde4af2b6ff7b49bcc6d429f78 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 9 Jan 2024 16:39:05 +0100 Subject: [PATCH] Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a 8f0d505f9a feat: add ngc events 9b8216e70c refactor: Make event dispatch ordered by receive time. 814c12a6f4 cleanup: Add dynamically derived array sizes to the API. 226b23be12 cleanup: Add explicit array sizes to toxencryptsave. ef33cb4de0 cleanup: Add Toxav alias for ToxAV. 1da723b34d cleanup: Make Tox_Options a typedef. b148a2afff chore: Simplify msvc build using vcpkg. 5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API. c9ca4007e3 refactor: Align group message sending with other send functions. 6c6c0b1b1b cleanup: Make setters take non-const `Tox *`. a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`. baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`. 79f55bd06a cleanup: Put the size of fixed arrays into the API types. 1e73698db2 cleanup: Add typedefs for public API int identifiers. cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash. 32576656bb Make the comment capitalization uniform aff4dda17c Spellcheck tox-bootstrapd 40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools. ebafd51be7 chore: Use GPL license with https. 0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function. 5407384211 cleanup: Use github actions matrix to simplify CI. 82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time. 1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators. 6a90ddfe4e cleanup: Run clang-tidy on headers, as well. bd930cc80a cleanup: Make TCP connection failures a warning instead of error. fad6e4e173 cleanup: Make all .c files include the headers they need. ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings. REVERT: f1df709b87 feat: add ngc events REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb --- .circleci/config.yml | 19 - .clang-tidy | 9 +- .github/scripts/tox-bootstrapd-docker | 7 +- .github/settings.yml | 86 --- .github/workflows/ci.yml | 182 ++--- .gitignore | 2 + BUILD.bazel | 2 +- CMakeLists.txt | 72 +- CMakePresets.json | 21 + LICENSE | 9 +- auto_tests/CMakeLists.txt | 39 +- auto_tests/auto_test_support.c | 2 + auto_tests/group_message_test.c | 11 +- auto_tests/group_moderation_test.c | 18 +- auto_tests/group_tcp_test.c | 2 +- auto_tests/tox_dispatch_test.c | 1 + cmake/Dependencies.cmake | 70 +- cmake/ModulePackage.cmake | 8 +- other/BUILD.bazel | 2 - other/DHT_bootstrap.c | 38 +- other/analysis/run-clang-tidy | 142 ++-- other/astyle/astylerc | 1 - other/bootstrap_daemon/BUILD.bazel | 1 - other/bootstrap_daemon/README.md | 2 +- other/bootstrap_daemon/docker/Dockerfile | 9 +- other/bootstrap_daemon/docker/fetch-sha256 | 46 ++ .../docker/tox-bootstrapd.sha256 | 2 +- .../src/command_line_arguments.c | 33 +- .../src/command_line_arguments.h | 14 +- other/bootstrap_daemon/src/config.c | 19 +- other/bootstrap_daemon/src/config.h | 4 +- other/bootstrap_daemon/src/log.c | 2 + .../bootstrap_daemon/src/log_backend_stdout.c | 3 + .../bootstrap_daemon/src/log_backend_syslog.c | 2 + other/bootstrap_daemon/src/tox-bootstrapd.c | 45 +- other/bootstrap_node_packets.c | 2 + other/docker/autotools/Dockerfile | 3 +- other/docker/clang-tidy/Dockerfile | 21 + other/docker/clang-tidy/run | 5 + other/docker/coverage/Dockerfile | 3 + other/docker/cppcheck/toxcore.cfg | 4 + other/docker/misra/Dockerfile | 3 +- other/docker/tokstyle/Dockerfile | 3 +- other/event_tooling/generate_event_c.cpp | 46 +- testing/BUILD.bazel | 1 - testing/CMakeLists.txt | 22 +- testing/fuzzing/protodump_reduce.cc | 1 + toxav/audio.c | 6 +- toxav/bwcontroller.c | 4 +- toxav/groupav.c | 14 +- toxav/msi.c | 10 +- toxav/toxav.h | 55 +- toxav/video.c | 5 +- toxcore/DHT.c | 16 +- toxcore/DHT.h | 2 +- toxcore/LAN_discovery.c | 8 +- toxcore/Messenger.c | 24 +- toxcore/TCP_client.c | 7 +- toxcore/TCP_common.c | 5 +- toxcore/TCP_connection.c | 13 +- toxcore/TCP_server.c | 13 +- toxcore/announce.c | 9 +- toxcore/bin_pack.c | 1 + toxcore/ccompat.c | 2 + toxcore/events/conference_connected.c | 6 +- toxcore/events/conference_invite.c | 3 +- toxcore/events/conference_message.c | 3 +- toxcore/events/conference_peer_list_changed.c | 6 +- toxcore/events/conference_peer_name.c | 4 +- toxcore/events/conference_title.c | 4 +- toxcore/events/events_alloc.c | 4 +- toxcore/events/events_alloc.h | 6 +- toxcore/events/file_chunk_request.c | 6 +- toxcore/events/file_recv.c | 4 +- toxcore/events/file_recv_chunk.c | 4 +- toxcore/events/file_recv_control.c | 5 +- toxcore/events/friend_connection_status.c | 5 +- toxcore/events/friend_lossless_packet.c | 4 +- toxcore/events/friend_lossy_packet.c | 4 +- toxcore/events/friend_message.c | 3 +- toxcore/events/friend_name.c | 4 +- toxcore/events/friend_read_receipt.c | 6 +- toxcore/events/friend_request.c | 2 +- toxcore/events/friend_status.c | 5 +- toxcore/events/friend_status_message.c | 4 +- toxcore/events/friend_typing.c | 6 +- toxcore/events/group_custom_packet.c | 4 +- toxcore/events/group_custom_private_packet.c | 4 +- toxcore/events/group_invite.c | 4 +- toxcore/events/group_join_fail.c | 5 +- toxcore/events/group_message.c | 3 +- toxcore/events/group_moderation.c | 5 +- toxcore/events/group_password.c | 4 +- toxcore/events/group_peer_exit.c | 3 +- toxcore/events/group_peer_join.c | 6 +- toxcore/events/group_peer_limit.c | 6 +- toxcore/events/group_peer_name.c | 4 +- toxcore/events/group_peer_status.c | 5 +- toxcore/events/group_privacy_state.c | 5 +- toxcore/events/group_private_message.c | 3 +- toxcore/events/group_self_join.c | 6 +- toxcore/events/group_topic.c | 4 +- toxcore/events/group_topic_lock.c | 5 +- toxcore/events/group_voice_state.c | 5 +- toxcore/events/self_connection_status.c | 5 +- toxcore/forwarding.c | 4 + toxcore/friend_connection.c | 15 +- toxcore/friend_requests.c | 4 +- toxcore/group.c | 8 + toxcore/group_announce.c | 5 +- toxcore/group_announce.h | 6 +- toxcore/group_announce_test.cc | 4 +- toxcore/group_chats.c | 18 +- toxcore/group_chats.h | 6 +- toxcore/group_common.h | 6 +- toxcore/group_connection.c | 3 + toxcore/group_connection.h | 6 +- toxcore/group_moderation.c | 9 +- toxcore/group_moderation_test.cc | 2 +- toxcore/group_onion_announce.c | 6 + toxcore/group_pack.c | 6 +- toxcore/group_pack.h | 6 +- toxcore/list.c | 2 + toxcore/logger.c | 1 - toxcore/mono_time.c | 101 +-- toxcore/mono_time_test.cc | 5 +- toxcore/net_crypto.c | 10 +- toxcore/network.c | 36 +- toxcore/network.h | 8 +- toxcore/onion.c | 8 +- toxcore/onion_announce.c | 7 +- toxcore/onion_client.c | 10 + toxcore/onion_client.h | 14 +- toxcore/ping.c | 4 +- toxcore/ping_array.c | 3 +- toxcore/shared_key_cache.c | 3 +- toxcore/state.c | 1 + toxcore/timed_auth.c | 2 + toxcore/tox.c | 124 ++-- toxcore/tox.h | 651 +++++++++++------- toxcore/tox_api.c | 49 +- toxcore/tox_dispatch.c | 4 +- toxcore/tox_event.c | 8 +- toxcore/tox_events.c | 9 +- toxcore/tox_events_test.cc | 6 +- toxcore/tox_private.c | 5 + toxcore/tox_private.h | 2 + toxcore/tox_unpack.c | 2 +- toxcore/util.c | 3 +- toxencryptsave/toxencryptsave.c | 32 +- toxencryptsave/toxencryptsave.h | 30 +- vcpkg.json | 11 + 152 files changed, 1542 insertions(+), 1140 deletions(-) delete mode 100644 .github/settings.yml create mode 100644 CMakePresets.json create mode 100755 other/bootstrap_daemon/docker/fetch-sha256 create mode 100644 other/docker/clang-tidy/Dockerfile create mode 100755 other/docker/clang-tidy/run create mode 100644 vcpkg.json diff --git a/.circleci/config.yml b/.circleci/config.yml index 9006f681..5531177d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,7 +15,6 @@ workflows: - ubsan # Static analysis - clang-analyze - - clang-tidy - cpplint - infer - static-analysis @@ -154,24 +153,6 @@ jobs: - run: git submodule update --init --recursive - run: other/analysis/run-clang-analyze - clang-tidy: - working_directory: ~/work - docker: - - image: ubuntu - - steps: - - run: *apt_install - - run: - apt-get install -y --no-install-recommends - ca-certificates - clang-tidy-14 - - checkout - - run: git submodule update --init --recursive - - run: - other/analysis/run-clang-tidy || - other/analysis/run-clang-tidy || - other/analysis/run-clang-tidy - cpplint: working_directory: ~/work docker: diff --git a/.clang-tidy b/.clang-tidy index 5ad92a14..f1aca9f9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,5 @@ # vim:ft=yaml +HeaderFilterRegex: "/c-toxcore/[^/]+/[^.].+" CheckOptions: - key: readability-identifier-naming.ClassCase value: Camel_Snake_Case @@ -35,6 +36,12 @@ CheckOptions: value: lower_case - key: llvmlibc-restrict-system-libc-headers.Includes - value: "arpa/inet.h,assert.h,ctype.h,errno.h,fcntl.h,getopt.h,libconfig.h,linux/netdevice.h,math.h,netdb.h,netinet/in.h,opus.h,pthread.h,signal.h,sodium/crypto_scalarmult_curve25519.h,sodium.h,sodium/randombytes.h,stdio.h,stdlib.h,string.h,sys/ioctl.h,syslog.h,sys/resource.h,sys/socket.h,sys/stat.h,sys/time.h,sys/types.h,time.h,unistd.h,vpx/vp8cx.h,vpx/vp8dx.h,vpx/vpx_decoder.h,vpx/vpx_encoder.h,vpx/vpx_image.h" + value: "arpa/inet.h,assert.h,ctype.h,errno.h,fcntl.h,getopt.h,libconfig.h,limits.h,linux/if.h,math.h,netdb.h,netinet/in.h,opus.h,pthread.h,signal.h,sodium/crypto_scalarmult_curve25519.h,sodium.h,sodium/randombytes.h,stdarg.h,stdbool.h,stddef.h,stdint.h,stdio.h,stdlib.h,string.h,sys/ioctl.h,syslog.h,sys/resource.h,sys/socket.h,sys/stat.h,sys/time.h,sys/types.h,time.h,unistd.h,vpx/vp8cx.h,vpx/vp8dx.h,vpx/vpx_decoder.h,vpx/vpx_encoder.h,vpx/vpx_image.h" - key: hicpp-signed-bitwise.IgnorePositiveIntegerLiterals value: true + - key: concurrency-mt-unsafe.FunctionSet + value: posix + - key: misc-include-cleaner.IgnoreHeaders + value: "pthread.h;stdbool.h;stddef.h;stdint.;stdint.h;stdint...;cstdint;sodium.*;sys/.*;unistd.h;opus.*;vpx.*;attributes.h;tox_struct.h" + - key: readability-function-cognitive-complexity.Threshold + value: 153 # TODO(iphydf): Decrease. tox_new is the highest at the moment. diff --git a/.github/scripts/tox-bootstrapd-docker b/.github/scripts/tox-bootstrapd-docker index d6c1ee28..48665537 100755 --- a/.github/scripts/tox-bootstrapd-docker +++ b/.github/scripts/tox-bootstrapd-docker @@ -1,12 +1,15 @@ #!/bin/bash -set -exu +set -exu -o pipefail LOCAL="${1:-}" readarray -t FILES <<<"$(git ls-files)" -tar c "${FILES[@]}" | docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node - +if ! tar c "${FILES[@]}" | docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node - 2>&1 | tee docker-build.log; then + grep -o "::error.*::[a-f0-9]* /usr/local/bin/tox-bootstrapd" docker-build.log + false +fi docker tag toxchat/bootstrap-node:latest toxchat/bootstrap-node:"$(other/print-version)" sudo useradd \ diff --git a/.github/settings.yml b/.github/settings.yml deleted file mode 100644 index 6e118f0d..00000000 --- a/.github/settings.yml +++ /dev/null @@ -1,86 +0,0 @@ ---- -_extends: .github - -repository: - name: c-toxcore - description: The future of online communications. - homepage: https://tox.chat/ - topics: toxcore, network, p2p, security, encryption, cryptography - -branches: - - name: "master" - protection: - required_status_checks: - contexts: - - "bazel-dbg" - - "bazel-opt" - - "build-alpine-s390x" - - "build-android" - - "build-autotools" - - "build-compcert" - - "build-macos" - - "build-tcc" - - "build-win32" - - "build-win64" - - "ci/circleci: asan" - - "ci/circleci: bazel-asan" - - "ci/circleci: bazel-msan" - - "ci/circleci: bazel-tsan" - - "ci/circleci: clang-analyze" - - "ci/circleci: clang-tidy" - - "ci/circleci: cpplint" - - "ci/circleci: infer" - - "ci/circleci: static-analysis" - - "ci/circleci: tsan" - - "ci/circleci: ubsan" - - "cimple" - - "cimplefmt" - - "CodeFactor" - - "code-review/reviewable" - - "common / buildifier" - - "coverage-linux" - - "docker-bootstrap-node" - - "docker-bootstrap-node-websocket" - - "docker-clusterfuzz" - - "docker-esp32" - - "docker-fuzzer" - - "docker-toxcore-js" - - "docker-win32" - - "docker-win64" - - "doxygen" - - "freebsd" - - "Hound" - - "misra" - - "mypy" - - "program-analysis" - - "sonar-scan" - - "tokstyle" - - "TokTok.c-toxcore" - - "TokTok.c-toxcore (windows_msvc_conan shared)" - - "TokTok.c-toxcore (windows_msvc_conan static)" - -# Labels specific to c-toxcore. -labels: - - name: "bootstrap" - color: "#01707f" - description: "Bootstrap" - - - name: "crypto" - color: "#1d76db" - description: "Crypto" - - - name: "file transfers" - color: "#e02abf" - description: "File Transfers" - - - name: "messenger" - color: "#d93f0b" - description: "Messenger" - - - name: "network" - color: "#d4c5f9" - description: "Network" - - - name: "toxav" - color: "#0052cc" - description: "Audio/video" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d6f715bd..01eb0fb7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,10 @@ jobs: common: uses: TokTok/ci-tools/.github/workflows/common-ci.yml@master - cppcheck: + analysis: + strategy: + matrix: + tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, misra, tcc, tokstyle] runs-on: ubuntu-latest steps: - name: Set up Docker Buildx @@ -21,84 +24,16 @@ jobs: - name: Docker Build uses: docker/build-push-action@v4 with: - file: other/docker/cppcheck/Dockerfile + file: other/docker/${{ matrix.tool }}/Dockerfile - mypy: + coverage-linux: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: recursive - - name: Set up Python 3.9 - uses: actions/setup-python@v1 - with: - python-version: 3.9 - - name: Install mypy - run: pip install mypy - - name: Run mypy - run: | - (find . -name "*.py" -and -not -name "conanfile.py"; grep -lR '^#!.*python') \ - | xargs -n1 -P8 mypy --strict - - doxygen: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/doxygen/Dockerfile - - tokstyle: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/tokstyle/Dockerfile - - misra: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/misra/Dockerfile - - build-autotools: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/autotools/Dockerfile - - build-tcc: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/tcc/Dockerfile - - build-compcert: - runs-on: ubuntu-latest - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Docker Build - uses: docker/build-push-action@v4 - with: - file: other/docker/compcert/Dockerfile + - name: Build, test, and upload coverage + run: other/docker/coverage/run generate-events: runs-on: ubuntu-latest @@ -120,42 +55,6 @@ jobs: - name: Run cimplefmt run: other/docker/cimplefmt/run -u $(find tox* -name "*.[ch]") - build-win32: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - submodules: recursive - - name: Cross compilation - run: .github/scripts/cmake-win32 script - - build-win64: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - submodules: recursive - - name: Cross compilation - run: .github/scripts/cmake-win64 script - - build-macos: - runs-on: macos-latest - steps: - - uses: actions/checkout@v4 - with: - submodules: recursive - - name: Build and test - run: .github/scripts/cmake-osx - - coverage-linux: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - submodules: recursive - - name: Build, test, and upload coverage - run: other/docker/coverage/run - build-android: runs-on: ubuntu-latest steps: @@ -166,3 +65,68 @@ jobs: - run: .github/scripts/cmake-android arm64-v8a - run: .github/scripts/cmake-android x86 - run: .github/scripts/cmake-android x86_64 + + build-macos: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Build and test + run: .github/scripts/cmake-osx + + build-msvc: + strategy: + matrix: + version: [2019, 2022] + runs-on: windows-${{ matrix.version }} + env: + VCPKG_ROOT: "C:/vcpkg" + VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite" + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Export GitHub Actions cache environment variables + uses: actions/github-script@v6 + with: + script: | + core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || ''); + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); + - name: Configure CMake + run: cmake --preset windows-default + - name: Build + run: cmake --build _build + - name: Test + run: | + cd _build + ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 --build-config Debug + + build-windows: + strategy: + matrix: + bits: [32, 64] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Cross compilation + run: .github/scripts/cmake-win${{ matrix.bits }} script + + mypy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Set up Python 3.9 + uses: actions/setup-python@v5 + with: + python-version: 3.9 + - name: Install mypy + run: pip install mypy + - name: Run mypy + run: | + (find . -name "*.py" -and -not -name "conanfile.py"; grep -lR '^#!.*python') \ + | xargs -n1 -P8 mypy --strict diff --git a/.gitignore b/.gitignore index fc3cc5f7..9ed3743e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ Thumbs.db /_build /_install /tox-0.0.0* +/.vs +/CppProperties.json CMakeCache.txt CMakeFiles Makefile diff --git a/BUILD.bazel b/BUILD.bazel index d740c217..db5f5693 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,7 +1,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library") load("//tools/project:build_defs.bzl", "project") -project() +project(license = "gpl3-https") genrule( name = "public_headers", diff --git a/CMakeLists.txt b/CMakeLists.txt index 113eaa28..8abcf609 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ find_package(GTest) set(CMAKE_MACOSX_RPATH ON) # Set standard version for compiler. -if(MSVC) +if(MSVC AND MSVC_TOOLSET_VERSION LESS 143) # https://developercommunity.visualstudio.com/t/older-winsdk-headers-are-incompatible-with-zcprepr/1593479 set(CMAKE_C_STANDARD 99) else() @@ -94,6 +94,35 @@ set(CMAKE_CXX_EXTENSIONS OFF) message(STATUS "Supported C compiler features = ${CMAKE_C_COMPILE_FEATURES}") message(STATUS "Supported C++ compiler features = ${CMAKE_CXX_COMPILE_FEATURES}") +# Enable some warnings if we know the compiler. +if(MSVC) + add_compile_options(/W4 /analyze) + add_compile_options(/wd4100) # unreferenced formal parameter + add_compile_options(/wd4267) # narrowing conversion + add_compile_options(/wd4244) # narrowing conversion + add_compile_options(/wd4127) # conditional expression is constant + add_compile_options(/wd4995) # #pragma deprecated + add_compile_options(/wd4018) # signed/unsigned compare + add_compile_options(/wd4310) # cast truncates constant value + add_compile_options(/wd4389) # signed/unsigned compare + add_compile_options(/wd4245) # signed/unsigned assign/return/function call + add_compile_options(/wd4200) # nonstandard extension used: zero-sized array in struct/union + add_compile_options(/wd4702) # unreachable code + add_compile_options(/wd6340) # unsigned int passed to signed parameter + add_compile_options(/wd6326) # potential comparison of a constant with another constant + + # TODO(iphydf): Look into these + add_compile_options(/wd4996) # use WSAAddressToStringW() instead of WSAAddressToStringA() + add_compile_options(/wd6255) # don't use alloca + add_compile_options(/wd6385) # reading invalid data + add_compile_options(/wd6001) # using uninitialized memory + add_compile_options(/wd6101) # returning uninitialized memory + add_compile_options(/wd6386) # buffer overrun + add_compile_options(/wd6011) # NULL dereference + add_compile_options(/wd6031) # sscanf return value ignored + add_compile_options(/wd6387) # passing NULL to fwrite +endif() + set(MIN_LOGGER_LEVEL "" CACHE STRING "Logging level to use (TRACE, DEBUG, INFO, WARNING, ERROR)") if(MIN_LOGGER_LEVEL) if(("${MIN_LOGGER_LEVEL}" STREQUAL "TRACE") OR @@ -327,10 +356,14 @@ set(toxcore_SOURCES toxcore/tox_unpack.h toxcore/util.c toxcore/util.h) -set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES}) -set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS}) -set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS}) -set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER}) +if(TARGET unofficial-sodium::sodium) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} unofficial-sodium::sodium) +else() + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES}) + set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS}) + set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS}) + set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER}) +endif() set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium) set(toxcore_API_HEADERS ${toxcore_SOURCE_DIR}/toxcore/tox.h^tox @@ -366,10 +399,14 @@ if(BUILD_TOXAV) set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav) - set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES}) - set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS}) - set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS}) - set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER}) + if(MSVC) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PkgConfig::OPUS PkgConfig::VPX) + else() + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES}) + set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS}) + set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS}) + set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER}) + endif() set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} opus vpx) endif() @@ -400,7 +437,6 @@ if(CMAKE_THREAD_LIBS_INIT) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} ${CMAKE_THREAD_LIBS_INIT}) endif() - if(NSL_LIBRARIES) set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${NSL_LIBRARIES}) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lnsl) @@ -416,9 +452,13 @@ if(SOCKET_LIBRARIES) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lsocket) endif() +if(TARGET PThreads4W::PThreads4W) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PThreads4W::PThreads4W) +elseif(TARGET Threads::Threads) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} Threads::Threads) +endif() if(WIN32) - set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ws2_32 iphlpapi) - set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lws2_32 -liphlpapi) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} iphlpapi wsock32 ws2_32) endif() ################################################################################ @@ -516,6 +556,14 @@ if(DHT_BOOTSTRAP) target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared) endif() target_link_libraries(DHT_bootstrap PRIVATE misc_tools) + if(TARGET unofficial-sodium::sodium) + target_link_libraries(DHT_bootstrap PRIVATE unofficial-sodium::sodium) + endif() + if(TARGET PThreads4W::PThreads4W) + target_link_libraries(DHT_bootstrap PRIVATE PThreads4W::PThreads4W) + elseif(TARGET Threads::Threads) + target_link_libraries(DHT_bootstrap PRIVATE Threads::Threads) + endif() install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin) endif() diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 00000000..45caa10d --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,21 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "windows-default", + "binaryDir": "${sourceDir}/_build", + "cacheVariables": { + "ENABLE_SHARED": true, + "ENABLE_STATIC": true, + "AUTOTEST": true, + "BUILD_MISC_TESTS": true, + "BOOTSTRAP_DAEMON": false, + "MUST_BUILD_TOXAV": true, + "TEST_TIMEOUT_SECONDS": "60", + "CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS": true, + "CMAKE_COMPILE_WARNING_AS_ERROR": true, + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + } + } + ] +} diff --git a/LICENSE b/LICENSE index 10926e87..f288702d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. @@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found. GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program. If not, see . + along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. @@ -664,12 +664,11 @@ might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see -. +. The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read -. - +. diff --git a/auto_tests/CMakeLists.txt b/auto_tests/CMakeLists.txt index ffdf8055..1bc962df 100644 --- a/auto_tests/CMakeLists.txt +++ b/auto_tests/CMakeLists.txt @@ -9,6 +9,11 @@ if(TARGET toxcore_static) else() target_link_libraries(auto_test_support PRIVATE toxcore_shared) endif() +if(TARGET PThreads4W::PThreads4W) + target_link_libraries(auto_test_support PRIVATE PThreads4W::PThreads4W) +elseif(TARGET Threads::Threads) + target_link_libraries(auto_test_support PRIVATE Threads::Threads) +endif() function(auto_test target) add_executable(auto_${target}_test ${target}_test.c) @@ -18,12 +23,15 @@ function(auto_test target) else() target_link_libraries(auto_${target}_test PRIVATE toxcore_shared) endif() - if(NOT ARGV1 STREQUAL "DONT_RUN") - add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) - set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") - # add the source dir as environment variable, so the testdata can be found - set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}") + if(TARGET PThreads4W::PThreads4W) + target_link_libraries(auto_${target}_test PRIVATE PThreads4W::PThreads4W) + elseif(TARGET Threads::Threads) + target_link_libraries(auto_${target}_test PRIVATE Threads::Threads) endif() + add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) + set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") + # add the source dir as environment variable, so the testdata can be found + set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}") endfunction() auto_test(TCP) @@ -89,15 +97,20 @@ if(BUILD_TOXAV) auto_test(toxav_basic) auto_test(toxav_many) - target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES}) - target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS}) - target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) - target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER}) + if(MSVC) + target_link_libraries(auto_toxav_basic_test PRIVATE PkgConfig::VPX) + target_link_libraries(auto_toxav_many_test PRIVATE PkgConfig::VPX) + else() + target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES}) + target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS}) + target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) + target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER}) - target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES}) - target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS}) - target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) - target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER}) + target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES}) + target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS}) + target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) + target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER}) + endif() endif() if(PROXY_TEST) diff --git a/auto_tests/auto_test_support.c b/auto_tests/auto_test_support.c index afc6c2cc..3a56039a 100644 --- a/auto_tests/auto_test_support.c +++ b/auto_tests/auto_test_support.c @@ -149,6 +149,8 @@ void set_mono_time_callback(AutoTox *autotox) Mono_Time *mono_time = autotox->tox->mono_time; autotox->clock = current_time_monotonic(mono_time); + ck_assert_msg(autotox->clock >= 1000, + "clock is too low (not initialised?): %lu", (unsigned long)autotox->clock); mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock); } diff --git a/auto_tests/group_message_test.c b/auto_tests/group_message_test.c index d93e460a..a8e20f99 100644 --- a/auto_tests/group_message_test.c +++ b/auto_tests/group_message_test.c @@ -395,8 +395,9 @@ static void group_message_test(AutoTox *autotoxes) iterate_all_wait(autotoxes, NUM_GROUP_TOXES, ITERATION_INTERVAL); if (state1->peer_joined && !state1->message_sent) { - tox_group_send_message(tox1, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)TEST_MESSAGE, - TEST_MESSAGE_LEN, &state1->pseudo_msg_id, &err_send); + state1->pseudo_msg_id = tox_group_send_message( + tox1, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)TEST_MESSAGE, + TEST_MESSAGE_LEN, &err_send); ck_assert(err_send == TOX_ERR_GROUP_SEND_MESSAGE_OK); state1->message_sent = true; } @@ -419,7 +420,7 @@ static void group_message_test(AutoTox *autotoxes) // tox1 sends group a message which should not be seen by tox0's message handler tox_group_send_message(tox1, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)IGNORE_MESSAGE, - IGNORE_MESSAGE_LEN, nullptr, &err_send); + IGNORE_MESSAGE_LEN, &err_send); iterate_all_wait(autotoxes, NUM_GROUP_TOXES, ITERATION_INTERVAL); @@ -506,7 +507,7 @@ static void group_message_test(AutoTox *autotoxes) memcpy(m + 2, &checksum, sizeof(uint16_t)); - tox_group_send_message(tox0, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)m, message_size, nullptr, &err_send); + tox_group_send_message(tox0, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)m, message_size, &err_send); ck_assert(err_send == TOX_ERR_GROUP_SEND_MESSAGE_OK); } @@ -528,7 +529,7 @@ static void group_message_test(AutoTox *autotoxes) memcpy(m, &i, sizeof(uint16_t)); - tox_group_send_message(tox0, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)m, 2, nullptr, &err_send); + tox_group_send_message(tox0, group_number, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)m, 2, &err_send); ck_assert(err_send == TOX_ERR_GROUP_SEND_MESSAGE_OK); } diff --git a/auto_tests/group_moderation_test.c b/auto_tests/group_moderation_test.c index 712c5907..cd12f21b 100644 --- a/auto_tests/group_moderation_test.c +++ b/auto_tests/group_moderation_test.c @@ -334,21 +334,21 @@ static void voice_state_message_test(AutoTox *autotox, Tox_Group_Voice_State voi ck_assert(sq_err == TOX_ERR_GROUP_SELF_QUERY_OK); Tox_Err_Group_Send_Message msg_err; - bool send_ret = tox_group_send_message(autotox->tox, state->group_number, TOX_MESSAGE_TYPE_NORMAL, - (const uint8_t *)"test", 4, nullptr, &msg_err); + tox_group_send_message(autotox->tox, state->group_number, TOX_MESSAGE_TYPE_NORMAL, + (const uint8_t *)"test", 4, &msg_err); switch (self_role) { case TOX_GROUP_ROLE_OBSERVER: { - ck_assert(!send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS); + ck_assert(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS); break; } case TOX_GROUP_ROLE_USER: { if (voice_state != TOX_GROUP_VOICE_STATE_ALL) { - ck_assert_msg(!send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS, - "%d, %d", send_ret, msg_err); + ck_assert_msg(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS, + "%d", msg_err); } else { - ck_assert(send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); + ck_assert(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); } break; @@ -356,16 +356,16 @@ static void voice_state_message_test(AutoTox *autotox, Tox_Group_Voice_State voi case TOX_GROUP_ROLE_MODERATOR: { if (voice_state != TOX_GROUP_VOICE_STATE_FOUNDER) { - ck_assert(send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); + ck_assert(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); } else { - ck_assert(!send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS); + ck_assert(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS); } break; } case TOX_GROUP_ROLE_FOUNDER: { - ck_assert(send_ret && msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); + ck_assert(msg_err == TOX_ERR_GROUP_SEND_MESSAGE_OK); break; } } diff --git a/auto_tests/group_tcp_test.c b/auto_tests/group_tcp_test.c index 43cfa666..78a339d8 100644 --- a/auto_tests/group_tcp_test.c +++ b/auto_tests/group_tcp_test.c @@ -206,7 +206,7 @@ static void group_tcp_test(AutoTox *autotoxes) Tox_Err_Group_Send_Message merr; tox_group_send_message(autotoxes[0].tox, groupnumber, TOX_MESSAGE_TYPE_NORMAL, - (const uint8_t *)CODEWORD, CODEWORD_LEN, nullptr, &merr); + (const uint8_t *)CODEWORD, CODEWORD_LEN, &merr); ck_assert(merr == TOX_ERR_GROUP_SEND_MESSAGE_OK); while (!state1->got_second_code) { diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index ca50bb61..01294589 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -10,6 +10,7 @@ #include "../toxcore/tox.h" #include "../toxcore/tox_dispatch.h" #include "../toxcore/tox_events.h" +#include "../toxcore/tox_private.h" #include "../toxcore/tox_unpack.h" #include "auto_test_support.h" #include "check_compat.h" diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 6f2ec906..900e9b18 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -1,19 +1,26 @@ ############################################################################### # -# :: For UNIX-like systems that have pkg-config. +# :: For systems that have pkg-config. # ############################################################################### -include(ModulePackage) - -find_package(Threads REQUIRED) +find_package(PkgConfig REQUIRED) find_library(NSL_LIBRARIES nsl ) find_library(RT_LIBRARIES rt ) find_library(SOCKET_LIBRARIES socket) +find_package(pthreads QUIET) +if(NOT TARGET PThreads4W::PThreads4W) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) +endif() + # For toxcore. -pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET) +pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET REQUIRED) +if(MSVC) + find_package(unofficial-sodium REQUIRED) +endif() # For toxav. pkg_search_module(OPUS opus IMPORTED_TARGET) @@ -27,56 +34,3 @@ endif() # For tox-bootstrapd. pkg_search_module(LIBCONFIG libconfig IMPORTED_TARGET) - -############################################################################### -# -# :: For MSVC Windows builds. -# -# These require specific installation paths of dependencies: -# - libsodium in third-party/libsodium/Win32/Release/v140/dynamic -# - pthreads in third-party/pthreads-win32/Pre-built.2 -# -############################################################################### - -if(MSVC) - # libsodium - # --------- - if(NOT LIBSODIUM_FOUND) - find_library(LIBSODIUM_LIBRARIES - NAMES sodium libsodium - PATHS - "third_party/libsodium/Win32/Release/v140/dynamic" - "third_party/libsodium/x64/Release/v140/dynamic" - ) - if(LIBSODIUM_LIBRARIES) - include_directories("third_party/libsodium/include") - set(LIBSODIUM_FOUND TRUE) - message("libsodium: ${LIBSODIUM_LIBRARIES}") - else() - message(FATAL_ERROR "libsodium libraries not found") - endif() - endif() - - # pthreads - # -------- - if(CMAKE_USE_WIN32_THREADS_INIT) - find_library(CMAKE_THREAD_LIBS_INIT - NAMES pthreadVC2 - PATHS - "third_party/pthreads-win32/Pre-built.2/lib/x86" - "third_party/pthreads-win32/Pre-built.2/lib/x64" - ) - if(CMAKE_THREAD_LIBS_INIT) - include_directories("third_party/pthreads-win32/Pre-built.2/include") - add_definitions(-DHAVE_STRUCT_TIMESPEC) - message("libpthreads: ${CMAKE_THREAD_LIBS_INIT}") - else() - find_package(pthreads4w) - if(NOT pthreads4w_FOUND) - message(FATAL_ERROR "libpthreads libraries not found") - endif() - include_directories(${pthreads4w_INCLUDE_DIR}) - link_libraries(${pthreads4w_LIBRARIES}) - endif() - endif() -endif() diff --git a/cmake/ModulePackage.cmake b/cmake/ModulePackage.cmake index cbc62f41..332110d7 100644 --- a/cmake/ModulePackage.cmake +++ b/cmake/ModulePackage.cmake @@ -19,18 +19,16 @@ if(FULLY_STATIC) set(ENABLE_STATIC ON) endif() -find_package(PkgConfig) - function(add_module lib) - set(${lib}_SOURCES ${ARGN} PARENT_SCOPE) - if(ENABLE_SHARED) add_library(${lib}_shared SHARED ${ARGN}) set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib}) endif() if(ENABLE_STATIC) add_library(${lib}_static STATIC ${ARGN}) - set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib}) + if(NOT MSVC) + set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib}) + endif() endif() endfunction() diff --git a/other/BUILD.bazel b/other/BUILD.bazel index 253cfcd0..50693bda 100644 --- a/other/BUILD.bazel +++ b/other/BUILD.bazel @@ -21,13 +21,11 @@ cc_binary( "//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", ], ) diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 93bf72a1..5fd2568d 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -16,12 +16,17 @@ #include "../toxcore/DHT.h" #include "../toxcore/LAN_discovery.h" #include "../toxcore/ccompat.h" -#include "../toxcore/friend_requests.h" +#include "../toxcore/crypto_core.h" +#include "../toxcore/forwarding.h" +#include "../toxcore/group_announce.h" #include "../toxcore/group_onion_announce.h" #include "../toxcore/logger.h" +#include "../toxcore/mem.h" #include "../toxcore/mono_time.h" +#include "../toxcore/network.h" +#include "../toxcore/onion.h" +#include "../toxcore/onion_announce.h" #include "../toxcore/tox.h" -#include "../toxcore/util.h" #define TCP_RELAY_ENABLED @@ -46,7 +51,7 @@ static const char *motd_str = ""; //Change this to anything within 256 bytes(but #define PORT 33445 -static void manage_keys(DHT *dht) +static bool manage_keys(DHT *dht) { enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE }; uint8_t keys[KEYS_SIZE]; @@ -60,7 +65,8 @@ static void manage_keys(DHT *dht) if (read_size != KEYS_SIZE) { printf("Error while reading the key file\nExiting.\n"); - exit(1); + fclose(keys_file); + return false; } dht_set_self_public_key(dht, keys); @@ -73,18 +79,20 @@ static void manage_keys(DHT *dht) if (keys_file == nullptr) { printf("Error opening key file in write mode.\nKeys will not be saved.\n"); - return; + return false; } if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { printf("Error while writing the key file.\nExiting.\n"); - exit(1); + fclose(keys_file); + return false; } printf("Keys saved successfully.\n"); } fclose(keys_file); + return true; } static const char *strlevel(Logger_Level level) @@ -121,7 +129,7 @@ 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); + return 0; } /* let user override default by cmdline */ @@ -129,7 +137,7 @@ int main(int argc, char *argv[]) int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) { - exit(1); + return 1; } /* Initialize networking - @@ -162,14 +170,16 @@ int main(int argc, char *argv[]) if (!(onion && forwarding && onion_a)) { printf("Something failed to initialize.\n"); - exit(1); + return 1; } gca_onion_init(gc_announces_list, onion_a); perror("Initialization"); - manage_keys(dht); + if (!manage_keys(dht)) { + return 1; + } printf("Public key: "); #ifdef TCP_RELAY_ENABLED @@ -179,7 +189,7 @@ int main(int argc, char *argv[]) if (tcp_s == nullptr) { printf("TCP server failed to initialize.\n"); - exit(1); + return 1; } #endif @@ -189,7 +199,7 @@ int main(int argc, char *argv[]) if (file == nullptr) { printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename); - exit(1); + return 1; } for (uint32_t i = 0; i < 32; ++i) { @@ -210,7 +220,7 @@ int main(int argc, char *argv[]) if (port_conv <= 0 || port_conv > UINT16_MAX) { printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]); - exit(1); + return 1; } const uint16_t port = net_htons((uint16_t)port_conv); @@ -222,7 +232,7 @@ int main(int argc, char *argv[]) if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); - exit(1); + return 1; } } diff --git a/other/analysis/run-clang-tidy b/other/analysis/run-clang-tidy index d1fcae1a..dc07f946 100755 --- a/other/analysis/run-clang-tidy +++ b/other/analysis/run-clang-tidy @@ -1,6 +1,64 @@ -#!/bin/bash +#!/usr/bin/env bash CHECKS="*" +ERRORS="*" + +# Need to investigate or disable and document these. +# ========================================================= + +# TODO(iphydf): Fix these. +ERRORS="$ERRORS,-cert-err34-c" +ERRORS="$ERRORS,-readability-suspicious-call-argument" + +# TODO(iphydf): Fix once cimple 0.0.19 is released. +CHECKS="$CHECKS,-google-readability-casting" + +# TODO(iphydf): Fix these. +CHECKS="$CHECKS,-bugprone-switch-missing-default-case" + +# TODO(iphydf): We might want some of these. For the ones we don't want, add a +# comment explaining why not. +CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding" +CHECKS="$CHECKS,-hicpp-signed-bitwise" + +# TODO(iphydf): Maybe fix these? +CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result" +CHECKS="$CHECKS,-bugprone-integer-division" +CHECKS="$CHECKS,-misc-no-recursion" + +# TODO(iphydf): Only happens in bootstrap_daemon. Fix it. +CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables" + +# TODO(iphydf): Probably fix these. +CHECKS="$CHECKS,-cert-err33-c" +CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers" +CHECKS="$CHECKS,-readability-magic-numbers" + +# TODO(iphydf): We're using a lot of macros for constants. Should we convert +# all of them to enum? +CHECKS="$CHECKS,-modernize-macro-to-enum" + +# Documented disabled checks. We don't want these for sure. +# ========================================================= + +# https://stackoverflow.com/questions/58672959/why-does-clang-tidy-say-vsnprintf-has-an-uninitialized-va-list-argument +CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized" + +# We pass a lot of ints around, so many function parameters are some kind of +# int type that can be converted from another int type. We won't be getting +# away from that anytime soon. +CHECKS="$CHECKS,-bugprone-easily-swappable-parameters" + +# Callback handlers often don't use all their parameters. There's +# IgnoreVirtual, but that doesn't work for C-style callbacks. +CHECKS="$CHECKS,-misc-unused-parameters" + +# We sometimes use #if 0. +CHECKS="$CHECKS,-readability-avoid-unconditional-preprocessor-if" + +# We have better macro hygiene checks with tokstyle. We can never pass macro +# arguments that require parentheses inside the macro. +CHECKS="$CHECKS,-bugprone-macro-parentheses" # We don't use memcpy_s. CHECKS="$CHECKS,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling" @@ -81,62 +139,48 @@ CHECKS="$CHECKS,-cert-dcl03-c" CHECKS="$CHECKS,-hicpp-static-assert" CHECKS="$CHECKS,-misc-static-assert" -# TODO(iphydf): We might want some of these. For the ones we don't want, add a -# comment explaining why not. -CHECKS="$CHECKS,-clang-analyzer-optin.performance.Padding" -CHECKS="$CHECKS,-hicpp-signed-bitwise" -CHECKS="$CHECKS,-misc-unused-parameters" -CHECKS="$CHECKS,-readability-function-cognitive-complexity" - -# TODO(iphydf): Maybe fix these? -CHECKS="$CHECKS,-bugprone-easily-swappable-parameters" -CHECKS="$CHECKS,-bugprone-implicit-widening-of-multiplication-result" -CHECKS="$CHECKS,-bugprone-integer-division" -CHECKS="$CHECKS,-clang-analyzer-core.NullDereference" -CHECKS="$CHECKS,-clang-analyzer-valist.Uninitialized" -CHECKS="$CHECKS,-concurrency-mt-unsafe" -CHECKS="$CHECKS,-cppcoreguidelines-avoid-non-const-global-variables" -CHECKS="$CHECKS,-misc-no-recursion" - -# TODO(iphydf): Probably fix these. -CHECKS="$CHECKS,-cert-err33-c" -CHECKS="$CHECKS,-cppcoreguidelines-avoid-magic-numbers" -CHECKS="$CHECKS,-google-readability-casting" -CHECKS="$CHECKS,-modernize-macro-to-enum" -CHECKS="$CHECKS,-readability-magic-numbers" - -# TODO(iphydf): These two trip on list.c. Investigate why. -CHECKS="$CHECKS,-clang-analyzer-core.NonNullParamChecker" -CHECKS="$CHECKS,-clang-analyzer-unix.Malloc" - -ERRORS="*" - -# TODO(iphydf): Fix these. -ERRORS="$ERRORS,-bugprone-macro-parentheses" -ERRORS="$ERRORS,-cert-err34-c" -ERRORS="$ERRORS,-cert-str34-c" -ERRORS="$ERRORS,-readability-suspicious-call-argument" - set -eux +# TODO(iphydf): Add toxav. +DIRS=( + other/bootstrap_daemon/src + other + toxcore + toxcore/events + toxencryptsave +) + +copy_files() { + find "${DIRS[@]}" \ + -maxdepth 1 -type d -exec mkdir -p "$1/{}" \; + find "${DIRS[@]}" \ + -maxdepth 1 -name "*.c" -exec cp "{}" "$1/{}" \; +} + 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-14 \ - -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 + ls .clang-tidy + copy_files a + if ! find "${DIRS[@]}" \ + -maxdepth 1 -name "*.c" -print0 \ + | xargs -0 -n15 -P"$(nproc)" clang-tidy \ + -p="$PWD/_build" \ + --extra-arg=-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \ + "${EXTRA_ARGS[@]}" \ + --fix \ + --checks="$CHECKS" \ + --warnings-as-errors="$ERRORS" \ + --use-color; then + copy_files b + colordiff -ru a b + rm -rf a b + false + fi + rm -rf a } cmake . -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON diff --git a/other/astyle/astylerc b/other/astyle/astylerc index 249747aa..9554a3e5 100644 --- a/other/astyle/astylerc +++ b/other/astyle/astylerc @@ -16,7 +16,6 @@ --align-reference=name # Formatting Options ---add-brackets --convert-tabs --max-code-length=120 diff --git a/other/bootstrap_daemon/BUILD.bazel b/other/bootstrap_daemon/BUILD.bazel index dce64bbe..6d4b157b 100644 --- a/other/bootstrap_daemon/BUILD.bazel +++ b/other/bootstrap_daemon/BUILD.bazel @@ -18,7 +18,6 @@ cc_binary( "//c-toxcore/toxcore:mono_time", "//c-toxcore/toxcore:onion_announce", "//c-toxcore/toxcore:tox", - "//c-toxcore/toxcore:util", "@libconfig", ], ) diff --git a/other/bootstrap_daemon/README.md b/other/bootstrap_daemon/README.md index 0f5ff4b7..2bda810f 100644 --- a/other/bootstrap_daemon/README.md +++ b/other/bootstrap_daemon/README.md @@ -259,7 +259,7 @@ docker run -d --name tox-bootstrapd --restart always \ 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. +We create a new user and protect its home directory in order to mount it in the Docker image, so that the keypair 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 diff --git a/other/bootstrap_daemon/docker/Dockerfile b/other/bootstrap_daemon/docker/Dockerfile index babaf219..9fc0f517 100644 --- a/other/bootstrap_daemon/docker/Dockerfile +++ b/other/bootstrap_daemon/docker/Dockerfile @@ -12,8 +12,7 @@ RUN ["apk", "--no-cache", "add",\ "libsodium-static",\ "musl-dev",\ "ninja",\ - "python3"\ -] + "python3"] WORKDIR /src/c-toxcore @@ -49,8 +48,10 @@ RUN CC=clang cmake -B_build -H. \ # 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 +RUN SHA256="$(sha256sum /usr/local/bin/tox-bootstrapd)" && \ + (sha256sum -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256 || \ + (echo "::error file=other/bootstrap_daemon/docker/tox-bootstrapd.sha256,line=1::$SHA256" && \ + false)) # Remove all the example bootstrap nodes from the config file. COPY other/bootstrap_daemon/tox-bootstrapd.conf other/bootstrap_daemon/ diff --git a/other/bootstrap_daemon/docker/fetch-sha256 b/other/bootstrap_daemon/docker/fetch-sha256 new file mode 100755 index 00000000..53a9f721 --- /dev/null +++ b/other/bootstrap_daemon/docker/fetch-sha256 @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import json +import os +import pprint +import subprocess +import sys +import urllib.request +from typing import Any + +SHA256_FILE = "other/bootstrap_daemon/docker/tox-bootstrapd.sha256" + +with open(f"{os.environ['HOME']}/.github-token") as fh: + token = fh.read().strip() + +head_sha = (subprocess.run(["git", "rev-parse", "HEAD"], + capture_output=True, + check=True).stdout.decode("utf-8").strip()) + + +def request(url: str) -> Any: + return json.loads( + urllib.request.urlopen( + urllib.request.Request( + url, + headers={ + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + token, + "X-GitHub-Api-Version": "2022-11-28", + }, + )).read()) + + +pp = pprint.PrettyPrinter(indent=2, compact=True) +annots = [ + a for r in request( + f"https://api.github.com/repos/TokTok/c-toxcore/commits/{head_sha}/check-runs?per_page=100" + )["check_runs"] if r["name"] == "docker-bootstrap-node" + for a in request(r["output"]["annotations_url"]) + if a["path"] == SHA256_FILE +] +if not annots: + print("could not find sha256sum output") + sys.exit(1) +with open(SHA256_FILE, "w") as fh: + fh.write(annots[0]["message"] + "\n") + print(f"updated {SHA256_FILE}") diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index f8d00e17..3175359d 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -b0bd5099f2f77fbd540a5a929a23cece39ff95e3a66702a5381342d01775cbd3 /usr/local/bin/tox-bootstrapd +8fadc6fd894bb8f60fd53d51dcd51ff24e62b41ae404d3921e0247b9337c9a30 /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/command_line_arguments.c b/other/bootstrap_daemon/src/command_line_arguments.c index f7af443e..a2e696c3 100644 --- a/other/bootstrap_daemon/src/command_line_arguments.c +++ b/other/bootstrap_daemon/src/command_line_arguments.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2024 The TokTok team. * Copyright © 2015-2016 Tox project. */ @@ -10,12 +10,12 @@ #include "command_line_arguments.h" #include "global.h" +#include "log.h" #include "../../../toxcore/ccompat.h" #include -#include #include @@ -24,9 +24,9 @@ */ static void print_help(void) { - // 2 space ident - // make sure all lines fit into 80 columns - // make sure options are listed in alphabetical order + // 2 space indent + // Make sure all lines fit into 80 columns + // Make sure options are listed in alphabetical order log_write(LOG_LEVEL_INFO, "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n" "\n" @@ -39,7 +39,7 @@ static void print_help(void) " (detach from the terminal) and won't use the PID file.\n" " --help Print this help message.\n" " --log-backend=BACKEND Specify which logging backend to use.\n" - " Valid BACKEND values (case sensetive):\n" + " Valid BACKEND values (case sensitive):\n" " syslog Writes log messages to syslog.\n" " Default option when no --log-backend is\n" " specified.\n" @@ -47,13 +47,14 @@ static void print_help(void) " --version Print version information.\n"); } -void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, - bool *run_in_foreground) +Cli_Status handle_command_line_arguments( + int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, + bool *run_in_foreground) { if (argc < 2) { log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); print_help(); - exit(1); + return CLI_STATUS_ERROR; } opterr = 0; @@ -89,7 +90,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, case 'h': print_help(); - exit(0); + return CLI_STATUS_DONE; case 'l': if (strcmp(optarg, "syslog") == 0) { @@ -101,24 +102,24 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, } else { log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg); print_help(); - exit(1); + return CLI_STATUS_ERROR; } break; case 'v': log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER); - exit(0); + return CLI_STATUS_DONE; case '?': log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]); print_help(); - exit(1); + return CLI_STATUS_ERROR; case ':': log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]); print_help(); - exit(1); + return CLI_STATUS_ERROR; } } @@ -129,6 +130,8 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, if (!cfg_file_path_set) { log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n"); print_help(); - exit(1); + return CLI_STATUS_ERROR; } + + return CLI_STATUS_OK; } diff --git a/other/bootstrap_daemon/src/command_line_arguments.h b/other/bootstrap_daemon/src/command_line_arguments.h index 2cb8edd9..3595ece3 100644 --- a/other/bootstrap_daemon/src/command_line_arguments.h +++ b/other/bootstrap_daemon/src/command_line_arguments.h @@ -12,6 +12,15 @@ #include "log.h" +typedef enum Cli_Status { + /** Continue the program. Command line processing completed. */ + CLI_STATUS_OK, + /** Stop the program with success status. */ + CLI_STATUS_DONE, + /** Stop the program with error status. */ + CLI_STATUS_ERROR, +} Cli_Status; + /** * Handles command line arguments, setting cfg_file_path and log_backend. * Terminates the application if incorrect arguments are specified. @@ -22,7 +31,8 @@ * @param log_backend Sets to the provided by the user log backend option. * @param run_in_foreground Sets to the provided by the user foreground option. */ -void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, - bool *run_in_foreground); +Cli_Status handle_command_line_arguments( + int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, + bool *run_in_foreground); #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_COMMAND_LINE_ARGUMENTS_H diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index 9f579d92..b20925b1 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2023 The TokTok team. + * Copyright © 2016-2024 The TokTok team. * Copyright © 2014-2016 Tox project. */ @@ -10,6 +10,7 @@ #include "config.h" #include "config_defaults.h" +#include "global.h" #include "log.h" #include @@ -18,6 +19,10 @@ #include +#include "../../../toxcore/DHT.h" +#include "../../../toxcore/ccompat.h" +#include "../../../toxcore/crypto_core.h" +#include "../../../toxcore/network.h" #include "../../bootstrap_node_packets.h" /** @@ -51,7 +56,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por log_write(LOG_LEVEL_INFO, "Port #%zu: %u\n", i, default_ports[i]); } - // similar procedure to the one of reading config file below + // Similar procedure to the one of reading config file below *tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t)); for (size_t i = 0; i < default_ports_count; ++i) { @@ -68,7 +73,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por ++*tcp_relay_port_count; } - // the loop above skips invalid ports, so we adjust the allocated memory size + // The loop above skips invalid ports, so we adjust the allocated memory size if ((*tcp_relay_port_count) > 0) { *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); } else { @@ -98,7 +103,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por config_setting_t *elem = config_setting_get_elem(ports_array, i); if (elem == nullptr) { - // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be + // It's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); break; } @@ -120,7 +125,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por ++*tcp_relay_port_count; } - // the loop above skips invalid ports, so we adjust the allocated memory size + // The loop above skips invalid ports, so we adjust the allocated memory size if ((*tcp_relay_port_count) > 0) { *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t)); } else { @@ -260,7 +265,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); - // show info about tcp ports only if tcp relay is enabled + // Show info about tcp ports only if tcp relay is enabled if (*enable_tcp_relay) { if (*tcp_relay_port_count == 0) { log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); @@ -407,7 +412,7 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) log_write(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); next: - // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly + // config_setting_lookup_string() allocates string inside and doesn't allow us to free it directly // though it's freed when the element is removed, so we free it right away in order to keep memory // consumption minimal config_setting_remove_elem(node_list, 0); diff --git a/other/bootstrap_daemon/src/config.h b/other/bootstrap_daemon/src/config.h index 50507b55..ae9aff8c 100644 --- a/other/bootstrap_daemon/src/config.h +++ b/other/bootstrap_daemon/src/config.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2024 The TokTok team. * Copyright © 2014-2016 Tox project. */ @@ -30,7 +30,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k * Bootstraps off nodes listed in the config file. * * @return 1 on success, some or no bootstrap nodes were added - * 0 on failure, a error accured while parsing config file. + * 0 on failure, an error occurred while parsing the config file. */ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6); diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c index bc36b9c8..ed3bf03d 100644 --- a/other/bootstrap_daemon/src/log.c +++ b/other/bootstrap_daemon/src/log.c @@ -3,6 +3,8 @@ * Copyright © 2015-2016 Tox project. */ +#include + /* * Tox DHT bootstrap daemon. * Logging utility with support of multiple logging backends. diff --git a/other/bootstrap_daemon/src/log_backend_stdout.c b/other/bootstrap_daemon/src/log_backend_stdout.c index 3fac48c0..854c7678 100644 --- a/other/bootstrap_daemon/src/log_backend_stdout.c +++ b/other/bootstrap_daemon/src/log_backend_stdout.c @@ -9,8 +9,11 @@ */ #include "log_backend_stdout.h" +#include #include +#include "log.h" + static FILE *log_backend_stdout_level(LOG_LEVEL level) { switch (level) { diff --git a/other/bootstrap_daemon/src/log_backend_syslog.c b/other/bootstrap_daemon/src/log_backend_syslog.c index ad2bf4eb..1f73d9a6 100644 --- a/other/bootstrap_daemon/src/log_backend_syslog.c +++ b/other/bootstrap_daemon/src/log_backend_syslog.c @@ -10,10 +10,12 @@ #include "log_backend_syslog.h" #include "global.h" +#include "log.h" #include "../../../toxcore/ccompat.h" #include +#include #include #include #include diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 9330e8ce..45b3bfc4 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -23,17 +23,24 @@ #include #include #include +#include // toxcore -#include "../../../toxcore/tox.h" +#include "../../../toxcore/DHT.h" #include "../../../toxcore/LAN_discovery.h" #include "../../../toxcore/TCP_server.h" #include "../../../toxcore/announce.h" +#include "../../../toxcore/ccompat.h" +#include "../../../toxcore/crypto_core.h" +#include "../../../toxcore/forwarding.h" +#include "../../../toxcore/group_announce.h" #include "../../../toxcore/group_onion_announce.h" #include "../../../toxcore/logger.h" +#include "../../../toxcore/mem.h" #include "../../../toxcore/mono_time.h" +#include "../../../toxcore/network.h" +#include "../../../toxcore/onion.h" #include "../../../toxcore/onion_announce.h" -#include "../../../toxcore/util.h" // misc #include "../../bootstrap_node_packets.h" @@ -116,7 +123,7 @@ static void print_public_key(const uint8_t *public_key) // Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend // Terminates the application if the daemonization fails. -static void daemonize(LOG_BACKEND log_backend, char *pid_file_path) +static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path) { // Check if the PID file exists FILE *pid_file = fopen(pid_file_path, "r"); @@ -131,7 +138,7 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path) if (pid_file == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); - exit(1); + return CLI_STATUS_ERROR; } // Fork off from the parent process @@ -141,27 +148,27 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path) fprintf(pid_file, "%d", pid); fclose(pid_file); log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid); - exit(0); + return CLI_STATUS_DONE; } else { fclose(pid_file); } if (pid < 0) { log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n"); - exit(1); + return CLI_STATUS_ERROR; } // Create a new SID for the child process if (setsid() < 0) { log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n"); - exit(1); + return CLI_STATUS_ERROR; } // Change the current working directory if ((chdir("/")) < 0) { log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n"); - exit(1); + return CLI_STATUS_ERROR; } // Go quiet @@ -170,6 +177,8 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path) close(STDIN_FILENO); close(STDERR_FILENO); } + + return CLI_STATUS_OK; } // Logs toxcore logger message using our logger facility @@ -212,11 +221,18 @@ int main(int argc, char *argv[]) char *cfg_file_path = nullptr; bool run_in_foreground = false; - // choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal + // Choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal LOG_BACKEND log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG; log_open(log_backend); - handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground); + switch (handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground)) { + case CLI_STATUS_OK: + break; + case CLI_STATUS_DONE: + return 0; + case CLI_STATUS_ERROR: + return 1; + } log_close(); log_open(log_backend); @@ -254,7 +270,14 @@ int main(int argc, char *argv[]) } if (!run_in_foreground) { - daemonize(log_backend, pid_file_path); + switch (daemonize(log_backend, pid_file_path)) { + case CLI_STATUS_OK: + break; + case CLI_STATUS_DONE: + return 0; + case CLI_STATUS_ERROR: + return 1; + } } free(pid_file_path); diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c index 7ff0e9e7..fb009218 100644 --- a/other/bootstrap_node_packets.c +++ b/other/bootstrap_node_packets.c @@ -12,6 +12,8 @@ #include +#include "../toxcore/network.h" + #define INFO_REQUEST_PACKET_LENGTH 78 static uint32_t bootstrap_version; diff --git a/other/docker/autotools/Dockerfile b/other/docker/autotools/Dockerfile index e61c83f1..fc85f505 100644 --- a/other/docker/autotools/Dockerfile +++ b/other/docker/autotools/Dockerfile @@ -2,7 +2,8 @@ # autotools-linux FROM ubuntu:22.04 -RUN apt-get update && apt-get install --no-install-recommends -y \ +RUN apt-get update && \ + DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ autoconf \ automake \ ca-certificates \ diff --git a/other/docker/clang-tidy/Dockerfile b/other/docker/clang-tidy/Dockerfile new file mode 100644 index 00000000..eaf23f98 --- /dev/null +++ b/other/docker/clang-tidy/Dockerfile @@ -0,0 +1,21 @@ +FROM alpine:3.19.0 + +RUN ["apk", "add", "--no-cache", \ + "bash", \ + "clang", \ + "clang-extra-tools", \ + "cmake", \ + "colordiff", \ + "libconfig-dev", \ + "libsodium-dev", \ + "libvpx-dev", \ + "linux-headers", \ + "opus-dev", \ + "pkgconfig", \ + "samurai"] + +ENV CC=clang CXX=clang++ + +COPY . /c-toxcore/ +WORKDIR /c-toxcore +RUN other/analysis/run-clang-tidy diff --git a/other/docker/clang-tidy/run b/other/docker/clang-tidy/run new file mode 100755 index 00000000..62036edf --- /dev/null +++ b/other/docker/clang-tidy/run @@ -0,0 +1,5 @@ +#!/bin/sh + +set -eux +BUILD=clang-tidy +docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" . diff --git a/other/docker/coverage/Dockerfile b/other/docker/coverage/Dockerfile index 56f88064..42530fb7 100644 --- a/other/docker/coverage/Dockerfile +++ b/other/docker/coverage/Dockerfile @@ -19,11 +19,14 @@ RUN apt-get update && \ make \ ninja-build \ pkg-config \ + python3-lxml \ python3-pip \ python3-pygments \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && pip3 install --no-cache-dir gcovr +# strip gtest so it doesn't end up in stack traces. This speeds up the +# mallocfail run below by a lot. RUN ["strip", "-g",\ "/usr/lib/x86_64-linux-gnu/libgtest.a",\ "/usr/lib/x86_64-linux-gnu/libgtest_main.a"] diff --git a/other/docker/cppcheck/toxcore.cfg b/other/docker/cppcheck/toxcore.cfg index f120246a..b497f9ea 100644 --- a/other/docker/cppcheck/toxcore.cfg +++ b/other/docker/cppcheck/toxcore.cfg @@ -92,6 +92,10 @@ new_networking_no_udp kill_networking + + net_new_strerror + net_kill_strerror + new_onion kill_onion diff --git a/other/docker/misra/Dockerfile b/other/docker/misra/Dockerfile index 3efcdedb..d005b4c9 100644 --- a/other/docker/misra/Dockerfile +++ b/other/docker/misra/Dockerfile @@ -1,6 +1,7 @@ FROM ubuntu:20.04 -RUN apt-get update && apt-get install --no-install-recommends -y \ +RUN apt-get update && \ + DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ ca-certificates \ cppcheck \ libopus-dev \ diff --git a/other/docker/tokstyle/Dockerfile b/other/docker/tokstyle/Dockerfile index 446583b6..3bb677b5 100644 --- a/other/docker/tokstyle/Dockerfile +++ b/other/docker/tokstyle/Dockerfile @@ -1,7 +1,8 @@ FROM toxchat/haskell:hs-tokstyle AS tokstyle FROM ubuntu:22.04 -RUN apt-get update && apt-get install --no-install-recommends -y \ +RUN apt-get update && \ + DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ ca-certificates \ clang \ git \ diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index a3dd991e..5cdbfb9e 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -// Copyright © 2023 The TokTok team. +// Copyright © 2023-2024 The TokTok team. // this file can be used to generate event.c files // requires c++17 @@ -119,22 +119,54 @@ void generate_event_impl(const std::string& event_name, const std::vector -#include -#include +#include )"; + if (need_stdlib_h) { + f << R"( +#include )"; + } + if (need_string_h) { + f << R"( +#include )"; + } + f << R"( #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" -#include "../tox_events.h" -#include "../tox_unpack.h" +#include "../tox_events.h")"; + if (need_tox_unpack_h) { + f << R"( +#include "../tox_unpack.h")"; + } + f << R"( /***************************************************** diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index bb7a4930..ac19a6df 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -13,7 +13,6 @@ sh_test( args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [ "-Wno-boolean-return", "-Wno-callback-names", - "-Wno-enum-names", "+RTS", "-N4", "-RTS", diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index 82f228bf..d664c0fa 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt @@ -8,10 +8,19 @@ if(TARGET toxcore_static) else() target_link_libraries(misc_tools PRIVATE toxcore_shared) endif() -target_link_libraries(misc_tools PRIVATE ${LIBSODIUM_LIBRARIES}) -target_link_directories(misc_tools PUBLIC ${LIBSODIUM_LIBRARY_DIRS}) -target_include_directories(misc_tools SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS}) -target_compile_options(misc_tools PRIVATE ${LIBSODIUM_CFLAGS_OTHER}) +if(TARGET unofficial-sodium::sodium) + target_link_libraries(misc_tools PRIVATE unofficial-sodium::sodium) +else() + target_link_libraries(misc_tools PRIVATE ${LIBSODIUM_LIBRARIES}) + target_link_directories(misc_tools PUBLIC ${LIBSODIUM_LIBRARY_DIRS}) + target_include_directories(misc_tools SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS}) + target_compile_options(misc_tools PRIVATE ${LIBSODIUM_CFLAGS_OTHER}) +endif() +if(TARGET PThreads4W::PThreads4W) + target_link_libraries(misc_tools PRIVATE PThreads4W::PThreads4W) +elseif(TARGET Threads::Threads) + target_link_libraries(misc_tools PRIVATE Threads::Threads) +endif() ################################################################################ # @@ -27,4 +36,9 @@ if(BUILD_MISC_TESTS) else() target_link_libraries(Messenger_test PRIVATE toxcore_shared) endif() + if(TARGET PThreads4W::PThreads4W) + target_link_libraries(Messenger_test PRIVATE PThreads4W::PThreads4W) + elseif(TARGET Threads::Threads) + target_link_libraries(Messenger_test PRIVATE Threads::Threads) + endif() endif() diff --git a/testing/fuzzing/protodump_reduce.cc b/testing/fuzzing/protodump_reduce.cc index 2bfe3a9b..847ac0b2 100644 --- a/testing/fuzzing/protodump_reduce.cc +++ b/testing/fuzzing/protodump_reduce.cc @@ -4,6 +4,7 @@ #include "../../toxcore/tox.h" #include "../../toxcore/tox_dispatch.h" #include "../../toxcore/tox_events.h" +#include "../../toxcore/tox_private.h" #include "fuzz_support.h" #include "fuzz_tox.h" diff --git a/toxav/audio.c b/toxav/audio.c index 7792fc85..4070b25f 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -196,13 +196,13 @@ void ac_iterate(ACSession *ac) int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg) { - if (acp == nullptr || msg == nullptr) { + ACSession *ac = (ACSession *)acp; + + if (ac == nullptr || msg == nullptr) { free(msg); return -1; } - ACSession *ac = (ACSession *)acp; - if ((msg->header.pt & 0x7f) == (RTP_TYPE_AUDIO + 2) % 128) { LOGGER_WARNING(ac->log, "Got dummy!"); free(msg); diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index be2cd6b9..b051a1a3 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c @@ -208,6 +208,8 @@ static int bwc_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const ui static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object) { + BWController *bwc = (BWController *)object; + if (length - 1 != sizeof(struct BWCMessage)) { return -1; } @@ -218,5 +220,5 @@ static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *d offset += net_unpack_u32(data + offset, &msg.recv); assert(offset == length); - return on_update((BWController *)object, &msg); + return on_update(bwc, &msg); } diff --git a/toxav/groupav.c b/toxav/groupav.c index 6a48c684..a0e52036 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -290,8 +290,9 @@ static void group_av_peer_delete(void *object, uint32_t groupnumber, void *peer_ static void group_av_groupchat_delete(void *object, uint32_t groupnumber) { - if (object != nullptr) { - kill_group_av((Group_AV *)object); + Group_AV *group_av = (Group_AV *)object; + if (group_av != nullptr) { + kill_group_av(group_av); } } @@ -404,12 +405,13 @@ static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, uint3 static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_t friendgroupnumber, void *peer_object, const uint8_t *packet, uint16_t length) { - if (peer_object == nullptr || object == nullptr || length <= sizeof(uint16_t)) { + Group_AV *group_av = (Group_AV *)object; + Group_Peer_AV *peer_av = (Group_Peer_AV *)peer_object; + + if (group_av == nullptr || peer_av == nullptr || length <= sizeof(uint16_t)) { return -1; } - Group_Peer_AV *peer_av = (Group_Peer_AV *)peer_object; - Group_Audio_Packet *pk = (Group_Audio_Packet *)calloc(1, sizeof(Group_Audio_Packet)); if (pk == nullptr) { @@ -433,7 +435,7 @@ static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_ return -1; } - while (decode_audio_packet((Group_AV *)object, peer_av, groupnumber, friendgroupnumber) == 0) { + while (decode_audio_packet(group_av, peer_av, groupnumber, friendgroupnumber) == 0) { /* Continue. */ } diff --git a/toxav/msi.c b/toxav/msi.c index 2b45e7cb..6375995a 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -60,7 +60,7 @@ typedef struct MSIMessage { static void msg_init(MSIMessage *dest, MSIRequest request); static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length); -static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, +static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const uint8_t *value, uint8_t value_len, uint16_t *length); static int send_message(const Messenger *m, uint32_t friend_number, const MSIMessage *msg); static int send_error(const Messenger *m, uint32_t friend_number, MSIError error); @@ -425,7 +425,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data return 0; } -static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, +static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const uint8_t *value, uint8_t value_len, uint16_t *length) { /* Parse a single header for sending */ @@ -657,12 +657,13 @@ CLEAR_CONTAINER: } static void on_peer_status(Messenger *m, uint32_t friend_number, uint8_t status, void *data) { + MSISession *session = (MSISession *)data; + if (status != 0) { // Friend is online. return; } - MSISession *session = (MSISession *)data; LOGGER_DEBUG(m->log, "Friend %d is now offline", friend_number); pthread_mutex_lock(session->mutex); @@ -851,9 +852,10 @@ static void handle_pop(MSICall *call, const MSIMessage *msg) } static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object) { + MSISession *session = (MSISession *)object; + LOGGER_DEBUG(m->log, "Got msi message"); - MSISession *session = (MSISession *)object; MSIMessage msg; if (msg_parse_in(m->log, &msg, data, length) == -1) { diff --git a/toxav/toxav.h b/toxav/toxav.h index 86e54a6a..ff65c3fa 100644 --- a/toxav/toxav.h +++ b/toxav/toxav.h @@ -65,15 +65,17 @@ extern "C" { /** * External Tox type. */ +#ifndef APIGEN_IGNORE #ifndef TOX_DEFINED #define TOX_DEFINED typedef struct Tox Tox; -#endif /* TOX_DEFINED */ +#endif /* !TOX_DEFINED */ +#endif /* !APIGEN_IGNORE */ /** * @brief The ToxAV instance type. * - * Each ToxAV instance can be bound to only one Tox instance, and Tox instance + * Each ToxAV instance can be bound to only one Tox instance, and Tox instance * can have only one ToxAV instance. One must make sure to close ToxAV instance * prior closing Tox instance otherwise undefined behaviour occurs. Upon * closing of ToxAV instance, all active calls will be forcibly terminated @@ -605,7 +607,7 @@ typedef enum Toxav_Err_Send_Frame { * @param sampling_rate Audio sampling rate used in this frame. Valid sampling * rates are 8000, 12000, 16000, 24000, or 48000. */ -bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count, +bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t pcm[], size_t sample_count, uint8_t channels, uint32_t sampling_rate, Toxav_Err_Send_Frame *error); /** @@ -652,8 +654,12 @@ void toxav_callback_audio_bit_rate(ToxAV *av, toxav_audio_bit_rate_cb *callback, * @param u U (Chroma) plane data. * @param v V (Chroma) plane data. */ -bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t *y, - const uint8_t *u, const uint8_t *v, Toxav_Err_Send_Frame *error); +bool toxav_video_send_frame( + ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height, + const uint8_t y[/*! height * width */], + const uint8_t u[/*! height/2 * width/2 */], + const uint8_t v[/*! height/2 * width/2 */], + Toxav_Err_Send_Frame *error); /** * Set the bit rate to be used in subsequent video frames. @@ -703,7 +709,7 @@ void toxav_callback_video_bit_rate(ToxAV *av, toxav_video_bit_rate_cb *callback, * @param sampling_rate Sampling rate used in this frame. * */ -typedef void toxav_audio_receive_frame_cb(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count, +typedef void toxav_audio_receive_frame_cb(ToxAV *av, uint32_t friend_number, const int16_t pcm[], size_t sample_count, uint8_t channels, uint32_t sampling_rate, void *user_data); @@ -735,8 +741,13 @@ void toxav_callback_audio_receive_frame(ToxAV *av, toxav_audio_receive_frame_cb * @param ustride U chroma plane stride. * @param vstride V chroma plane stride. */ -typedef void toxav_video_receive_frame_cb(ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height, - const uint8_t *y, const uint8_t *u, const uint8_t *v, int32_t ystride, int32_t ustride, int32_t vstride, +typedef void toxav_video_receive_frame_cb( + ToxAV *av, uint32_t friend_number, + uint16_t width, uint16_t height, + const uint8_t y[/*! max(width, abs(ystride)) * height */], + const uint8_t u[/*! max(width/2, abs(ustride)) * (height/2) */], + const uint8_t v[/*! max(width/2, abs(vstride)) * (height/2) */], + int32_t ystride, int32_t ustride, int32_t vstride, void *user_data); @@ -746,6 +757,8 @@ typedef void toxav_video_receive_frame_cb(ToxAV *av, uint32_t friend_number, uin */ void toxav_callback_video_receive_frame(ToxAV *av, toxav_video_receive_frame_cb *callback, void *user_data); +#ifndef APIGEN_IGNORE + /*** * NOTE Compatibility with old toxav group calls. TODO(iphydf): remove * @@ -755,10 +768,10 @@ void toxav_callback_video_receive_frame(ToxAV *av, toxav_video_receive_frame_cb */ // TODO(iphydf): Use this better typed one instead of the void-pointer one below. -typedef void toxav_group_audio_cb(Tox *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm, +typedef void toxav_group_audio_cb(Tox *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t pcm[], uint32_t samples, uint8_t channels, uint32_t sample_rate, void *user_data); -typedef void toxav_audio_data_cb(void *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm, +typedef void toxav_audio_data_cb(void *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t pcm[], uint32_t samples, uint8_t channels, uint32_t sample_rate, void *userdata); /** @brief Create a new toxav group. @@ -768,7 +781,7 @@ typedef void toxav_audio_data_cb(void *tox, uint32_t groupnumber, uint32_t peern * * Note that total size of pcm in bytes is equal to `samples * channels * sizeof(int16_t)`. */ -int toxav_add_av_groupchat(Tox *tox, toxav_audio_data_cb *audio_callback, void *userdata); +int32_t toxav_add_av_groupchat(Tox *tox, toxav_audio_data_cb *audio_callback, void *userdata); /** @brief Join a AV group (you need to have been invited first). * @@ -777,8 +790,9 @@ int toxav_add_av_groupchat(Tox *tox, toxav_audio_data_cb *audio_callback, void * * * Note that total size of pcm in bytes is equal to `samples * channels * sizeof(int16_t)`. */ -int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data, uint16_t length, - toxav_audio_data_cb *audio_callback, void *userdata); +int32_t toxav_join_av_groupchat( + Tox *tox, uint32_t friendnumber, const uint8_t data[], uint16_t length, + toxav_audio_data_cb *audio_callback, void *userdata); /** @brief Send audio to the group chat. * @@ -794,8 +808,9 @@ int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data * * Recommended values are: samples = 960, channels = 1, sample_rate = 48000 */ -int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, - uint32_t sample_rate); +int32_t toxav_group_send_audio( + Tox *tox, uint32_t groupnumber, const int16_t pcm[], uint32_t samples, uint8_t channels, + uint32_t sample_rate); /** @brief Enable A/V in a groupchat. * @@ -812,19 +827,22 @@ int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, u * * Note that total size of pcm in bytes is equal to `samples * channels * sizeof(int16_t)`. */ -int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, - toxav_audio_data_cb *audio_callback, void *userdata); +int32_t toxav_groupchat_enable_av( + Tox *tox, uint32_t groupnumber, + toxav_audio_data_cb *audio_callback, void *userdata); /** @brief Disable A/V in a groupchat. * * @retval 0 on success. * @retval -1 on failure. */ -int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber); +int32_t toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber); /** @brief Return whether A/V is enabled in the groupchat. */ bool toxav_groupchat_av_enabled(Tox *tox, uint32_t groupnumber); +#endif /* !APIGEN_IGNORE */ + /** @} */ #ifdef __cplusplus @@ -834,6 +852,7 @@ bool toxav_groupchat_av_enabled(Tox *tox, uint32_t groupnumber); //!TOKSTYLE- #ifndef DOXYGEN_IGNORE +typedef ToxAV Toxav; typedef Toxav_Err_Call TOXAV_ERR_CALL; typedef Toxav_Err_New TOXAV_ERR_NEW; typedef Toxav_Err_Answer TOXAV_ERR_ANSWER; diff --git a/toxav/video.c b/toxav/video.c index cf5c3867..e8212cf2 100644 --- a/toxav/video.c +++ b/toxav/video.c @@ -345,17 +345,18 @@ void vc_iterate(VCSession *vc) int vc_queue_message(Mono_Time *mono_time, void *vcp, struct RTPMessage *msg) { + VCSession *vc = (VCSession *)vcp; + /* This function is called with complete messages * they have already been assembled. * this function gets called from handle_rtp_packet() and handle_rtp_packet_v3() */ - if (vcp == nullptr || msg == nullptr) { + if (vc == nullptr || msg == nullptr) { free(msg); return -1; } - VCSession *vc = (VCSession *)vcp; const struct RTPHeader *const header = &msg->header; if (msg->header.pt == (RTP_TYPE_VIDEO + 2) % 128) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 66a16600..42b89215 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -16,13 +16,15 @@ #include "LAN_discovery.h" #include "bin_pack.h" #include "ccompat.h" +#include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "ping.h" +#include "ping_array.h" #include "shared_key_cache.h" #include "state.h" -#include "util.h" /** The timeout after which a node is discarded completely. */ #define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) @@ -420,7 +422,8 @@ static bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port * non_null() static bool bin_pack_ip_port_handler(Bin_Pack *bp, const Logger *logger, const void *obj) { - return bin_pack_ip_port(bp, logger, (const IP_Port *)obj); + const IP_Port *ip_port = (const IP_Port *)obj; + return bin_pack_ip_port(bp, logger, ip_port); } int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port) @@ -508,7 +511,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool return -1; } - *ip_port = empty_ip_port; + ipport_reset(ip_port); if (is_ipv4) { const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); @@ -1477,12 +1480,12 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t non_null() static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata) { + DHT *const dht = (DHT *)object; + if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) { return 1; } - DHT *const dht = (DHT *)object; - /* Check if packet is from ourself. */ if (pk_equal(packet + 1, dht->self_public_key)) { return 1; @@ -2263,11 +2266,12 @@ non_null() static int handle_nat_ping(void *object, const IP_Port *source, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length, void *userdata) { + DHT *const dht = (DHT *)object; + if (length != sizeof(uint64_t) + 1) { return 1; } - DHT *const dht = (DHT *)object; uint64_t ping_id; memcpy(&ping_id, packet + 1, sizeof(uint64_t)); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 3e5ab36a..bc58f8e4 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -232,7 +232,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool non_null() int dht_create_packet(const Memory *mem, const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], - const uint8_t *shared_key, const uint8_t type, + const uint8_t *shared_key, uint8_t type, const uint8_t *plain, size_t plain_length, uint8_t *packet, size_t length); diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index ef44d3b5..db995520 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -32,7 +32,7 @@ #endif #ifdef __linux__ -#include +#include #endif #if defined(__FreeBSD__) || defined(__DragonFly__) @@ -41,7 +41,7 @@ #include "ccompat.h" #include "crypto_core.h" -#include "util.h" +#include "network.h" #define MAX_INTERFACES 16 @@ -243,11 +243,11 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) ip.ip.v6.uint8[15] = 0x01; } else if (net_family_is_ipv4(family_broadcast)) { ip.family = net_family_ipv6(); - ip.ip.v6 = ip6_broadcast; + ip.ip.v6 = get_ip6_broadcast(); } } else if (net_family_is_ipv4(family_socket) && net_family_is_ipv4(family_broadcast)) { ip.family = net_family_ipv4(); - ip.ip.v4 = ip4_broadcast; + ip.ip.v4 = get_ip4_broadcast(); } return ip; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 03872cc6..bdf17112 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -10,17 +10,33 @@ #include #include -#include #include #include #include "DHT.h" +#include "TCP_client.h" +#include "TCP_connection.h" +#include "TCP_server.h" +#include "announce.h" +#include "bin_pack.h" +#include "bin_unpack.h" #include "ccompat.h" +#include "crypto_core.h" +#include "forwarding.h" +#include "friend_connection.h" +#include "friend_requests.h" +#include "group_announce.h" #include "group_chats.h" +#include "group_common.h" #include "group_onion_announce.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "net_crypto.h" #include "network.h" +#include "onion.h" +#include "onion_announce.h" +#include "onion_client.h" #include "state.h" #include "util.h" @@ -2385,11 +2401,12 @@ static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uin non_null(1, 3) nullable(5) static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata) { + Messenger *m = (Messenger *)object; + if (len == 0) { return -1; } - Messenger *m = (Messenger *)object; const uint8_t packet_id = temp[0]; const uint8_t *data = temp + 1; const uint16_t data_length = len - 1; @@ -3176,7 +3193,8 @@ static void pack_groupchats(const GC_Session *c, Bin_Pack *bp) non_null() static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void *obj) { - pack_groupchats((const GC_Session *)obj, bp); + const GC_Session *session = (const GC_Session *)obj; + pack_groupchats(session, bp); return true; // TODO(iphydf): Return bool from pack functions. } diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index a698eca8..06476ebb 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -10,12 +10,17 @@ #include #include -#include #include +#include "DHT.h" #include "TCP_common.h" #include "ccompat.h" +#include "crypto_core.h" +#include "forwarding.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "network.h" #include "util.h" typedef struct TCP_Client_Conn { diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index 4d0d009f..157ec144 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -5,10 +5,13 @@ #include "TCP_common.h" -#include #include #include "ccompat.h" +#include "crypto_core.h" +#include "logger.h" +#include "mem.h" +#include "network.h" void wipe_priority_list(const Memory *mem, TCP_Priority_List *p) { diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index c6118cdd..8e54c2ab 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -9,12 +9,17 @@ #include "TCP_connection.h" #include -#include #include +#include "DHT.h" #include "TCP_client.h" #include "ccompat.h" +#include "crypto_core.h" +#include "forwarding.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "network.h" #include "util.h" struct TCP_Connections { @@ -1133,11 +1138,12 @@ non_null(1, 4) nullable(6) static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length, void *userdata) { + const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; + if (length == 0) { return -1; } - const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con); const unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); @@ -1164,11 +1170,12 @@ non_null() static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) { + const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object; + if (length == 0) { return -1; } - const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con); const unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index c07b176a..9809da13 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -8,7 +8,6 @@ */ #include "TCP_server.h" -#include #include #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) #include @@ -19,11 +18,17 @@ #include #endif +#include "DHT.h" #include "TCP_common.h" #include "ccompat.h" +#include "crypto_core.h" +#include "forwarding.h" #include "list.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" -#include "util.h" +#include "network.h" +#include "onion.h" #ifdef TCP_SERVER_USE_EPOLL #define TCP_SOCKET_LISTENING 0 @@ -909,7 +914,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns, if (!sock_valid(sock)) { LOGGER_ERROR(logger, "TCP socket creation failed (family = %d)", family.value); - return net_invalid_socket; + return net_invalid_socket(); } bool ok = set_socket_nonblock(ns, sock); @@ -930,7 +935,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns, port, family.value, error != nullptr ? error : "(null)"); net_kill_strerror(error); kill_sock(ns, sock); - return net_invalid_socket; + return net_invalid_socket(); } LOGGER_DEBUG(logger, "successfully bound to TCP port %d", port); diff --git a/toxcore/announce.c b/toxcore/announce.c index 5ac49867..19f15f41 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -12,8 +12,15 @@ #include #include +#include "DHT.h" #include "LAN_discovery.h" #include "ccompat.h" +#include "crypto_core.h" +#include "forwarding.h" +#include "logger.h" +#include "mem.h" +#include "mono_time.h" +#include "network.h" #include "shared_key_cache.h" #include "timed_auth.h" #include "util.h" @@ -427,7 +434,7 @@ static int create_reply_plain_store_announce_request(Announcements *announce, const uint8_t *to_auth, uint16_t to_auth_length) { const int plain_len = (int)length - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE); - const int announcement_len = (int)plain_len - (TIMED_AUTH_SIZE + sizeof(uint32_t) + 1); + const int announcement_len = plain_len - (TIMED_AUTH_SIZE + sizeof(uint32_t) + 1); const uint8_t *const data_public_key = data; diff --git a/toxcore/bin_pack.c b/toxcore/bin_pack.c index 51f2ff5b..774f03a6 100644 --- a/toxcore/bin_pack.c +++ b/toxcore/bin_pack.c @@ -10,6 +10,7 @@ #include "../third_party/cmp/cmp.h" #include "ccompat.h" +#include "logger.h" struct Bin_Pack { uint8_t *bytes; diff --git a/toxcore/ccompat.c b/toxcore/ccompat.c index 30e689d0..6e468973 100644 --- a/toxcore/ccompat.c +++ b/toxcore/ccompat.c @@ -2,3 +2,5 @@ * Copyright © 2022 The TokTok team. */ #include "ccompat.h" + +static_assert(sizeof(int) >= 4, "toxcore does not support 16-bit platforms"); diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index 74935e76..8fd64541 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index f47b72ec..e62b01dd 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 7f79e7ae..2d32166b 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 049f3e38..1bcb685d 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 2a056945..a9be07fc 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 5cb4e676..9bec8cf6 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index d470351d..69d5cc31 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -5,9 +5,11 @@ #include "events_alloc.h" #include -#include #include "../ccompat.h" +#include "../mem.h" +#include "../tox_event.h" +#include "../tox_events.h" Tox_Events_State *tox_events_alloc(void *user_data) { diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index 90b6f9fc..11ac6c99 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -2,8 +2,8 @@ * Copyright © 2022 The TokTok team. */ -#ifndef C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H -#define C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H +#ifndef C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H +#define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #include "../attributes.h" #include "../bin_pack.h" @@ -84,4 +84,4 @@ bool tox_events_add(Tox_Events *events, const Tox_Event *event); } #endif -#endif // C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H +#endif // C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 8734c6d9..ae8269c6 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 5c211623..238550e4 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index bec4b386..295c37ce 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index b1a87f34..bfff9a05 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 64553b42..a84ac25e 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 1999d4cb..1005df06 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 010bd938..3a2b80a8 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 8a0fc555..6a61a314 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index a526acb5..cad85237 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 23d2ff32..bf53234f 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 2b9424e5..7f68a43d 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -5,12 +5,12 @@ #include "events_alloc.h" #include -#include #include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 1d4b65b3..794aa668 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index e2613f75..99628ba6 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index ffbe44b1..563cbbc8 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c index fb29dfee..999a5ec0 100644 --- a/toxcore/events/group_custom_packet.c +++ b/toxcore/events/group_custom_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c index 12c5725c..e1f1e476 100644 --- a/toxcore/events/group_custom_private_packet.c +++ b/toxcore/events/group_custom_private_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c index 6e6fdb25..9eeaec43 100644 --- a/toxcore/events/group_invite.c +++ b/toxcore/events/group_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c index 154523ea..0e526131 100644 --- a/toxcore/events/group_join_fail.c +++ b/toxcore/events/group_join_fail.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c index f3413a9d..7a821c9a 100644 --- a/toxcore/events/group_message.c +++ b/toxcore/events/group_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c index 7e573753..74a5e9d0 100644 --- a/toxcore/events/group_moderation.c +++ b/toxcore/events/group_moderation.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c index 82f9166e..b15d0f81 100644 --- a/toxcore/events/group_password.c +++ b/toxcore/events/group_password.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c index 3a800a99..a7f54bd9 100644 --- a/toxcore/events/group_peer_exit.c +++ b/toxcore/events/group_peer_exit.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c index f9cc1758..f19ab492 100644 --- a/toxcore/events/group_peer_join.c +++ b/toxcore/events/group_peer_join.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c index 6a2b72cb..47cdb5b5 100644 --- a/toxcore/events/group_peer_limit.c +++ b/toxcore/events/group_peer_limit.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c index 56b67dd9..d58b2384 100644 --- a/toxcore/events/group_peer_name.c +++ b/toxcore/events/group_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c index 4fdefa94..cbf5f2cb 100644 --- a/toxcore/events/group_peer_status.c +++ b/toxcore/events/group_peer_status.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c index b0b4967d..330ed224 100644 --- a/toxcore/events/group_privacy_state.c +++ b/toxcore/events/group_privacy_state.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c index 9b588add..14784426 100644 --- a/toxcore/events/group_private_message.c +++ b/toxcore/events/group_private_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,6 +11,7 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c index a8ad880f..5d23cdea 100644 --- a/toxcore/events/group_self_join.c +++ b/toxcore/events/group_self_join.c @@ -1,19 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c index fab0a8a4..49f90ebf 100644 --- a/toxcore/events/group_topic.c +++ b/toxcore/events/group_topic.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" @@ -11,9 +11,9 @@ #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c index a628f8ae..1f301667 100644 --- a/toxcore/events/group_topic_lock.c +++ b/toxcore/events/group_topic_lock.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c index c1c402d8..8ed19827 100644 --- a/toxcore/events/group_voice_state.c +++ b/toxcore/events/group_voice_state.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 95e06a7c..60043a4b 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -1,16 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023 The TokTok team. + * Copyright © 2023-2024 The TokTok team. */ #include "events_alloc.h" #include -#include -#include #include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" +#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_unpack.h" diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index be96a21d..37a0f841 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -10,6 +10,10 @@ #include "DHT.h" #include "ccompat.h" +#include "crypto_core.h" +#include "logger.h" +#include "mono_time.h" +#include "network.h" #include "timed_auth.h" struct Forwarding { diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 2326560c..f41563e1 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -11,9 +11,16 @@ #include #include +#include "DHT.h" +#include "LAN_discovery.h" +#include "TCP_connection.h" #include "ccompat.h" +#include "crypto_core.h" +#include "logger.h" #include "mono_time.h" -#include "util.h" +#include "net_crypto.h" +#include "network.h" +#include "onion_client.h" #define PORTS_PER_DISCOVERY 10 @@ -467,11 +474,12 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub non_null() static int handle_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata) { + Friend_Connections *const fr_c = (Friend_Connections *)object; + if (length == 0) { return -1; } - Friend_Connections *const fr_c = (Friend_Connections *)object; Friend_Conn *friend_con = get_conn(fr_c, number); if (friend_con == nullptr) { @@ -526,11 +534,12 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t non_null() static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata) { + const Friend_Connections *const fr_c = (const Friend_Connections *)object; + if (length == 0) { return -1; } - const Friend_Connections *const fr_c = (const Friend_Connections *)object; const Friend_Conn *friend_con = get_conn(fr_c, number); if (friend_con == nullptr) { diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 7f18b1ff..c780f3e3 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -12,7 +12,9 @@ #include #include "ccompat.h" -#include "util.h" +#include "crypto_core.h" +#include "friend_connection.h" +#include "onion_client.h" /** * NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. diff --git a/toxcore/group.c b/toxcore/group.c index 57a81ef2..0086a17e 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -12,8 +12,16 @@ #include #include +#include "DHT.h" +#include "Messenger.h" #include "ccompat.h" +#include "crypto_core.h" +#include "friend_connection.h" +#include "group_common.h" +#include "logger.h" #include "mono_time.h" +#include "net_crypto.h" +#include "network.h" #include "state.h" #include "util.h" diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index 896b043d..3308ecbd 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -8,10 +8,11 @@ #include #include -#include "LAN_discovery.h" +#include "DHT.h" #include "ccompat.h" +#include "logger.h" #include "mono_time.h" -#include "util.h" +#include "network.h" /** * Removes `announces` from `gc_announces_list`. diff --git a/toxcore/group_announce.h b/toxcore/group_announce.h index 801363d6..19ca2985 100644 --- a/toxcore/group_announce.h +++ b/toxcore/group_announce.h @@ -6,8 +6,8 @@ /** * Similar to ping.h, but designed for group chat purposes */ -#ifndef GROUP_ANNOUNCE_H -#define GROUP_ANNOUNCE_H +#ifndef C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H +#define C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H #include @@ -215,4 +215,4 @@ bool gca_is_valid_announce(const GC_Announce *announce); } // extern "C" #endif -#endif // GROUP_ANNOUNCE_H +#endif // C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index 4f0eac6c..6252e426 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -9,7 +9,7 @@ namespace { struct Announces : ::testing::Test { protected: const Memory *mem_ = system_memory(); - uint64_t clock_ = 0; + uint64_t clock_ = 1000; Mono_Time *mono_time_ = nullptr; GC_Announces_List *gca_ = nullptr; GC_Announce _ann1; @@ -141,7 +141,7 @@ protected: ann2.ip_port_is_set = 1; ann2.tcp_relays_count = 1; ann2.tcp_relays[0].ip_port.ip.family = net_family_ipv4(); - ann2.tcp_relays[0].ip_port.ip.ip.v4 = ip4_broadcast; + ann2.tcp_relays[0].ip_port.ip.ip.v4 = get_ip4_broadcast(); ann2.tcp_relays[0].public_key[0] = 0xea; } diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 901c73d5..e8f21371 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -9,23 +9,31 @@ #include "group_chats.h" -#include - #include +#include +#include #include #include "DHT.h" -#include "LAN_discovery.h" #include "Messenger.h" #include "TCP_connection.h" +#include "bin_pack.h" +#include "bin_unpack.h" #include "ccompat.h" +#include "crypto_core.h" #include "friend_connection.h" +#include "group_announce.h" #include "group_common.h" +#include "group_connection.h" #include "group_moderation.h" #include "group_pack.h" +#include "logger.h" #include "mono_time.h" +#include "net_crypto.h" #include "network.h" +#include "onion_announce.h" +#include "onion_client.h" #include "util.h" /* The minimum size of a plaintext group handshake packet */ @@ -1374,7 +1382,7 @@ static int make_gc_shared_state_packet(const GC_Chat *chat, uint8_t *data, uint1 return -1; } - return (int)(header_len + packed_len); + return header_len + packed_len; } /** @brief Creates a signature for the group's shared state in packed form. @@ -3241,7 +3249,7 @@ static int make_gc_sanctions_list_packet(const GC_Chat *chat, uint8_t *data, uin return -1; } - return (int)(length + packed_len); + return length + packed_len; } /** @brief Sends the sanctions list to peer. diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 920e1a67..55fc199b 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -7,8 +7,8 @@ * An implementation of massive text only group chats. */ -#ifndef GROUP_CHATS_H -#define GROUP_CHATS_H +#ifndef C_TOXCORE_TOXCORE_GROUP_CHATS_H +#define C_TOXCORE_TOXCORE_GROUP_CHATS_H #include #include @@ -805,4 +805,4 @@ GC_Chat *gc_get_group_by_public_key(const GC_Session *c, const uint8_t *public_k non_null() int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uint8_t gc_announces_count); -#endif // GROUP_CHATS_H +#endif // C_TOXCORE_TOXCORE_GROUP_CHATS_H diff --git a/toxcore/group_common.h b/toxcore/group_common.h index 7daec303..49c21369 100644 --- a/toxcore/group_common.h +++ b/toxcore/group_common.h @@ -6,8 +6,8 @@ * Common groupchat data structures. */ -#ifndef GROUP_COMMON_H -#define GROUP_COMMON_H +#ifndef C_TOXCORE_TOXCORE_GROUP_COMMON_H +#define C_TOXCORE_TOXCORE_GROUP_COMMON_H #include #include @@ -410,4 +410,4 @@ int unpack_gc_saved_peers(GC_Chat *chat, const uint8_t *data, uint16_t length); non_null(1, 2) nullable(4) int pack_gc_saved_peers(const GC_Chat *chat, uint8_t *data, uint16_t length, uint16_t *processed); -#endif // GROUP_COMMON_H +#endif // C_TOXCORE_TOXCORE_GROUP_COMMON_H diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 9d919361..2a8045a8 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -15,11 +15,14 @@ #include #include "DHT.h" +#include "TCP_connection.h" #include "ccompat.h" #include "crypto_core.h" #include "group_chats.h" #include "group_common.h" +#include "logger.h" #include "mono_time.h" +#include "network.h" #include "util.h" /** Seconds since last direct UDP packet was received before the connection is considered dead */ diff --git a/toxcore/group_connection.h b/toxcore/group_connection.h index 2202c7ab..5987f010 100644 --- a/toxcore/group_connection.h +++ b/toxcore/group_connection.h @@ -7,8 +7,8 @@ * An implementation of massive text only group chats. */ -#ifndef GROUP_CONNECTION_H -#define GROUP_CONNECTION_H +#ifndef C_TOXCORE_TOXCORE_GROUP_CONNECTION_H +#define C_TOXCORE_TOXCORE_GROUP_CONNECTION_H #include "group_common.h" @@ -186,4 +186,4 @@ void gcc_peer_cleanup(GC_Connection *gconn); non_null() void gcc_cleanup(const GC_Chat *chat); -#endif // GROUP_CONNECTION_H +#endif // C_TOXCORE_TOXCORE_GROUP_CONNECTION_H diff --git a/toxcore/group_moderation.c b/toxcore/group_moderation.c index eeed26d0..90a95e17 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -15,9 +15,10 @@ #include #include +#include "DHT.h" #include "ccompat.h" #include "crypto_core.h" -#include "mono_time.h" +#include "logger.h" #include "network.h" #include "util.h" @@ -305,7 +306,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc return -1; } - return (int)(packed_len + cred_len); + return packed_len + cred_len; } uint16_t sanctions_creds_unpack(Mod_Sanction_Creds *creds, const uint8_t *data) @@ -457,7 +458,7 @@ static bool sanctions_list_validate_entry(const Moderation *moderation, const Mo uint8_t packed_data[MOD_SANCTION_PACKED_SIZE]; const int packed_len = sanctions_list_pack(packed_data, sizeof(packed_data), sanction, 1, nullptr); - if (packed_len <= (int) SIGNATURE_SIZE) { + if (packed_len <= SIGNATURE_SIZE) { return false; } @@ -785,7 +786,7 @@ static bool sanctions_list_sign_entry(const Moderation *moderation, Mod_Sanction uint8_t packed_data[MOD_SANCTION_PACKED_SIZE]; const int packed_len = sanctions_list_pack(packed_data, sizeof(packed_data), sanction, 1, nullptr); - if (packed_len <= (int) SIGNATURE_SIZE) { + if (packed_len <= SIGNATURE_SIZE) { LOGGER_ERROR(moderation->log, "Failed to pack sanctions list: %d", packed_len); return false; } diff --git a/toxcore/group_moderation_test.cc b/toxcore/group_moderation_test.cc index e5512289..3524abef 100644 --- a/toxcore/group_moderation_test.cc +++ b/toxcore/group_moderation_test.cc @@ -79,7 +79,7 @@ TEST(ModList, UnpackingFromEmptyBufferFails) std::vector packed(1); Moderation mods{system_memory()}; - EXPECT_EQ(mod_list_unpack(&mods, packed.end().base(), 0, 1), -1); + EXPECT_EQ(mod_list_unpack(&mods, packed.data(), 0, 1), -1); } TEST(ModList, HashOfEmptyModListZeroesOutBuffer) diff --git a/toxcore/group_onion_announce.c b/toxcore/group_onion_announce.c index 7efb4af6..16368e5c 100644 --- a/toxcore/group_onion_announce.c +++ b/toxcore/group_onion_announce.c @@ -9,6 +9,12 @@ #include #include "ccompat.h" +#include "crypto_core.h" +#include "group_announce.h" +#include "logger.h" +#include "mono_time.h" +#include "network.h" +#include "onion_announce.h" static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE, "GC_Announce does not fit into the onion packet extra data"); diff --git a/toxcore/group_pack.c b/toxcore/group_pack.c index c681d821..f0351650 100644 --- a/toxcore/group_pack.c +++ b/toxcore/group_pack.c @@ -13,9 +13,13 @@ #include #include +#include "DHT.h" #include "bin_pack.h" #include "bin_unpack.h" #include "ccompat.h" +#include "group_common.h" +#include "group_moderation.h" +#include "logger.h" #include "util.h" bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out) @@ -428,7 +432,7 @@ static void save_pack_self_info(const GC_Chat *chat, Bin_Pack *bp) bin_pack_u16(bp, self->nick_length); // 1 bin_pack_u08(bp, (uint8_t)self->role); // 2 - bin_pack_u08(bp, (uint8_t)self->status); // 3 + bin_pack_u08(bp, self->status); // 3 bin_pack_bin(bp, self->nick, self->nick_length); // 4 } diff --git a/toxcore/group_pack.h b/toxcore/group_pack.h index deea3fab..70b0df8d 100644 --- a/toxcore/group_pack.h +++ b/toxcore/group_pack.h @@ -7,8 +7,8 @@ * Packer and unpacker functions for saving and loading groups. */ -#ifndef GROUP_PACK_H -#define GROUP_PACK_H +#ifndef C_TOXCORE_TOXCORE_GROUP_PACK_H +#define C_TOXCORE_TOXCORE_GROUP_PACK_H #include @@ -37,4 +37,4 @@ bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out); non_null() bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out); -#endif // GROUP_PACK_H +#endif // C_TOXCORE_TOXCORE_GROUP_PACK_H diff --git a/toxcore/list.c b/toxcore/list.c index e3674873..3f6ad228 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -10,6 +10,7 @@ */ #include "list.h" +#include #include #include #include @@ -206,6 +207,7 @@ bool bs_list_add(BS_List *list, const uint8_t *data, int id) } // insert data to element array + assert(list->data != nullptr); memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, (list->n - i) * list->element_size); memcpy(list->data + i * list->element_size, data, list->element_size); diff --git a/toxcore/logger.c b/toxcore/logger.c index 6c532623..468e9ad5 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c @@ -8,7 +8,6 @@ */ #include "logger.h" -#include #include #include #include diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index 7076cb07..a3e5baa0 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2020 The TokTok team. + * Copyright © 2016-2023 The TokTok team. * Copyright © 2014 Tox project. */ #ifndef _XOPEN_SOURCE @@ -26,24 +26,20 @@ #include #endif +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION #include +#endif #include -#include #include #include "ccompat.h" +#include "mem.h" #include "util.h" /** don't call into system billions of times for no reason */ struct Mono_Time { uint64_t cur_time; uint64_t base_time; -#ifdef OS_WIN32 - /* protect `last_clock_update` and `last_clock_mono` from concurrent access */ - pthread_mutex_t last_clock_lock; - uint32_t last_clock_mono; - bool last_clock_update; -#endif #ifndef ESP_PLATFORM /* protect `time` from concurrent access */ @@ -54,42 +50,33 @@ struct Mono_Time { void *user_data; }; +static uint64_t timespec_to_u64(struct timespec clock_mono) +{ + return UINT64_C(1000) * clock_mono.tv_sec + (clock_mono.tv_nsec / UINT64_C(1000000)); +} + #ifdef OS_WIN32 non_null() static uint64_t current_time_monotonic_default(void *user_data) { - Mono_Time *const mono_time = (Mono_Time *)user_data; - - /* Must hold mono_time->last_clock_lock here */ - - /* GetTickCount provides only a 32 bit counter, but we can't use - * GetTickCount64 for backwards compatibility, so we handle wraparound - * ourselves. - */ - const uint32_t ticks = GetTickCount(); - - /* the higher 32 bits count the number of wrap arounds */ - uint64_t old_ovf = mono_time->cur_time & ~((uint64_t)UINT32_MAX); - - /* Check if time has decreased because of 32 bit wrap from GetTickCount() */ - if (ticks < mono_time->last_clock_mono) { - /* account for overflow */ - old_ovf += UINT32_MAX + UINT64_C(1); + LARGE_INTEGER freq; + LARGE_INTEGER count; + if (!QueryPerformanceFrequency(&freq)) { + return 0; } - - if (mono_time->last_clock_update) { - mono_time->last_clock_mono = ticks; - mono_time->last_clock_update = false; + if (!QueryPerformanceCounter(&count)) { + return 0; } - - /* splice the low and high bits back together */ - return old_ovf + ticks; -} -#else // !OS_WIN32 -static uint64_t timespec_to_u64(struct timespec clock_mono) -{ - return 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL); + struct timespec sp = {0}; + sp.tv_sec = count.QuadPart / freq.QuadPart; + if (freq.QuadPart < 1000000000) { + sp.tv_nsec = (count.QuadPart % freq.QuadPart) * 1000000000 / freq.QuadPart; + } else { + sp.tv_nsec = (long)((count.QuadPart % freq.QuadPart) * (1000000000.0 / freq.QuadPart)); + } + return timespec_to_u64(sp); } +#else #ifdef __APPLE__ non_null() static uint64_t current_time_monotonic_default(void *user_data) @@ -148,19 +135,6 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t mono_time_set_current_time_callback(mono_time, current_time_callback, user_data); -#ifdef OS_WIN32 - - mono_time->last_clock_mono = 0; - mono_time->last_clock_update = false; - - if (pthread_mutex_init(&mono_time->last_clock_lock, nullptr) < 0) { - mem_delete(mem, mono_time->time_update_lock); - mem_delete(mem, mono_time); - return nullptr; - } - -#endif - mono_time->cur_time = 0; #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION // Maximum reproducibility. Never return time = 0. @@ -168,7 +142,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t #else // Never return time = 0 in case time() returns 0 (e.g. on microcontrollers // without battery-powered RTC or ones where NTP didn't initialise it yet). - mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * 1000ULL - current_time_monotonic(mono_time); + mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * UINT64_C(1000) - current_time_monotonic(mono_time); #endif mono_time_update(mono_time); @@ -181,9 +155,6 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time) if (mono_time == nullptr) { return; } -#ifdef OS_WIN32 - pthread_mutex_destroy(&mono_time->last_clock_lock); -#endif #ifndef ESP_PLATFORM pthread_rwlock_destroy(mono_time->time_update_lock); mem_delete(mem, mono_time->time_update_lock); @@ -193,16 +164,8 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time) void mono_time_update(Mono_Time *mono_time) { -#ifdef OS_WIN32 - /* we actually want to update the overflow state of mono_time here */ - pthread_mutex_lock(&mono_time->last_clock_lock); - mono_time->last_clock_update = true; -#endif const uint64_t cur_time = mono_time->base_time + mono_time->current_time_callback(mono_time->user_data); -#ifdef OS_WIN32 - pthread_mutex_unlock(&mono_time->last_clock_lock); -#endif #ifndef ESP_PLATFORM pthread_rwlock_wrlock(mono_time->time_update_lock); @@ -228,7 +191,7 @@ uint64_t mono_time_get_ms(const Mono_Time *mono_time) uint64_t mono_time_get(const Mono_Time *mono_time) { - return mono_time_get_ms(mono_time) / 1000ULL; + return mono_time_get_ms(mono_time) / UINT64_C(1000); } bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout) @@ -255,15 +218,5 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time, */ uint64_t current_time_monotonic(Mono_Time *mono_time) { - /* For WIN32 we don't want to change overflow state of mono_time here */ -#ifdef OS_WIN32 - /* We don't want to update the overflow state of mono_time here, - * but must protect against other threads */ - pthread_mutex_lock(&mono_time->last_clock_lock); -#endif - const uint64_t cur_time = mono_time->current_time_callback(mono_time->user_data); -#ifdef OS_WIN32 - pthread_mutex_unlock(&mono_time->last_clock_lock); -#endif - return cur_time; + return mono_time->current_time_callback(mono_time->user_data); } diff --git a/toxcore/mono_time_test.cc b/toxcore/mono_time_test.cc index cc667199..3a649b7b 100644 --- a/toxcore/mono_time_test.cc +++ b/toxcore/mono_time_test.cc @@ -53,11 +53,14 @@ TEST(MonoTime, IsTimeoutReal) uint64_t const start = mono_time_get(mono_time); EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5)); + const uint64_t before_sleep = mono_time_get(mono_time); std::this_thread::sleep_for(std::chrono::milliseconds(100)); mono_time_update(mono_time); + const uint64_t after_sleep = mono_time_get(mono_time); // should still not have timed out (5sec) after sleeping ~100ms - EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5)); + EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5)) + << "before sleep: " << before_sleep << ", after sleep: " << after_sleep; mono_time_free(mem, mono_time); } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index cf11ef15..52bc2e84 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -10,13 +10,19 @@ */ #include "net_crypto.h" -#include -#include #include +#include "DHT.h" +#include "LAN_discovery.h" +#include "TCP_client.h" +#include "TCP_connection.h" #include "ccompat.h" +#include "crypto_core.h" #include "list.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "network.h" #include "util.h" typedef struct Packet_Data { diff --git a/toxcore/network.c b/toxcore/network.c index 726a3410..b583fd2f 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -89,7 +89,7 @@ #include "ccompat.h" #include "logger.h" -#include "mono_time.h" +#include "mem.h" #include "util.h" // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD @@ -340,12 +340,20 @@ static void fill_addr6(const IP6 *ip, struct in6_addr *addr) #endif static const IP empty_ip = {{0}}; -const IP_Port empty_ip_port = {{{0}}}; -const IP4 ip4_broadcast = { INADDR_BROADCAST }; -const IP6 ip6_broadcast = { - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } -}; +IP4 get_ip4_broadcast(void) +{ + const IP4 ip4_broadcast = { INADDR_BROADCAST }; + return ip4_broadcast; +} + +IP6 get_ip6_broadcast(void) +{ + const IP6 ip6_broadcast = { + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } + }; + return ip6_broadcast; +} IP4 get_ip4_loopback(void) { @@ -357,7 +365,7 @@ IP4 get_ip4_loopback(void) IP6 get_ip6_loopback(void) { /* in6addr_loopback isn't available everywhere, so we do it ourselves. */ - IP6 loopback = empty_ip_port.ip.ip.v6; + IP6 loopback = empty_ip.ip.v6; loopback.uint8[15] = 1; return loopback; } @@ -366,7 +374,11 @@ IP6 get_ip6_loopback(void) #define INVALID_SOCKET (-1) #endif -const Socket net_invalid_socket = { (int)INVALID_SOCKET }; +Socket net_invalid_socket(void) +{ + const Socket invalid_socket = { (int)INVALID_SOCKET }; + return invalid_socket; +} Family net_family_unspec(void) { @@ -460,7 +472,8 @@ bool net_family_is_tox_tcp_ipv6(Family family) bool sock_valid(Socket sock) { - return sock.sock != net_invalid_socket.sock; + const Socket invalid_socket = net_invalid_socket(); + return sock.sock != invalid_socket.sock; } struct Network_Addr { @@ -1447,6 +1460,7 @@ void ipport_reset(IP_Port *ipport) return; } + const IP_Port empty_ip_port = {{{0}}}; *ipport = empty_ip_port; } @@ -1748,8 +1762,8 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por // Non-blocking socket: "Operation in progress" means it's connecting. if (!should_ignore_connect_error(error)) { char *net_strerror = net_new_strerror(error); - LOGGER_ERROR(log, "failed to connect to %s:%d: %d (%s)", - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror); + LOGGER_WARNING(log, "failed to connect to %s:%d: %d (%s)", + net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror); net_kill_strerror(net_strerror); return false; } diff --git a/toxcore/network.h b/toxcore/network.h index d6056764..be7c52f7 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -183,7 +183,7 @@ typedef union IP4 { } IP4; IP4 get_ip4_loopback(void); -extern const IP4 ip4_broadcast; +IP4 get_ip4_broadcast(void); typedef union IP6 { uint8_t uint8[16]; @@ -193,7 +193,7 @@ typedef union IP6 { } IP6; IP6 get_ip6_loopback(void); -extern const IP6 ip6_broadcast; +IP6 get_ip6_broadcast(void); typedef union IP_Union { IP4 v4; @@ -210,8 +210,6 @@ typedef struct IP_Port { uint16_t port; } IP_Port; -extern const IP_Port empty_ip_port; - typedef struct Socket { int sock; } Socket; @@ -226,7 +224,7 @@ Socket net_socket(const Network *ns, Family domain, int type, int protocol); */ bool sock_valid(Socket sock); -extern const Socket net_invalid_socket; +Socket net_invalid_socket(void); /** * Calls send(sockfd, buf, len, MSG_NOSIGNAL). diff --git a/toxcore/onion.c b/toxcore/onion.c index 3e515752..ae201cf7 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -9,12 +9,16 @@ #include "onion.h" #include -#include #include +#include "DHT.h" #include "ccompat.h" +#include "crypto_core.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" -#include "util.h" +#include "network.h" +#include "shared_key_cache.h" #define RETURN_1 ONION_RETURN_1 #define RETURN_2 ONION_RETURN_2 diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index ffe368e3..fe489c37 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -15,9 +15,14 @@ #include "DHT.h" #include "LAN_discovery.h" #include "ccompat.h" +#include "crypto_core.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "network.h" +#include "onion.h" #include "shared_key_cache.h" -#include "util.h" +#include "timed_auth.h" #define PING_ID_TIMEOUT ONION_ANNOUNCE_TIMEOUT diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 53718ffc..56441ce1 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -13,10 +13,20 @@ #include #include +#include "DHT.h" #include "LAN_discovery.h" +#include "TCP_connection.h" #include "ccompat.h" +#include "crypto_core.h" #include "group_onion_announce.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" +#include "net_crypto.h" +#include "network.h" +#include "onion.h" +#include "onion_announce.h" +#include "ping_array.h" #include "util.h" /** @brief defines for the array size and timeout for onion announce packets. */ diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index d8a2e875..0b8fbffb 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -228,13 +228,13 @@ Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c); typedef struct Onion_Friend Onion_Friend; -non_null() uint16_t onion_get_friend_count(const Onion_Client *const onion_c); -non_null() Onion_Friend *onion_get_friend(const Onion_Client *const onion_c, uint16_t friend_num); -non_null() const uint8_t *onion_friend_get_gc_public_key(const Onion_Friend *const onion_friend); -non_null() const uint8_t *onion_friend_get_gc_public_key_num(const Onion_Client *const onion_c, uint32_t num); -non_null() void onion_friend_set_gc_public_key(Onion_Friend *const onion_friend, const uint8_t *public_key); +non_null() uint16_t onion_get_friend_count(const Onion_Client *onion_c); +non_null() Onion_Friend *onion_get_friend(const Onion_Client *onion_c, uint16_t friend_num); +non_null() const uint8_t *onion_friend_get_gc_public_key(const Onion_Friend *onion_friend); +non_null() const uint8_t *onion_friend_get_gc_public_key_num(const Onion_Client *onion_c, uint32_t num); +non_null() void onion_friend_set_gc_public_key(Onion_Friend *onion_friend, const uint8_t *public_key); non_null(1) nullable(2) -void onion_friend_set_gc_data(Onion_Friend *const onion_friend, const uint8_t *gc_data, uint16_t gc_data_length); -non_null() bool onion_friend_is_groupchat(const Onion_Friend *const onion_friend); +void onion_friend_set_gc_data(Onion_Friend *onion_friend, const uint8_t *gc_data, uint16_t gc_data_length); +non_null() bool onion_friend_is_groupchat(const Onion_Friend *onion_friend); #endif diff --git a/toxcore/ping.c b/toxcore/ping.c index f8a96edf..dc7658a2 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -9,15 +9,15 @@ */ #include "ping.h" -#include #include #include "DHT.h" #include "ccompat.h" +#include "crypto_core.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "ping_array.h" -#include "util.h" #define PING_NUM_MAX 512 diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index f303d802..7c0ce87e 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -8,13 +8,12 @@ */ #include "ping_array.h" -#include #include #include "ccompat.h" #include "crypto_core.h" +#include "mem.h" #include "mono_time.h" -#include "util.h" typedef struct Ping_Array_Entry { uint8_t *data; diff --git a/toxcore/shared_key_cache.c b/toxcore/shared_key_cache.c index 0adc1e82..8f8f6c1b 100644 --- a/toxcore/shared_key_cache.c +++ b/toxcore/shared_key_cache.c @@ -4,12 +4,13 @@ #include "shared_key_cache.h" -#include #include #include // memcpy(...) #include "ccompat.h" #include "crypto_core.h" +#include "logger.h" +#include "mem.h" #include "mono_time.h" typedef struct Shared_Key { diff --git a/toxcore/state.c b/toxcore/state.c index e4f769ad..69f11f3c 100644 --- a/toxcore/state.c +++ b/toxcore/state.c @@ -7,6 +7,7 @@ #include #include "ccompat.h" +#include "logger.h" /** state load/save */ int state_load(const Logger *log, state_load_cb *state_load_callback, void *outer, diff --git a/toxcore/timed_auth.c b/toxcore/timed_auth.c index ebd51002..a52f9d9a 100644 --- a/toxcore/timed_auth.c +++ b/toxcore/timed_auth.c @@ -6,6 +6,8 @@ #include #include "ccompat.h" +#include "crypto_core.h" +#include "mono_time.h" non_null(1,6) nullable(4) static void create_timed_auth_to_hash(const Mono_Time *mono_time, uint16_t timeout, bool previous, const uint8_t *data, diff --git a/toxcore/tox.c b/toxcore/tox.c index 290f6bb8..00914ec1 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -13,18 +13,24 @@ #include "tox.h" #include -#include #include +#include "DHT.h" #include "Messenger.h" +#include "TCP_client.h" #include "ccompat.h" +#include "crypto_core.h" +#include "friend_requests.h" #include "group.h" #include "group_chats.h" -#include "group_moderation.h" +#include "group_common.h" #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_crypto.h" #include "network.h" +#include "onion_client.h" +#include "state.h" #include "tox_private.h" #include "tox_struct.h" @@ -165,7 +171,7 @@ static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number static m_friend_request_cb tox_friend_request_handler; non_null(1, 2, 3) nullable(5) -static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length, +static void tox_friend_request_handler(Messenger *m, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], const uint8_t *message, size_t length, void *user_data) { struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; @@ -335,11 +341,11 @@ non_null(1, 4) nullable(6) static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data, size_t length, void *user_data) { + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + assert(data != nullptr); assert(length > 0); - struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; - if (tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id] != nullptr) { tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length, tox_data->user_data); @@ -351,11 +357,11 @@ non_null(1, 4) nullable(6) static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data, size_t length, void *user_data) { + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + assert(data != nullptr); assert(length > 0); - struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; - if (tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id] != nullptr) { tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length, tox_data->user_data); @@ -1007,7 +1013,7 @@ void tox_get_savedata(const Tox *tox, uint8_t *savedata) } non_null(5) nullable(1, 2, 4, 6) -static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, +static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], IP_Port **root, Tox_Err_Bootstrap *error) { assert(tox != nullptr); @@ -1036,7 +1042,7 @@ static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, return count; } -bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, Tox_Err_Bootstrap *error) +bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Bootstrap *error) { IP_Port *root; const int32_t count = resolve_bootstrap_node(tox, host, port, public_key, &root, error); @@ -1087,7 +1093,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub return true; } -bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, +bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Bootstrap *error) { IP_Port *root; @@ -1176,7 +1182,7 @@ void tox_iterate(Tox *tox, void *user_data) tox_unlock(tox); } -void tox_self_get_address(const Tox *tox, uint8_t *address) +void tox_self_get_address(const Tox *tox, uint8_t address[TOX_ADDRESS_SIZE]) { assert(tox != nullptr); @@ -1204,7 +1210,7 @@ uint32_t tox_self_get_nospam(const Tox *tox) return ret; } -void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) +void tox_self_get_public_key(const Tox *tox, uint8_t public_key[TOX_PUBLIC_KEY_SIZE]) { assert(tox != nullptr); @@ -1215,7 +1221,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) } } -void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key) +void tox_self_get_secret_key(const Tox *tox, uint8_t secret_key[TOX_SECRET_KEY_SIZE]) { assert(tox != nullptr); @@ -1376,7 +1382,7 @@ static void set_friend_error(const Logger *log, int32_t ret, Tox_Err_Friend_Add } } -uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message, size_t length, +uint32_t tox_friend_add(Tox *tox, const uint8_t address[TOX_ADDRESS_SIZE], const uint8_t *message, size_t length, Tox_Err_Friend_Add *error) { assert(tox != nullptr); @@ -1400,7 +1406,7 @@ uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message return UINT32_MAX; } -uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_Friend_Add *error) +uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_Add *error) { assert(tox != nullptr); @@ -1440,7 +1446,7 @@ bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete * return true; } -uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error) +uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_By_Public_Key *error) { assert(tox != nullptr); @@ -1463,7 +1469,7 @@ uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox return (uint32_t)ret; } -bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t *public_key, +bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_Get_Public_Key *error) { assert(tox != nullptr); @@ -1789,7 +1795,7 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback) tox->friend_message_callback = callback; } -bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) +bool tox_hash(uint8_t hash[TOX_HASH_LENGTH], const uint8_t *data, size_t length) { if (hash == nullptr || (data == nullptr && length != 0)) { return false; @@ -1919,7 +1925,7 @@ void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback tox->file_recv_control_callback = callback; } -bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t *file_id, +bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t file_id[TOX_FILE_ID_LENGTH], Tox_Err_File_Get *error) { assert(tox != nullptr); @@ -1947,7 +1953,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ return false; } -uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id, +uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t file_id[TOX_FILE_ID_LENGTH], const uint8_t *filename, size_t filename_length, Tox_Err_File_Send *error) { assert(tox != nullptr); @@ -2209,7 +2215,7 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui } bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t peer_number, - uint8_t *public_key, Tox_Err_Conference_Peer_Query *error) + uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error) { assert(tox != nullptr); tox_lock(tox); @@ -2330,7 +2336,7 @@ bool tox_conference_offline_peer_get_name(const Tox *tox, uint32_t conference_nu bool tox_conference_offline_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t offline_peer_number, - uint8_t *public_key, Tox_Err_Conference_Peer_Query *error) + uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error) { assert(tox != nullptr); tox_lock(tox); @@ -2381,12 +2387,12 @@ uint64_t tox_conference_offline_peer_get_last_active(const Tox *tox, uint32_t co } bool tox_conference_set_max_offline(Tox *tox, uint32_t conference_number, - uint32_t max_offline_peers, + uint32_t max_offline, Tox_Err_Conference_Set_Max_Offline *error) { assert(tox != nullptr); tox_lock(tox); - const int ret = group_set_max_frozen(tox->m->conferences_object, conference_number, max_offline_peers); + const int ret = group_set_max_frozen(tox->m->conferences_object, conference_number, max_offline); tox_unlock(tox); if (ret == -1) { @@ -2623,7 +2629,7 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ return (Tox_Conference_Type)ret; } -bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id) +bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t id[TOX_CONFERENCE_ID_SIZE]) { assert(tox != nullptr); tox_lock(tox); @@ -2633,13 +2639,13 @@ bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t * } // TODO(iphydf): Delete in 0.3.0. -bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t *uid) +bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t uid[TOX_CONFERENCE_UID_SIZE]) { assert(tox != nullptr); return tox_conference_get_id(tox, conference_number, uid); } -uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Conference_By_Id *error) +uint32_t tox_conference_by_id(const Tox *tox, const uint8_t id[TOX_CONFERENCE_ID_SIZE], Tox_Err_Conference_By_Id *error) { assert(tox != nullptr); @@ -2663,7 +2669,7 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Confere } // TODO(iphydf): Delete in 0.3.0. -uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, Tox_Err_Conference_By_Uid *error) +uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t uid[TOX_CONFERENCE_UID_SIZE], Tox_Err_Conference_By_Uid *error) { assert(tox != nullptr); Tox_Err_Conference_By_Id id_error; @@ -2799,7 +2805,7 @@ void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb } } -void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) +void tox_self_get_dht_id(const Tox *tox, uint8_t dht_id[TOX_PUBLIC_KEY_SIZE]) { assert(tox != nullptr); @@ -3001,7 +3007,7 @@ uint32_t tox_group_new(Tox *tox, Tox_Group_Privacy_State privacy_state, const ui return UINT32_MAX; } -uint32_t tox_group_join(Tox *tox, const uint8_t *chat_id, const uint8_t *name, size_t name_length, +uint32_t tox_group_join(Tox *tox, const uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], const uint8_t *name, size_t name_length, const uint8_t *password, size_t password_length, Tox_Err_Group_Join *error) { assert(tox != nullptr); @@ -3187,7 +3193,7 @@ bool tox_group_leave(Tox *tox, uint32_t group_number, const uint8_t *part_messag return false; } -bool tox_group_self_set_name(const Tox *tox, uint32_t group_number, const uint8_t *name, size_t length, +bool tox_group_self_set_name(Tox *tox, uint32_t group_number, const uint8_t *name, size_t length, Tox_Err_Group_Self_Name_Set *error) { assert(tox != nullptr); @@ -3271,7 +3277,7 @@ bool tox_group_self_get_name(const Tox *tox, uint32_t group_number, uint8_t *nam return true; } -bool tox_group_self_set_status(const Tox *tox, uint32_t group_number, Tox_User_Status status, +bool tox_group_self_set_status(Tox *tox, uint32_t group_number, Tox_User_Status status, Tox_Err_Group_Self_Status_Set *error) { assert(tox != nullptr); @@ -3366,7 +3372,7 @@ uint32_t tox_group_self_get_peer_id(const Tox *tox, uint32_t group_number, Tox_E return ret; } -bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t *public_key, +bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Group_Self_Query *error) { assert(tox != nullptr); @@ -3492,7 +3498,7 @@ Tox_Group_Role tox_group_peer_get_role(const Tox *tox, uint32_t group_number, ui return (Tox_Group_Role)ret; } -bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *public_key, +bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Group_Peer_Query *error) { assert(tox != nullptr); @@ -3544,7 +3550,7 @@ Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, uint32_t gro return (Tox_Connection)ret; } -bool tox_group_set_topic(const Tox *tox, uint32_t group_number, const uint8_t *topic, size_t length, +bool tox_group_set_topic(Tox *tox, uint32_t group_number, const uint8_t *topic, size_t length, Tox_Err_Group_Topic_Set *error) { assert(tox != nullptr); @@ -3662,7 +3668,7 @@ size_t tox_group_get_name_size(const Tox *tox, uint32_t group_number, Tox_Err_Gr return ret; } -bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_name, Tox_Err_Group_State_Queries *error) +bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *name, Tox_Err_Group_State_Queries *error) { assert(tox != nullptr); @@ -3675,7 +3681,7 @@ bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_na return false; } - gc_get_group_name(chat, group_name); + gc_get_group_name(chat, name); tox_unlock(tox); SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_STATE_QUERIES_OK); @@ -3683,7 +3689,7 @@ bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_na return true; } -bool tox_group_get_chat_id(const Tox *tox, uint32_t group_number, uint8_t *chat_id, Tox_Err_Group_State_Queries *error) +bool tox_group_get_chat_id(const Tox *tox, uint32_t group_number, uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], Tox_Err_Group_State_Queries *error) { assert(tox != nullptr); @@ -3843,8 +3849,9 @@ bool tox_group_get_password(const Tox *tox, uint32_t group_number, uint8_t *pass return true; } -bool tox_group_send_message(const Tox *tox, uint32_t group_number, Tox_Message_Type type, const uint8_t *message, - size_t length, uint32_t *message_id, Tox_Err_Group_Send_Message *error) +Tox_Group_Message_Id tox_group_send_message( + const Tox *tox, uint32_t group_number, Tox_Message_Type type, const uint8_t *message, + size_t length, Tox_Err_Group_Send_Message *error) { assert(tox != nullptr); @@ -3854,54 +3861,55 @@ bool tox_group_send_message(const Tox *tox, uint32_t group_number, Tox_Message_T if (chat == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_GROUP_NOT_FOUND); tox_unlock(tox); - return false; + return -1; } if (chat->connection_state == CS_DISCONNECTED) { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_DISCONNECTED); tox_unlock(tox); - return false; + return -1; } - const int ret = gc_send_message(chat, message, length, type, message_id); + uint32_t message_id = 0; + const int ret = gc_send_message(chat, message, length, type, &message_id); tox_unlock(tox); switch (ret) { case 0: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_OK); - return true; + return message_id; } case -1: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_TOO_LONG); - return false; + return -1; } case -2: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_EMPTY); - return false; + return -1; } case -3: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_BAD_TYPE); - return false; + return -1; } case -4: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS); - return false; + return -1; } case -5: { SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_FAIL_SEND); - return false; + return -1; } } /* can't happen */ LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); - return false; + return -1; } bool tox_group_send_private_message(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, @@ -4199,7 +4207,7 @@ uint32_t tox_group_invite_accept(Tox *tox, uint32_t friend_number, const uint8_t return UINT32_MAX; } -bool tox_group_founder_set_password(const Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, +bool tox_group_founder_set_password(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, Tox_Err_Group_Founder_Set_Password *error) { assert(tox != nullptr); @@ -4255,7 +4263,7 @@ bool tox_group_founder_set_password(const Tox *tox, uint32_t group_number, const return false; } -bool tox_group_founder_set_privacy_state(const Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, +bool tox_group_founder_set_privacy_state(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, Tox_Err_Group_Founder_Set_Privacy_State *error) { assert(tox != nullptr); @@ -4302,7 +4310,7 @@ bool tox_group_founder_set_privacy_state(const Tox *tox, uint32_t group_number, return false; } -bool tox_group_founder_set_topic_lock(const Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, +bool tox_group_founder_set_topic_lock(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, Tox_Err_Group_Founder_Set_Topic_Lock *error) { assert(tox != nullptr); @@ -4354,7 +4362,7 @@ bool tox_group_founder_set_topic_lock(const Tox *tox, uint32_t group_number, Tox return false; } -bool tox_group_founder_set_voice_state(const Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, +bool tox_group_founder_set_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, Tox_Err_Group_Founder_Set_Voice_State *error) { assert(tox != nullptr); @@ -4401,7 +4409,7 @@ bool tox_group_founder_set_voice_state(const Tox *tox, uint32_t group_number, To return false; } -bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uint16_t max_peers, +bool tox_group_founder_set_peer_limit(Tox *tox, uint32_t group_number, uint16_t peer_limit, Tox_Err_Group_Founder_Set_Peer_Limit *error) { assert(tox != nullptr); @@ -4421,7 +4429,7 @@ bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uin return false; } - const int ret = gc_founder_set_max_peers(chat, max_peers); + const int ret = gc_founder_set_max_peers(chat, peer_limit); tox_unlock(tox); switch (ret) { @@ -4452,7 +4460,7 @@ bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uin return false; } -bool tox_group_set_ignore(const Tox *tox, uint32_t group_number, uint32_t peer_id, bool ignore, +bool tox_group_set_ignore(Tox *tox, uint32_t group_number, uint32_t peer_id, bool ignore, Tox_Err_Group_Set_Ignore *error) { assert(tox != nullptr); @@ -4492,7 +4500,7 @@ bool tox_group_set_ignore(const Tox *tox, uint32_t group_number, uint32_t peer_i return false; } -bool tox_group_mod_set_role(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Role role, +bool tox_group_mod_set_role(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Role role, Tox_Err_Group_Mod_Set_Role *error) { assert(tox != nullptr); diff --git a/toxcore/tox.h b/toxcore/tox.h index 7bf26d44..566bd576 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -536,6 +536,7 @@ typedef struct Tox_System Tox_System; * members. The struct will become opaque (i.e. the definition will become * private) in v0.3.0. */ +typedef struct Tox_Options Tox_Options; struct Tox_Options { /** @@ -702,77 +703,77 @@ struct Tox_Options { }; -bool tox_options_get_ipv6_enabled(const struct Tox_Options *options); +bool tox_options_get_ipv6_enabled(const Tox_Options *options); -void tox_options_set_ipv6_enabled(struct Tox_Options *options, bool ipv6_enabled); +void tox_options_set_ipv6_enabled(Tox_Options *options, bool ipv6_enabled); -bool tox_options_get_udp_enabled(const struct Tox_Options *options); +bool tox_options_get_udp_enabled(const Tox_Options *options); -void tox_options_set_udp_enabled(struct Tox_Options *options, bool udp_enabled); +void tox_options_set_udp_enabled(Tox_Options *options, bool udp_enabled); -bool tox_options_get_local_discovery_enabled(const struct Tox_Options *options); +bool tox_options_get_local_discovery_enabled(const Tox_Options *options); -void tox_options_set_local_discovery_enabled(struct Tox_Options *options, bool local_discovery_enabled); +void tox_options_set_local_discovery_enabled(Tox_Options *options, bool local_discovery_enabled); -bool tox_options_get_dht_announcements_enabled(const struct Tox_Options *options); +bool tox_options_get_dht_announcements_enabled(const Tox_Options *options); -void tox_options_set_dht_announcements_enabled(struct Tox_Options *options, bool dht_announcements_enabled); +void tox_options_set_dht_announcements_enabled(Tox_Options *options, bool dht_announcements_enabled); -Tox_Proxy_Type tox_options_get_proxy_type(const struct Tox_Options *options); +Tox_Proxy_Type tox_options_get_proxy_type(const Tox_Options *options); -void tox_options_set_proxy_type(struct Tox_Options *options, Tox_Proxy_Type type); +void tox_options_set_proxy_type(Tox_Options *options, Tox_Proxy_Type proxy_type); -const char *tox_options_get_proxy_host(const struct Tox_Options *options); +const char *tox_options_get_proxy_host(const Tox_Options *options); -void tox_options_set_proxy_host(struct Tox_Options *options, const char *host); +void tox_options_set_proxy_host(Tox_Options *options, const char *proxy_host); -uint16_t tox_options_get_proxy_port(const struct Tox_Options *options); +uint16_t tox_options_get_proxy_port(const Tox_Options *options); -void tox_options_set_proxy_port(struct Tox_Options *options, uint16_t port); +void tox_options_set_proxy_port(Tox_Options *options, uint16_t proxy_port); -uint16_t tox_options_get_start_port(const struct Tox_Options *options); +uint16_t tox_options_get_start_port(const Tox_Options *options); -void tox_options_set_start_port(struct Tox_Options *options, uint16_t start_port); +void tox_options_set_start_port(Tox_Options *options, uint16_t start_port); -uint16_t tox_options_get_end_port(const struct Tox_Options *options); +uint16_t tox_options_get_end_port(const Tox_Options *options); -void tox_options_set_end_port(struct Tox_Options *options, uint16_t end_port); +void tox_options_set_end_port(Tox_Options *options, uint16_t end_port); -uint16_t tox_options_get_tcp_port(const struct Tox_Options *options); +uint16_t tox_options_get_tcp_port(const Tox_Options *options); -void tox_options_set_tcp_port(struct Tox_Options *options, uint16_t tcp_port); +void tox_options_set_tcp_port(Tox_Options *options, uint16_t tcp_port); -bool tox_options_get_hole_punching_enabled(const struct Tox_Options *options); +bool tox_options_get_hole_punching_enabled(const Tox_Options *options); -void tox_options_set_hole_punching_enabled(struct Tox_Options *options, bool hole_punching_enabled); +void tox_options_set_hole_punching_enabled(Tox_Options *options, bool hole_punching_enabled); -Tox_Savedata_Type tox_options_get_savedata_type(const struct Tox_Options *options); +Tox_Savedata_Type tox_options_get_savedata_type(const Tox_Options *options); -void tox_options_set_savedata_type(struct Tox_Options *options, Tox_Savedata_Type type); +void tox_options_set_savedata_type(Tox_Options *options, Tox_Savedata_Type savedata_type); -const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options); +const uint8_t *tox_options_get_savedata_data(const Tox_Options *options); -void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length); +void tox_options_set_savedata_data(Tox_Options *options, const uint8_t savedata_data[], size_t length); -size_t tox_options_get_savedata_length(const struct Tox_Options *options); +size_t tox_options_get_savedata_length(const Tox_Options *options); -void tox_options_set_savedata_length(struct Tox_Options *options, size_t length); +void tox_options_set_savedata_length(Tox_Options *options, size_t savedata_length); -tox_log_cb *tox_options_get_log_callback(const struct Tox_Options *options); +tox_log_cb *tox_options_get_log_callback(const Tox_Options *options); -void tox_options_set_log_callback(struct Tox_Options *options, tox_log_cb *callback); +void tox_options_set_log_callback(Tox_Options *options, tox_log_cb *log_callback); -void *tox_options_get_log_user_data(const struct Tox_Options *options); +void *tox_options_get_log_user_data(const Tox_Options *options); -void tox_options_set_log_user_data(struct Tox_Options *options, void *user_data); +void tox_options_set_log_user_data(Tox_Options *options, void *log_user_data); -bool tox_options_get_experimental_thread_safety(const struct Tox_Options *options); +bool tox_options_get_experimental_thread_safety(const Tox_Options *options); -void tox_options_set_experimental_thread_safety(struct Tox_Options *options, bool experimental_thread_safety); +void tox_options_set_experimental_thread_safety(Tox_Options *options, bool experimental_thread_safety); -const Tox_System *tox_options_get_operating_system(const struct Tox_Options *options); +const Tox_System *tox_options_get_operating_system(const Tox_Options *options); -void tox_options_set_operating_system(struct Tox_Options *options, const Tox_System *operating_system); +void tox_options_set_operating_system(Tox_Options *options, const Tox_System *operating_system); /** * @brief Initialises a Tox_Options object with the default options. @@ -785,7 +786,7 @@ void tox_options_set_operating_system(struct Tox_Options *options, const Tox_Sys * * @param options An options object to be filled with default options. */ -void tox_options_default(struct Tox_Options *options); +void tox_options_default(Tox_Options *options); typedef enum Tox_Err_Options_New { @@ -816,7 +817,7 @@ const char *tox_err_options_new_to_string(Tox_Err_Options_New value); * * @return A new Tox_Options object with default options or NULL on failure. */ -struct Tox_Options *tox_options_new(Tox_Err_Options_New *error); +Tox_Options *tox_options_new(Tox_Err_Options_New *error); /** * @brief Releases all resources associated with an options objects. @@ -824,7 +825,7 @@ struct Tox_Options *tox_options_new(Tox_Err_Options_New *error); * Passing a pointer that was not returned by tox_options_new results in * undefined behaviour. */ -void tox_options_free(struct Tox_Options *options); +void tox_options_free(Tox_Options *options); /** @} */ @@ -911,7 +912,7 @@ const char *tox_err_new_to_string(Tox_Err_New value); * * @return A new Tox instance pointer on success or NULL on failure. */ -Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error); +Tox *tox_new(const Tox_Options *options, Tox_Err_New *error); /** * @brief Releases all resources associated with the Tox instance and @@ -922,8 +923,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error); */ void tox_kill(Tox *tox); -const Tox_System *tox_get_system(Tox *tox); - /** * @brief Calculates the number of bytes required to store the tox instance with * tox_get_savedata. @@ -941,7 +940,7 @@ size_t tox_get_savedata_size(const Tox *tox); * data. Call tox_get_savedata_size to find the number of bytes required. If this parameter * is NULL, this function has no effect. */ -void tox_get_savedata(const Tox *tox, uint8_t *savedata); +void tox_get_savedata(const Tox *tox, uint8_t savedata[]); /** @} */ @@ -994,7 +993,7 @@ const char *tox_err_bootstrap_to_string(Tox_Err_Bootstrap value); * (TOX_PUBLIC_KEY_SIZE bytes). * @return true on success. */ -bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, Tox_Err_Bootstrap *error); +bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Bootstrap *error); /** * @brief Adds additional host:port pair as TCP relay. @@ -1010,7 +1009,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub * (TOX_PUBLIC_KEY_SIZE bytes). * @return true on success. */ -bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, Tox_Err_Bootstrap *error); +bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Bootstrap *error); /** * @brief Protocols that can be used to connect to the network or friends. @@ -1108,7 +1107,7 @@ void tox_iterate(Tox *tox, void *user_data); * parameter is NULL, this function has no effect. * @see TOX_ADDRESS_SIZE for the address format. */ -void tox_self_get_address(const Tox *tox, uint8_t *address); +void tox_self_get_address(const Tox *tox, uint8_t address[TOX_ADDRESS_SIZE]); /** * @brief Set the 4-byte nospam part of the address. @@ -1133,7 +1132,7 @@ uint32_t tox_self_get_nospam(const Tox *tox); * @param public_key A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If * this parameter is NULL, this function has no effect. */ -void tox_self_get_public_key(const Tox *tox, uint8_t *public_key); +void tox_self_get_public_key(const Tox *tox, uint8_t public_key[TOX_PUBLIC_KEY_SIZE]); /** * @brief Copy the Tox Secret Key from the Tox object. @@ -1141,7 +1140,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key); * @param secret_key A memory region of at least TOX_SECRET_KEY_SIZE bytes. If * this parameter is NULL, this function has no effect. */ -void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key); +void tox_self_get_secret_key(const Tox *tox, uint8_t secret_key[TOX_SECRET_KEY_SIZE]); /** @} */ @@ -1187,7 +1186,7 @@ const char *tox_err_set_info_to_string(Tox_Err_Set_Info value); * * @return true on success. */ -bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set_Info *error); +bool tox_self_set_name(Tox *tox, const uint8_t name[], size_t length, Tox_Err_Set_Info *error); /** * @brief Return the length of the current nickname as passed to tox_self_set_name. @@ -1211,7 +1210,7 @@ size_t tox_self_get_name_size(const Tox *tox); * @param name A valid memory location large enough to hold the nickname. * If this parameter is NULL, the function has no effect. */ -void tox_self_get_name(const Tox *tox, uint8_t *name); +void tox_self_get_name(const Tox *tox, uint8_t name[]); /** * @brief Set the client's status message. @@ -1220,7 +1219,8 @@ void tox_self_get_name(const Tox *tox, uint8_t *name); * length is 0, the status parameter is ignored (it can be NULL), and the * user status is set back to empty. */ -bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t length, Tox_Err_Set_Info *error); +bool tox_self_set_status_message( + Tox *tox, const uint8_t status_message[], size_t length, Tox_Err_Set_Info *error); /** * @brief Return the length of the current status message as passed to tox_self_set_status_message. @@ -1244,7 +1244,7 @@ size_t tox_self_get_status_message_size(const Tox *tox); * @param status_message A valid memory location large enough to hold the * status message. If this parameter is NULL, the function has no effect. */ -void tox_self_get_status_message(const Tox *tox, uint8_t *status_message); +void tox_self_get_status_message(const Tox *tox, uint8_t status_message[]); /** * @brief Set the client's user status. @@ -1265,6 +1265,8 @@ Tox_User_Status tox_self_get_status(const Tox *tox); * @name Friend list management */ +typedef uint32_t Tox_Friend_Number; + typedef enum Tox_Err_Friend_Add { /** @@ -1343,8 +1345,10 @@ const char *tox_err_friend_add_to_string(Tox_Err_Friend_Add value); * * @return the friend number on success, an unspecified value on failure. */ -uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message, size_t length, - Tox_Err_Friend_Add *error); +Tox_Friend_Number tox_friend_add( + Tox *tox, const uint8_t address[TOX_ADDRESS_SIZE], + const uint8_t message[], size_t length, + Tox_Err_Friend_Add *error); /** * @brief Add a friend without sending a friend request. @@ -1364,7 +1368,8 @@ uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message * @return the friend number on success, an unspecified value on failure. * @see tox_friend_add for a more detailed description of friend numbers. */ -uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_Friend_Add *error); +Tox_Friend_Number tox_friend_add_norequest( + Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_Add *error); typedef enum Tox_Err_Friend_Delete { @@ -1394,7 +1399,7 @@ const char *tox_err_friend_delete_to_string(Tox_Err_Friend_Delete value); * * @return true on success. */ -bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete *error); +bool tox_friend_delete(Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Delete *error); /** @} */ @@ -1431,13 +1436,13 @@ const char *tox_err_friend_by_public_key_to_string(Tox_Err_Friend_By_Public_Key * @return the friend number on success, an unspecified value on failure. * @param public_key A byte array containing the Public Key. */ -uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error); +Tox_Friend_Number tox_friend_by_public_key(const Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_By_Public_Key *error); /** * @brief Checks if a friend with the given friend number exists and returns true if * it does. */ -bool tox_friend_exists(const Tox *tox, uint32_t friend_number); +bool tox_friend_exists(const Tox *tox, Tox_Friend_Number friend_number); /** * @brief Return the number of friends on the friend list. @@ -1455,7 +1460,7 @@ size_t tox_self_get_friend_list_size(const Tox *tox); * @param friend_list A memory region with enough space to hold the friend * list. If this parameter is NULL, this function has no effect. */ -void tox_self_get_friend_list(const Tox *tox, uint32_t *friend_list); +void tox_self_get_friend_list(const Tox *tox, Tox_Friend_Number friend_list[]); typedef enum Tox_Err_Friend_Get_Public_Key { @@ -1483,8 +1488,9 @@ const char *tox_err_friend_get_public_key_to_string(Tox_Err_Friend_Get_Public_Ke * * @return true on success. */ -bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t *public_key, - Tox_Err_Friend_Get_Public_Key *error); +bool tox_friend_get_public_key( + const Tox *tox, Tox_Friend_Number friend_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], + Tox_Err_Friend_Get_Public_Key *error); typedef enum Tox_Err_Friend_Get_Last_Online { @@ -1511,7 +1517,8 @@ const char *tox_err_friend_get_last_online_to_string(Tox_Err_Friend_Get_Last_Onl * * @param friend_number The friend number you want to query. */ -uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Get_Last_Online *error); +uint64_t tox_friend_get_last_online( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Get_Last_Online *error); /** @} */ @@ -1555,7 +1562,8 @@ const char *tox_err_friend_query_to_string(Tox_Err_Friend_Query value); * The return value is equal to the `length` argument received by the last * `friend_name` callback. */ -size_t tox_friend_get_name_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error); +size_t tox_friend_get_name_size( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Query *error); /** * @brief Write the name of the friend designated by the given friend number to a byte @@ -1571,7 +1579,8 @@ size_t tox_friend_get_name_size(const Tox *tox, uint32_t friend_number, Tox_Err_ * * @return true on success. */ -bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, Tox_Err_Friend_Query *error); +bool tox_friend_get_name( + const Tox *tox, Tox_Friend_Number friend_number, uint8_t name[], Tox_Err_Friend_Query *error); /** * @param friend_number The friend number of the friend whose name changed. @@ -1580,7 +1589,9 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, * @param length A value equal to the return value of * tox_friend_get_name_size. */ -typedef void tox_friend_name_cb(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data); +typedef void tox_friend_name_cb( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t name[], size_t length, void *user_data); /** @@ -1597,7 +1608,8 @@ void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback); * * If the friend number isinvalid, the return value is SIZE_MAX. */ -size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error); +size_t tox_friend_get_status_message_size( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Query *error); /** * @brief Write the status message of the friend designated by the given friend number to a byte @@ -1611,8 +1623,9 @@ size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number * * @param status_message A valid memory region large enough to store the friend's status message. */ -bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8_t *status_message, - Tox_Err_Friend_Query *error); +bool tox_friend_get_status_message( + const Tox *tox, Tox_Friend_Number friend_number, uint8_t status_message[], + Tox_Err_Friend_Query *error); /** * @param friend_number The friend number of the friend whose status message @@ -1622,8 +1635,9 @@ bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8 * @param length A value equal to the return value of * tox_friend_get_status_message_size. */ -typedef void tox_friend_status_message_cb(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, - void *user_data); +typedef void tox_friend_status_message_cb( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t message[], size_t length, void *user_data); /** @@ -1646,14 +1660,16 @@ void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb * * @deprecated This getter is deprecated. Use the event and store the status * in the client state. */ -Tox_User_Status tox_friend_get_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error); +Tox_User_Status tox_friend_get_status( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Query *error); /** * @param friend_number The friend number of the friend whose user status * changed. * @param status The new user status. */ -typedef void tox_friend_status_cb(Tox *tox, uint32_t friend_number, Tox_User_Status status, void *user_data); +typedef void tox_friend_status_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_User_Status status, void *user_data); /** @@ -1680,7 +1696,8 @@ void tox_callback_friend_status(Tox *tox, tox_friend_status_cb *callback); * @deprecated This getter is deprecated. Use the event and store the status * in the client state. */ -Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error); +Tox_Connection tox_friend_get_connection_status( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Query *error); /** * @param friend_number The friend number of the friend whose connection status @@ -1688,8 +1705,8 @@ Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_ * @param connection_status The result of calling * tox_friend_get_connection_status on the passed friend_number. */ -typedef void tox_friend_connection_status_cb(Tox *tox, uint32_t friend_number, Tox_Connection connection_status, - void *user_data); +typedef void tox_friend_connection_status_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_Connection connection_status, void *user_data); /** @@ -1717,7 +1734,8 @@ void tox_callback_friend_connection_status(Tox *tox, tox_friend_connection_statu * @deprecated This getter is deprecated. Use the event and store the status * in the client state. */ -bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error); +bool tox_friend_get_typing( + const Tox *tox, Tox_Friend_Number friend_number, Tox_Err_Friend_Query *error); /** * @param friend_number The friend number of the friend who started or stopped @@ -1725,7 +1743,8 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien * @param typing The result of calling tox_friend_get_typing on the passed * friend_number. */ -typedef void tox_friend_typing_cb(Tox *tox, uint32_t friend_number, bool typing, void *user_data); +typedef void tox_friend_typing_cb( + Tox *tox, Tox_Friend_Number friend_number, bool typing, void *user_data); /** @@ -1771,7 +1790,8 @@ const char *tox_err_set_typing_to_string(Tox_Err_Set_Typing value); * * @return true on success. */ -bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool typing, Tox_Err_Set_Typing *error); +bool tox_self_set_typing( + Tox *tox, Tox_Friend_Number friend_number, bool typing, Tox_Err_Set_Typing *error); typedef enum Tox_Err_Friend_Send_Message { @@ -1814,6 +1834,7 @@ typedef enum Tox_Err_Friend_Send_Message { const char *tox_err_friend_send_message_to_string(Tox_Err_Friend_Send_Message value); +typedef uint32_t Tox_Friend_Message_Id; /** * @brief Send a text chat message to an online friend. @@ -1838,15 +1859,17 @@ const char *tox_err_friend_send_message_to_string(Tox_Err_Friend_Send_Message va * containing the message text. * @param length Length of the message to be sent. */ -uint32_t tox_friend_send_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, - size_t length, Tox_Err_Friend_Send_Message *error); +Tox_Friend_Message_Id tox_friend_send_message( + Tox *tox, Tox_Friend_Number friend_number, Tox_Message_Type type, + const uint8_t message[], size_t length, Tox_Err_Friend_Send_Message *error); /** * @param friend_number The friend number of the friend who received the message. * @param message_id The message ID as returned from tox_friend_send_message * corresponding to the message sent. */ -typedef void tox_friend_read_receipt_cb(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data); +typedef void tox_friend_read_receipt_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_Friend_Message_Id message_id, void *user_data); /** @@ -1871,8 +1894,10 @@ void tox_callback_friend_read_receipt(Tox *tox, tox_friend_read_receipt_cb *call * @param message The message they sent along with the request. * @param length The size of the message byte array. */ -typedef void tox_friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, - void *user_data); +typedef void tox_friend_request_cb( + Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], + const uint8_t message[], size_t length, + void *user_data); /** @@ -1889,8 +1914,9 @@ void tox_callback_friend_request(Tox *tox, tox_friend_request_cb *callback); * @param message The message data they sent. * @param length The size of the message byte array. */ -typedef void tox_friend_message_cb(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, - size_t length, void *user_data); +typedef void tox_friend_message_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_Message_Type type, + const uint8_t message[], size_t length, void *user_data); /** @@ -1909,6 +1935,8 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback); * @name File transmission: common between sending and receiving */ +typedef uint32_t Tox_File_Number; + /** * @brief Generates a cryptographic hash of the given data. * @@ -1928,7 +1956,7 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback); * * @return true if hash was not NULL. */ -bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length); +bool tox_hash(uint8_t hash[TOX_HASH_LENGTH], const uint8_t data[], size_t length); /** * @brief A list of pre-defined file kinds. @@ -2057,8 +2085,9 @@ const char *tox_err_file_control_to_string(Tox_Err_File_Control value); * * @return true on success. */ -bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, - Tox_Err_File_Control *error); +bool tox_file_control( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, Tox_File_Control control, + Tox_Err_File_Control *error); /** * @brief When receiving TOX_FILE_CONTROL_CANCEL, the client should release the @@ -2069,8 +2098,9 @@ bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, To * associated with. * @param control The file control command received. */ -typedef void tox_file_recv_control_cb(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, - void *user_data); +typedef void tox_file_recv_control_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, Tox_File_Control control, + void *user_data); /** @@ -2136,7 +2166,8 @@ const char *tox_err_file_seek_to_string(Tox_Err_File_Seek value); * @param file_number The friend-specific identifier for the file transfer. * @param position The position that the file should be seeked to. */ -bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, Tox_Err_File_Seek *error); +bool tox_file_seek( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, uint64_t position, Tox_Err_File_Seek *error); typedef enum Tox_Err_File_Get { @@ -2176,8 +2207,10 @@ const char *tox_err_file_get_to_string(Tox_Err_File_Get value); * * @return true on success. */ -bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t *file_id, - Tox_Err_File_Get *error); +bool tox_file_get_file_id( + const Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, + uint8_t file_id[TOX_FILE_ID_LENGTH], + Tox_Err_File_Get *error); /** @} */ @@ -2282,8 +2315,10 @@ const char *tox_err_file_send_to_string(Tox_Err_File_Send value); * On failure, this function returns an unspecified value. Any pattern in file numbers * should not be relied on. */ -uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id, - const uint8_t *filename, size_t filename_length, Tox_Err_File_Send *error); +Tox_File_Number tox_file_send( + Tox *tox, Tox_Friend_Number friend_number, uint32_t kind, uint64_t file_size, + const uint8_t file_id[TOX_FILE_ID_LENGTH], const uint8_t filename[], size_t filename_length, + Tox_Err_File_Send *error); typedef enum Tox_Err_File_Send_Chunk { @@ -2356,8 +2391,9 @@ const char *tox_err_file_send_chunk_to_string(Tox_Err_File_Send_Chunk value); * @param position The file or stream position from which to continue reading. * @return true on success. */ -bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, - size_t length, Tox_Err_File_Send_Chunk *error); +bool tox_file_send_chunk( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, uint64_t position, + const uint8_t data[], size_t length, Tox_Err_File_Send_Chunk *error); /** * If the length parameter is 0, the file transfer is finished, and the client's @@ -2381,8 +2417,9 @@ bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, * @param position The file or stream position from which to continue reading. * @param length The number of bytes requested for the current chunk. */ -typedef void tox_file_chunk_request_cb(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - size_t length, void *user_data); +typedef void tox_file_chunk_request_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, uint64_t position, + size_t length, void *user_data); /** @@ -2419,8 +2456,9 @@ void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callba * name will be sent along with the file send request. * @param filename_length Size in bytes of the filename. */ -typedef void tox_file_recv_cb(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, - const uint8_t *filename, size_t filename_length, void *user_data); +typedef void tox_file_recv_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, uint32_t kind, uint64_t file_size, + const uint8_t filename[], size_t filename_length, void *user_data); /** @@ -2448,8 +2486,9 @@ void tox_callback_file_recv(Tox *tox, tox_file_recv_cb *callback); * @param data A byte array containing the received chunk. * @param length The length of the received chunk. */ -typedef void tox_file_recv_chunk_cb(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - const uint8_t *data, size_t length, void *user_data); +typedef void tox_file_recv_chunk_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_File_Number file_number, uint64_t position, + const uint8_t data[], size_t length, void *user_data); /** @@ -2469,6 +2508,9 @@ void tox_callback_file_recv_chunk(Tox *tox, tox_file_recv_chunk_cb *callback); * @name Conference management */ +typedef uint32_t Tox_Conference_Number; +typedef uint32_t Tox_Conference_Peer_Number; + /** * @brief Conference types for the conference_invite event. */ @@ -2499,8 +2541,9 @@ const char *tox_conference_type_to_string(Tox_Conference_Type value); * conference. * @param length The length of the cookie. */ -typedef void tox_conference_invite_cb(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, - size_t length, void *user_data); +typedef void tox_conference_invite_cb( + Tox *tox, Tox_Friend_Number friend_number, Tox_Conference_Type type, + const uint8_t cookie[], size_t length, void *user_data); /** @@ -2515,7 +2558,7 @@ void tox_callback_conference_invite(Tox *tox, tox_conference_invite_cb *callback /** * @param conference_number The conference number of the conference to which we have connected. */ -typedef void tox_conference_connected_cb(Tox *tox, uint32_t conference_number, void *user_data); +typedef void tox_conference_connected_cb(Tox *tox, Tox_Conference_Number conference_number, void *user_data); /** @@ -2536,8 +2579,9 @@ void tox_callback_conference_connected(Tox *tox, tox_conference_connected_cb *ca * @param message The message data. * @param length The length of the message. */ -typedef void tox_conference_message_cb(Tox *tox, uint32_t conference_number, uint32_t peer_number, - Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data); +typedef void tox_conference_message_cb( + Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + Tox_Message_Type type, const uint8_t message[], size_t length, void *user_data); /** @@ -2556,8 +2600,9 @@ void tox_callback_conference_message(Tox *tox, tox_conference_message_cb *callba * @param title The title data. * @param length The title length. */ -typedef void tox_conference_title_cb(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, - size_t length, void *user_data); +typedef void tox_conference_title_cb( + Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + const uint8_t title[], size_t length, void *user_data); /** @@ -2578,8 +2623,9 @@ void tox_callback_conference_title(Tox *tox, tox_conference_title_cb *callback); * @param name A byte array containing the new nickname. * @param length The size of the name byte array. */ -typedef void tox_conference_peer_name_cb(Tox *tox, uint32_t conference_number, uint32_t peer_number, - const uint8_t *name, size_t length, void *user_data); +typedef void tox_conference_peer_name_cb( + Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + const uint8_t name[], size_t length, void *user_data); /** @@ -2595,7 +2641,7 @@ void tox_callback_conference_peer_name(Tox *tox, tox_conference_peer_name_cb *ca * @param conference_number The conference number of the conference the * peer is in. */ -typedef void tox_conference_peer_list_changed_cb(Tox *tox, uint32_t conference_number, void *user_data); +typedef void tox_conference_peer_list_changed_cb(Tox *tox, Tox_Conference_Number conference_number, void *user_data); /** @@ -2633,7 +2679,7 @@ const char *tox_err_conference_new_to_string(Tox_Err_Conference_New value); * - conference number on success * - an unspecified value on failure */ -uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error); +Tox_Conference_Number tox_conference_new(Tox *tox, Tox_Err_Conference_New *error); typedef enum Tox_Err_Conference_Delete { @@ -2659,7 +2705,7 @@ const char *tox_err_conference_delete_to_string(Tox_Err_Conference_Delete value) * * @return true on success. */ -bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Conference_Delete *error); +bool tox_conference_delete(Tox *tox, Tox_Conference_Number conference_number, Tox_Err_Conference_Delete *error); /** * @brief Error codes for peer info queries. @@ -2698,14 +2744,16 @@ const char *tox_err_conference_peer_query_to_string(Tox_Err_Conference_Peer_Quer * peer_number for the functions querying these peers. Return value is * unspecified on failure. */ -uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Peer_Query *error); +Tox_Conference_Peer_Number tox_conference_peer_count( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Err_Conference_Peer_Query *error); /** * @brief Return the length of the peer's name. * * Return value is unspecified on failure. */ -size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_number, uint32_t peer_number, +size_t tox_conference_peer_get_name_size( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, Tox_Err_Conference_Peer_Query *error); /** @@ -2717,8 +2765,9 @@ size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_num * * @return true on success. */ -bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, uint32_t peer_number, uint8_t *name, - Tox_Err_Conference_Peer_Query *error); +bool tox_conference_peer_get_name( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + uint8_t name[], Tox_Err_Conference_Peer_Query *error); /** * @brief Copy the public key of peer_number who is in conference_number to public_key. @@ -2727,14 +2776,16 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui * * @return true on success. */ -bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t peer_number, - uint8_t *public_key, Tox_Err_Conference_Peer_Query *error); +bool tox_conference_peer_get_public_key( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error); /** * @brief Return true if passed peer_number corresponds to our own. */ -bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_number, uint32_t peer_number, - Tox_Err_Conference_Peer_Query *error); +bool tox_conference_peer_number_is_ours( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number peer_number, + Tox_Err_Conference_Peer_Query *error); /** * @brief Return the number of offline peers in the conference. @@ -2744,7 +2795,8 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb * * Return value is unspecified on failure. */ -uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_number, +uint32_t tox_conference_offline_peer_count( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Err_Conference_Peer_Query *error); /** @@ -2752,8 +2804,9 @@ uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_n * * Return value is unspecified on failure. */ -size_t tox_conference_offline_peer_get_name_size(const Tox *tox, uint32_t conference_number, - uint32_t offline_peer_number, Tox_Err_Conference_Peer_Query *error); +size_t tox_conference_offline_peer_get_name_size( + const Tox *tox, Tox_Conference_Number conference_number, + Tox_Conference_Peer_Number offline_peer_number, Tox_Err_Conference_Peer_Query *error); /** * @brief Copy the name of offline_peer_number who is in conference_number to name. @@ -2765,8 +2818,9 @@ size_t tox_conference_offline_peer_get_name_size(const Tox *tox, uint32_t confer * * @return true on success. */ -bool tox_conference_offline_peer_get_name(const Tox *tox, uint32_t conference_number, uint32_t offline_peer_number, - uint8_t *name, Tox_Err_Conference_Peer_Query *error); +bool tox_conference_offline_peer_get_name( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Conference_Peer_Number offline_peer_number, + uint8_t name[], Tox_Err_Conference_Peer_Query *error); /** * @brief Copy the public key of offline_peer_number who is in conference_number to public_key. @@ -2775,14 +2829,16 @@ bool tox_conference_offline_peer_get_name(const Tox *tox, uint32_t conference_nu * * @return true on success. */ -bool tox_conference_offline_peer_get_public_key(const Tox *tox, uint32_t conference_number, - uint32_t offline_peer_number, uint8_t *public_key, Tox_Err_Conference_Peer_Query *error); +bool tox_conference_offline_peer_get_public_key( + const Tox *tox, Tox_Conference_Number conference_number, + Tox_Conference_Peer_Number offline_peer_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error); /** * @brief Return a unix-time timestamp of the last time offline_peer_number was seen to be active. */ -uint64_t tox_conference_offline_peer_get_last_active(const Tox *tox, uint32_t conference_number, - uint32_t offline_peer_number, Tox_Err_Conference_Peer_Query *error); +uint64_t tox_conference_offline_peer_get_last_active( + const Tox *tox, Tox_Conference_Number conference_number, + Tox_Conference_Peer_Number offline_peer_number, Tox_Err_Conference_Peer_Query *error); typedef enum Tox_Err_Conference_Set_Max_Offline { @@ -2804,8 +2860,9 @@ const char *tox_err_conference_set_max_offline_to_string(Tox_Err_Conference_Set_ /** * @brief Set maximum number of offline peers to store, overriding the default. */ -bool tox_conference_set_max_offline(Tox *tox, uint32_t conference_number, uint32_t max_offline_peers, - Tox_Err_Conference_Set_Max_Offline *error); +bool tox_conference_set_max_offline( + Tox *tox, Tox_Conference_Number conference_number, uint32_t max_offline, + Tox_Err_Conference_Set_Max_Offline *error); typedef enum Tox_Err_Conference_Invite { @@ -2842,8 +2899,9 @@ const char *tox_err_conference_invite_to_string(Tox_Err_Conference_Invite value) * * @return true on success. */ -bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference_number, - Tox_Err_Conference_Invite *error); +bool tox_conference_invite( + Tox *tox, Tox_Friend_Number friend_number, Tox_Conference_Number conference_number, + Tox_Err_Conference_Invite *error); typedef enum Tox_Err_Conference_Join { @@ -2904,8 +2962,10 @@ const char *tox_err_conference_join_to_string(Tox_Err_Conference_Join value); * * @return conference number on success, an unspecified value on failure. */ -uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *cookie, size_t length, - Tox_Err_Conference_Join *error); +Tox_Conference_Number tox_conference_join( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t cookie[], size_t length, + Tox_Err_Conference_Join *error); typedef enum Tox_Err_Conference_Send_Message { @@ -2958,8 +3018,10 @@ const char *tox_err_conference_send_message_to_string(Tox_Err_Conference_Send_Me * * @return true on success. */ -bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Message_Type type, const uint8_t *message, - size_t length, Tox_Err_Conference_Send_Message *error); +bool tox_conference_send_message( + Tox *tox, Tox_Conference_Number conference_number, Tox_Message_Type type, + const uint8_t message[], size_t length, + Tox_Err_Conference_Send_Message *error); typedef enum Tox_Err_Conference_Title { @@ -2996,7 +3058,8 @@ const char *tox_err_conference_title_to_string(Tox_Err_Conference_Title value); * The return value is equal to the `length` argument received by the last * `conference_title` callback. */ -size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Title *error); +size_t tox_conference_get_title_size( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Err_Conference_Title *error); /** * @brief Write the title designated by the given conference number to a byte array. @@ -3011,8 +3074,10 @@ size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, * * @return true on success. */ -bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_t *title, - Tox_Err_Conference_Title *error); +bool tox_conference_get_title( + const Tox *tox, Tox_Conference_Number conference_number, + uint8_t title[], + Tox_Err_Conference_Title *error); /** * @brief Set the conference title and broadcast it to the rest of the conference. @@ -3021,8 +3086,10 @@ bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_ * * @return true on success. */ -bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_t *title, size_t length, - Tox_Err_Conference_Title *error); +bool tox_conference_set_title( + Tox *tox, Tox_Conference_Number conference_number, + const uint8_t title[], size_t length, + Tox_Err_Conference_Title *error); /** * @brief Return the number of conferences in the Tox instance. @@ -3045,7 +3112,7 @@ size_t tox_conference_get_chatlist_size(const Tox *tox); * The conference number of a loaded conference may differ from the conference * number it had when it was saved. */ -void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist); +void tox_conference_get_chatlist(const Tox *tox, Tox_Conference_Number chatlist[]); /** * @brief Returns the type of conference (Tox_Conference_Type) that conference_number is. @@ -3072,7 +3139,8 @@ const char *tox_err_conference_get_type_to_string(Tox_Err_Conference_Get_Type va /** * @brief Get the type (text or A/V) for the conference. */ -Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_number, +Tox_Conference_Type tox_conference_get_type( + const Tox *tox, Tox_Conference_Number conference_number, Tox_Err_Conference_Get_Type *error); /** @@ -3084,7 +3152,8 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ * * @return true on success. */ -bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id); +bool tox_conference_get_id( + const Tox *tox, Tox_Conference_Number conference_number, uint8_t id[TOX_CONFERENCE_ID_SIZE]); typedef enum Tox_Err_Conference_By_Id { @@ -3115,7 +3184,8 @@ const char *tox_err_conference_by_id_to_string(Tox_Err_Conference_By_Id value); * * @return the conference number on success, an unspecified value on failure. */ -uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Conference_By_Id *error); +Tox_Conference_Number tox_conference_by_id( + const Tox *tox, const uint8_t id[TOX_CONFERENCE_ID_SIZE], Tox_Err_Conference_By_Id *error); /** * @brief Get the conference unique ID. @@ -3127,7 +3197,8 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Confere * @return true on success. * @deprecated use tox_conference_get_id instead (exactly the same function, just renamed). */ -bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t *uid); +bool tox_conference_get_uid( + const Tox *tox, Tox_Conference_Number conference_number, uint8_t uid[TOX_CONFERENCE_UID_SIZE]); typedef enum Tox_Err_Conference_By_Uid { @@ -3159,7 +3230,8 @@ const char *tox_err_conference_by_uid_to_string(Tox_Err_Conference_By_Uid value) * @return the conference number on success, an unspecified value on failure. * @deprecated use tox_conference_by_id instead (exactly the same function, just renamed). */ -uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, Tox_Err_Conference_By_Uid *error); +Tox_Conference_Number tox_conference_by_uid( + const Tox *tox, const uint8_t uid[TOX_CONFERENCE_UID_SIZE], Tox_Err_Conference_By_Uid *error); /** @} */ @@ -3236,8 +3308,10 @@ const char *tox_err_friend_custom_packet_to_string(Tox_Err_Friend_Custom_Packet * * @return true on success. */ -bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, - Tox_Err_Friend_Custom_Packet *error); +bool tox_friend_send_lossy_packet( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t data[], size_t length, + Tox_Err_Friend_Custom_Packet *error); /** * @brief Send a custom lossless packet to a friend. @@ -3255,16 +3329,20 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ * * @return true on success. */ -bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, - Tox_Err_Friend_Custom_Packet *error); +bool tox_friend_send_lossless_packet( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t data[], size_t length, + Tox_Err_Friend_Custom_Packet *error); /** * @param friend_number The friend number of the friend who sent a lossy packet. * @param data A byte array containing the received packet data. * @param length The length of the packet data byte array. */ -typedef void tox_friend_lossy_packet_cb(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, - void *user_data); +typedef void tox_friend_lossy_packet_cb( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t data[], size_t length, + void *user_data); /** @@ -3279,7 +3357,9 @@ void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *call * @param data A byte array containing the received packet data. * @param length The length of the packet data byte array. */ -typedef void tox_friend_lossless_packet_cb(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, +typedef void tox_friend_lossless_packet_cb( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t data[], size_t length, void *user_data); @@ -3326,7 +3406,7 @@ const char *tox_err_get_port_to_string(Tox_Err_Get_Port value); * @param dht_id A memory region of at least TOX_PUBLIC_KEY_SIZE bytes. If this * parameter is NULL, this function has no effect. */ -void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id); +void tox_self_get_dht_id(const Tox *tox, uint8_t dht_id[TOX_PUBLIC_KEY_SIZE]); /** * @brief Return the UDP port this Tox instance is bound to. @@ -3342,12 +3422,13 @@ uint16_t tox_self_get_tcp_port(const Tox *tox, Tox_Err_Get_Port *error); /** @} */ -/******************************************************************************* - * - * :: Group chats - * - ******************************************************************************/ +/** @{ + * @name Group chats + */ +typedef uint32_t Tox_Group_Number; +typedef uint32_t Tox_Group_Peer_Number; +typedef uint32_t Tox_Group_Message_Id; @@ -3359,7 +3440,7 @@ uint16_t tox_self_get_tcp_port(const Tox *tox, Tox_Err_Get_Port *error); -/** @{ +/** * Maximum length of a group topic. */ #define TOX_GROUP_MAX_TOPIC_LENGTH 512 @@ -3607,8 +3688,10 @@ const char *tox_err_group_new_to_string(Tox_Err_Group_New value); * * @return group_number on success, UINT32_MAX on failure. */ -uint32_t tox_group_new(Tox *tox, Tox_Group_Privacy_State privacy_state, const uint8_t *group_name, - size_t group_name_length, const uint8_t *name, size_t name_length, Tox_Err_Group_New *error); +Tox_Group_Number tox_group_new( + Tox *tox, Tox_Group_Privacy_State privacy_state, + const uint8_t group_name[], size_t group_name_length, + const uint8_t name[], size_t name_length, Tox_Err_Group_New *error); typedef enum Tox_Err_Group_Join { @@ -3670,8 +3753,11 @@ const char *tox_err_group_join_to_string(Tox_Err_Group_Join value); * * @return group_number on success, UINT32_MAX on failure. */ -uint32_t tox_group_join(Tox *tox, const uint8_t *chat_id, const uint8_t *name, size_t name_length, - const uint8_t *password, size_t password_length, Tox_Err_Group_Join *error); +Tox_Group_Number tox_group_join( + Tox *tox, const uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], + const uint8_t name[], size_t name_length, + const uint8_t password[], size_t password_length, + Tox_Err_Group_Join *error); typedef enum Tox_Err_Group_Is_Connected { @@ -3696,7 +3782,7 @@ const char *tox_err_group_is_connected_to_string(Tox_Err_Group_Is_Connected valu * * @param group_number The group number of the designated group. */ -bool tox_group_is_connected(const Tox *tox, uint32_t group_number, Tox_Err_Group_Is_Connected *error); +bool tox_group_is_connected(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Is_Connected *error); typedef enum Tox_Err_Group_Disconnect { @@ -3726,7 +3812,7 @@ const char *tox_err_group_disconnect_to_string(Tox_Err_Group_Disconnect value); * * @param group_number The group number of the designated group. */ -bool tox_group_disconnect(const Tox *tox, uint32_t group_number, Tox_Err_Group_Disconnect *error); +bool tox_group_disconnect(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Disconnect *error); typedef enum Tox_Err_Group_Reconnect { @@ -3760,7 +3846,7 @@ const char *tox_err_group_reconnect_to_string(Tox_Err_Group_Reconnect value); * * @return true on success. */ -bool tox_group_reconnect(Tox *tox, uint32_t group_number, Tox_Err_Group_Reconnect *error); +bool tox_group_reconnect(Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Reconnect *error); typedef enum Tox_Err_Group_Leave { @@ -3802,8 +3888,10 @@ const char *tox_err_group_leave_to_string(Tox_Err_Group_Leave value); * * @return true if the group chat instance is successfully deleted. */ -bool tox_group_leave(Tox *tox, uint32_t group_number, const uint8_t *part_message, size_t length, - Tox_Err_Group_Leave *error); +bool tox_group_leave( + Tox *tox, Tox_Group_Number group_number, + const uint8_t part_message[], size_t length, + Tox_Err_Group_Leave *error); /******************************************************************************* @@ -3880,8 +3968,10 @@ const char *tox_err_group_self_name_set_to_string(Tox_Err_Group_Self_Name_Set va * * @return true on success. */ -bool tox_group_self_set_name(const Tox *tox, uint32_t group_number, const uint8_t *name, size_t length, - Tox_Err_Group_Self_Name_Set *error); +bool tox_group_self_set_name( + Tox *tox, Tox_Group_Number group_number, + const uint8_t name[], size_t length, + Tox_Err_Group_Self_Name_Set *error); /** * Return the length of the client's current nickname for the group instance designated @@ -3892,7 +3982,7 @@ bool tox_group_self_set_name(const Tox *tox, uint32_t group_number, const uint8_ * * @see threading for concurrency implications. */ -size_t tox_group_self_get_name_size(const Tox *tox, uint32_t group_number, Tox_Err_Group_Self_Query *error); +size_t tox_group_self_get_name_size(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Self_Query *error); /** * Write the nickname set by tox_group_self_set_name to a byte array. @@ -3907,7 +3997,9 @@ size_t tox_group_self_get_name_size(const Tox *tox, uint32_t group_number, Tox_E * * @return true on success. */ -bool tox_group_self_get_name(const Tox *tox, uint32_t group_number, uint8_t *name, Tox_Err_Group_Self_Query *error); +bool tox_group_self_get_name( + const Tox *tox, Tox_Group_Number group_number, + uint8_t name[], Tox_Err_Group_Self_Query *error); /** * Error codes for self status setting. @@ -3939,26 +4031,26 @@ const char *tox_err_group_self_status_set_to_string(Tox_Err_Group_Self_Status_Se * * @return true on success. */ -bool tox_group_self_set_status(const Tox *tox, uint32_t group_number, Tox_User_Status status, +bool tox_group_self_set_status(Tox *tox, Tox_Group_Number group_number, Tox_User_Status status, Tox_Err_Group_Self_Status_Set *error); /** * returns the client's status for the group instance on success. * return value is unspecified on failure. */ -Tox_User_Status tox_group_self_get_status(const Tox *tox, uint32_t group_number, Tox_Err_Group_Self_Query *error); +Tox_User_Status tox_group_self_get_status(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Self_Query *error); /** * returns the client's role for the group instance on success. * return value is unspecified on failure. */ -Tox_Group_Role tox_group_self_get_role(const Tox *tox, uint32_t group_number, Tox_Err_Group_Self_Query *error); +Tox_Group_Role tox_group_self_get_role(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Self_Query *error); /** * returns the client's peer id for the group instance on success. * return value is unspecified on failure. */ -uint32_t tox_group_self_get_peer_id(const Tox *tox, uint32_t group_number, Tox_Err_Group_Self_Query *error); +Tox_Group_Peer_Number tox_group_self_get_peer_id(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Self_Query *error); /** * Write the client's group public key designated by the given group number to a byte array. @@ -3974,7 +4066,7 @@ uint32_t tox_group_self_get_peer_id(const Tox *tox, uint32_t group_number, Tox_E * * @return true on success. */ -bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t *public_key, +bool tox_group_self_get_public_key(const Tox *tox, Tox_Group_Number group_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Group_Self_Query *error); @@ -4021,7 +4113,7 @@ const char *tox_err_group_peer_query_to_string(Tox_Err_Group_Peer_Query value); * The return value is equal to the `length` argument received by the last * `group_peer_name` callback. */ -size_t tox_group_peer_get_name_size(const Tox *tox, uint32_t group_number, uint32_t peer_id, +size_t tox_group_peer_get_name_size(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Err_Group_Peer_Query *error); /** @@ -4039,8 +4131,9 @@ size_t tox_group_peer_get_name_size(const Tox *tox, uint32_t group_number, uint3 * * @return true on success. */ -bool tox_group_peer_get_name(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *name, - Tox_Err_Group_Peer_Query *error); +bool tox_group_peer_get_name( + const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + uint8_t name[], Tox_Err_Group_Peer_Query *error); /** * Return the peer's user status (away/busy/...). If the ID or group number is @@ -4052,7 +4145,7 @@ bool tox_group_peer_get_name(const Tox *tox, uint32_t group_number, uint32_t pee * The status returned is equal to the last status received through the * `group_peer_status` callback. */ -Tox_User_Status tox_group_peer_get_status(const Tox *tox, uint32_t group_number, uint32_t peer_id, +Tox_User_Status tox_group_peer_get_status(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Err_Group_Peer_Query *error); /** @@ -4065,7 +4158,7 @@ Tox_User_Status tox_group_peer_get_status(const Tox *tox, uint32_t group_number, * The role returned is equal to the last role received through the * `group_moderation` callback. */ -Tox_Group_Role tox_group_peer_get_role(const Tox *tox, uint32_t group_number, uint32_t peer_id, +Tox_Group_Role tox_group_peer_get_role(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Err_Group_Peer_Query *error); /** @@ -4077,7 +4170,7 @@ Tox_Group_Role tox_group_peer_get_role(const Tox *tox, uint32_t group_number, ui * @param group_number The group number of the group we wish to query. * @param peer_id The ID of the peer whose connection status we wish to query. */ -Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, uint32_t group_number, uint32_t peer_id, +Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Err_Group_Peer_Query *error); /** @@ -4096,8 +4189,9 @@ Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, uint32_t gro * * @return true on success. */ -bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *public_key, - Tox_Err_Group_Peer_Query *error); +bool tox_group_peer_get_public_key( + const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Group_Peer_Query *error); /** * @param group_number The group number of the group the name change is intended for. @@ -4105,8 +4199,9 @@ bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32 * @param name The name data. * @param length The length of the name. */ -typedef void tox_group_peer_name_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *name, - size_t length, void *user_data); +typedef void tox_group_peer_name_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + const uint8_t name[], size_t length, void *user_data); /** @@ -4121,7 +4216,7 @@ void tox_callback_group_peer_name(Tox *tox, tox_group_peer_name_cb *callback); * @param peer_id The ID of the peer who has changed their status. * @param status The new status of the peer. */ -typedef void tox_group_peer_status_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status, +typedef void tox_group_peer_status_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_User_Status status, void *user_data); @@ -4214,8 +4309,10 @@ const char *tox_err_group_topic_set_to_string(Tox_Err_Group_Topic_Set value); * * @return true on success. */ -bool tox_group_set_topic(const Tox *tox, uint32_t group_number, const uint8_t *topic, size_t length, - Tox_Err_Group_Topic_Set *error); +bool tox_group_set_topic( + Tox *tox, Tox_Group_Number group_number, + const uint8_t topic[], size_t length, + Tox_Err_Group_Topic_Set *error); /** * Return the length of the group topic. If the group number is invalid, the @@ -4224,7 +4321,7 @@ bool tox_group_set_topic(const Tox *tox, uint32_t group_number, const uint8_t *t * The return value is equal to the `length` argument received by the last * `group_topic` callback. */ -size_t tox_group_get_topic_size(const Tox *tox, uint32_t group_number, Tox_Err_Group_State_Queries *error); +size_t tox_group_get_topic_size(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * Write the topic designated by the given group number to a byte array. @@ -4239,7 +4336,9 @@ size_t tox_group_get_topic_size(const Tox *tox, uint32_t group_number, Tox_Err_G * * @return true on success. */ -bool tox_group_get_topic(const Tox *tox, uint32_t group_number, uint8_t *topic, Tox_Err_Group_State_Queries *error); +bool tox_group_get_topic( + const Tox *tox, Tox_Group_Number group_number, + uint8_t topic[], Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group the topic change is intended for. @@ -4248,8 +4347,10 @@ bool tox_group_get_topic(const Tox *tox, uint32_t group_number, uint8_t *topic, * @param topic The topic data. * @param length The topic length. */ -typedef void tox_group_topic_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length, - void *user_data); +typedef void tox_group_topic_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + const uint8_t topic[], size_t length, + void *user_data); /** @@ -4263,19 +4364,21 @@ void tox_callback_group_topic(Tox *tox, tox_group_topic_cb *callback); * Return the length of the group name. If the group number is invalid, the * return value is unspecified. */ -size_t tox_group_get_name_size(const Tox *tox, uint32_t group_number, Tox_Err_Group_State_Queries *error); +size_t tox_group_get_name_size(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * Write the name of the group designated by the given group number to a byte array. * * Call tox_group_get_name_size to determine the allocation size for the `name` parameter. * - * @param group_name A valid memory region large enough to store the group name. + * @param name A valid memory region large enough to store the group name. * If this parameter is NULL, this function call has no effect. * * @return true on success. */ -bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_name, Tox_Err_Group_State_Queries *error); +bool tox_group_get_name( + const Tox *tox, Tox_Group_Number group_number, + uint8_t name[], Tox_Err_Group_State_Queries *error); /** * Write the Chat ID designated by the given group number to a byte array. @@ -4287,7 +4390,9 @@ bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_na * * @return true on success. */ -bool tox_group_get_chat_id(const Tox *tox, uint32_t group_number, uint8_t *chat_id, Tox_Err_Group_State_Queries *error); +bool tox_group_get_chat_id( + const Tox *tox, Tox_Group_Number group_number, uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], + Tox_Err_Group_State_Queries *error); /** * Return the number of groups in the Tox chats array. @@ -4303,14 +4408,14 @@ uint32_t tox_group_get_number_groups(const Tox *tox); * * @see the `Group chat founder controls` section for the respective set function. */ -Tox_Group_Privacy_State tox_group_get_privacy_state(const Tox *tox, uint32_t group_number, +Tox_Group_Privacy_State tox_group_get_privacy_state(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group the privacy state is intended for. * @param privacy_state The new privacy state. */ -typedef void tox_group_privacy_state_cb(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, +typedef void tox_group_privacy_state_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Privacy_State privacy_state, void *user_data); @@ -4329,14 +4434,14 @@ void tox_callback_group_privacy_state(Tox *tox, tox_group_privacy_state_cb *call * * @see the `Group chat founder controls` section for the respective set function. */ -Tox_Group_Voice_State tox_group_get_voice_state(const Tox *tox, uint32_t group_number, +Tox_Group_Voice_State tox_group_get_voice_state(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group the voice state change is intended for. * @param voice_state The new voice state. */ -typedef void tox_group_voice_state_cb(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, +typedef void tox_group_voice_state_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Voice_State voice_state, void *user_data); @@ -4356,14 +4461,14 @@ void tox_callback_group_voice_state(Tox *tox, tox_group_voice_state_cb *callback * * @see the `Group chat founder contols` section for the respective set function. */ -Tox_Group_Topic_Lock tox_group_get_topic_lock(const Tox *tox, uint32_t group_number, +Tox_Group_Topic_Lock tox_group_get_topic_lock(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group for which the topic lock has changed. * @param topic_lock The new topic lock state. */ -typedef void tox_group_topic_lock_cb(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, void *user_data); +typedef void tox_group_topic_lock_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Topic_Lock topic_lock, void *user_data); @@ -4383,13 +4488,13 @@ void tox_callback_group_topic_lock(Tox *tox, tox_group_topic_lock_cb *callback); * * @see the `Group chat founder controls` section for the respective set function. */ -uint16_t tox_group_get_peer_limit(const Tox *tox, uint32_t group_number, Tox_Err_Group_State_Queries *error); +uint16_t tox_group_get_peer_limit(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group for which the peer limit has changed. * @param peer_limit The new peer limit for the group. */ -typedef void tox_group_peer_limit_cb(Tox *tox, uint32_t group_number, uint32_t peer_limit, void *user_data); +typedef void tox_group_peer_limit_cb(Tox *tox, Tox_Group_Number group_number, uint32_t peer_limit, void *user_data); /** @@ -4403,7 +4508,7 @@ void tox_callback_group_peer_limit(Tox *tox, tox_group_peer_limit_cb *callback); * Return the length of the group password. If the group number is invalid, the * return value is unspecified. */ -size_t tox_group_get_password_size(const Tox *tox, uint32_t group_number, Tox_Err_Group_State_Queries *error); +size_t tox_group_get_password_size(const Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_State_Queries *error); /** * Write the password for the group designated by the given group number to a byte array. @@ -4420,16 +4525,19 @@ size_t tox_group_get_password_size(const Tox *tox, uint32_t group_number, Tox_Er * * @return true on success. */ -bool tox_group_get_password(const Tox *tox, uint32_t group_number, uint8_t *password, - Tox_Err_Group_State_Queries *error); +bool tox_group_get_password( + const Tox *tox, Tox_Group_Number group_number, uint8_t password[], + Tox_Err_Group_State_Queries *error); /** * @param group_number The group number of the group for which the password has changed. * @param password The new group password. * @param length The length of the password. */ -typedef void tox_group_password_cb(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, - void *user_data); +typedef void tox_group_password_cb( + Tox *tox, Tox_Group_Number group_number, + const uint8_t password[], size_t length, + void *user_data); /** @@ -4510,14 +4618,14 @@ const char *tox_err_group_send_message_to_string(Tox_Err_Group_Send_Message valu * @param message A non-NULL pointer to the first element of a byte array * containing the message text. * @param length Length of the message to be sent. - * @param message_id A pointer to a uint32_t. The message_id of this message will be returned - * unless the parameter is NULL, in which case the returned parameter value will be undefined. - * If this function returns false the returned parameter `message_id` value will also be undefined. * - * @return true on success. + * @return The message_id of this message. If this function has an error, the + * returned message ID value will be undefined. */ -bool tox_group_send_message(const Tox *tox, uint32_t group_number, Tox_Message_Type type, const uint8_t *message, - size_t length, uint32_t *message_id, Tox_Err_Group_Send_Message *error); +Tox_Group_Message_Id tox_group_send_message( + const Tox *tox, Tox_Group_Number group_number, Tox_Message_Type type, + const uint8_t message[], size_t length, + Tox_Err_Group_Send_Message *error); typedef enum Tox_Err_Group_Send_Private_Message { @@ -4589,8 +4697,10 @@ const char *tox_err_group_send_private_message_to_string(Tox_Err_Group_Send_Priv * * @return true on success. */ -bool tox_group_send_private_message(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, - const uint8_t *message, size_t length, Tox_Err_Group_Send_Private_Message *error); +bool tox_group_send_private_message( + const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Message_Type type, + const uint8_t message[], size_t length, + Tox_Err_Group_Send_Private_Message *error); typedef enum Tox_Err_Group_Send_Custom_Packet { @@ -4655,9 +4765,10 @@ const char *tox_err_group_send_custom_packet_to_string(Tox_Err_Group_Send_Custom * * @return true on success. */ -bool tox_group_send_custom_packet(const Tox *tox, uint32_t group_number, bool lossless, const uint8_t *data, - size_t length, - Tox_Err_Group_Send_Custom_Packet *error); +bool tox_group_send_custom_packet( + const Tox *tox, Tox_Group_Number group_number, bool lossless, + const uint8_t data[], size_t length, + Tox_Err_Group_Send_Custom_Packet *error); typedef enum Tox_Err_Group_Send_Custom_Private_Packet { @@ -4733,8 +4844,8 @@ const char *tox_err_group_send_custom_private_packet_to_string(Tox_Err_Group_Sen * * @return true on success. */ -bool tox_group_send_custom_private_packet(const Tox *tox, uint32_t group_number, uint32_t peer_id, bool lossless, - const uint8_t *data, size_t length, +bool tox_group_send_custom_private_packet(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, bool lossless, + const uint8_t data[], size_t length, Tox_Err_Group_Send_Custom_Private_Packet *error); @@ -4754,8 +4865,9 @@ bool tox_group_send_custom_private_packet(const Tox *tox, uint32_t group_number, * @param message_id A pseudo message id that clients can use to uniquely identify this group message. * @param length The length of the message. */ -typedef void tox_group_message_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, - const uint8_t *message, size_t length, uint32_t message_id, void *user_data); +typedef void tox_group_message_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Message_Type type, + const uint8_t message[], size_t length, Tox_Group_Message_Id message_id, void *user_data); /** @@ -4771,8 +4883,9 @@ void tox_callback_group_message(Tox *tox, tox_group_message_cb *callback); * @param message The message data. * @param length The length of the message. */ -typedef void tox_group_private_message_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, - const uint8_t *message, size_t length, void *user_data); +typedef void tox_group_private_message_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Message_Type type, + const uint8_t message[], size_t length, void *user_data); /** @@ -4788,8 +4901,9 @@ void tox_callback_group_private_message(Tox *tox, tox_group_private_message_cb * * @param data The packet data. * @param length The length of the data. */ -typedef void tox_group_custom_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, - size_t length, void *user_data); +typedef void tox_group_custom_packet_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + const uint8_t data[], size_t length, void *user_data); /** @@ -4805,8 +4919,9 @@ void tox_callback_group_custom_packet(Tox *tox, tox_group_custom_packet_cb *call * @param data The packet data. * @param length The length of the data. */ -typedef void tox_group_custom_private_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, - size_t length, void *user_data); +typedef void tox_group_custom_private_packet_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, + const uint8_t data[], size_t length, void *user_data); /** @@ -4872,8 +4987,9 @@ const char *tox_err_group_invite_friend_to_string(Tox_Err_Group_Invite_Friend va * * @return true on success. */ -bool tox_group_invite_friend(const Tox *tox, uint32_t group_number, uint32_t friend_number, - Tox_Err_Group_Invite_Friend *error); +bool tox_group_invite_friend( + const Tox *tox, Tox_Group_Number group_number, Tox_Friend_Number friend_number, + Tox_Err_Group_Invite_Friend *error); typedef enum Tox_Err_Group_Invite_Accept { @@ -4937,17 +5053,23 @@ const char *tox_err_group_invite_accept_to_string(Tox_Err_Group_Invite_Accept va * * @return the group_number on success, UINT32_MAX on failure. */ -uint32_t tox_group_invite_accept(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, - const uint8_t *name, size_t name_length, const uint8_t *password, size_t password_length, - Tox_Err_Group_Invite_Accept *error); +Tox_Group_Number tox_group_invite_accept( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t invite_data[], size_t length, + const uint8_t name[], size_t name_length, + const uint8_t password[], size_t password_length, + Tox_Err_Group_Invite_Accept *error); /** * @param friend_number The friend number of the contact who sent the invite. * @param invite_data The invite data. * @param length The length of invite_data. */ -typedef void tox_group_invite_cb(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, - const uint8_t *group_name, size_t group_name_length, void *user_data); +typedef void tox_group_invite_cb( + Tox *tox, Tox_Friend_Number friend_number, + const uint8_t invite_data[], size_t length, + const uint8_t group_name[], size_t group_name_length, + void *user_data); /** @@ -4963,7 +5085,7 @@ void tox_callback_group_invite(Tox *tox, tox_group_invite_cb *callback); * @param peer_id The permanent ID of the new peer. This id should not be relied on for * client behaviour and should be treated as a random value. */ -typedef void tox_group_peer_join_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, void *user_data); +typedef void tox_group_peer_join_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, void *user_data); /** @@ -5024,8 +5146,10 @@ const char *tox_group_exit_type_to_string(Tox_Group_Exit_Type value); * @param part_message The parting message data. * @param part_message_length The length of the parting message. */ -typedef void tox_group_peer_exit_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, - const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length, void *user_data); +typedef void tox_group_peer_exit_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Group_Exit_Type exit_type, + const uint8_t name[], size_t name_length, + const uint8_t part_message[], size_t part_message_length, void *user_data); /** @@ -5038,7 +5162,7 @@ void tox_callback_group_peer_exit(Tox *tox, tox_group_peer_exit_cb *callback); /** * @param group_number The group number of the group that the client has joined. */ -typedef void tox_group_self_join_cb(Tox *tox, uint32_t group_number, void *user_data); +typedef void tox_group_self_join_cb(Tox *tox, Tox_Group_Number group_number, void *user_data); /** @@ -5080,7 +5204,7 @@ const char *tox_group_join_fail_to_string(Tox_Group_Join_Fail value); * @param group_number The group number of the group for which the join has failed. * @param fail_type The type of group rejection. */ -typedef void tox_group_join_fail_cb(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type, void *user_data); +typedef void tox_group_join_fail_cb(Tox *tox, Tox_Group_Number group_number, Tox_Group_Join_Fail fail_type, void *user_data); /** @@ -5153,8 +5277,10 @@ const char *tox_err_group_founder_set_password_to_string(Tox_Err_Group_Founder_S * * @return true on success. */ -bool tox_group_founder_set_password(const Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, - Tox_Err_Group_Founder_Set_Password *error); +bool tox_group_founder_set_password( + Tox *tox, Tox_Group_Number group_number, + const uint8_t password[], size_t length, + Tox_Err_Group_Founder_Set_Password *error); typedef enum Tox_Err_Group_Founder_Set_Topic_Lock { @@ -5213,7 +5339,7 @@ const char *tox_err_group_founder_set_topic_lock_to_string(Tox_Err_Group_Founder * * @return true on success. */ -bool tox_group_founder_set_topic_lock(const Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, +bool tox_group_founder_set_topic_lock(Tox *tox, Tox_Group_Number group_number, Tox_Group_Topic_Lock topic_lock, Tox_Err_Group_Founder_Set_Topic_Lock *error); typedef enum Tox_Err_Group_Founder_Set_Voice_State { @@ -5267,7 +5393,7 @@ const char *tox_err_group_founder_set_voice_state_to_string(Tox_Err_Group_Founde * * @return true on success. */ -bool tox_group_founder_set_voice_state(const Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, +bool tox_group_founder_set_voice_state(Tox *tox, Tox_Group_Number group_number, Tox_Group_Voice_State voice_state, Tox_Err_Group_Founder_Set_Voice_State *error); typedef enum Tox_Err_Group_Founder_Set_Privacy_State { @@ -5321,7 +5447,7 @@ const char *tox_err_group_founder_set_privacy_state_to_string(Tox_Err_Group_Foun * * @return true on success. */ -bool tox_group_founder_set_privacy_state(const Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, +bool tox_group_founder_set_privacy_state(Tox *tox, Tox_Group_Number group_number, Tox_Group_Privacy_State privacy_state, Tox_Err_Group_Founder_Set_Privacy_State *error); typedef enum Tox_Err_Group_Founder_Set_Peer_Limit { @@ -5369,11 +5495,11 @@ const char *tox_err_group_founder_set_peer_limit_to_string(Tox_Err_Group_Founder * group shared state including the change, and distributes it to the rest of the group. * * @param group_number The group number of the group for which we wish to set the peer limit. - * @param max_peers The maximum number of peers to allow in the group. + * @param peer_limit The maximum number of peers to allow in the group. * * @return true on success. */ -bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uint16_t max_peers, +bool tox_group_founder_set_peer_limit(Tox *tox, Tox_Group_Number group_number, uint16_t peer_limit, Tox_Err_Group_Founder_Set_Peer_Limit *error); @@ -5421,7 +5547,7 @@ const char *tox_err_group_set_ignore_to_string(Tox_Err_Group_Set_Ignore value); * * @return true on success. */ -bool tox_group_set_ignore(const Tox *tox, uint32_t group_number, uint32_t peer_id, bool ignore, +bool tox_group_set_ignore(Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, bool ignore, Tox_Err_Group_Set_Ignore *error); typedef enum Tox_Err_Group_Mod_Set_Role { @@ -5481,7 +5607,7 @@ const char *tox_err_group_mod_set_role_to_string(Tox_Err_Group_Mod_Set_Role valu * * @return true on success. */ -bool tox_group_mod_set_role(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Role role, +bool tox_group_mod_set_role(Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Group_Role role, Tox_Err_Group_Mod_Set_Role *error); typedef enum Tox_Err_Group_Mod_Kick_Peer { @@ -5538,7 +5664,7 @@ const char *tox_err_group_mod_kick_peer_to_string(Tox_Err_Group_Mod_Kick_Peer va * * @return true on success. */ -bool tox_group_mod_kick_peer(const Tox *tox, uint32_t group_number, uint32_t peer_id, +bool tox_group_mod_kick_peer(const Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number peer_id, Tox_Err_Group_Mod_Kick_Peer *error); /** @@ -5577,8 +5703,9 @@ const char *tox_group_mod_event_to_string(Tox_Group_Mod_Event value); * @param target_peer_id The ID of the peer who is the target of the event. * @param mod_type The type of event. */ -typedef void tox_group_moderation_cb(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, - Tox_Group_Mod_Event mod_type, void *user_data); +typedef void tox_group_moderation_cb( + Tox *tox, Tox_Group_Number group_number, Tox_Group_Peer_Number source_peer_id, Tox_Group_Peer_Number target_peer_id, + Tox_Group_Mod_Event mod_type, void *user_data); /** diff --git a/toxcore/tox_api.c b/toxcore/tox_api.c index 9b3a888d..d92c98b0 100644 --- a/toxcore/tox_api.c +++ b/toxcore/tox_api.c @@ -4,7 +4,6 @@ #include "tox.h" #include -#include #include "ccompat.h" #include "tox_private.h" @@ -139,33 +138,33 @@ uint32_t tox_dht_node_public_key_size(void) //!TOKSTYLE- -#define ACCESSORS(type, ns, name) \ -type tox_options_get_##ns##name(const struct Tox_Options *options) \ +#define ACCESSORS(type, name) \ +type tox_options_get_##name(const struct Tox_Options *options) \ { \ - return options->ns##name; \ + return options->name; \ } \ -void tox_options_set_##ns##name(struct Tox_Options *options, type name) \ +void tox_options_set_##name(struct Tox_Options *options, type name) \ { \ - options->ns##name = name; \ + options->name = name; \ } -ACCESSORS(bool,, ipv6_enabled) -ACCESSORS(bool,, udp_enabled) -ACCESSORS(Tox_Proxy_Type, proxy_, type) -ACCESSORS(const char *, proxy_, host) -ACCESSORS(uint16_t, proxy_, port) -ACCESSORS(uint16_t,, start_port) -ACCESSORS(uint16_t,, end_port) -ACCESSORS(uint16_t,, tcp_port) -ACCESSORS(bool,, hole_punching_enabled) -ACCESSORS(Tox_Savedata_Type, savedata_, type) -ACCESSORS(size_t, savedata_, length) -ACCESSORS(tox_log_cb *, log_, callback) -ACCESSORS(void *, log_, user_data) -ACCESSORS(bool,, local_discovery_enabled) -ACCESSORS(bool,, dht_announcements_enabled) -ACCESSORS(bool,, experimental_thread_safety) -ACCESSORS(const Tox_System *,, operating_system) +ACCESSORS(bool, ipv6_enabled) +ACCESSORS(bool, udp_enabled) +ACCESSORS(Tox_Proxy_Type, proxy_type) +ACCESSORS(const char *, proxy_host) +ACCESSORS(uint16_t, proxy_port) +ACCESSORS(uint16_t, start_port) +ACCESSORS(uint16_t, end_port) +ACCESSORS(uint16_t, tcp_port) +ACCESSORS(bool, hole_punching_enabled) +ACCESSORS(Tox_Savedata_Type, savedata_type) +ACCESSORS(size_t, savedata_length) +ACCESSORS(tox_log_cb *, log_callback) +ACCESSORS(void *, log_user_data) +ACCESSORS(bool, local_discovery_enabled) +ACCESSORS(bool, dht_announcements_enabled) +ACCESSORS(bool, experimental_thread_safety) +ACCESSORS(const Tox_System *, operating_system) //!TOKSTYLE+ @@ -174,9 +173,9 @@ const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) return options->savedata_data; } -void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length) +void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *savedata_data, size_t length) { - options->savedata_data = data; + options->savedata_data = savedata_data; options->savedata_length = length; } diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 9f304b0c..63b4eb02 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,8 +7,10 @@ #include #include "ccompat.h" -#include "events/events_alloc.h" +#include "events/events_alloc.h" // IWYU pragma: keep +#include "tox.h" #include "tox_event.h" +#include "tox_events.h" struct Tox_Dispatch { tox_events_conference_connected_cb *conference_connected_callback; diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c index 17a6dbfb..d7ac2a90 100644 --- a/toxcore/tox_event.c +++ b/toxcore/tox_event.c @@ -5,14 +5,12 @@ #include "tox_event.h" #include -#include -#include -#include +#include "bin_pack.h" #include "bin_unpack.h" #include "ccompat.h" -#include "events/events_alloc.h" -#include "tox.h" +#include "mem.h" +#include "tox_events.h" const char *tox_event_type_to_string(Tox_Event_Type type) { diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 431f0766..fea4adc5 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -5,16 +5,18 @@ #include "tox_events.h" #include -#include -#include #include +#include "bin_pack.h" #include "bin_unpack.h" #include "ccompat.h" #include "events/events_alloc.h" +#include "logger.h" #include "mem.h" #include "tox.h" +#include "tox_event.h" #include "tox_private.h" +#include "tox_struct.h" /***************************************************** @@ -143,7 +145,8 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem) non_null(1) nullable(2, 3) static bool tox_events_bin_pack_handler(Bin_Pack *bp, const Logger *logger, const void *obj) { - return tox_events_pack((const Tox_Events *)obj, bp); + const Tox_Events *events = (const Tox_Events *)obj; + return tox_events_pack(events, bp); } uint32_t tox_events_bytes_size(const Tox_Events *events) diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index fa76d393..5de29b5e 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -48,14 +48,14 @@ TEST(ToxEvents, NullEventsPacksToEmptyArray) TEST(ToxEvents, PackedEventsCanBeUnpacked) { const Tox_System sys = tox_default_system(); - // [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } - std::array packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01}; + // [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } + std::array packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01}; Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size()); ASSERT_NE(events, nullptr); std::array bytes; ASSERT_EQ(tox_events_bytes_size(events), bytes.size()); tox_events_get_bytes(events, bytes.data()); - EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x01, 0x01})); + EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x00, 0x01})); tox_events_free(events); } diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index abe09d6b..11526f95 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -10,10 +10,15 @@ #include +#include "DHT.h" #include "ccompat.h" +#include "crypto_core.h" #include "group_chats.h" +#include "group_common.h" #include "mem.h" +#include "net_crypto.h" #include "network.h" +#include "tox.h" #include "tox_struct.h" #define SET_ERROR_PARAMETER(param, x) \ diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index 541641a5..14b209d3 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -31,6 +31,8 @@ Tox_System tox_default_system(void); void tox_lock(const Tox *tox); void tox_unlock(const Tox *tox); +const Tox_System *tox_get_system(Tox *tox); + /** * Set the callback for the `friend_lossy_packet` event for a specific packet ID. * Pass NULL to unset. diff --git a/toxcore/tox_unpack.c b/toxcore/tox_unpack.c index 9d58879a..35bffb10 100644 --- a/toxcore/tox_unpack.c +++ b/toxcore/tox_unpack.c @@ -7,7 +7,7 @@ #include #include "bin_unpack.h" -#include "ccompat.h" +#include "tox.h" non_null() static bool tox_conference_type_from_int(uint32_t value, Tox_Conference_Type *out) diff --git a/toxcore/util.c b/toxcore/util.c index 507524df..a84d181b 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -13,11 +13,10 @@ #include "util.h" -#include #include -#include #include "ccompat.h" +#include "mem.h" bool is_power_of_2(uint64_t x) { diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index 45003055..a8110fb9 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -79,7 +79,9 @@ void tox_pass_key_free(Tox_Pass_Key *key) * * @return true on success. */ -bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *error) +bool tox_get_salt( + const uint8_t ciphertext[TOX_PASS_ENCRYPTION_EXTRA_LENGTH], + uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Get_Salt *error) { if (ciphertext == nullptr || salt == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_GET_SALT_NULL); @@ -112,8 +114,9 @@ bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *er * * @return new symmetric key on success, NULL on failure. */ -Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_len, - Tox_Err_Key_Derivation *error) +Tox_Pass_Key *tox_pass_key_derive( + const uint8_t passphrase[], size_t passphrase_len, + Tox_Err_Key_Derivation *error) { const Random *rng = system_random(); @@ -136,8 +139,9 @@ Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_l * * @return new symmetric key on success, NULL on failure. */ -Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t passphrase_len, - const uint8_t *salt, Tox_Err_Key_Derivation *error) +Tox_Pass_Key *tox_pass_key_derive_with_salt( + const uint8_t passphrase[], size_t passphrase_len, + const uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Key_Derivation *error) { if (salt == nullptr || (passphrase == nullptr && passphrase_len != 0)) { SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_NULL); @@ -189,8 +193,8 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pa * * @return true on success. */ -bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, size_t plaintext_len, - uint8_t *ciphertext, Tox_Err_Encryption *error) +bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], size_t plaintext_len, + uint8_t ciphertext[], Tox_Err_Encryption *error) { const Random *rng = system_random(); @@ -250,8 +254,8 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz * * @return true on success. */ -bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint8_t *passphrase, size_t passphrase_len, - uint8_t *ciphertext, Tox_Err_Encryption *error) +bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uint8_t passphrase[], size_t passphrase_len, + uint8_t ciphertext[/*! plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Encryption *error) { Tox_Err_Key_Derivation err; Tox_Pass_Key *key = tox_pass_key_derive(passphrase, passphrase_len, &err); @@ -281,8 +285,8 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint * * @return true on success. */ -bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, size_t ciphertext_len, - uint8_t *plaintext, Tox_Err_Decryption *error) +bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len, + uint8_t plaintext[], Tox_Err_Decryption *error) { if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); @@ -333,8 +337,8 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si * * @return true on success. */ -bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const uint8_t *passphrase, - size_t passphrase_len, uint8_t *plaintext, Tox_Err_Decryption *error) +bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const uint8_t passphrase[], + size_t passphrase_len, uint8_t plaintext[/*! ciphertext_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Decryption *error) { if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); @@ -382,7 +386,7 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui * * @return true if the data is encrypted by this module. */ -bool tox_is_data_encrypted(const uint8_t *data) +bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH]) { return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0; } diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h index 90d8255e..4bc532ad 100644 --- a/toxencryptsave/toxencryptsave.h +++ b/toxencryptsave/toxencryptsave.h @@ -186,8 +186,8 @@ typedef enum Tox_Err_Decryption { * * @return true on success. */ -bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint8_t *passphrase, size_t passphrase_len, - uint8_t *ciphertext, Tox_Err_Encryption *error); +bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uint8_t passphrase[], size_t passphrase_len, + uint8_t ciphertext[/*! plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Encryption *error); /** * Decrypts the given data with the given passphrase. @@ -203,8 +203,8 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint * * @return true on success. */ -bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const uint8_t *passphrase, - size_t passphrase_len, uint8_t *plaintext, Tox_Err_Decryption *error); +bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const uint8_t passphrase[], + size_t passphrase_len, uint8_t plaintext[/*! ciphertext_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Decryption *error); /******************************************************************************* @@ -255,7 +255,8 @@ void tox_pass_key_free(Tox_Pass_Key *key); * * @return new symmetric key on success, NULL on failure. */ -Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_len, +Tox_Pass_Key *tox_pass_key_derive( + const uint8_t passphrase[], size_t passphrase_len, Tox_Err_Key_Derivation *error); /** @@ -267,8 +268,9 @@ Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_l * * @return new symmetric key on success, NULL on failure. */ -Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t passphrase_len, - const uint8_t *salt, Tox_Err_Key_Derivation *error); +Tox_Pass_Key *tox_pass_key_derive_with_salt( + const uint8_t passphrase[], size_t passphrase_len, + const uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Key_Derivation *error); /** * Encrypt a plain text with a key produced by tox_pass_key_derive or tox_pass_key_derive_with_salt. @@ -282,8 +284,8 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pa * * @return true on success. */ -bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, size_t plaintext_len, - uint8_t *ciphertext, Tox_Err_Encryption *error); +bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], size_t plaintext_len, + uint8_t ciphertext[/*! plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Encryption *error); /** * This is the inverse of tox_pass_key_encrypt, also using only keys produced by @@ -295,8 +297,8 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz * * @return true on success. */ -bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, size_t ciphertext_len, - uint8_t *plaintext, Tox_Err_Decryption *error); +bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len, + uint8_t plaintext[/*! ciphertext_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH */], Tox_Err_Decryption *error); typedef enum Tox_Err_Get_Salt { @@ -338,7 +340,9 @@ typedef enum Tox_Err_Get_Salt { * * @return true on success. */ -bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *error); +bool tox_get_salt( + const uint8_t ciphertext[TOX_PASS_ENCRYPTION_EXTRA_LENGTH], + uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Get_Salt *error); /** * Determines whether or not the given data is encrypted by this module. @@ -354,7 +358,7 @@ bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *er * * @return true if the data is encrypted by this module. */ -bool tox_is_data_encrypted(const uint8_t *data); +bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH]); #ifdef __cplusplus diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 00000000..96eb8e72 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,11 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", + "dependencies": [ + "gtest", + "libsodium", + "libvpx", + "opus", + "pthreads", + "winsock2" + ] +}