summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c16
-rw-r--r--toxcore/Lossless_UDP.c14
-rw-r--r--toxcore/Messenger.c31
-rw-r--r--toxcore/net_crypto.c13
4 files changed, 44 insertions, 30 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 50bddbd6..94168a69 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -543,7 +543,10 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
543 sizeof(ping_id) + num_nodes * sizeof(Node_format), 543 sizeof(ping_id) + num_nodes * sizeof(Node_format),
544 encrypt ); 544 encrypt );
545 545
546 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) 546 if (len == -1)
547 return -1;
548
549 if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
547 return -1; 550 return -1;
548 551
549 data[0] = NET_PACKET_SEND_NODES; 552 data[0] = NET_PACKET_SEND_NODES;
@@ -609,7 +612,10 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
609 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 612 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
610 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain ); 613 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain );
611 614
612 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) 615 if (len == -1)
616 return -1;
617
618 if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
613 return 1; 619 return 1;
614 620
615 memcpy(&ping_id, plain, sizeof(ping_id)); 621 memcpy(&ping_id, plain, sizeof(ping_id));
@@ -877,7 +883,8 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
877 883
878 /* If ip is not zero and node is good */ 884 /* If ip is not zero and node is good */
879 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { 885 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
880 if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) 886 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
887 if (retval != -1 && (uint32_t)retval == length)
881 ++sent; 888 ++sent;
882 } 889 }
883 } 890 }
@@ -916,7 +923,8 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
916 if (n < 1) 923 if (n < 1)
917 return 0; 924 return 0;
918 925
919 if (sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length) == length) 926 int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length);
927 if (retval != -1 && (uint32_t)retval == length)
920 return 1; 928 return 1;
921 929
922 return 0; 930 return 0;
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 270b4b35..f7b9d4b6 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -226,7 +226,7 @@ static void free_connections(Lossless_UDP *ludp)
226 */ 226 */
227int kill_connection(Lossless_UDP *ludp, int connection_id) 227int kill_connection(Lossless_UDP *ludp, int connection_id)
228{ 228{
229 if (connection_id >= 0 && connection_id < ludp->connections.len) { 229 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) {
230 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 230 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
231 231
232 if (connection->status > 0) { 232 if (connection->status > 0) {
@@ -248,7 +248,7 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
248 */ 248 */
249int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 249int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
250{ 250{
251 if (connection_id >= 0 && connection_id < ludp->connections.len) { 251 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) {
252 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 252 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
253 253
254 if (connection->status > 0) { 254 if (connection->status > 0) {
@@ -270,7 +270,7 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
270 */ 270 */
271int is_connected(Lossless_UDP *ludp, int connection_id) 271int is_connected(Lossless_UDP *ludp, int connection_id)
272{ 272{
273 if (connection_id >= 0 && connection_id < ludp->connections.len) 273 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len)
274 return tox_array_get(&ludp->connections, connection_id, Connection).status; 274 return tox_array_get(&ludp->connections, connection_id, Connection).status;
275 275
276 return 0; 276 return 0;
@@ -279,7 +279,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
279/* return the ip_port of the corresponding connection. */ 279/* return the ip_port of the corresponding connection. */
280IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 280IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
281{ 281{
282 if (connection_id >= 0 && connection_id < ludp->connections.len) 282 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len)
283 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 283 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
284 284
285 IP_Port zero = {{{{0}}, 0, 0}}; 285 IP_Port zero = {{{{0}}, 0, 0}};
@@ -289,7 +289,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
289/* returns the number of packets in the queue waiting to be successfully sent. */ 289/* returns the number of packets in the queue waiting to be successfully sent. */
290uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) 290uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
291{ 291{
292 if (connection_id < 0 || connection_id >= ludp->connections.len) 292 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len)
293 return 0; 293 return 0;
294 294
295 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 295 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -299,7 +299,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
299/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */ 299/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
300uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 300uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
301{ 301{
302 if (connection_id < 0 || connection_id >= ludp->connections.len) 302 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len)
303 return 0; 303 return 0;
304 304
305 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 305 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -311,7 +311,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
311 */ 311 */
312char id_packet(Lossless_UDP *ludp, int connection_id) 312char id_packet(Lossless_UDP *ludp, int connection_id)
313{ 313{
314 if (connection_id < 0 || connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0) 314 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0)
315 return -1; 315 return -1;
316 316
317 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 317 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 70623611..33c37b06 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,6 +28,9 @@
28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); 28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
30 30
31// friend_is_valid determines if the friendnumber passed is valid in the Messenger object
32static uint8_t friend_is_valid(int friendnumber, Messenger *m) { return friendnumber < 0 || (uint32_t)friendnumber >= m->numfriends; }
33
31/* return 1 if we are online. 34/* return 1 if we are online.
32 * return 0 if we are offline. 35 * return 0 if we are offline.
33 * static uint8_t online; 36 * static uint8_t online;
@@ -76,7 +79,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
76 */ 79 */
77int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
78{ 81{
79 if (friend_id >= m->numfriends || friend_id < 0) 82 if (friend_is_valid(friend_id,m))
80 return -1; 83 return -1;
81 84
82 if (m->friendlist[friend_id].status > 0) { 85 if (m->friendlist[friend_id].status > 0) {
@@ -246,7 +249,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
246 */ 249 */
247int m_delfriend(Messenger *m, int friendnumber) 250int m_delfriend(Messenger *m, int friendnumber)
248{ 251{
249 if (friendnumber >= m->numfriends || friendnumber < 0) 252 if (friend_is_valid(friendnumber,m))
250 return -1; 253 return -1;
251 254
252 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id); 255 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
@@ -276,7 +279,7 @@ int m_delfriend(Messenger *m, int friendnumber)
276 */ 279 */
277int m_friendstatus(Messenger *m, int friendnumber) 280int m_friendstatus(Messenger *m, int friendnumber)
278{ 281{
279 if (friendnumber < 0 || friendnumber >= m->numfriends) 282 if (friend_is_valid(friendnumber,m))
280 return NOFRIEND; 283 return NOFRIEND;
281 284
282 return m->friendlist[friendnumber].status; 285 return m->friendlist[friendnumber].status;
@@ -288,7 +291,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
288 */ 291 */
289uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 292uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
290{ 293{
291 if (friendnumber < 0 || friendnumber >= m->numfriends) 294 if (friend_is_valid(friendnumber,m))
292 return 0; 295 return 0;
293 296
294 uint32_t msgid = ++m->friendlist[friendnumber].message_id; 297 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
@@ -341,7 +344,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
341 */ 344 */
342static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 345static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
343{ 346{
344 if (friendnumber >= m->numfriends || friendnumber < 0) 347 if (friend_is_valid(friendnumber,m))
345 return -1; 348 return -1;
346 349
347 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 350 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
@@ -395,7 +398,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
395 */ 398 */
396int getname(Messenger *m, int friendnumber, uint8_t *name) 399int getname(Messenger *m, int friendnumber, uint8_t *name)
397{ 400{
398 if (friendnumber >= m->numfriends || friendnumber < 0) 401 if (friend_is_valid(friendnumber,m))
399 return -1; 402 return -1;
400 403
401 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); 404 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
@@ -438,7 +441,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
438 */ 441 */
439int m_get_statusmessage_size(Messenger *m, int friendnumber) 442int m_get_statusmessage_size(Messenger *m, int friendnumber)
440{ 443{
441 if (friendnumber >= m->numfriends || friendnumber < 0) 444 if (friend_is_valid(friendnumber,m))
442 return -1; 445 return -1;
443 446
444 return m->friendlist[friendnumber].statusmessage_length; 447 return m->friendlist[friendnumber].statusmessage_length;
@@ -449,7 +452,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
449 */ 452 */
450int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 453int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
451{ 454{
452 if (friendnumber >= m->numfriends || friendnumber < 0) 455 if (friend_is_valid(friendnumber,m))
453 return -1; 456 return -1;
454 457
455 memset(buf, 0, maxlen); 458 memset(buf, 0, maxlen);
@@ -466,7 +469,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
466 469
467USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) 470USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
468{ 471{
469 if (friendnumber >= m->numfriends || friendnumber < 0) 472 if (friend_is_valid(friendnumber,m))
470 return USERSTATUS_INVALID; 473 return USERSTATUS_INVALID;
471 474
472 USERSTATUS status = m->friendlist[friendnumber].userstatus; 475 USERSTATUS status = m->friendlist[friendnumber].userstatus;
@@ -502,7 +505,7 @@ static int send_ping(Messenger *m, int friendnumber)
502 505
503static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 506static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
504{ 507{
505 if (friendnumber >= m->numfriends || friendnumber < 0) 508 if (friend_is_valid(friendnumber,m))
506 return -1; 509 return -1;
507 510
508 uint8_t *newstatus = calloc(length, 1); 511 uint8_t *newstatus = calloc(length, 1);
@@ -524,7 +527,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
524 if (yesno != 0 || yesno != 1) 527 if (yesno != 0 || yesno != 1)
525 return; 528 return;
526 529
527 if (friendnumber >= m->numfriends || friendnumber < 0) 530 if (friend_is_valid(friendnumber,m))
528 return; 531 return;
529 532
530 m->friendlist[friendnumber].receives_read_receipts = yesno; 533 m->friendlist[friendnumber].receives_read_receipts = yesno;
@@ -606,7 +609,7 @@ void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
606 609
607int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 610int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
608{ 611{
609 if (friendnumber < 0 || friendnumber >= m->numfriends) 612 if (friend_is_valid(friendnumber,m))
610 return 0; 613 return 0;
611 614
612 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) 615 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
@@ -853,7 +856,7 @@ void doFriends(Messenger *m)
853 case PACKET_ID_RECEIPT: { 856 case PACKET_ID_RECEIPT: {
854 uint32_t msgid; 857 uint32_t msgid;
855 858
856 if (data_length < sizeof(msgid)) 859 if (data_length < 0 || (uint32_t)data_length < sizeof(msgid))
857 break; 860 break;
858 861
859 memcpy(&msgid, data, sizeof(msgid)); 862 memcpy(&msgid, data, sizeof(msgid));
@@ -959,7 +962,7 @@ void Messenger_save(Messenger *m, uint8_t *data)
959/* Load the messenger from data of size length. */ 962/* Load the messenger from data of size length. */
960int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 963int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
961{ 964{
962 if (length == ~0) 965 if (length == ~((uint32_t)0))
963 return -1; 966 return -1;
964 967
965 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) 968 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 87c98089..f55f40a1 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -32,6 +32,8 @@
32#define CONN_ESTABLISHED 3 32#define CONN_ESTABLISHED 3
33#define CONN_TIMED_OUT 4 33#define CONN_TIMED_OUT 4
34 34
35static uint8_t crypt_id_valid(int crypt_connection_id, Net_Crypto *c) { return crypt_connection_id < 0 || (uint32_t)crypt_connection_id >= c->crypto_connections_length; }
36
35/* Use this instead of memcmp; not vulnerable to timing attacks. */ 37/* Use this instead of memcmp; not vulnerable to timing attacks. */
36uint8_t crypto_iszero(uint8_t *mem, uint32_t length) 38uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
37{ 39{
@@ -150,7 +152,7 @@ void random_nonce(uint8_t *nonce)
150 */ 152 */
151int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 153int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
152{ 154{
153 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 155 if (crypt_id_valid(crypt_connection_id,c))
154 return 0; 156 return 0;
155 157
156 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) 158 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
@@ -182,7 +184,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
182 */ 184 */
183int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 185int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
184{ 186{
185 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 187 if (crypt_id_valid(crypt_connection_id,c))
186 return 0; 188 return 0;
187 189
188 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) 190 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
@@ -304,7 +306,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
304 len); 306 len);
305 307
306 } else { /* If request is not for us, try routing it. */ 308 } else { /* If request is not for us, try routing it. */
307 if (route_packet(dht, packet + 1, packet, length) == length) 309 int retval = route_packet(dht, packet + 1, packet, length);
310 if (retval < 0 || (uint32_t)retval == length)
308 return 0; 311 return 0;
309 } 312 }
310 } 313 }
@@ -505,7 +508,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
505 */ 508 */
506int crypto_kill(Net_Crypto *c, int crypt_connection_id) 509int crypto_kill(Net_Crypto *c, int crypt_connection_id)
507{ 510{
508 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 511 if (crypt_id_valid(crypt_connection_id,c))
509 return 1; 512 return 1;
510 513
511 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { 514 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
@@ -596,7 +599,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
596 */ 599 */
597int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 600int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
598{ 601{
599 if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) 602 if (crypt_connection_id >= 0 && (uint32_t)crypt_connection_id < c->crypto_connections_length)
600 return c->crypto_connections[crypt_connection_id].status; 603 return c->crypto_connections[crypt_connection_id].status;
601 604
602 return CONN_NO_CONNECTION; 605 return CONN_NO_CONNECTION;