From e5791ed9ef59ee5786016b39686b3d9aacf940c0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 26 Feb 2015 13:13:26 -0500 Subject: Added different error codes for custom packet functions in Messenger. --- toxcore/Messenger.c | 31 ++++++++++++++++++++++--------- toxcore/Messenger.h | 12 ++++++++++-- 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'toxcore') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 33d087de..aa7b05bc 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1379,11 +1379,24 @@ int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uin if (friend_not_valid(m, friendnumber)) return -1; + if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) + 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)) + return -3; + if (m->friendlist[friendnumber].status != FRIEND_ONLINE) - return -1; + return -4; - return send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id), data, length); + if (send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, + m->friendlist[friendnumber].friendcon_id), data, length) == -1) { + return -5; + } else { + return 0; + } } static int handle_custom_lossless_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length) @@ -1417,21 +1430,21 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const if (friend_not_valid(m, friendnumber)) return -1; - if (length == 0) - return -1; + if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) + return -2; if (data[0] < PACKET_ID_LOSSLESS_RANGE_START) - return -1; + return -3; if (data[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE)) - return -1; + return -3; if (m->friendlist[friendnumber].status != FRIEND_ONLINE) - return -1; + return -4; if (write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), data, length, 1) == -1) { - return -1; + return -5; } else { return 0; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 5d5e5879..f4046f2e 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -650,7 +650,11 @@ void custom_lossy_packet_registerhandler(Messenger *m, void (*packet_handler_cal /* High level function to send custom lossy packets. * - * return -1 on failure. + * return -1 if friend invalid. + * return -2 if length wrong. + * return -3 if first byte invalid. + * return -4 if friend offline. + * return -5 if packet failed to send because of other error. * return 0 on success. */ int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length); @@ -664,7 +668,11 @@ void custom_lossless_packet_registerhandler(Messenger *m, void (*packet_handler_ /* High level function to send custom lossless packets. * - * return -1 on failure. + * return -1 if friend invalid. + * return -2 if length wrong. + * return -3 if first byte invalid. + * return -4 if friend offline. + * return -5 if packet failed to send because of other error. * return 0 on success. */ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length); -- cgit v1.2.3