Collect PACKET_ID* constants in net_crypto.h, cleanup their uses

This commit is contained in:
Jan Malakhovski 2018-07-01 23:26:39 +00:00 committed by iphydf
parent 3f6b6842f3
commit e7a5f52c14
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
5 changed files with 82 additions and 83 deletions

View File

@ -515,7 +515,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
}
VLA(uint8_t, packet, length + 1);
packet[0] = type + PACKET_ID_MESSAGE;
packet[0] = PACKET_ID_MESSAGE + type;
if (length != 0) {
memcpy(packet + 1, message, length);
@ -1816,11 +1816,12 @@ static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint
return 1;
}
if (packet[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) {
if (m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function) {
return m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function(
m, friend_num, packet, length, m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] %
PACKET_LOSSY_AV_RESERVED].object);
if (packet[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
const RTP_Packet_Handler *const ph =
&m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
if (ph->function) {
return ph->function(m, friend_num, packet, length, ph->object);
}
return 1;
@ -1845,16 +1846,12 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo
return -1;
}
if (byte < PACKET_ID_LOSSY_RANGE_START) {
if (byte < PACKET_ID_RANGE_LOSSY_AV_START || byte > PACKET_ID_RANGE_LOSSY_AV_END) {
return -1;
}
if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) {
return -1;
}
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = function;
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].object = object;
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].function = function;
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].object = object;
return 0;
}
@ -1869,11 +1866,7 @@ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const u
return -2;
}
if (data[0] < PACKET_ID_LOSSY_RANGE_START) {
return -3;
}
if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
return -3;
}
@ -1898,11 +1891,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin
return -1;
}
if (packet[0] < PACKET_ID_LOSSLESS_RANGE_START) {
return -1;
}
if (packet[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) {
if (packet[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || packet[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
return -1;
}
@ -1928,11 +1917,7 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
return -2;
}
if (data[0] < PACKET_ID_LOSSLESS_RANGE_START) {
return -3;
}
if (data[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) {
if (data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
return -3;
}

View File

@ -28,6 +28,7 @@
#include "friend_connection.h"
#include "friend_requests.h"
#include "logger.h"
#include "net_crypto.h"
#define MAX_NAME_LENGTH 128
/* TODO(irungentoo): this must depend on other variable. */
@ -49,30 +50,6 @@ typedef enum Message_Type {
MESSAGE_ACTION
} Message_Type;
/* NOTE: Packet ids below 24 must never be used. */
#define PACKET_ID_ONLINE 24
#define PACKET_ID_OFFLINE 25
#define PACKET_ID_NICKNAME 48
#define PACKET_ID_STATUSMESSAGE 49
#define PACKET_ID_USERSTATUS 50
#define PACKET_ID_TYPING 51
#define PACKET_ID_MESSAGE 64
#define PACKET_ID_ACTION (PACKET_ID_MESSAGE + MESSAGE_ACTION) // 65
#define PACKET_ID_MSI 69
#define PACKET_ID_FILE_SENDREQUEST 80
#define PACKET_ID_FILE_CONTROL 81
#define PACKET_ID_FILE_DATA 82
#define PACKET_ID_INVITE_CONFERENCE 96
#define PACKET_ID_ONLINE_PACKET 97
#define PACKET_ID_DIRECT_CONFERENCE 98
#define PACKET_ID_MESSAGE_CONFERENCE 99
#define PACKET_ID_LOSSY_CONFERENCE 199
/* All packets starting with a byte in this range can be used for anything. */
#define PACKET_ID_LOSSLESS_RANGE_START 160
#define PACKET_ID_LOSSLESS_RANGE_SIZE 32
#define PACKET_LOSSY_AV_RESERVED 8 // Number of lossy packet types at start of range reserved for A/V.
typedef struct Messenger_Options {
bool ipv6enabled;
bool udp_disabled;
@ -247,7 +224,7 @@ typedef struct Friend {
uint32_t num_sending_files;
struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES];
RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_LOSSY_AV_RESERVED];
RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_ID_RANGE_LOSSY_AV_SIZE];
struct Receipts *receipts_start;
struct Receipts *receipts_end;

View File

@ -1549,7 +1549,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
}
set_buffer_end(c->log, &conn->recv_array, num);
} else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) {
} else if (real_data[0] >= PACKET_ID_RANGE_LOSSLESS_START && real_data[0] <= PACKET_ID_RANGE_LOSSLESS_END) {
Packet_Data dt = {0};
dt.length = real_length;
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
/* Packet counter. */
++conn->packet_counter;
} else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START &&
real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
} else if (real_data[0] >= PACKET_ID_RANGE_LOSSY_START && real_data[0] <= PACKET_ID_RANGE_LOSSY_END) {
set_buffer_end(c->log, &conn->recv_array, num);
@ -2702,6 +2701,8 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue.
*
* The first byte of data must in the PACKET_ID_RANGE_LOSSLESS.
*
* congestion_control: should congestion control apply to this packet?
*/
int64_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
return -1;
}
if (data[0] < CRYPTO_RESERVED_PACKETS) {
return -1;
}
if (data[0] >= PACKET_ID_LOSSY_RANGE_START) {
if (data[0] < PACKET_ID_RANGE_LOSSLESS_START || data[0] > PACKET_ID_RANGE_LOSSLESS_END) {
return -1;
}
@ -2780,10 +2777,12 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet
return 0;
}
/* return -1 on failure.
/* Sends a lossy cryptopacket.
*
* return -1 on failure.
* return 0 on success.
*
* Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
* The first byte of data must in the PACKET_ID_RANGE_LOSSY.
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
{
@ -2791,11 +2790,7 @@ int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
return -1;
}
if (data[0] < PACKET_ID_LOSSY_RANGE_START) {
return -1;
}
if (data[0] >= PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE) {
if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
return -1;
}

View File

@ -31,6 +31,57 @@
#include <pthread.h>
/*** Crypto payloads. ***/
/** Ranges. **/
/* Packets in this range are reserved for net_crypto internal use. */
#define PACKET_ID_RANGE_RESERVED_START 0
#define PACKET_ID_RANGE_RESERVED_END 15
/* Packets in this range are reserved for Messenger use. */
#define PACKET_ID_RANGE_LOSSLESS_START 16
#define PACKET_ID_RANGE_LOSSLESS_NORMAL_START 16
#define PACKET_ID_RANGE_LOSSLESS_NORMAL_END 159
/* Packets in this range can be used for anything. */
#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_START 160
#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_END 191
#define PACKET_ID_RANGE_LOSSLESS_END 191
/* Packets in this range are reserved for AV use. */
#define PACKET_ID_RANGE_LOSSY_START 192
#define PACKET_ID_RANGE_LOSSY_AV_START 192
#define PACKET_ID_RANGE_LOSSY_AV_SIZE 8
#define PACKET_ID_RANGE_LOSSY_AV_END 199
/* Packets in this range can be used for anything. */
#define PACKET_ID_RANGE_LOSSY_CUSTOM_START 200
#define PACKET_ID_RANGE_LOSSY_CUSTOM_END 254
#define PACKET_ID_RANGE_LOSSY_END 254
/** Messages. **/
#define PACKET_ID_PADDING 0 /* Denotes padding */
#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
#define PACKET_ID_KILL 2 /* Used to kill connection */
#define PACKET_ID_ONLINE 24
#define PACKET_ID_OFFLINE 25
#define PACKET_ID_NICKNAME 48
#define PACKET_ID_STATUSMESSAGE 49
#define PACKET_ID_USERSTATUS 50
#define PACKET_ID_TYPING 51
#define PACKET_ID_MESSAGE 64
#define PACKET_ID_ACTION 65 /* PACKET_ID_MESSAGE + MESSAGE_ACTION */
#define PACKET_ID_MSI 69 /* Used by AV to setup calls and etc */
#define PACKET_ID_FILE_SENDREQUEST 80
#define PACKET_ID_FILE_CONTROL 81
#define PACKET_ID_FILE_DATA 82
#define PACKET_ID_INVITE_CONFERENCE 96
#define PACKET_ID_ONLINE_PACKET 97
#define PACKET_ID_DIRECT_CONFERENCE 98
#define PACKET_ID_MESSAGE_CONFERENCE 99
#define PACKET_ID_LOSSY_CONFERENCE 199
/*** Crypto connections. ***/
typedef enum Crypto_Conn_State {
CRYPTO_CONN_NO_CONNECTION = 0,
CRYPTO_CONN_COOKIE_REQUESTING = 1, // send cookie request packets
@ -66,20 +117,9 @@ typedef enum Crypto_Conn_State {
/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
#define UDP_DIRECT_TIMEOUT 8
#define PACKET_ID_PADDING 0 /* Denotes padding */
#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
#define PACKET_ID_KILL 2 /* Used to kill connection */
/* Packet ids 0 to CRYPTO_RESERVED_PACKETS - 1 are reserved for use by net_crypto. */
#define CRYPTO_RESERVED_PACKETS 16
#define MAX_TCP_CONNECTIONS 64
#define MAX_TCP_RELAYS_PEER 4
/* All packets starting with a byte in this range are considered lossy packets. */
#define PACKET_ID_LOSSY_RANGE_START 192
#define PACKET_ID_LOSSY_RANGE_SIZE 63
#define CRYPTO_MAX_PADDING 8 /* All packets will be padded a number of bytes based on this number. */
/* 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);
* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue.
*
* The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
* The first byte of data must be in the PACKET_ID_RANGE_LOSSLESS.
*
* congestion_control: should congestion control apply to this packet?
*/
@ -225,10 +265,12 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
*/
int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet_number);
/* return -1 on failure.
/* Sends a lossy cryptopacket.
*
* return -1 on failure.
* return 0 on success.
*
* Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
* The first byte of data must be in the PACKET_ID_RANGE_LOSSY.
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length);

View File

@ -1522,7 +1522,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
return 0;
}
if (data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) {
if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID);
return 0;
}