diff options
author | Maxim Biro <nurupo.contributions@gmail.com> | 2013-08-09 16:28:12 -0400 |
---|---|---|
committer | Maxim Biro <nurupo.contributions@gmail.com> | 2013-08-09 16:28:12 -0400 |
commit | c6d06ae6ee99749b90c1f9498589cd0175ab81f4 (patch) | |
tree | ba461390047a2fe0a60102081ce5838ee290f899 | |
parent | 19bbed475dd044163f7bbeaf533abaa850380633 (diff) |
Moved some checks around
-rw-r--r-- | core/Messenger.c | 31 |
1 files changed, 13 insertions, 18 deletions
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; | |||
56 | 56 | ||
57 | 57 | ||
58 | static void set_friend_status(int friendnumber, uint8_t status); | 58 | static void set_friend_status(int friendnumber, uint8_t status); |
59 | static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length); | 59 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); |
60 | 60 | ||
61 | /* 1 if we are online | 61 | /* 1 if we are online |
62 | 0 if we are offline | 62 | 0 if we are offline |
@@ -235,16 +235,11 @@ uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | |||
235 | 235 | ||
236 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | 236 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) |
237 | { | 237 | { |
238 | if (friendnumber < 0 || friendnumber >= numfriends) | ||
239 | return 0; | ||
240 | if (length >= (MAX_DATA_SIZE - sizeof(theid)) || friendlist[friendnumber].status != FRIEND_ONLINE) | ||
241 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | ||
242 | return 0; | ||
243 | uint8_t temp[MAX_DATA_SIZE]; | 238 | uint8_t temp[MAX_DATA_SIZE]; |
244 | theid = htonl(theid); | 239 | theid = htonl(theid); |
245 | memcpy(temp, &theid, sizeof(theid)); | 240 | memcpy(temp, &theid, sizeof(theid)); |
246 | memcpy(temp + sizeof(theid), message, length); | 241 | memcpy(temp + sizeof(theid), message, length); |
247 | return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); | 242 | return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); |
248 | } | 243 | } |
249 | 244 | ||
250 | /* send an action to an online friend | 245 | /* send an action to an online friend |
@@ -252,11 +247,7 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message | |||
252 | return 0 if it was not */ | 247 | return 0 if it was not */ |
253 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) | 248 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) |
254 | { | 249 | { |
255 | if (friendnumber < 0 || friendnumber >= numfriends) | 250 | return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length); |
256 | return 0; | ||
257 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) | ||
258 | return 0; | ||
259 | return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_ACTION, action, length); | ||
260 | } | 251 | } |
261 | 252 | ||
262 | /* send a name packet to friendnumber | 253 | /* send a name packet to friendnumber |
@@ -265,7 +256,7 @@ static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) | |||
265 | { | 256 | { |
266 | if(length > MAX_NAME_LENGTH || length == 0) | 257 | if(length > MAX_NAME_LENGTH || length == 0) |
267 | return 0; | 258 | return 0; |
268 | return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_NICKNAME, name, length); | 259 | return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length); |
269 | } | 260 | } |
270 | 261 | ||
271 | /* set the name of a friend | 262 | /* set the name of a friend |
@@ -393,12 +384,12 @@ USERSTATUS m_get_self_userstatus(void) | |||
393 | 384 | ||
394 | static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 385 | static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) |
395 | { | 386 | { |
396 | return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_STATUSMESSAGE, status, length); | 387 | return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length); |
397 | } | 388 | } |
398 | 389 | ||
399 | static int send_userstatus(int friendnumber, USERSTATUS status) | 390 | static int send_userstatus(int friendnumber, USERSTATUS status) |
400 | { | 391 | { |
401 | return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_USERSTATUS, (uint8_t*)&status, sizeof(USERSTATUS)); | 392 | return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, (uint8_t*)&status, sizeof(USERSTATUS)); |
402 | } | 393 | } |
403 | 394 | ||
404 | static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 395 | 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) | |||
501 | friendlist[friendnumber].status = status; | 492 | friendlist[friendnumber].status = status; |
502 | } | 493 | } |
503 | 494 | ||
504 | static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length) | 495 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) |
505 | { | 496 | { |
497 | if (friendnumber < 0 || friendnumber >= numfriends) | ||
498 | return 0; | ||
499 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) | ||
500 | return 0; | ||
506 | uint8_t packet[length + 1]; | 501 | uint8_t packet[length + 1]; |
507 | packet[0] = packet_id; | 502 | packet[0] = packet_id; |
508 | memcpy(packet + 1, data, length); | 503 | memcpy(packet + 1, data, length); |
509 | return write_cryptpacket(crypt_connection_id, packet, length + 1); | 504 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1); |
510 | } | 505 | } |
511 | 506 | ||
512 | #define PORT 33445 | 507 | #define PORT 33445 |
@@ -611,7 +606,7 @@ static void doFriends(void) | |||
611 | } | 606 | } |
612 | case PACKET_ID_MESSAGE: { | 607 | case PACKET_ID_MESSAGE: { |
613 | if (friendlist[i].receives_read_receipts) { | 608 | if (friendlist[i].receives_read_receipts) { |
614 | write_cryptpacket_id(friendlist[i].crypt_connection_id, PACKET_ID_RECEIPT, temp + 1, 4); | 609 | write_cryptpacket_id(i, PACKET_ID_RECEIPT, temp + 1, 4); |
615 | } | 610 | } |
616 | if (friend_message_isset) | 611 | if (friend_message_isset) |
617 | (*friend_message)(i, temp + 5, len - 5); | 612 | (*friend_message)(i, temp + 5, len - 5); |