summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c21
-rw-r--r--toxcore/LAN_discovery.c2
-rw-r--r--toxcore/Lossless_UDP.c14
-rw-r--r--toxcore/Messenger.c38
-rw-r--r--toxcore/misc_tools.h4
-rw-r--r--toxcore/net_crypto.c17
-rw-r--r--toxcore/network.c12
-rw-r--r--toxcore/network.h10
8 files changed, 75 insertions, 43 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 47595d06..749eb78c 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -129,7 +129,7 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
129/* Check if client with client_id is already in list of length length. 129/* Check if client with client_id is already in list of length length.
130 * If it is then set its corresponding timestamp to current time. 130 * If it is then set its corresponding timestamp to current time.
131 * If the id is already in the list with a different ip_port, update it. 131 * If the id is already in the list with a different ip_port, update it.
132 * TODO: Maybe optimize this. 132 * TODO: Maybe optimize this.
133 * 133 *
134 * return True(1) or False(0) 134 * return True(1) or False(0)
135 */ 135 */
@@ -548,7 +548,10 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
548 sizeof(ping_id) + num_nodes * sizeof(Node_format), 548 sizeof(ping_id) + num_nodes * sizeof(Node_format),
549 encrypt ); 549 encrypt );
550 550
551 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) 551 if (len == -1)
552 return -1;
553
554 if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
552 return -1; 555 return -1;
553 556
554 data[0] = NET_PACKET_SEND_NODES; 557 data[0] = NET_PACKET_SEND_NODES;
@@ -614,7 +617,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
614 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 617 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
615 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain ); 618 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain );
616 619
617 if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) 620 if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
618 return 1; 621 return 1;
619 622
620 memcpy(&ping_id, plain, sizeof(ping_id)); 623 memcpy(&ping_id, plain, sizeof(ping_id));
@@ -856,7 +859,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
856 859
857 860
858/* Send the following packet to everyone who tells us they are connected to friend_id. 861/* Send the following packet to everyone who tells us they are connected to friend_id.
859 * 862 *
860 * return ip for friend. 863 * return ip for friend.
861 * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2). 864 * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2).
862 */ 865 */
@@ -884,7 +887,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
884 887
885 /* If ip is not zero and node is good. */ 888 /* If ip is not zero and node is good. */
886 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { 889 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
887 if (sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length) == length) 890 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
891
892 if ((unsigned int)retval == length)
888 ++sent; 893 ++sent;
889 } 894 }
890 } 895 }
@@ -924,7 +929,9 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
924 if (n < 1) 929 if (n < 1)
925 return 0; 930 return 0;
926 931
927 if (sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length) == length) 932 int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length);
933
934 if ((unsigned int)retval == length)
928 return 1; 935 return 1;
929 936
930 return 0; 937 return 0;
@@ -1074,7 +1081,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1074 for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { 1081 for (i = dht->friends_list[friend_num].punching_index; i != top; i++) {
1075 /* TODO: Improve port guessing algorithm. */ 1082 /* TODO: Improve port guessing algorithm. */
1076 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); 1083 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
1077 IP_Port pinging = {{ip, htons(port)}}; 1084 IP_Port pinging = {{ip, htons(port), 0}};
1078 send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); 1085 send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id);
1079 } 1086 }
1080 1087
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 4ccb893b..8998e0a9 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -145,7 +145,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
145 uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; 145 uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
146 data[0] = NET_PACKET_LAN_DISCOVERY; 146 data[0] = NET_PACKET_LAN_DISCOVERY;
147 memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); 147 memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
148 IP_Port ip_port = {{broadcast_ip(), port}}; 148 IP_Port ip_port = {{broadcast_ip(), port, 0}};
149 return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 149 return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
150} 150}
151 151
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index d501ea40..0d6d8694 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -228,7 +228,7 @@ static void free_connections(Lossless_UDP *ludp)
228 */ 228 */
229int kill_connection(Lossless_UDP *ludp, int connection_id) 229int kill_connection(Lossless_UDP *ludp, int connection_id)
230{ 230{
231 if (connection_id >= 0 && connection_id < ludp->connections.len) { 231 if ((unsigned int)connection_id < ludp->connections.len) {
232 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 232 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
233 233
234 if (connection->status > 0) { 234 if (connection->status > 0) {
@@ -251,7 +251,7 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
251 */ 251 */
252int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 252int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
253{ 253{
254 if (connection_id >= 0 && connection_id < ludp->connections.len) { 254 if ((unsigned int)connection_id < ludp->connections.len) {
255 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 255 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
256 256
257 if (connection->status > 0) { 257 if (connection->status > 0) {
@@ -274,7 +274,7 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
274 */ 274 */
275int is_connected(Lossless_UDP *ludp, int connection_id) 275int is_connected(Lossless_UDP *ludp, int connection_id)
276{ 276{
277 if (connection_id >= 0 && connection_id < ludp->connections.len) 277 if ((unsigned int)connection_id < ludp->connections.len)
278 return tox_array_get(&ludp->connections, connection_id, Connection).status; 278 return tox_array_get(&ludp->connections, connection_id, Connection).status;
279 279
280 return 0; 280 return 0;
@@ -283,7 +283,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
283/* return the ip_port of the corresponding connection. */ 283/* return the ip_port of the corresponding connection. */
284IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 284IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
285{ 285{
286 if (connection_id >= 0 && connection_id < ludp->connections.len) 286 if ((unsigned int)connection_id < ludp->connections.len)
287 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 287 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
288 288
289 IP_Port zero = {{{{0}}, 0, 0}}; 289 IP_Port zero = {{{{0}}, 0, 0}};
@@ -293,7 +293,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
293/* return the number of packets in the queue waiting to be successfully sent. */ 293/* return the number of packets in the queue waiting to be successfully sent. */
294uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) 294uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
295{ 295{
296 if (connection_id < 0 || connection_id >= ludp->connections.len) 296 if ((unsigned int)connection_id >= ludp->connections.len)
297 return 0; 297 return 0;
298 298
299 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 299 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -303,7 +303,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
303/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */ 303/* return the number of packets in the queue waiting to be successfully read with read_packet(...). */
304uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 304uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
305{ 305{
306 if (connection_id < 0 || connection_id >= ludp->connections.len) 306 if ((unsigned int)connection_id >= ludp->connections.len)
307 return 0; 307 return 0;
308 308
309 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 309 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -315,7 +315,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
315 */ 315 */
316char id_packet(Lossless_UDP *ludp, int connection_id) 316char id_packet(Lossless_UDP *ludp, int connection_id)
317{ 317{
318 if (connection_id < 0 || connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0) 318 if (recvqueue(ludp, connection_id) == 0)
319 return -1; 319 return -1;
320 320
321 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 321 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index e28ff9db..621f7130 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,11 +28,11 @@
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/* static uint8_t online; 31// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
32 * 32static uint8_t friend_not_valid(Messenger *m, int friendnumber)
33 * return 1 if we are online. 33{
34 * return 0 if we are offline. 34 return (unsigned int)friendnumber >= m->numfriends;
35 */ 35}
36 36
37/* Set the size of the friend list to numfriends. 37/* Set the size of the friend list to numfriends.
38 * 38 *
@@ -79,7 +79,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
79 */ 79 */
80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
81{ 81{
82 if (friend_id >= m->numfriends || friend_id < 0) 82 if (friend_not_valid(m, friend_id))
83 return -1; 83 return -1;
84 84
85 if (m->friendlist[friend_id].status > 0) { 85 if (m->friendlist[friend_id].status > 0) {
@@ -249,7 +249,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
249 */ 249 */
250int m_delfriend(Messenger *m, int friendnumber) 250int m_delfriend(Messenger *m, int friendnumber)
251{ 251{
252 if (friendnumber >= m->numfriends || friendnumber < 0) 252 if (friend_not_valid(m, friendnumber))
253 return -1; 253 return -1;
254 254
255 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id); 255 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
@@ -279,7 +279,7 @@ int m_delfriend(Messenger *m, int friendnumber)
279 */ 279 */
280int m_friendstatus(Messenger *m, int friendnumber) 280int m_friendstatus(Messenger *m, int friendnumber)
281{ 281{
282 if (friendnumber < 0 || friendnumber >= m->numfriends) 282 if (friend_not_valid(m, friendnumber))
283 return NOFRIEND; 283 return NOFRIEND;
284 284
285 return m->friendlist[friendnumber].status; 285 return m->friendlist[friendnumber].status;
@@ -292,7 +292,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
292 */ 292 */
293uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 293uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
294{ 294{
295 if (friendnumber < 0 || friendnumber >= m->numfriends) 295 if (friend_not_valid(m, friendnumber))
296 return 0; 296 return 0;
297 297
298 uint32_t msgid = ++m->friendlist[friendnumber].message_id; 298 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
@@ -347,7 +347,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
347 */ 347 */
348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
349{ 349{
350 if (friendnumber >= m->numfriends || friendnumber < 0) 350 if (friend_not_valid(m, friendnumber))
351 return -1; 351 return -1;
352 352
353 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 353 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
@@ -404,7 +404,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
404 */ 404 */
405int getname(Messenger *m, int friendnumber, uint8_t *name) 405int getname(Messenger *m, int friendnumber, uint8_t *name)
406{ 406{
407 if (friendnumber >= m->numfriends || friendnumber < 0) 407 if (friend_not_valid(m, friendnumber))
408 return -1; 408 return -1;
409 409
410 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); 410 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
@@ -447,7 +447,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
447 */ 447 */
448int m_get_statusmessage_size(Messenger *m, int friendnumber) 448int m_get_statusmessage_size(Messenger *m, int friendnumber)
449{ 449{
450 if (friendnumber >= m->numfriends || friendnumber < 0) 450 if (friend_not_valid(m, friendnumber))
451 return -1; 451 return -1;
452 452
453 return m->friendlist[friendnumber].statusmessage_length; 453 return m->friendlist[friendnumber].statusmessage_length;
@@ -458,7 +458,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
458 */ 458 */
459int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 459int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
460{ 460{
461 if (friendnumber >= m->numfriends || friendnumber < 0) 461 if (friend_not_valid(m, friendnumber))
462 return -1; 462 return -1;
463 463
464 memset(buf, 0, maxlen); 464 memset(buf, 0, maxlen);
@@ -475,7 +475,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
475 475
476USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) 476USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
477{ 477{
478 if (friendnumber >= m->numfriends || friendnumber < 0) 478 if (friend_not_valid(m, friendnumber))
479 return USERSTATUS_INVALID; 479 return USERSTATUS_INVALID;
480 480
481 USERSTATUS status = m->friendlist[friendnumber].userstatus; 481 USERSTATUS status = m->friendlist[friendnumber].userstatus;
@@ -511,7 +511,7 @@ static int send_ping(Messenger *m, int friendnumber)
511 511
512static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 512static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
513{ 513{
514 if (friendnumber >= m->numfriends || friendnumber < 0) 514 if (friend_not_valid(m, friendnumber))
515 return -1; 515 return -1;
516 516
517 uint8_t *newstatus = calloc(length, 1); 517 uint8_t *newstatus = calloc(length, 1);
@@ -533,7 +533,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
533 if (yesno != 0 || yesno != 1) 533 if (yesno != 0 || yesno != 1)
534 return; 534 return;
535 535
536 if (friendnumber >= m->numfriends || friendnumber < 0) 536 if (friend_not_valid(m, friendnumber))
537 return; 537 return;
538 538
539 m->friendlist[friendnumber].receives_read_receipts = yesno; 539 m->friendlist[friendnumber].receives_read_receipts = yesno;
@@ -615,7 +615,7 @@ void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
615 615
616int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 616int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
617{ 617{
618 if (friendnumber < 0 || friendnumber >= m->numfriends) 618 if (friend_not_valid(m, friendnumber))
619 return 0; 619 return 0;
620 620
621 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) 621 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
@@ -786,7 +786,7 @@ void doFriends(Messenger *m)
786 len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp); 786 len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp);
787 uint8_t packet_id = temp[0]; 787 uint8_t packet_id = temp[0];
788 uint8_t *data = temp + 1; 788 uint8_t *data = temp + 1;
789 int data_length = len - 1; 789 uint32_t data_length = len - 1;
790 790
791 if (len > 0) { 791 if (len > 0) {
792 switch (packet_id) { 792 switch (packet_id) {
@@ -968,7 +968,7 @@ void Messenger_save(Messenger *m, uint8_t *data)
968/* Load the messenger from data of size length. */ 968/* Load the messenger from data of size length. */
969int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 969int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
970{ 970{
971 if (length == ~0) 971 if (length == ~((uint32_t)0))
972 return -1; 972 return -1;
973 973
974 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) 974 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h
index af31ff54..448c1cbd 100644
--- a/toxcore/misc_tools.h
+++ b/toxcore/misc_tools.h
@@ -196,10 +196,10 @@ static inline void tox_array_pop(tox_array *arr, uint32_t num)
196 * Quick Sort: Complexity O(nlogn) 196 * Quick Sort: Complexity O(nlogn)
197 * arr - the array to sort 197 * arr - the array to sort
198 * n - the sort index (should be called with n = length(arr)) 198 * n - the sort index (should be called with n = length(arr))
199 * cmpfn - a function that compares two values of type type. 199 * cmpfn - a function that compares two values of type type.
200 * Must return -1, 0, 1 for a < b, a == b, and a > b respectively. 200 * Must return -1, 0, 1 for a < b, a == b, and a > b respectively.
201 */ 201 */
202/* Must be called in the header file. */ 202/* Must be called in the header file. */
203#define declare_quick_sort(type) \ 203#define declare_quick_sort(type) \
204void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type)); 204void type##_quick_sort(type *arr, int n, int (*cmpfn)(type, type));
205 205
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 739003a4..59a068c8 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -32,6 +32,11 @@
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_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id)
36{
37 return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
38}
39
35/* Use this instead of memcmp; not vulnerable to timing attacks. */ 40/* Use this instead of memcmp; not vulnerable to timing attacks. */
36uint8_t crypto_iszero(uint8_t *mem, uint32_t length) 41uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
37{ 42{
@@ -150,7 +155,7 @@ void random_nonce(uint8_t *nonce)
150 */ 155 */
151int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 156int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
152{ 157{
153 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 158 if (crypt_connection_id_not_valid(c, crypt_connection_id))
154 return 0; 159 return 0;
155 160
156 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) 161 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
@@ -182,7 +187,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
182 */ 187 */
183int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 188int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
184{ 189{
185 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 190 if (crypt_connection_id_not_valid(c, crypt_connection_id))
186 return 0; 191 return 0;
187 192
188 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) 193 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
@@ -305,7 +310,9 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
305 len); 310 len);
306 311
307 } else { /* If request is not for us, try routing it. */ 312 } else { /* If request is not for us, try routing it. */
308 if (route_packet(dht, packet + 1, packet, length) == length) 313 int retval = route_packet(dht, packet + 1, packet, length);
314
315 if ((unsigned int)retval == length)
309 return 0; 316 return 0;
310 } 317 }
311 } 318 }
@@ -512,7 +519,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
512 */ 519 */
513int crypto_kill(Net_Crypto *c, int crypt_connection_id) 520int crypto_kill(Net_Crypto *c, int crypt_connection_id)
514{ 521{
515 if (crypt_connection_id < 0 || crypt_connection_id >= c->crypto_connections_length) 522 if (crypt_connection_id_not_valid(c, crypt_connection_id))
516 return 1; 523 return 1;
517 524
518 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { 525 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
@@ -604,7 +611,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
604 */ 611 */
605int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 612int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
606{ 613{
607 if (crypt_connection_id >= 0 && crypt_connection_id < c->crypto_connections_length) 614 if ((unsigned int)crypt_connection_id < c->crypto_connections_length)
608 return c->crypto_connections[crypt_connection_id].status; 615 return c->crypto_connections[crypt_connection_id].status;
609 616
610 return CONN_NO_CONNECTION; 617 return CONN_NO_CONNECTION;
diff --git a/toxcore/network.c b/toxcore/network.c
index 666846db..9e329949 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -60,9 +60,13 @@ uint32_t random_int(void)
60/* Basic network functions: 60/* Basic network functions:
61 * Function to send packet(data) of length length to ip_port. 61 * Function to send packet(data) of length length to ip_port.
62 */ 62 */
63#ifdef WIN32
64int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length)
65#else
63int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) 66int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length)
67#endif
64{ 68{
65 ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; 69 ADDR addr = {AF_INET, ip_port.port, ip_port.ip, {0}};
66 return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); 70 return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
67} 71}
68 72
@@ -72,7 +76,11 @@ int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length)
72 * Packet length is put into length. 76 * Packet length is put into length.
73 * Dump all empty packets. 77 * Dump all empty packets.
74 */ 78 */
79#ifdef WIN32
80static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
81#else
75static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) 82static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
83#endif
76{ 84{
77 ADDR addr; 85 ADDR addr;
78#ifdef WIN32 86#ifdef WIN32
@@ -207,7 +215,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
207#endif 215#endif
208 216
209 /* Bind our socket to port PORT and address 0.0.0.0 */ 217 /* Bind our socket to port PORT and address 0.0.0.0 */
210 ADDR addr = {AF_INET, htons(port), ip}; 218 ADDR addr = {AF_INET, htons(port), ip, {0}};
211 bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr)); 219 bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr));
212 return temp; 220 return temp;
213} 221}
diff --git a/toxcore/network.h b/toxcore/network.h
index eb725d25..ca79a81b 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -120,7 +120,12 @@ typedef struct {
120typedef struct { 120typedef struct {
121 Packet_Handles packethandlers[256]; 121 Packet_Handles packethandlers[256];
122 /* Our UDP socket. */ 122 /* Our UDP socket. */
123#ifdef WIN32
124 unsigned int sock;
125#else
123 int sock; 126 int sock;
127#endif
128
124} Networking_Core; 129} Networking_Core;
125 130
126/* return current time in milleseconds since the epoch. */ 131/* return current time in milleseconds since the epoch. */
@@ -134,7 +139,12 @@ uint32_t random_int(void);
134/* Basic network functions: */ 139/* Basic network functions: */
135 140
136/* Function to send packet(data) of length length to ip_port. */ 141/* Function to send packet(data) of length length to ip_port. */
142#ifdef WIN32
143int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
144#else
137int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); 145int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
146#endif
147
138 148
139/* Function to call when packet beginning with byte is received. */ 149/* Function to call when packet beginning with byte is received. */
140void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); 150void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object);