From ee580500f0f6b9b445f9492bddc38ede33168c0b Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 25 Jun 2022 17:59:35 +0200 Subject: [PATCH] wip time sending --- .gitignore | 23 ++ .gitmodules | 5 + CMakeLists.txt | 56 +++++ README.md | 10 + external/CMakeLists.txt | 19 ++ external/toxcore/CMakeLists.txt | 6 + external/toxcore/c-toxcore | 1 + external/toxcore/cmake/Findsodium.cmake | 297 ++++++++++++++++++++++++ external/toxcore/toxcore.cmake | 171 ++++++++++++++ src/CMakeLists.txt | 30 +++ src/test1_common.hpp | 175 ++++++++++++++ src/test1_receiver.cpp | 94 ++++++++ src/test1_sender.cpp | 112 +++++++++ 13 files changed, 999 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 external/CMakeLists.txt create mode 100644 external/toxcore/CMakeLists.txt create mode 160000 external/toxcore/c-toxcore create mode 100644 external/toxcore/cmake/Findsodium.cmake create mode 100644 external/toxcore/toxcore.cmake create mode 100644 src/CMakeLists.txt create mode 100644 src/test1_common.hpp create mode 100644 src/test1_receiver.cpp create mode 100644 src/test1_sender.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed1c16d --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +.vs/ +*.o +*.swp +~* +*~ +.idea/ +cmake-build-debug/ +cmake-build-debugandtest/ +cmake-build-release/ +*.stackdump +*.coredump +compile_commands.json +/build* +.clangd +.cache + +.DS_Store +.AppleDouble +.LSOverride + +CMakeLists.txt.user* +CMakeCache.txt + diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bdea472 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,5 @@ +[submodule "external/toxcore/c-toxcore"] + path = external/toxcore/c-toxcore + url = https://github.com/TokTok/c-toxcore.git + shallow = true + ignore = dirty diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a8cb6ca --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +# cmake setup begin +project(tox_delay_tests C CXX) + +# defaulting to debug mode, if not specified +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") +endif() + +# setup my vim ycm :D +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# paths +#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +# more paths +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + +# external libs +add_subdirectory(./external) # before increasing warn levels (sad :( ) + +set(CMAKE_CXX_EXTENSIONS OFF) + +# bump up warning levels appropriately for clang, gcc & msvc +if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + add_compile_options( + -Wall -Wextra # Reasonable and standard + -Wpedantic # Warn if non-standard C++ is used + -Wunused # Warn on anything being unused + #-Wconversion # Warn on type conversions that may lose data + #-Wsign-conversion # Warn on sign conversions + -Wshadow # Warn if a variable declaration shadows one from a parent context + ) + + if(NOT WIN32) + link_libraries(-fno-omit-frame-pointer) + link_libraries(-fsanitize=address) + link_libraries(-fsanitize=undefined) + #link_libraries(-fsanitize-memory-track-origins) + #link_libraries(-fsanitize=memory) + endif() +elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") + if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() +endif() + +# cmake setup end + +add_subdirectory(./src) + diff --git a/README.md b/README.md new file mode 100644 index 0000000..7e5e201 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# tox delay tests + +test environment for delay based congetion controll for file transfers + +## resources: + +https://www.bittorrent.org/beps/bep_0029.html#congestion-control + +https://datatracker.ietf.org/doc/rfc6297/ + diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt new file mode 100644 index 0000000..d823734 --- /dev/null +++ b/external/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +option(TOXCORE_HACK OFF) + +# HACK +if(TOXCORE_HACK) + #add_library(toxcore SHARED IMPORTED GLOBAL) + #set_property(TARGET toxcore PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/../../tmp_tox_install/lib/libtoxcore.so.2") + + add_library(toxcore STATIC IMPORTED GLOBAL) + set_property(TARGET toxcore PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/../../tmp_tox_install/lib/libtoxcore.a") + + target_include_directories(toxcore INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/../../tmp_tox_install/include/") + # TODO: find Threads and sodium + target_link_libraries(toxcore INTERFACE pthread sodium) +else() + add_subdirectory(./toxcore) +endif() + diff --git a/external/toxcore/CMakeLists.txt b/external/toxcore/CMakeLists.txt new file mode 100644 index 0000000..e82989e --- /dev/null +++ b/external/toxcore/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.2 FATAL_ERROR) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +include(./toxcore.cmake) + diff --git a/external/toxcore/c-toxcore b/external/toxcore/c-toxcore new file mode 160000 index 0000000..32ed67c --- /dev/null +++ b/external/toxcore/c-toxcore @@ -0,0 +1 @@ +Subproject commit 32ed67cbf346a17bad2f31a24e40bf78bf34fb71 diff --git a/external/toxcore/cmake/Findsodium.cmake b/external/toxcore/cmake/Findsodium.cmake new file mode 100644 index 0000000..a210c00 --- /dev/null +++ b/external/toxcore/cmake/Findsodium.cmake @@ -0,0 +1,297 @@ +# Written in 2016 by Henrik Steffen Gaßmann +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication +# along with this software. If not, see +# +# http://creativecommons.org/publicdomain/zero/1.0/ +# +######################################################################## +# Tries to find the local libsodium installation. +# +# On Windows the sodium_DIR environment variable is used as a default +# hint which can be overridden by setting the corresponding cmake variable. +# +# Once done the following variables will be defined: +# +# sodium_FOUND +# sodium_INCLUDE_DIR +# sodium_LIBRARY_DEBUG +# sodium_LIBRARY_RELEASE +# +# +# Furthermore an imported "sodium" target is created. +# + +if (CMAKE_C_COMPILER_ID STREQUAL "GNU" + OR CMAKE_C_COMPILER_ID STREQUAL "Clang") + set(_GCC_COMPATIBLE 1) +endif() + +# static library option +if (NOT DEFINED sodium_USE_STATIC_LIBS) + option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF) +endif() +if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST)) + unset(sodium_LIBRARY CACHE) + unset(sodium_LIBRARY_DEBUG CACHE) + unset(sodium_LIBRARY_RELEASE CACHE) + unset(sodium_DLL_DEBUG CACHE) + unset(sodium_DLL_RELEASE CACHE) + set(sodium_USE_STATIC_LIBS_LAST ${sodium_USE_STATIC_LIBS} CACHE INTERNAL "internal change tracking variable") +endif() + + +######################################################################## +# UNIX +if (UNIX) + # import pkg-config + find_package(PkgConfig QUIET) + if (PKG_CONFIG_FOUND) + pkg_check_modules(sodium_PKG QUIET libsodium) + endif() + + if(sodium_USE_STATIC_LIBS) + foreach(_libname ${sodium_PKG_STATIC_LIBRARIES}) + if (NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending with .a + list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a") + endif() + endforeach() + list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES) + + # if pkgconfig for libsodium doesn't provide + # static lib info, then override PKG_STATIC here.. + if (NOT sodium_PKG_STATIC_FOUND) + set(sodium_PKG_STATIC_LIBRARIES libsodium.a) + endif() + + set(XPREFIX sodium_PKG_STATIC) + else() + if (NOT sodium_PKG_FOUND) + set(sodium_PKG_LIBRARIES sodium) + endif() + + set(XPREFIX sodium_PKG) + endif() + + find_path(sodium_INCLUDE_DIR sodium.h + HINTS ${${XPREFIX}_INCLUDE_DIRS} + ) + find_library(sodium_LIBRARY_DEBUG NAMES ${${XPREFIX}_LIBRARIES} + HINTS ${${XPREFIX}_LIBRARY_DIRS} + ) + find_library(sodium_LIBRARY_RELEASE NAMES ${${XPREFIX}_LIBRARIES} + HINTS ${${XPREFIX}_LIBRARY_DIRS} + ) + + +######################################################################## +# Windows +elseif (WIN32) + set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory") + mark_as_advanced(sodium_DIR) + + find_path(sodium_INCLUDE_DIR sodium.h + HINTS ${sodium_DIR} + PATH_SUFFIXES include + ) + + if (MSVC) + # detect target architecture + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" [=[ + #if defined _M_IX86 + #error ARCH_VALUE x86_32 + #elif defined _M_X64 + #error ARCH_VALUE x86_64 + #endif + #error ARCH_VALUE unknown + ]=]) + try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" + OUTPUT_VARIABLE _COMPILATION_LOG + ) + string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}") + + # construct library path + if (_TARGET_ARCH STREQUAL "x86_32") + string(APPEND _PLATFORM_PATH "Win32") + elseif(_TARGET_ARCH STREQUAL "x86_64") + string(APPEND _PLATFORM_PATH "x64") + else() + message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.") + endif() + string(APPEND _PLATFORM_PATH "/$$CONFIG$$") + + if (MSVC_VERSION LESS 1900) + math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60") + else() + math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50") + endif() + string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}") + + if (sodium_USE_STATIC_LIBS) + string(APPEND _PLATFORM_PATH "/static") + else() + string(APPEND _PLATFORM_PATH "/dynamic") + endif() + + string(REPLACE "$$CONFIG$$" "Debug" _DEBUG_PATH_SUFFIX "${_PLATFORM_PATH}") + string(REPLACE "$$CONFIG$$" "Release" _RELEASE_PATH_SUFFIX "${_PLATFORM_PATH}") + + find_library(sodium_LIBRARY_DEBUG libsodium.lib + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} + ) + find_library(sodium_LIBRARY_RELEASE libsodium.lib + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} + ) + if (NOT sodium_USE_STATIC_LIBS) + set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES}) + set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll") + find_library(sodium_DLL_DEBUG libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX} + ) + find_library(sodium_DLL_RELEASE libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX} + ) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK}) + endif() + + elseif(_GCC_COMPATIBLE) + if (sodium_USE_STATIC_LIBS) + find_library(sodium_LIBRARY_DEBUG libsodium.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + find_library(sodium_LIBRARY_RELEASE libsodium.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + else() + find_library(sodium_LIBRARY_DEBUG libsodium.dll.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + find_library(sodium_LIBRARY_RELEASE libsodium.dll.a + HINTS ${sodium_DIR} + PATH_SUFFIXES lib + ) + + file(GLOB _DLL + LIST_DIRECTORIES false + RELATIVE "${sodium_DIR}/bin" + "${sodium_DIR}/bin/libsodium*.dll" + ) + find_library(sodium_DLL_DEBUG ${_DLL} libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES bin + ) + find_library(sodium_DLL_RELEASE ${_DLL} libsodium + HINTS ${sodium_DIR} + PATH_SUFFIXES bin + ) + endif() + else() + message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") + endif() + + +######################################################################## +# unsupported +else() + message(FATAL_ERROR "this platform is not supported by FindSodium.cmake") +endif() + + +######################################################################## +# common stuff + +# extract sodium version +if (sodium_INCLUDE_DIR) + set(_VERSION_HEADER "${_INCLUDE_DIR}/sodium/version.h") + if (EXISTS _VERSION_HEADER) + file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT) + string(REGEX REPLACE ".*#[ \t]*define[ \t]*SODIUM_VERSION_STRING[ \t]*\"([^\n]*)\".*" "\\1" + sodium_VERSION "${_VERSION_HEADER_CONTENT}") + set(sodium_VERSION "${sodium_VERSION}" PARENT_SCOPE) + endif() +endif() + +# communicate results +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + sodium # The name must be either uppercase or match the filename case. + REQUIRED_VARS + sodium_LIBRARY_RELEASE + sodium_LIBRARY_DEBUG + sodium_INCLUDE_DIR + VERSION_VAR + sodium_VERSION +) + +if(Sodium_FOUND) + set(sodium_LIBRARIES + optimized ${sodium_LIBRARY_RELEASE} debug ${sodium_LIBRARY_DEBUG}) +endif() + +# mark file paths as advanced +mark_as_advanced(sodium_INCLUDE_DIR) +mark_as_advanced(sodium_LIBRARY_DEBUG) +mark_as_advanced(sodium_LIBRARY_RELEASE) +if (WIN32) + mark_as_advanced(sodium_DLL_DEBUG) + mark_as_advanced(sodium_DLL_RELEASE) +endif() + +# create imported target +if(sodium_USE_STATIC_LIBS) + set(_LIB_TYPE STATIC) +else() + set(_LIB_TYPE SHARED) +endif() + +if(NOT TARGET sodium) + add_library(sodium ${_LIB_TYPE} IMPORTED) +endif() + +set_target_properties(sodium PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" +) + +if (sodium_USE_STATIC_LIBS) + set_target_properties(sodium PROPERTIES + INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC" + IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" + IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" + ) +else() + if (UNIX) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}" + IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}" + ) + elseif (WIN32) + set_target_properties(sodium PROPERTIES + IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}" + IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}" + ) + if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND")) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}" + ) + endif() + if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND")) + set_target_properties(sodium PROPERTIES + IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}" + IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}" + IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}" + ) + endif() + endif() +endif() diff --git a/external/toxcore/toxcore.cmake b/external/toxcore/toxcore.cmake new file mode 100644 index 0000000..9966d2b --- /dev/null +++ b/external/toxcore/toxcore.cmake @@ -0,0 +1,171 @@ +set(TOX_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c-toxcore/") + +# TODO: shared +add_library(toxcore STATIC + ${TOX_DIR}third_party/cmp/cmp.c + ${TOX_DIR}third_party/cmp/cmp.h + + ${TOX_DIR}toxcore/announce.c + ${TOX_DIR}toxcore/announce.h + ${TOX_DIR}toxcore/bin_pack.c + ${TOX_DIR}toxcore/bin_pack.h + ${TOX_DIR}toxcore/bin_unpack.c + ${TOX_DIR}toxcore/bin_unpack.h + ${TOX_DIR}toxcore/ccompat.c + ${TOX_DIR}toxcore/ccompat.h + ${TOX_DIR}toxcore/crypto_core.c + ${TOX_DIR}toxcore/crypto_core.h + ${TOX_DIR}toxcore/DHT.c + ${TOX_DIR}toxcore/DHT.h + ${TOX_DIR}toxcore/events/conference_connected.c + ${TOX_DIR}toxcore/events/conference_invite.c + ${TOX_DIR}toxcore/events/conference_message.c + ${TOX_DIR}toxcore/events/conference_peer_list_changed.c + ${TOX_DIR}toxcore/events/conference_peer_name.c + ${TOX_DIR}toxcore/events/conference_title.c + ${TOX_DIR}toxcore/events/events_alloc.c + ${TOX_DIR}toxcore/events/events_alloc.h + ${TOX_DIR}toxcore/events/file_chunk_request.c + ${TOX_DIR}toxcore/events/file_recv.c + ${TOX_DIR}toxcore/events/file_recv_chunk.c + ${TOX_DIR}toxcore/events/file_recv_control.c + ${TOX_DIR}toxcore/events/friend_connection_status.c + ${TOX_DIR}toxcore/events/friend_lossless_packet.c + ${TOX_DIR}toxcore/events/friend_lossy_packet.c + ${TOX_DIR}toxcore/events/friend_message.c + ${TOX_DIR}toxcore/events/friend_name.c + ${TOX_DIR}toxcore/events/friend_read_receipt.c + ${TOX_DIR}toxcore/events/friend_request.c + ${TOX_DIR}toxcore/events/friend_status.c + ${TOX_DIR}toxcore/events/friend_status_message.c + ${TOX_DIR}toxcore/events/friend_typing.c + ${TOX_DIR}toxcore/events/self_connection_status.c + ${TOX_DIR}toxcore/forwarding.c + ${TOX_DIR}toxcore/forwarding.h + ${TOX_DIR}toxcore/friend_connection.c + ${TOX_DIR}toxcore/friend_connection.h + ${TOX_DIR}toxcore/friend_requests.c + ${TOX_DIR}toxcore/friend_requests.h + ${TOX_DIR}toxcore/group.c + ${TOX_DIR}toxcore/group.h + ${TOX_DIR}toxcore/group_announce.c + ${TOX_DIR}toxcore/group_announce.h + ${TOX_DIR}toxcore/group_moderation.c + ${TOX_DIR}toxcore/group_moderation.h + + #TODO: uncomment when ngc + #${TOX_DIR}toxcore/group_chats.c + #${TOX_DIR}toxcore/group_chats.h + #${TOX_DIR}toxcore/group_common.h + #${TOX_DIR}toxcore/group_connection.c + #${TOX_DIR}toxcore/group_connection.h + #${TOX_DIR}toxcore/group_onion_announce.c + #${TOX_DIR}toxcore/group_onion_announce.h + #${TOX_DIR}toxcore/group_pack.c + #${TOX_DIR}toxcore/group_pack.h + + ${TOX_DIR}toxcore/LAN_discovery.c + ${TOX_DIR}toxcore/LAN_discovery.h + ${TOX_DIR}toxcore/list.c + ${TOX_DIR}toxcore/list.h + ${TOX_DIR}toxcore/logger.c + ${TOX_DIR}toxcore/logger.h + ${TOX_DIR}toxcore/Messenger.c + ${TOX_DIR}toxcore/Messenger.h + ${TOX_DIR}toxcore/mono_time.c + ${TOX_DIR}toxcore/mono_time.h + ${TOX_DIR}toxcore/net_crypto.c + ${TOX_DIR}toxcore/net_crypto.h + ${TOX_DIR}toxcore/network.c + ${TOX_DIR}toxcore/network.h + ${TOX_DIR}toxcore/onion_announce.c + ${TOX_DIR}toxcore/onion_announce.h + ${TOX_DIR}toxcore/onion.c + ${TOX_DIR}toxcore/onion_client.c + ${TOX_DIR}toxcore/onion_client.h + ${TOX_DIR}toxcore/onion.h + ${TOX_DIR}toxcore/ping_array.c + ${TOX_DIR}toxcore/ping_array.h + ${TOX_DIR}toxcore/ping.c + ${TOX_DIR}toxcore/ping.h + ${TOX_DIR}toxcore/state.c + ${TOX_DIR}toxcore/state.h + ${TOX_DIR}toxcore/TCP_client.c + ${TOX_DIR}toxcore/TCP_client.h + ${TOX_DIR}toxcore/TCP_common.c + ${TOX_DIR}toxcore/TCP_common.h + ${TOX_DIR}toxcore/TCP_connection.c + ${TOX_DIR}toxcore/TCP_connection.h + ${TOX_DIR}toxcore/TCP_server.c + ${TOX_DIR}toxcore/TCP_server.h + ${TOX_DIR}toxcore/timed_auth.c + ${TOX_DIR}toxcore/timed_auth.h + ${TOX_DIR}toxcore/tox_api.c + ${TOX_DIR}toxcore/tox.c + ${TOX_DIR}toxcore/tox_dispatch.c + ${TOX_DIR}toxcore/tox_dispatch.h + ${TOX_DIR}toxcore/tox_events.c + ${TOX_DIR}toxcore/tox_events.h + ${TOX_DIR}toxcore/tox.h + ${TOX_DIR}toxcore/tox_private.c + ${TOX_DIR}toxcore/tox_private.h + ${TOX_DIR}toxcore/tox_unpack.c + ${TOX_DIR}toxcore/tox_unpack.h + ${TOX_DIR}toxcore/util.c + ${TOX_DIR}toxcore/util.h +) + +#HACK: "install" api headers into self +# this is dirty, should be binary dir +# TODO: add the others +configure_file( + ${TOX_DIR}toxcore/tox.h + ${TOX_DIR}tox/tox.h + @ONLY +) + +target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore") +target_include_directories(toxcore PUBLIC "${TOX_DIR}") + +target_compile_definitions(toxcore PUBLIC USE_IPV6=1) +#target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG) + +#target_link_libraries(toxcore msgpackc) + +find_package(unofficial-sodium CONFIG QUIET) +find_package(sodium QUIET) +if(unofficial-sodium_FOUND) # vcpkg + target_link_libraries(toxcore unofficial-sodium::sodium unofficial-sodium::sodium_config_public) +elseif(sodium_FOUND) + target_link_libraries(toxcore sodium) +else() + message(SEND_ERROR "missing libsodium") +endif() + +#if(RT_LIBRARIES) + #set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${RT_LIBRARIES}) + #set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lrt) +#endif() + +#if(SOCKET_LIBRARIES) + #set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${SOCKET_LIBRARIES}) + #set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lsocket) +#endif() + +if(WIN32) + target_link_libraries(toxcore ws2_32 iphlpapi) +endif() + +find_package(Threads REQUIRED) +target_link_libraries(toxcore Threads::Threads) + +add_executable(DHT_Bootstrap EXCLUDE_FROM_ALL + ${TOX_DIR}other/DHT_bootstrap.c + ${TOX_DIR}other/bootstrap_node_packets.h + ${TOX_DIR}other/bootstrap_node_packets.c + ${TOX_DIR}testing/misc_tools.h + ${TOX_DIR}testing/misc_tools.c +) + +target_link_libraries(DHT_Bootstrap toxcore) + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..7268a17 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +project(tox_delay_tests C CXX) + +######################################## + +add_executable(test1_receiver + ./test1_common.hpp + ./test1_receiver.cpp +) + +target_compile_features(test1_receiver PUBLIC cxx_std_17) + +target_link_libraries(test1_receiver + toxcore +) + +######################################## + +add_executable(test1_sender + ./test1_common.hpp + ./test1_sender.cpp +) + +target_compile_features(test1_sender PUBLIC cxx_std_17) + +target_link_libraries(test1_sender + toxcore +) + diff --git a/src/test1_common.hpp b/src/test1_common.hpp new file mode 100644 index 0000000..b7133ca --- /dev/null +++ b/src/test1_common.hpp @@ -0,0 +1,175 @@ +extern "C" { +#include +//#include +#include +} + +#include +#include +#include +#include +#include +#include +#include + +inline std::vector hex2bin(const std::string& str) { + std::vector bin{}; + bin.resize(str.size()/2, 0); + + sodium_hex2bin(bin.data(), bin.size(), str.c_str(), str.length(), nullptr, nullptr, nullptr); + + return bin; +} + +inline std::string bin2hex(const std::vector& bin) { + std::string str{}; + str.resize(bin.size()*2, '?'); + + // HECK, std is 1 larger than size returns ('\0') + sodium_bin2hex(str.data(), str.size()+1, bin.data(), bin.size()); + + return str; +} + +inline std::string tox_get_own_address(const Tox *tox) { + std::vector self_addr{}; + self_addr.resize(TOX_ADDRESS_SIZE); + + tox_self_get_address(tox, self_addr.data()); + + return bin2hex(self_addr); +} + +// callbacks +inline void log_cb(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *); +inline void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, void *); +inline void friend_connection_status_cb(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, void *); +inline void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *); +inline void friend_lossy_packet_cb(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void*); + +class ToxService { + protected: + const bool _tcp_only; + + Tox* _tox = nullptr; + std::optional _friend_number; + + ToxService(void) = delete; + + public: + ToxService(bool tcp_only) : _tcp_only(tcp_only) { + TOX_ERR_OPTIONS_NEW err_opt_new; + Tox_Options* options = tox_options_new(&err_opt_new); + assert(err_opt_new == TOX_ERR_OPTIONS_NEW::TOX_ERR_OPTIONS_NEW_OK); + tox_options_set_log_callback(options, log_cb); +#ifndef USE_TEST_NETWORK + tox_options_set_local_discovery_enabled(options, true); +#endif + tox_options_set_udp_enabled(options, !_tcp_only); + tox_options_set_hole_punching_enabled(options, true); + tox_options_set_tcp_port(options, 0); + + TOX_ERR_NEW err_new; + _tox = tox_new(options, &err_new); + tox_options_free(options); + if (err_new != TOX_ERR_NEW_OK) { + throw std::runtime_error{"tox_new failed with error code " + std::to_string(err_new)}; + } + + std::cout << "created tox instance with addr:" << tox_get_own_address(_tox) << "\n"; + +#define CALLBACK_REG(x) tox_callback_##x(_tox, x##_cb) + CALLBACK_REG(self_connection_status); + + CALLBACK_REG(friend_connection_status); + CALLBACK_REG(friend_request); + + CALLBACK_REG(friend_lossy_packet); +#undef CALLBACK_REG + +#if 0 // enable and fill for bootstrapping and tcp relays + // dht bootstrap + { + struct DHT_node { + const char *ip; + uint16_t port; + const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; // 1 for null terminator + unsigned char key_bin[TOX_PUBLIC_KEY_SIZE]; + }; + + DHT_node nodes[] = + { + // own bootsrap node, to reduce load + {"tox.plastiras.org", 33445, "8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725", {}}, // 14 + }; + + for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { + sodium_hex2bin( + nodes[i].key_bin, sizeof(nodes[i].key_bin), + nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, + NULL, NULL, NULL + ); + tox_bootstrap(_tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); + // TODO: use extra tcp option to avoid error msgs + // ... this is hardcore + tox_add_tcp_relay(_tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); + } + } +#endif + + } + + ~ToxService(void) { + tox_kill(_tox); + } + + bool add_friend(const std::string& addr) { + auto addr_bin = hex2bin(addr); + if (addr_bin.size() != TOX_ADDRESS_SIZE) { + return false; + } + + Tox_Err_Friend_Add e_fa {TOX_ERR_FRIEND_ADD_NULL}; + tox_friend_add(_tox, addr_bin.data(), reinterpret_cast("nope"), 4, &e_fa); + + return e_fa == TOX_ERR_FRIEND_ADD_OK; + } + + void friend_online(uint32_t friend_number) { + _friend_number = friend_number; + } + + virtual void handle_lossy_packet(const uint8_t *data, size_t length) = 0; +}; + +inline void log_cb(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *) { + std::cerr << "TOX " << level << " " << file << ":" << line << "(" << func << ") " << message << "\n"; +} + +inline void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, void *) { + std::cout << "self_connection_status_cb " << connection_status << "\n"; +} + +// friend +inline void friend_connection_status_cb(Tox* /*tox*/, uint32_t friend_number, TOX_CONNECTION connection_status, void* user_data) { + std::cout << "friend_connection_status_cb " << connection_status << "\n"; + if (connection_status != TOX_CONNECTION_NONE) { + static_cast(user_data)->friend_online(friend_number); + } +} + +inline void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *) { + std::cout << "friend_request_cb\n"; + + Tox_Err_Friend_Add e_fa = TOX_ERR_FRIEND_ADD::TOX_ERR_FRIEND_ADD_OK; + tox_friend_add_norequest(tox, public_key, &e_fa); +} + +// custom packets +inline void friend_lossy_packet_cb(Tox* /*tox*/, uint32_t friend_number, const uint8_t *data, size_t length, void* user_data) { +#ifndef NDEBUG + std::cout << "friend_lossy_packet_cb " << length << "\n"; +#endif + static_cast(user_data)->handle_lossy_packet(data, length); +} + diff --git a/src/test1_receiver.cpp b/src/test1_receiver.cpp new file mode 100644 index 0000000..e7bd1dc --- /dev/null +++ b/src/test1_receiver.cpp @@ -0,0 +1,94 @@ +#include "./test1_common.hpp" + +class ToxServiceReceiver : public ToxService { + public: + + using ToxService::ToxService; + + // blocks + void run(void) { + while (true) { + using namespace std::literals; + std::this_thread::sleep_for(1ms); + + tox_iterate(_tox, this); + + if (!_friend_number) { + continue; + } + } + } + + void handle_lossy_packet(const uint8_t *data, size_t length) override { + const size_t min_packet_len = + 1 // tox_pkg_id + + sizeof(uint16_t) // seq id + + sizeof(uint32_t) // sender time + + 0; // payload minimum, 0 for testing + + if (length < min_packet_len) { + // TODO: warn + return; + } + + if (data[0] != 200) { + return; // invalid channel + std::cerr << "invalid channel " << (int) data[0] << "\n"; + } + + // packet is: + // - 16bit sequence id + // - 32bit sender timestamp (in ms?) + // - payload + + // we assume little endian + uint16_t pk_seq_id = *reinterpret_cast(data+1); + uint16_t pk_sender_time = *reinterpret_cast(data+3); + + std::cout << "got packet " << pk_seq_id << " t:" << pk_sender_time << "\n"; + } +}; + +// command line : +// +// [friend_tox_addr] +int main(int argc, char** argv) { + if (argc < 2) { + std::cerr << "not enough params, usage:\n$ " << argv[0] << " [friend_tox_addr]\n"; + return -1; + } + + // TODO: just use some arg lib + + // contype + bool tcp_only = false; + std::string_view type_sv{argv[1]}; + if (type_sv == "tcp") { + tcp_only = true; + } else if (type_sv == "mixed") { + tcp_only = false; + } else { + std::cerr << "error: invalid type " << type_sv << ", must be either tcp or mixed.\n"; + return -1; + } + std::cout << "set type to " << type_sv << "\n"; + + std::string_view friend_sv{}; + if (argc == 3) { // friend? + friend_sv = argv[2]; + } + + ToxServiceReceiver ts{tcp_only}; + + if (!friend_sv.empty()) { + if (!ts.add_friend(std::string(friend_sv))) { + std::cerr << "error adding friend!\n"; + return -1; + } + } + + ts.run(); + + return 0; +} + diff --git a/src/test1_sender.cpp b/src/test1_sender.cpp new file mode 100644 index 0000000..690ed33 --- /dev/null +++ b/src/test1_sender.cpp @@ -0,0 +1,112 @@ +#include "./test1_common.hpp" + +#include +#include + +class ToxServiceSender : public ToxService { + uint16_t _seq_id {0}; + public: + + using ToxService::ToxService; + + // blocks + void run(void) { + while (true) { + using namespace std::literals; + std::this_thread::sleep_for(1ms); + + tox_iterate(_tox, this); + + if (!_friend_number) { + continue; + } + + if (_seq_id == std::numeric_limits::max()) { + std::cout << "reached max seq, quitting\n"; + break; + } + + if (true) { // can send + // 192-254 for lossy + const size_t max_pkg_size = 1024 + 1; + uint8_t buffer[max_pkg_size] {200}; // fist byte is tox pkg id + size_t pkg_size {1}; + + // seq_id + *reinterpret_cast(buffer+pkg_size) = _seq_id++; + pkg_size += sizeof(uint16_t); + + { // time stamp + // TODO: C + //std::chrono::time_point>> time = std::chrono::steady_clock::now(); + auto time_raw = std::chrono::steady_clock::now(); + auto time = std::chrono::duration_cast>>(time_raw.time_since_epoch()); + + *reinterpret_cast(buffer+pkg_size) = time.count(); + pkg_size += sizeof(uint32_t); + } + + Tox_Err_Friend_Custom_Packet e_fcp = TOX_ERR_FRIEND_CUSTOM_PACKET_OK; + tox_friend_send_lossy_packet(_tox, *_friend_number, buffer, pkg_size, &e_fcp); + if (e_fcp != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { + std::cerr << "error sending lossy pkg " << e_fcp << "\n"; + } + } + } + } + + void handle_lossy_packet(const uint8_t *data, size_t length) override { + if (length < 2) { + return; + } + + if (data[0] != 200) { + return; // invalid channel + std::cerr << "invalid channel " << (int) data[0] << "\n"; + } + } +}; + +// command line : +// +// [friend_tox_addr] +int main(int argc, char** argv) { + if (argc < 2) { + std::cerr << "not enough params, usage:\n$ " << argv[0] << " [friend_tox_addr]\n"; + return -1; + } + + // TODO: just use some arg lib + + // contype + bool tcp_only = false; + std::string_view type_sv{argv[1]}; + if (type_sv == "tcp") { + tcp_only = true; + } else if (type_sv == "mixed") { + tcp_only = false; + } else { + std::cerr << "error: invalid type " << type_sv << ", must be either tcp or mixed.\n"; + return -1; + } + std::cout << "set type to " << type_sv << "\n"; + + std::string_view friend_sv{}; + if (argc == 3) { // friend? + friend_sv = argv[2]; + } + + ToxServiceSender ts{tcp_only}; + + if (!friend_sv.empty()) { + if (!ts.add_friend(std::string(friend_sv))) { + std::cerr << "error adding friend!\n"; + return -1; + } + } + + ts.run(); + + return 0; +} +