From e5791ed9ef59ee5786016b39686b3d9aacf940c0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 26 Feb 2015 13:13:26 -0500 Subject: [PATCH] Added different error codes for custom packet functions in Messenger. --- toxav/rtp.c | 7 ++++--- toxcore/Messenger.c | 33 +++++++++++++++++++++++---------- toxcore/Messenger.h | 12 ++++++++++-- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/toxav/rtp.c b/toxav/rtp.c index beef1919..ccac7564 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -428,13 +428,14 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat if ( !msg ) return -1; - if ( -1 == send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length) ) { - LOGGER_WARNING("Failed to send full packet (len: %d)! std error: %s", length, strerror(errno)); + int ret = send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length); + + if ( 0 != ret) { + LOGGER_WARNING("Failed to send full packet (len: %d)! error: %i", length, ret); rtp_free_msg ( session, msg ); return rtp_ErrorSending; } - /* Set sequ number */ session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1; rtp_free_msg ( session, msg ); 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 (m->friendlist[friendnumber].status != FRIEND_ONLINE) - return -1; + if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) + return -2; - return send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id), data, length); + 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 -4; + + 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);