9ed2fa80d fix(toxav): remove extra copy of video frame on encode de30cf3ad docs: Add new file kinds, that should be useful to all clients. d5b5e879d fix(DHT): Correct node skipping logic timed out nodes. 30e71fe97 refactor: Generate event dispatch functions and add tox_events_dispatch. 8fdbb0b50 style: Format parameter lists in event handlers. d00dee12b refactor: Add warning logs when losing chat invites. b144e8db1 feat: Add a way to look up a file number by ID. 849281ea0 feat: Add a way to fetch groups by chat ID. a2c177396 refactor: Harden event system and improve type safety. 8f5caa656 refactor: Add MessagePack string support to bin_pack. 34e8d5ad5 chore: Add GitHub CodeQL workflow and local Docker runner. f7b068010 refactor: Add nullability annotations to event headers. 788abe651 refactor(toxav): Use system allocator for mutexes. 2e4b423eb refactor: Use specific typedefs for public API arrays. 2baf34775 docs(toxav): update idle iteration interval see 679444751876fa3882a717772918ebdc8f083354 2f87ac67b feat: Add Event Loop abstraction (Ev). f8dfc38d8 test: Fix data race in ToxScenario virtual_clock. 38313921e test(TCP): Add regression test for TCP priority queue integrity. f94a50d9a refactor(toxav): Replace mutable_mutex with dynamically allocated mutex. ad054511e refactor: Internalize DHT structs and add debug helpers. 8b467cc96 fix: Prevent potential integer overflow in group chat handshake. 4962bdbb8 test: Improve TCP simulation and add tests 5f0227093 refactor: Allow nullable data in group chat handlers. e97b18ea9 chore: Improve Windows Docker support. b14943bbd refactor: Move Logger out of Messenger into Tox. dd3136250 cleanup: Apply nullability qualifiers to C++ codebase. 1849f70fc refactor: Extract low-level networking code to net and os_network. 8fec75421 refactor: Delete tox_random, align on rng and os_random. a03ae8051 refactor: Delete tox_memory, align on mem and os_memory. 4c88fed2c refactor: Use `std::` prefixes more consistently in C++ code. 72452f2ae test: Add some more tests for onion and shared key cache. d5a51b09a cleanup: Use tox_attributes.h in tox_private.h and install it. b6f5b9fc5 test: Add some benchmarks for various high level things. 8a8d02785 test(support): Introduce threaded Tox runner and simulation barrier d68d1d095 perf(toxav): optimize audio and video intermediate buffers by keeping them around REVERT: c9cdae001 fix(toxav): remove extra copy of video frame on encode git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 9ed2fa80d582c714d6bdde6a7648220a92cddff8
304 lines
6.6 KiB
C
304 lines
6.6 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
* Copyright © 2016-2026 The TokTok team.
|
|
* Copyright © 2013 Tox project.
|
|
*/
|
|
|
|
#include "net.h"
|
|
|
|
int net_socket_to_native(Socket sock)
|
|
{
|
|
return (force int)sock.value;
|
|
}
|
|
|
|
Socket net_socket_from_native(int sock)
|
|
{
|
|
const Socket res = {(force Socket_Value)sock};
|
|
return res;
|
|
}
|
|
|
|
int ns_close(const Network *ns, Socket sock)
|
|
{
|
|
return ns->funcs->close(ns->obj, sock);
|
|
}
|
|
|
|
Socket ns_accept(const Network *ns, Socket sock)
|
|
{
|
|
return ns->funcs->accept(ns->obj, sock);
|
|
}
|
|
|
|
int ns_bind(const Network *ns, Socket sock, const IP_Port *addr)
|
|
{
|
|
return ns->funcs->bind(ns->obj, sock, addr);
|
|
}
|
|
|
|
int ns_listen(const Network *ns, Socket sock, int backlog)
|
|
{
|
|
return ns->funcs->listen(ns->obj, sock, backlog);
|
|
}
|
|
|
|
int ns_connect(const Network *ns, Socket sock, const IP_Port *addr)
|
|
{
|
|
return ns->funcs->connect(ns->obj, sock, addr);
|
|
}
|
|
|
|
int ns_recvbuf(const Network *ns, Socket sock)
|
|
{
|
|
return ns->funcs->recvbuf(ns->obj, sock);
|
|
}
|
|
|
|
int ns_recv(const Network *ns, Socket sock, uint8_t *buf, size_t len)
|
|
{
|
|
return ns->funcs->recv(ns->obj, sock, buf, len);
|
|
}
|
|
|
|
int ns_recvfrom(const Network *ns, Socket sock, uint8_t *buf, size_t len, IP_Port *addr)
|
|
{
|
|
return ns->funcs->recvfrom(ns->obj, sock, buf, len, addr);
|
|
}
|
|
|
|
int ns_send(const Network *ns, Socket sock, const uint8_t *buf, size_t len)
|
|
{
|
|
return ns->funcs->send(ns->obj, sock, buf, len);
|
|
}
|
|
|
|
int ns_sendto(const Network *ns, Socket sock, const uint8_t *buf, size_t len, const IP_Port *addr)
|
|
{
|
|
return ns->funcs->sendto(ns->obj, sock, buf, len, addr);
|
|
}
|
|
|
|
Socket ns_socket(const Network *ns, int domain, int type, int proto)
|
|
{
|
|
return ns->funcs->socket(ns->obj, domain, type, proto);
|
|
}
|
|
|
|
int ns_socket_nonblock(const Network *ns, Socket sock, bool nonblock)
|
|
{
|
|
return ns->funcs->socket_nonblock(ns->obj, sock, nonblock);
|
|
}
|
|
|
|
int ns_getsockopt(const Network *ns, Socket sock, int level, int optname, void *optval, size_t *optlen)
|
|
{
|
|
return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen);
|
|
}
|
|
|
|
int ns_setsockopt(const Network *ns, Socket sock, int level, int optname, const void *optval, size_t optlen)
|
|
{
|
|
return ns->funcs->setsockopt(ns->obj, sock, level, optname, optval, optlen);
|
|
}
|
|
|
|
int ns_getaddrinfo(const Network *ns, const Memory *mem, const char *address, int family, int protocol, IP_Port **addrs)
|
|
{
|
|
return ns->funcs->getaddrinfo(ns->obj, mem, address, family, protocol, addrs);
|
|
}
|
|
|
|
int ns_freeaddrinfo(const Network *ns, const Memory *mem, IP_Port *addrs)
|
|
{
|
|
return ns->funcs->freeaddrinfo(ns->obj, mem, addrs);
|
|
}
|
|
|
|
size_t net_pack_bool(uint8_t *bytes, bool v)
|
|
{
|
|
bytes[0] = v ? 1 : 0;
|
|
return 1;
|
|
}
|
|
|
|
size_t net_pack_u16(uint8_t *bytes, uint16_t v)
|
|
{
|
|
bytes[0] = (v >> 8) & 0xff;
|
|
bytes[1] = v & 0xff;
|
|
return sizeof(v);
|
|
}
|
|
|
|
size_t net_pack_u32(uint8_t *bytes, uint32_t v)
|
|
{
|
|
uint8_t *p = bytes;
|
|
p += net_pack_u16(p, (v >> 16) & 0xffff);
|
|
p += net_pack_u16(p, v & 0xffff);
|
|
return p - bytes;
|
|
}
|
|
|
|
size_t net_pack_u64(uint8_t *bytes, uint64_t v)
|
|
{
|
|
uint8_t *p = bytes;
|
|
p += net_pack_u32(p, (v >> 32) & 0xffffffff);
|
|
p += net_pack_u32(p, v & 0xffffffff);
|
|
return p - bytes;
|
|
}
|
|
|
|
size_t net_unpack_bool(const uint8_t *bytes, bool *v)
|
|
{
|
|
*v = bytes[0] != 0;
|
|
return 1;
|
|
}
|
|
|
|
size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
|
|
{
|
|
const uint8_t hi = bytes[0];
|
|
const uint8_t lo = bytes[1];
|
|
*v = ((uint16_t)hi << 8) | lo;
|
|
return sizeof(*v);
|
|
}
|
|
|
|
size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
|
|
{
|
|
const uint8_t *p = bytes;
|
|
uint16_t hi;
|
|
uint16_t lo;
|
|
p += net_unpack_u16(p, &hi);
|
|
p += net_unpack_u16(p, &lo);
|
|
*v = ((uint32_t)hi << 16) | lo;
|
|
return p - bytes;
|
|
}
|
|
|
|
size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
|
|
{
|
|
const uint8_t *p = bytes;
|
|
uint32_t hi;
|
|
uint32_t lo;
|
|
p += net_unpack_u32(p, &hi);
|
|
p += net_unpack_u32(p, &lo);
|
|
*v = ((uint64_t)hi << 32) | lo;
|
|
return p - bytes;
|
|
}
|
|
|
|
static const Family family_unspec = {TOX_AF_UNSPEC};
|
|
static const Family family_ipv4 = {TOX_AF_INET};
|
|
static const Family family_ipv6 = {TOX_AF_INET6};
|
|
static const Family family_tcp_server = {TCP_SERVER_FAMILY};
|
|
static const Family family_tcp_client = {TCP_CLIENT_FAMILY};
|
|
static const Family family_tcp_ipv4 = {TCP_INET};
|
|
static const Family family_tcp_ipv6 = {TCP_INET6};
|
|
static const Family family_tox_tcp_ipv4 = {TOX_TCP_INET};
|
|
static const Family family_tox_tcp_ipv6 = {TOX_TCP_INET6};
|
|
|
|
Family net_family_unspec(void)
|
|
{
|
|
return family_unspec;
|
|
}
|
|
|
|
Family net_family_ipv4(void)
|
|
{
|
|
return family_ipv4;
|
|
}
|
|
|
|
Family net_family_ipv6(void)
|
|
{
|
|
return family_ipv6;
|
|
}
|
|
|
|
Family net_family_tcp_server(void)
|
|
{
|
|
return family_tcp_server;
|
|
}
|
|
|
|
Family net_family_tcp_client(void)
|
|
{
|
|
return family_tcp_client;
|
|
}
|
|
|
|
Family net_family_tcp_ipv4(void)
|
|
{
|
|
return family_tcp_ipv4;
|
|
}
|
|
|
|
Family net_family_tcp_ipv6(void)
|
|
{
|
|
return family_tcp_ipv6;
|
|
}
|
|
|
|
Family net_family_tox_tcp_ipv4(void)
|
|
{
|
|
return family_tox_tcp_ipv4;
|
|
}
|
|
|
|
Family net_family_tox_tcp_ipv6(void)
|
|
{
|
|
return family_tox_tcp_ipv6;
|
|
}
|
|
|
|
bool net_family_is_unspec(Family family)
|
|
{
|
|
return family.value == family_unspec.value;
|
|
}
|
|
|
|
bool net_family_is_ipv4(Family family)
|
|
{
|
|
return family.value == family_ipv4.value;
|
|
}
|
|
|
|
bool net_family_is_ipv6(Family family)
|
|
{
|
|
return family.value == family_ipv6.value;
|
|
}
|
|
|
|
bool net_family_is_tcp_server(Family family)
|
|
{
|
|
return family.value == family_tcp_server.value;
|
|
}
|
|
|
|
bool net_family_is_tcp_client(Family family)
|
|
{
|
|
return family.value == family_tcp_client.value;
|
|
}
|
|
|
|
bool net_family_is_tcp_ipv4(Family family)
|
|
{
|
|
return family.value == family_tcp_ipv4.value;
|
|
}
|
|
|
|
bool net_family_is_tcp_ipv6(Family family)
|
|
{
|
|
return family.value == family_tcp_ipv6.value;
|
|
}
|
|
|
|
bool net_family_is_tox_tcp_ipv4(Family family)
|
|
{
|
|
return family.value == family_tox_tcp_ipv4.value;
|
|
}
|
|
|
|
bool net_family_is_tox_tcp_ipv6(Family family)
|
|
{
|
|
return family.value == family_tox_tcp_ipv6.value;
|
|
}
|
|
|
|
const char *net_family_to_string(Family family)
|
|
{
|
|
if (net_family_is_unspec(family)) {
|
|
return "TOX_AF_UNSPEC";
|
|
}
|
|
|
|
if (net_family_is_ipv4(family)) {
|
|
return "TOX_AF_INET";
|
|
}
|
|
|
|
if (net_family_is_ipv6(family)) {
|
|
return "TOX_AF_INET6";
|
|
}
|
|
|
|
if (net_family_is_tcp_server(family)) {
|
|
return "TCP_SERVER_FAMILY";
|
|
}
|
|
|
|
if (net_family_is_tcp_client(family)) {
|
|
return "TCP_CLIENT_FAMILY";
|
|
}
|
|
|
|
if (net_family_is_tcp_ipv4(family)) {
|
|
return "TCP_INET";
|
|
}
|
|
|
|
if (net_family_is_tcp_ipv6(family)) {
|
|
return "TCP_INET6";
|
|
}
|
|
|
|
if (net_family_is_tox_tcp_ipv4(family)) {
|
|
return "TOX_TCP_INET";
|
|
}
|
|
|
|
if (net_family_is_tox_tcp_ipv6(family)) {
|
|
return "TOX_TCP_INET6";
|
|
}
|
|
|
|
return "<invalid Family>";
|
|
}
|