Squashed 'external/toxcore/c-toxcore/' changes from 11ab1d2a723..d9b8fa6098d

d9b8fa6098d fix: Fake broadcast address for 127.x.x.x
aa649165a57 chore: Add code for future netprof TCP testing
9e5693de5ac chore: add to_string functions for netprof enums
52d915e6a90 cleanup: Heap allocate network profile objects
80fabd4a729 feat: Implement Tox network profiler
05abe083cb6 cleanup: Some random cleanups, mostly related to mem.
5cca24513b8 cleanup: Check that onion IP/Port packing worked.
e092ecd1244 cleanup: Use tox memory allocator in some more places.
3cfe41c7587 fix: Avoid `memcpy`-ing structs into onion ping id data.
e32ac001938 fix: Add more information on why the frame was not sent.
ab887003687 fix: Allow TCP connections to fail `connect` calls.
7603170e663 refactor: Use tox memory in group connection allocations.
5bd8a85eb89 cleanup: Align internal logger with external on type of source line.
e9bf524d9e1 cleanup: Add missing `#include` to sort_test.cc.
d10c966b998 feat: Add `to_string` functions for toxencryptsave errors.
7bfd0dc8003 docs: Update the docs for group join functions
380dde9f2ae test: Add more logging to TCP connection constructor.
0f12f384c8c cleanup: Reduce stack frame sizes to below 4096 bytes.
bc43cec0626 chore: Happy new year!
fbe78f1702e cleanup: Add a `TOX_HIDE_DEPRECATED` check to hide deprecated symbols.
44d9da07e77 refactor: Use tox memory for group moderation/pack allocations.
7f26d520168 refactor: Use tox memory in group chats allocations.
2f62f3d0e77 refactor: Use tox Memory for group allocations.
8a968162041 chore: Add dispatch/events headers to bazel export.
2bbfb35abf6 docs: Output the error code string instead of int. in toxav logging
d55d0e4eaef cleanup: Remove redundant code for checking if group exists
2a6dc643338 chore: Upgrade dependencies for websockify.
fc0650601c1 fix: Allow peers to reconnect to group chats using a password

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: d9b8fa6098de6c074038b6664d2572627540b148
This commit is contained in:
Green Sky
2025-01-18 15:53:06 +01:00
parent 261d2e53b7
commit 3b6bb15e86
213 changed files with 2341 additions and 758 deletions

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2016-2025 The TokTok team.
* Copyright © 2013 Tox project.
*/
@ -8,8 +8,6 @@
*/
#include "LAN_discovery.h"
#include <stdlib.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
// The mingw32/64 Windows library warns about including winsock2.h after
// windows.h even though with the above it's a valid thing to do. So, to make
@ -41,11 +39,14 @@
#include "attributes.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "mem.h"
#include "network.h"
#define MAX_INTERFACES 16
struct Broadcast_Info {
const Memory *mem;
uint32_t count;
IP ips[MAX_INTERFACES];
};
@ -53,29 +54,31 @@ struct Broadcast_Info {
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
if (broadcast == nullptr) {
return nullptr;
}
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
broadcast->mem = mem;
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO));
if (adapter_info == nullptr) {
free(broadcast);
mem_delete(mem, broadcast);
return nullptr;
}
unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
free(adapter_info);
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len);
mem_delete(mem, adapter_info);
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len);
if (new_adapter_info == nullptr) {
free(broadcast);
mem_delete(mem, broadcast);
return nullptr;
}
@ -113,7 +116,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
}
if (adapter_info != nullptr) {
free(adapter_info);
mem_delete(mem, adapter_info);
}
return broadcast;
@ -122,14 +125,19 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
#elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__))
non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static bool ip4_is_local(const IP4 *ip4);
non_null()
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
if (broadcast == nullptr) {
return nullptr;
}
broadcast->mem = mem;
/* Not sure how many platforms this will run on,
* so it's wrapped in `__linux__` for now.
* Definitely won't work like this on Windows...
@ -137,7 +145,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0);
if (!sock_valid(sock)) {
free(broadcast);
mem_delete(mem, broadcast);
return nullptr;
}
@ -150,7 +158,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) {
kill_sock(ns, sock);
free(broadcast);
mem_delete(mem, broadcast);
return nullptr;
}
@ -162,7 +170,8 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
const int n = ifc.ifc_len / sizeof(struct ifreq);
for (int i = 0; i < n; ++i) {
/* there are interfaces with are incapable of broadcast */
/* there are interfaces with are incapable of broadcast
* on Linux, `lo` has no broadcast address, but this function returns `>=0` */
if (ioctl(net_socket_to_native(sock), SIOCGIFBRDADDR, &i_faces[i]) < 0) {
continue;
}
@ -172,7 +181,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
continue;
}
const struct sockaddr_in *sock4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr;
const struct sockaddr_in *broadaddr4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr;
if (broadcast->count >= MAX_INTERFACES) {
break;
@ -180,10 +189,27 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
IP *ip = &broadcast->ips[broadcast->count];
ip->family = net_family_ipv4();
ip->ip.v4.uint32 = sock4->sin_addr.s_addr;
ip->ip.v4.uint32 = broadaddr4->sin_addr.s_addr;
// if no broadcast address
if (ip->ip.v4.uint32 == 0) {
continue;
if (ioctl(net_socket_to_native(sock), SIOCGIFADDR, &i_faces[i]) < 0) {
continue;
}
const struct sockaddr_in *addr4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_addr;
IP4 ip4_staging;
ip4_staging.uint32 = addr4->sin_addr.s_addr;
if (ip4_is_local(&ip4_staging)) {
// this is 127.x.x.x
ip->ip.v4.uint32 = ip4_staging.uint32;
} else {
// give up.
continue;
}
}
++broadcast->count;
@ -197,9 +223,17 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
#else // TODO(irungentoo): Other platforms?
non_null()
static Broadcast_Info *fetch_broadcast_info(const Network *ns)
static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns)
{
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info));
if (broadcast == nullptr) {
return nullptr;
}
broadcast->mem = mem;
return broadcast;
}
#endif /* platforms */
@ -375,12 +409,16 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
return res;
}
Broadcast_Info *lan_discovery_init(const Network *ns)
Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns)
{
return fetch_broadcast_info(ns);
return fetch_broadcast_info(mem, ns);
}
void lan_discovery_kill(Broadcast_Info *broadcast)
{
free(broadcast);
if (broadcast == nullptr) {
return;
}
mem_delete(broadcast->mem, broadcast);
}