From c6d06ae6ee99749b90c1f9498589cd0175ab81f4 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Fri, 9 Aug 2013 16:28:12 -0400 Subject: Moved some checks around --- core/Messenger.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'core/Messenger.c') diff --git a/core/Messenger.c b/core/Messenger.c index 5589e752..1bd0d541 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -56,7 +56,7 @@ static uint32_t numfriends; static void set_friend_status(int friendnumber, uint8_t status); -static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length); +static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); /* 1 if we are online 0 if we are offline @@ -235,16 +235,11 @@ uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) { - if (friendnumber < 0 || friendnumber >= numfriends) - return 0; - if (length >= (MAX_DATA_SIZE - sizeof(theid)) || friendlist[friendnumber].status != FRIEND_ONLINE) - /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ - return 0; uint8_t temp[MAX_DATA_SIZE]; theid = htonl(theid); memcpy(temp, &theid, sizeof(theid)); memcpy(temp + sizeof(theid), message, length); - return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); + return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); } /* send an action to an online friend @@ -252,11 +247,7 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message return 0 if it was not */ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) { - if (friendnumber < 0 || friendnumber >= numfriends) - return 0; - if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) - return 0; - return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_ACTION, action, length); + return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length); } /* send a name packet to friendnumber @@ -265,7 +256,7 @@ static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) { if(length > MAX_NAME_LENGTH || length == 0) return 0; - return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_NICKNAME, name, length); + return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length); } /* set the name of a friend @@ -393,12 +384,12 @@ USERSTATUS m_get_self_userstatus(void) static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) { - return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_STATUSMESSAGE, status, length); + return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length); } static int send_userstatus(int friendnumber, USERSTATUS status) { - return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_USERSTATUS, (uint8_t*)&status, sizeof(USERSTATUS)); + return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, (uint8_t*)&status, sizeof(USERSTATUS)); } static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) @@ -501,12 +492,16 @@ static void set_friend_status(int friendnumber, uint8_t status) friendlist[friendnumber].status = status; } -static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length) +static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) { + if (friendnumber < 0 || friendnumber >= numfriends) + return 0; + if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) + return 0; uint8_t packet[length + 1]; packet[0] = packet_id; memcpy(packet + 1, data, length); - return write_cryptpacket(crypt_connection_id, packet, length + 1); + return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1); } #define PORT 33445 @@ -611,7 +606,7 @@ static void doFriends(void) } case PACKET_ID_MESSAGE: { if (friendlist[i].receives_read_receipts) { - write_cryptpacket_id(friendlist[i].crypt_connection_id, PACKET_ID_RECEIPT, temp + 1, 4); + write_cryptpacket_id(i, PACKET_ID_RECEIPT, temp + 1, 4); } if (friend_message_isset) (*friend_message)(i, temp + 5, len - 5); -- cgit v1.2.3