summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorJan Malakhovski <oxij@oxij.org>2018-07-01 23:26:39 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-19 19:24:00 +0000
commite7a5f52c14c3603f883b5dbdcc9ab589b50a3ed0 (patch)
tree332dc17d7b7042612a652d83da0c47e330fb1d53 /toxcore
parent3f6b6842f33f6ac74bdaa61cfa47565d509fa453 (diff)
Collect `PACKET_ID*` constants in `net_crypto.h`, cleanup their uses
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c41
-rw-r--r--toxcore/Messenger.h27
-rw-r--r--toxcore/net_crypto.c25
-rw-r--r--toxcore/net_crypto.h70
-rw-r--r--toxcore/tox.c2
5 files changed, 82 insertions, 83 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 416b937c..cb7e3df5 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -515,7 +515,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
515 } 515 }
516 516
517 VLA(uint8_t, packet, length + 1); 517 VLA(uint8_t, packet, length + 1);
518 packet[0] = type + PACKET_ID_MESSAGE; 518 packet[0] = PACKET_ID_MESSAGE + type;
519 519
520 if (length != 0) { 520 if (length != 0) {
521 memcpy(packet + 1, message, length); 521 memcpy(packet + 1, message, length);
@@ -1816,11 +1816,12 @@ static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint
1816 return 1; 1816 return 1;
1817 } 1817 }
1818 1818
1819 if (packet[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) { 1819 if (packet[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
1820 if (m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function) { 1820 const RTP_Packet_Handler *const ph =
1821 return m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function( 1821 &m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
1822 m, friend_num, packet, length, m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % 1822
1823 PACKET_LOSSY_AV_RESERVED].object); 1823 if (ph->function) {
1824 return ph->function(m, friend_num, packet, length, ph->object);
1824 } 1825 }
1825 1826
1826 return 1; 1827 return 1;
@@ -1845,16 +1846,12 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo
1845 return -1; 1846 return -1;
1846 } 1847 }
1847 1848
1848 if (byte < PACKET_ID_LOSSY_RANGE_START) { 1849 if (byte < PACKET_ID_RANGE_LOSSY_AV_START || byte > PACKET_ID_RANGE_LOSSY_AV_END) {
1849 return -1;
1850 }
1851
1852 if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) {
1853 return -1; 1850 return -1;
1854 } 1851 }
1855 1852
1856 m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = function; 1853 m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].function = function;
1857 m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].object = object; 1854 m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].object = object;
1858 return 0; 1855 return 0;
1859} 1856}
1860 1857
@@ -1869,11 +1866,7 @@ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const u
1869 return -2; 1866 return -2;
1870 } 1867 }
1871 1868
1872 if (data[0] < PACKET_ID_LOSSY_RANGE_START) { 1869 if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
1873 return -3;
1874 }
1875
1876 if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
1877 return -3; 1870 return -3;
1878 } 1871 }
1879 1872
@@ -1898,11 +1891,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin
1898 return -1; 1891 return -1;
1899 } 1892 }
1900 1893
1901 if (packet[0] < PACKET_ID_LOSSLESS_RANGE_START) { 1894 if (packet[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || packet[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
1902 return -1;
1903 }
1904
1905 if (packet[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) {
1906 return -1; 1895 return -1;
1907 } 1896 }
1908 1897
@@ -1928,11 +1917,7 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
1928 return -2; 1917 return -2;
1929 } 1918 }
1930 1919
1931 if (data[0] < PACKET_ID_LOSSLESS_RANGE_START) { 1920 if (data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
1932 return -3;
1933 }
1934
1935 if (data[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) {
1936 return -3; 1921 return -3;
1937 } 1922 }
1938 1923
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 833e6b2b..8a4686f1 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -28,6 +28,7 @@
28#include "friend_connection.h" 28#include "friend_connection.h"
29#include "friend_requests.h" 29#include "friend_requests.h"
30#include "logger.h" 30#include "logger.h"
31#include "net_crypto.h"
31 32
32#define MAX_NAME_LENGTH 128 33#define MAX_NAME_LENGTH 128
33/* TODO(irungentoo): this must depend on other variable. */ 34/* TODO(irungentoo): this must depend on other variable. */
@@ -49,30 +50,6 @@ typedef enum Message_Type {
49 MESSAGE_ACTION 50 MESSAGE_ACTION
50} Message_Type; 51} Message_Type;
51 52
52/* NOTE: Packet ids below 24 must never be used. */
53#define PACKET_ID_ONLINE 24
54#define PACKET_ID_OFFLINE 25
55#define PACKET_ID_NICKNAME 48
56#define PACKET_ID_STATUSMESSAGE 49
57#define PACKET_ID_USERSTATUS 50
58#define PACKET_ID_TYPING 51
59#define PACKET_ID_MESSAGE 64
60#define PACKET_ID_ACTION (PACKET_ID_MESSAGE + MESSAGE_ACTION) // 65
61#define PACKET_ID_MSI 69
62#define PACKET_ID_FILE_SENDREQUEST 80
63#define PACKET_ID_FILE_CONTROL 81
64#define PACKET_ID_FILE_DATA 82
65#define PACKET_ID_INVITE_CONFERENCE 96
66#define PACKET_ID_ONLINE_PACKET 97
67#define PACKET_ID_DIRECT_CONFERENCE 98
68#define PACKET_ID_MESSAGE_CONFERENCE 99
69#define PACKET_ID_LOSSY_CONFERENCE 199
70
71/* All packets starting with a byte in this range can be used for anything. */
72#define PACKET_ID_LOSSLESS_RANGE_START 160
73#define PACKET_ID_LOSSLESS_RANGE_SIZE 32
74#define PACKET_LOSSY_AV_RESERVED 8 // Number of lossy packet types at start of range reserved for A/V.
75
76typedef struct Messenger_Options { 53typedef struct Messenger_Options {
77 bool ipv6enabled; 54 bool ipv6enabled;
78 bool udp_disabled; 55 bool udp_disabled;
@@ -247,7 +224,7 @@ typedef struct Friend {
247 uint32_t num_sending_files; 224 uint32_t num_sending_files;
248 struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; 225 struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES];
249 226
250 RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_LOSSY_AV_RESERVED]; 227 RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_ID_RANGE_LOSSY_AV_SIZE];
251 228
252 struct Receipts *receipts_start; 229 struct Receipts *receipts_start;
253 struct Receipts *receipts_end; 230 struct Receipts *receipts_end;
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 6ca29ef3..8f5fd507 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1549,7 +1549,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
1549 } 1549 }
1550 1550
1551 set_buffer_end(c->log, &conn->recv_array, num); 1551 set_buffer_end(c->log, &conn->recv_array, num);
1552 } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { 1552 } else if (real_data[0] >= PACKET_ID_RANGE_LOSSLESS_START && real_data[0] <= PACKET_ID_RANGE_LOSSLESS_END) {
1553 Packet_Data dt = {0}; 1553 Packet_Data dt = {0};
1554 dt.length = real_length; 1554 dt.length = real_length;
1555 memcpy(dt.data, real_data, real_length); 1555 memcpy(dt.data, real_data, real_length);
@@ -1582,8 +1582,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
1582 1582
1583 /* Packet counter. */ 1583 /* Packet counter. */
1584 ++conn->packet_counter; 1584 ++conn->packet_counter;
1585 } else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START && 1585 } else if (real_data[0] >= PACKET_ID_RANGE_LOSSY_START && real_data[0] <= PACKET_ID_RANGE_LOSSY_END) {
1586 real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
1587 1586
1588 set_buffer_end(c->log, &conn->recv_array, num); 1587 set_buffer_end(c->log, &conn->recv_array, num);
1589 1588
@@ -2702,6 +2701,8 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
2702 * return -1 if data could not be put in packet queue. 2701 * return -1 if data could not be put in packet queue.
2703 * return positive packet number if data was put into the queue. 2702 * return positive packet number if data was put into the queue.
2704 * 2703 *
2704 * The first byte of data must in the PACKET_ID_RANGE_LOSSLESS.
2705 *
2705 * congestion_control: should congestion control apply to this packet? 2706 * congestion_control: should congestion control apply to this packet?
2706 */ 2707 */
2707int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, 2708int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
@@ -2711,11 +2712,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
2711 return -1; 2712 return -1;
2712 } 2713 }
2713 2714
2714 if (data[0] < CRYPTO_RESERVED_PACKETS) { 2715 if (data[0] < PACKET_ID_RANGE_LOSSLESS_START || data[0] > PACKET_ID_RANGE_LOSSLESS_END) {
2715 return -1;
2716 }
2717
2718 if (data[0] >= PACKET_ID_LOSSY_RANGE_START) {
2719 return -1; 2716 return -1;
2720 } 2717 }
2721 2718
@@ -2780,10 +2777,12 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet
2780 return 0; 2777 return 0;
2781} 2778}
2782 2779
2783/* return -1 on failure. 2780/* Sends a lossy cryptopacket.
2781 *
2782 * return -1 on failure.
2784 * return 0 on success. 2783 * return 0 on success.
2785 * 2784 *
2786 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) 2785 * The first byte of data must in the PACKET_ID_RANGE_LOSSY.
2787 */ 2786 */
2788int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) 2787int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
2789{ 2788{
@@ -2791,11 +2790,7 @@ int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
2791 return -1; 2790 return -1;
2792 } 2791 }
2793 2792
2794 if (data[0] < PACKET_ID_LOSSY_RANGE_START) { 2793 if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
2795 return -1;
2796 }
2797
2798 if (data[0] >= PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE) {
2799 return -1; 2794 return -1;
2800 } 2795 }
2801 2796
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 2ed35883..e40b7bfd 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -31,6 +31,57 @@
31 31
32#include <pthread.h> 32#include <pthread.h>
33 33
34/*** Crypto payloads. ***/
35
36/** Ranges. **/
37
38/* Packets in this range are reserved for net_crypto internal use. */
39#define PACKET_ID_RANGE_RESERVED_START 0
40#define PACKET_ID_RANGE_RESERVED_END 15
41/* Packets in this range are reserved for Messenger use. */
42#define PACKET_ID_RANGE_LOSSLESS_START 16
43#define PACKET_ID_RANGE_LOSSLESS_NORMAL_START 16
44#define PACKET_ID_RANGE_LOSSLESS_NORMAL_END 159
45/* Packets in this range can be used for anything. */
46#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_START 160
47#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_END 191
48#define PACKET_ID_RANGE_LOSSLESS_END 191
49/* Packets in this range are reserved for AV use. */
50#define PACKET_ID_RANGE_LOSSY_START 192
51#define PACKET_ID_RANGE_LOSSY_AV_START 192
52#define PACKET_ID_RANGE_LOSSY_AV_SIZE 8
53#define PACKET_ID_RANGE_LOSSY_AV_END 199
54/* Packets in this range can be used for anything. */
55#define PACKET_ID_RANGE_LOSSY_CUSTOM_START 200
56#define PACKET_ID_RANGE_LOSSY_CUSTOM_END 254
57#define PACKET_ID_RANGE_LOSSY_END 254
58
59/** Messages. **/
60
61#define PACKET_ID_PADDING 0 /* Denotes padding */
62#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
63#define PACKET_ID_KILL 2 /* Used to kill connection */
64
65#define PACKET_ID_ONLINE 24
66#define PACKET_ID_OFFLINE 25
67#define PACKET_ID_NICKNAME 48
68#define PACKET_ID_STATUSMESSAGE 49
69#define PACKET_ID_USERSTATUS 50
70#define PACKET_ID_TYPING 51
71#define PACKET_ID_MESSAGE 64
72#define PACKET_ID_ACTION 65 /* PACKET_ID_MESSAGE + MESSAGE_ACTION */
73#define PACKET_ID_MSI 69 /* Used by AV to setup calls and etc */
74#define PACKET_ID_FILE_SENDREQUEST 80
75#define PACKET_ID_FILE_CONTROL 81
76#define PACKET_ID_FILE_DATA 82
77#define PACKET_ID_INVITE_CONFERENCE 96
78#define PACKET_ID_ONLINE_PACKET 97
79#define PACKET_ID_DIRECT_CONFERENCE 98
80#define PACKET_ID_MESSAGE_CONFERENCE 99
81#define PACKET_ID_LOSSY_CONFERENCE 199
82
83/*** Crypto connections. ***/
84
34typedef enum Crypto_Conn_State { 85typedef enum Crypto_Conn_State {
35 CRYPTO_CONN_NO_CONNECTION = 0, 86 CRYPTO_CONN_NO_CONNECTION = 0,
36 CRYPTO_CONN_COOKIE_REQUESTING = 1, // send cookie request packets 87 CRYPTO_CONN_COOKIE_REQUESTING = 1, // send cookie request packets
@@ -66,20 +117,9 @@ typedef enum Crypto_Conn_State {
66/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ 117/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
67#define UDP_DIRECT_TIMEOUT 8 118#define UDP_DIRECT_TIMEOUT 8
68 119
69#define PACKET_ID_PADDING 0 /* Denotes padding */
70#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
71#define PACKET_ID_KILL 2 /* Used to kill connection */
72
73/* Packet ids 0 to CRYPTO_RESERVED_PACKETS - 1 are reserved for use by net_crypto. */
74#define CRYPTO_RESERVED_PACKETS 16
75
76#define MAX_TCP_CONNECTIONS 64 120#define MAX_TCP_CONNECTIONS 64
77#define MAX_TCP_RELAYS_PEER 4 121#define MAX_TCP_RELAYS_PEER 4
78 122
79/* All packets starting with a byte in this range are considered lossy packets. */
80#define PACKET_ID_LOSSY_RANGE_START 192
81#define PACKET_ID_LOSSY_RANGE_SIZE 63
82
83#define CRYPTO_MAX_PADDING 8 /* All packets will be padded a number of bytes based on this number. */ 123#define CRYPTO_MAX_PADDING 8 /* All packets will be padded a number of bytes based on this number. */
84 124
85/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken 125/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken
@@ -209,7 +249,7 @@ bool max_speed_reached(Net_Crypto *c, int crypt_connection_id);
209 * return -1 if data could not be put in packet queue. 249 * return -1 if data could not be put in packet queue.
210 * return positive packet number if data was put into the queue. 250 * return positive packet number if data was put into the queue.
211 * 251 *
212 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 252 * The first byte of data must be in the PACKET_ID_RANGE_LOSSLESS.
213 * 253 *
214 * congestion_control: should congestion control apply to this packet? 254 * congestion_control: should congestion control apply to this packet?
215 */ 255 */
@@ -225,10 +265,12 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
225 */ 265 */
226int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet_number); 266int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet_number);
227 267
228/* return -1 on failure. 268/* Sends a lossy cryptopacket.
269 *
270 * return -1 on failure.
229 * return 0 on success. 271 * return 0 on success.
230 * 272 *
231 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) 273 * The first byte of data must be in the PACKET_ID_RANGE_LOSSY.
232 */ 274 */
233int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length); 275int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length);
234 276
diff --git a/toxcore/tox.c b/toxcore/tox.c
index cae2dcb3..95acfd2a 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -1522,7 +1522,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
1522 return 0; 1522 return 0;
1523 } 1523 }
1524 1524
1525 if (data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) { 1525 if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
1526 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID); 1526 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID);
1527 return 0; 1527 return 0;
1528 } 1528 }