diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3d9b671d..653a2ee1 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1658,14 +1658,13 @@ static int handle_custom_lossy_packet(void *object, int friend_num, uint8_t *pac if (friend_not_valid(m, friend_num)) return 1; - if (m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function) - return m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function( - m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length); + if (m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function) + return m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function( + m->friendlist[friend_num].lossy_packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length); return 1; } - int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object) { @@ -1678,8 +1677,8 @@ int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) return -1; - m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback; - m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object; + m->friendlist[friendnumber].lossy_packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback; + m->friendlist[friendnumber].lossy_packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object; return 0; } @@ -1697,6 +1696,57 @@ int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data, return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length); } +static int handle_custom_lossless_packet(void *object, int friend_num, uint8_t *packet, uint16_t length) +{ + Messenger *m = object; + + if (friend_not_valid(m, friend_num)) + 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)) + return -1; + + if (m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function) + return m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].function( + m->friendlist[friend_num].lossless_packethandlers[packet[0] % PACKET_ID_LOSSLESS_RANGE_SIZE].object, packet, length); + + return 1; +} + +int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, + int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object) +{ + if (friend_not_valid(m, friendnumber)) + return -1; + + if (byte < PACKET_ID_LOSSLESS_RANGE_START) + return -1; + + if (byte >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) + return -1; + + m->friendlist[friendnumber].lossless_packethandlers[byte % PACKET_ID_LOSSLESS_RANGE_SIZE].function = + packet_handler_callback; + m->friendlist[friendnumber].lossless_packethandlers[byte % PACKET_ID_LOSSLESS_RANGE_SIZE].object = object; + return 0; +} + +int send_custom_lossless_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length) +{ + if (friend_not_valid(m, friendnumber)) + return -1; + + if (m->friendlist[friendnumber].status != FRIEND_ONLINE) + return -1; + + if (m->friendlist[friendnumber].crypt_connection_id == -1) + return -1; + + return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length) != -1; +} /* Function to filter out some friend requests*/ static int friend_already_added(const uint8_t *client_id, void *data) @@ -2118,6 +2168,8 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) if (m->msi_packet) (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata); + + break; } case PACKET_ID_SHARE_RELAYS: { @@ -2132,9 +2184,11 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) for (i = 0; i < n; i++) { add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id); } + break; } default: { + handle_custom_lossless_packet(object, i, temp, len); break; } } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 6d522936..e6ea59c1 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -63,6 +63,10 @@ /* Max number of tcp relays sent to friends */ #define MAX_SHARED_RELAYS 16 +/* 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 + /* Status definitions. */ enum { NOFRIEND, @@ -170,7 +174,12 @@ typedef struct { struct { int (*function)(void *object, uint8_t *data, uint32_t len); void *object; - } packethandlers[PACKET_ID_LOSSY_RANGE_SIZE]; + } lossy_packethandlers[PACKET_ID_LOSSY_RANGE_SIZE]; + + struct { + int (*function)(void *object, uint8_t *data, uint32_t len); + void *object; + } lossless_packethandlers[PACKET_ID_LOSSLESS_RANGE_SIZE]; } Friend; @@ -704,6 +713,24 @@ int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint */ int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length); + +/* Set handlers for custom lossless packets. + * + * byte must be in PACKET_ID_LOSSLESS_RANGE_START PACKET_ID_LOSSLESS_RANGE_SIZE range. + * + * return -1 on failure. + * return 0 on success. + */ +int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, + int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object); + +/* High level function to send custom lossless packets. + * + * return -1 on failure. + * return 0 on success. + */ +int send_custom_lossless_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length); + /**********************************************/ /* Run this at startup. * return allocated instance of Messenger on success.