Skip to content

Commit

Permalink
Merge onion_announce changes from new groupchats fork
Browse files Browse the repository at this point in the history
These changes will make the DHT compatible with the new groupchat
implementation.
  • Loading branch information
JFreegman committed Mar 20, 2022
1 parent c3a938e commit 88bf101
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .github/scripts/flags-clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ add_flag -Wno-thread-safety-analysis
add_flag -Wno-type-limits
# Callbacks often don't use all their parameters.
add_flag -Wno-unused-parameter
# cimple does this better
add_flag -Wno-unused-function
# libvpx uses __attribute__((unused)) for "potentially unused" static
# functions to avoid unused static function warnings.
add_flag -Wno-used-but-marked-unused
Expand Down
2 changes: 2 additions & 0 deletions .github/scripts/flags-gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ add_flag -Wno-sign-compare
add_flag -Wno-type-limits
# Callbacks often don't use all their parameters.
add_flag -Wno-unused-parameter
# cimple does this better
add_flag -Wno-unused-function
# struct Foo foo = {0}; is a common idiom. Missing braces means we'd need to
# write {{{0}}} in some cases, which is ugly and a maintenance burden.
add_flag -Wno-missing-braces
Expand Down
14 changes: 7 additions & 7 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static int handle_test_1(void *object, const IP_Port *source, const uint8_t *pac

const char req_message[] = "Install Gentoo";
uint8_t req_packet[1 + sizeof(req_message)];
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST_OLD;
memcpy(req_packet + 1, req_message, sizeof(req_message));

if (memcmp(packet, req_packet, sizeof(req_packet)) != 0) {
Expand All @@ -50,7 +50,7 @@ static int handle_test_1(void *object, const IP_Port *source, const uint8_t *pac

const char res_message[] = "install gentoo";
uint8_t res_packet[1 + sizeof(res_message)];
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE_OLD;
memcpy(res_packet + 1, res_message, sizeof(res_message));

if (send_onion_response(onion->net, source, res_packet, sizeof(res_packet),
Expand All @@ -67,7 +67,7 @@ static int handle_test_2(void *object, const IP_Port *source, const uint8_t *pac
{
const char res_message[] = "install gentoo";
uint8_t res_packet[1 + sizeof(res_message)];
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE_OLD;
memcpy(res_packet + 1, res_message, sizeof(res_message));

if (length != sizeof(res_packet)) {
Expand Down Expand Up @@ -200,7 +200,7 @@ static void test_basic(void)
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true, false));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true, false));
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST_OLD, &handle_test_1, onion2);

IP_Port on1 = {ip, net_port(onion1->net)};
Node_format n1;
Expand All @@ -214,7 +214,7 @@ static void test_basic(void)

const char req_message[] = "Install Gentoo";
uint8_t req_packet[1 + sizeof(req_message)];
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST_OLD;
memcpy(req_packet + 1, req_message, sizeof(req_message));

Node_format nodes[4];
Expand All @@ -233,7 +233,7 @@ static void test_basic(void)
do_onion(onion2);
} while (handled_test_1 == 0);

networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_2, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_2, onion1);
handled_test_2 = 0;

do {
Expand All @@ -243,7 +243,7 @@ static void test_basic(void)

Onion_Announce *onion1_a = new_onion_announce(log1, mono_time1, onion1->dht);
Onion_Announce *onion2_a = new_onion_announce(log2, mono_time2, onion2->dht);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_3, onion1);
ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing.");
uint8_t zeroes[64] = {0};
random_bytes(sb_data, sizeof(sb_data));
Expand Down
3 changes: 0 additions & 3 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,6 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke
return len1;
}

#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)
#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)

/** @return packet size of packed node with ip_family on success.
* @retval -1 on failure.
*/
Expand Down
3 changes: 3 additions & 0 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ extern "C" {
#define CRYPTO_PACKET_DHTPK 156
#define CRYPTO_PACKET_NAT_PING 254 // NAT ping crypto packet ID.

#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)
#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE)

/**
* @brief Create a request to peer.
*
Expand Down
9 changes: 7 additions & 2 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8
*length = 0;

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
const int fail_or_len = fuzz_recvfrom(sock.sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
const int fail_or_len = fuzz_recvfrom(sock.sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr,
&addrlen);
#else
int fail_or_len = recvfrom(sock.sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
#endif
Expand Down Expand Up @@ -979,11 +980,13 @@ Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t por

if (net_family_is_ipv6(ip->family)) {
const bool is_dualstack = set_socket_dualstack(temp->sock);

if (is_dualstack) {
LOGGER_TRACE(log, "Dual-stack socket: enabled");
} else {
LOGGER_ERROR(log, "Dual-stack socket failed to enable, won't be able to receive from/send to IPv4 addresses");
}

/* multicast local nodes */
struct ipv6_mreq mreq;
memset(&mreq, 0, sizeof(mreq));
Expand Down Expand Up @@ -1458,8 +1461,9 @@ bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
return true;
#else
LOGGER_DEBUG(log, "connecting socket %d to %s:%d",
(int)sock.sock, ip_ntoa(&ip_port->ip, ip_str, sizeof(ip_str)), ip_port->port);
(int)sock.sock, ip_ntoa(&ip_port->ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port->port));
errno = 0;

if (connect(sock.sock, (struct sockaddr *)&addr, addrsize) == -1) {
const int error = net_error();

Expand All @@ -1471,6 +1475,7 @@ bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
return false;
}
}

return true;
#endif
}
Expand Down
53 changes: 51 additions & 2 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,46 @@ extern const Family net_family_tox_tcp_ipv6;

#define MAX_UDP_PACKET_SIZE 2048

#ifdef USE_TEST_NETWORK
typedef enum Net_Packet_Type {
NET_PACKET_PING_REQUEST = 0x05, /* Ping request packet ID. */
NET_PACKET_PING_RESPONSE = 0x06, /* Ping response packet ID. */
NET_PACKET_GET_NODES = 0x07, /* Get nodes request packet ID. */
NET_PACKET_SEND_NODES_IPV6 = 0x08, /* Send nodes response packet ID for other addresses. */
NET_PACKET_COOKIE_REQUEST = 0x1c, /* Cookie request packet */
NET_PACKET_COOKIE_RESPONSE = 0x1d, /* Cookie response packet */
NET_PACKET_CRYPTO_HS = 0x1e, /* Crypto handshake packet */
NET_PACKET_CRYPTO_DATA = 0x1f, /* Crypto data packet */
NET_PACKET_CRYPTO = 0x24, /* Encrypted data packet ID. */
NET_PACKET_LAN_DISCOVERY = 0x25, /* LAN discovery packet ID. */

// TODO(Jfreegman): Uncomment these when we merge the rest of new groupchats
// NET_PACKET_GC_HANDSHAKE = 0x62, /* Group chat handshake packet ID */
// NET_PACKET_GC_LOSSLESS = 0x63, /* Group chat lossless packet ID */
// NET_PACKET_GC_LOSSY = 0x64, /* Group chat lossy packet ID */

/* See: `docs/Prevent_Tracking.txt` and `onion.{c,h}` */
NET_PACKET_ONION_SEND_INITIAL = 0x8f,
NET_PACKET_ONION_SEND_1 = 0x90,
NET_PACKET_ONION_SEND_2 = 0x91,

NET_PACKET_ANNOUNCE_REQUEST = 0x92,
NET_PACKET_ANNOUNCE_RESPONSE = 0x93,
NET_PACKET_ONION_DATA_REQUEST = 0x94,
NET_PACKET_ONION_DATA_RESPONSE = 0x95,

NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x96, /* TODO: DEPRECATE */
NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x97, /* TODO: DEPRECATE */

NET_PACKET_ONION_RECV_3 = 0x9b,
NET_PACKET_ONION_RECV_2 = 0x9c,
NET_PACKET_ONION_RECV_1 = 0x9d,

BOOTSTRAP_INFO_PACKET_ID = 0xf1, /* Only used for bootstrap nodes */

NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */
} Net_Packet_Type;
#else
typedef enum Net_Packet_Type {
NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */
NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */
Expand All @@ -57,15 +97,23 @@ typedef enum Net_Packet_Type {
NET_PACKET_CRYPTO = 0x20, /* Encrypted data packet ID. */
NET_PACKET_LAN_DISCOVERY = 0x21, /* LAN discovery packet ID. */

// TODO(Jfreegman): Uncomment these when we merge the rest of new groupchats
// NET_PACKET_GC_HANDSHAKE = 0x5a, /* Group chat handshake packet ID */
// NET_PACKET_GC_LOSSLESS = 0x5b, /* Group chat lossless packet ID */
// NET_PACKET_GC_LOSSY = 0x5c, /* Group chat lossy packet ID */

/* See: `docs/Prevent_Tracking.txt` and `onion.{c,h}` */
NET_PACKET_ONION_SEND_INITIAL = 0x80,
NET_PACKET_ONION_SEND_1 = 0x81,
NET_PACKET_ONION_SEND_2 = 0x82,

NET_PACKET_ANNOUNCE_REQUEST = 0x83,
NET_PACKET_ANNOUNCE_RESPONSE = 0x84,
NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x83, /* TODO: DEPRECATE */
NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x84, /* TODO: DEPRECATE */

NET_PACKET_ONION_DATA_REQUEST = 0x85,
NET_PACKET_ONION_DATA_RESPONSE = 0x86,
NET_PACKET_ANNOUNCE_REQUEST = 0x87,
NET_PACKET_ANNOUNCE_RESPONSE = 0x88,

NET_PACKET_ONION_RECV_3 = 0x8c,
NET_PACKET_ONION_RECV_2 = 0x8d,
Expand All @@ -75,6 +123,7 @@ typedef enum Net_Packet_Type {

NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */
} Net_Packet_Type;
#endif // test network


#define TOX_PORTRANGE_FROM 33445
Expand Down
8 changes: 4 additions & 4 deletions toxcore/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static int handle_send_2(void *object, const IP_Port *source, const uint8_t *pac

const uint8_t packet_id = plain[SIZE_IPPORT];

if (packet_id != NET_PACKET_ANNOUNCE_REQUEST && packet_id != NET_PACKET_ONION_DATA_REQUEST) {
if (packet_id != NET_PACKET_ANNOUNCE_REQUEST_OLD && packet_id != NET_PACKET_ONION_DATA_REQUEST) {
return 1;
}

Expand Down Expand Up @@ -504,7 +504,7 @@ static int handle_recv_3(void *object, const IP_Port *source, const uint8_t *pac

const uint8_t packet_id = packet[1 + RETURN_3];

if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}

Expand Down Expand Up @@ -552,7 +552,7 @@ static int handle_recv_2(void *object, const IP_Port *source, const uint8_t *pac

const uint8_t packet_id = packet[1 + RETURN_2];

if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}

Expand Down Expand Up @@ -600,7 +600,7 @@ static int handle_recv_1(void *object, const IP_Port *source, const uint8_t *pac

const uint8_t packet_id = packet[1 + RETURN_1];

if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
if (packet_id != NET_PACKET_ANNOUNCE_RESPONSE_OLD && packet_id != NET_PACKET_ONION_DATA_RESPONSE) {
return 1;
}

Expand Down
Loading

0 comments on commit 88bf101

Please sign in to comment.