From bdb00322e3ecb9eb213ce01688089cdad39304f0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 5 Jul 2014 12:46:58 -0400 Subject: Fixed bug when loading friends list already containing an added friend. --- toxcore/Messenger.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'toxcore') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 4344fdcb..2adbd870 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2598,6 +2598,10 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) if (temp.status >= 3) { int fnum = m_addfriend_norequest(m, temp.client_id); + + if (fnum < 0) + continue; + setfriendname(m, fnum, temp.name, ntohs(temp.name_length)); set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length)); set_friend_userstatus(m, fnum, temp.userstatus); -- cgit v1.2.3 From d8406db1133a66d9183c2eade9e4fe0f9f22dd73 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 5 Jul 2014 19:47:13 -0400 Subject: delete groupchat when join fails --- toxcore/Messenger.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'toxcore') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 2adbd870..b65f09ae 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1206,8 +1206,10 @@ int join_groupchat(Messenger *m, int32_t friendnumber, const uint8_t *friend_gro IP_Port friend_ip = get_friend_ipport(m, friendnumber); - if (friend_ip.ip.family == 0) + if (friend_ip.ip.family == 0) { + del_groupchat(m, groupnum); return -1; + } id_copy(data, friend_group_public_key); id_copy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key); @@ -1218,6 +1220,7 @@ int join_groupchat(Messenger *m, int32_t friendnumber, const uint8_t *friend_gro return groupnum; } + del_groupchat(m, groupnum); return -1; } -- cgit v1.2.3 From a13bc5e70c097c1c0a02b25c86a603d9b400f3ec Mon Sep 17 00:00:00 2001 From: kyle condon Date: Sun, 6 Jul 2014 09:55:46 +0100 Subject: Update ping_array.c Change calloc call on line 138 from empty_array->entries = calloc(size * sizeof(Ping_Array_Entry), 1);, to empty_array->entries = calloc(size, sizeof(Ping_Array_Entry)); --- toxcore/ping_array.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'toxcore') diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index 5c92527e..93dade05 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -135,7 +135,7 @@ int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout) if (size == 0 || timeout == 0 || empty_array == NULL) return -1; - empty_array->entries = calloc(size * sizeof(Ping_Array_Entry), 1); + empty_array->entries = calloc(size, sizeof(Ping_Array_Entry)); if (empty_array->entries == NULL) return -1; -- cgit v1.2.3 From f9f3a810c074e4c0a36faefa07ad8001b6a23901 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 6 Jul 2014 13:29:25 -0400 Subject: Typo fix. --- toxcore/tox.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'toxcore') diff --git a/toxcore/tox.h b/toxcore/tox.h index 5418b03e..d931b9be 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -90,7 +90,7 @@ void tox_get_address(const Tox *tox, uint8_t *address); * data is the data and length is the length. * * return the friend number if success. - * return TOX_FA_TOOLONG if message length is too long. + * return TOX_FAERR_TOOLONG if message length is too long. * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte). * return TOX_FAERR_OWNKEY if user's own key. * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend. -- cgit v1.2.3 From b4f43b4222eb033ee9af940641a00535f6091e95 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 7 Jul 2014 19:49:01 -0400 Subject: Fixed overflow bug. --- toxcore/group_chats.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'toxcore') diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 1f76878b..53ef69d3 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -471,16 +471,6 @@ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, const int ok = add_closepeer(chat, chat->group[peernum].client_id, source); - if (chat->assoc) { - ippts_send.ip_port = chat->group[peernum].ping_via; - ippts_send.timestamp = chat->group[peernum].last_pinged; - - IP_Port ipp_recv; - ipp_recv = source; - - Assoc_add_entry(chat->assoc, contents.nodes[i].client_id, &ippts_send, &ipp_recv, ok == 0 ? 1 : 0); - } - return 0; } -- cgit v1.2.3 From 2772bffeebcda9c1947c9855fc4ad42ddb32cbea Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 8 Jul 2014 10:38:48 -0400 Subject: Fixed possible out of bounds read. --- toxcore/onion_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'toxcore') diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 7d0efc79..42982ea8 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -831,7 +831,7 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num) uint32_t i; for (i = onion_c->num_friends; i != 0; --i) { - if (onion_c->friends_list[i].status != 0) + if (onion_c->friends_list[i - 1].status != 0) break; } -- cgit v1.2.3 From 422ff3b77b3023c930161b992866ec54c419dbcc Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 9 Jul 2014 20:11:01 -0400 Subject: TCP should be a bit more solid. When a TCP ping request is recieved, try to send the response until success instead of just dropping it if sending the response fails on the first try. --- toxcore/TCP_client.c | 24 ++++++++++++++++++++---- toxcore/TCP_client.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 087188f7..82720ae8 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -197,6 +197,8 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v con->status_callback_object = object; } +static int send_ping_response(TCP_Client_Connection *con); + /* return 1 on success. * return 0 if could not send packet. * return -1 on failure. @@ -209,6 +211,9 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u if (con->connections[con_id].status != 2) return -1; + if (send_ping_response(con) == 0) + return 0; + uint8_t packet[1 + length]; packet[0] = con_id + NUM_RESERVED_PORTS; memcpy(packet + 1, data, length); @@ -293,12 +298,21 @@ static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id) * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id) +static int send_ping_response(TCP_Client_Connection *con) { + if (!con->ping_response_id) + return 1; + uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PONG; - memcpy(packet + 1, &ping_id, sizeof(uint64_t)); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); + int ret; + + if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { + con->ping_response_id = 0; + } + + return ret; } /* return 1 on success. @@ -468,7 +482,8 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u uint64_t ping_id; memcpy(&ping_id, data + 1, sizeof(uint64_t)); - send_ping_response(conn, ping_id); + conn->ping_response_id = ping_id; + send_ping_response(conn); return 0; } @@ -523,6 +538,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u static int do_confirmed_TCP(TCP_Client_Connection *conn) { send_pending_data(conn); + send_ping_response(conn); uint8_t packet[MAX_PACKET_SIZE]; int len; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 2622b4f7..e6d232ed 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -57,6 +57,7 @@ typedef struct { uint64_t last_pinged; uint64_t ping_id; + uint64_t ping_response_id; void *net_crypto_pointer; uint32_t net_crypto_location; struct { -- cgit v1.2.3 From e2d388b13799ddbe5ba2a81b442d6481cec5b611 Mon Sep 17 00:00:00 2001 From: notsecure Date: Thu, 10 Jul 2014 10:34:38 -0400 Subject: fix send rate going up when peer disconnects --- toxcore/net_crypto.c | 28 ++++++++++++++++++++++++++-- toxcore/net_crypto.h | 4 ++-- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index e0319f34..bc22a23e 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2250,6 +2250,24 @@ static void send_crypto_packets(Net_Crypto *c) notes: needs improvement but seems to work fine for packet loss <1% */ + /* additional step: adjust the send rate based on the size change of the send queue */ + uint32_t queue_size = num_packets_array(&conn->send_array); + if(queue_size > conn->last_queue_size + 100) { + double v1 = (double)(conn->packets_sent) / (double)(queue_size); + double v2 = (double)(conn->last_packets_sent) / (double)(conn->last_queue_size == 0 ? 1 : conn->last_queue_size); + if(v1 < v2) { + conn->packets_resent = conn->packets_sent; + conn->rate_increase = 0; + } + + conn->last_queue_size = queue_size; + conn->last_packets_sent = conn->packets_sent; + } else if(queue_size < conn->last_queue_size) { + conn->last_queue_size = queue_size; + conn->last_packets_sent = conn->packets_sent; + } + + //hack to prevent 1 packet lost from affecting calculations at low send rates if (conn->packets_resent == 1) { conn->packets_resent = 0; @@ -2300,18 +2318,22 @@ static void send_crypto_packets(Net_Crypto *c) double linear_increase = realrate * 0.0025 + 1.0; //final send rate: average of "real" and previous send rates + increases - conn->packet_send_rate = (realrate + conn->packet_send_rate) / 2.0 + conn->rate_increase + linear_increase; + double newrate = (realrate + conn->packet_send_rate) / 2.0 + conn->rate_increase + linear_increase; + conn->last_send_rate = conn->packet_send_rate; + conn->packet_send_rate = newrate; conn->dropped = dropped; conn->drop_ignore = drop_ignore_new; conn->packets_resent = 0; - if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending) { + if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending || !conn->packets_sent) { conn->rate_increase = 0; conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; } + conn->packets_sent = 0; + if (conn->sending != 0 && num_packets_array(&conn->send_array) < CRYPTO_MIN_QUEUE_LENGTH / 2) { --conn->sending; } @@ -2342,6 +2364,7 @@ static void send_crypto_packets(Net_Crypto *c) conn->packets_resent += ret; conn->packets_left -= ret; + conn->packets_sent += ret; } if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) { @@ -2421,6 +2444,7 @@ int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const ui return -1; --conn->packets_left; + conn->packets_sent++; conn->sending = CONN_SENDING_VALUE; return ret; } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 4fed0000..2bc9a716 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -143,9 +143,9 @@ typedef struct { uint32_t packets_left; uint64_t last_packets_left_set; - double dropped, drop_ignore, rate_increase; + double dropped, drop_ignore, rate_increase, last_send_rate; uint64_t drop_ignore_start, rate_increase_stop_start; - uint32_t packets_resent; + uint32_t packets_resent, last_queue_size, packets_sent, last_packets_sent; uint8_t sending; /* indicates if data is being sent or not. */ -- cgit v1.2.3 From 1d6c3934736c3694a7c9f694d818252e4159cde3 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 10 Jul 2014 18:15:55 -0400 Subject: Made TCP a bit more solid. TCP ping requests in TCP_client should have a higher success rate. --- toxcore/TCP_client.c | 33 +++++++++++++++++++-------------- toxcore/TCP_client.h | 2 ++ 2 files changed, 21 insertions(+), 14 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 82720ae8..6ead44a5 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -198,6 +198,7 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v } static int send_ping_response(TCP_Client_Connection *con); +static int send_ping_request(TCP_Client_Connection *con); /* return 1 on success. * return 0 if could not send packet. @@ -211,7 +212,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u if (con->connections[con_id].status != 2) return -1; - if (send_ping_response(con) == 0) + if (send_ping_response(con) == 0 || send_ping_request(con) == 0) return 0; uint8_t packet[1 + length]; @@ -286,12 +287,21 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id) * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id) +static int send_ping_request(TCP_Client_Connection *con) { + if (!con->ping_request_id) + return 1; + uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PING; - memcpy(packet + 1, &ping_id, sizeof(uint64_t)); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); + int ret; + + if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { + con->ping_request_id = 0; + } + + return ret; } /* return 1 on success. @@ -539,6 +549,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn) { send_pending_data(conn); send_ping_response(conn); + send_ping_request(conn); + uint8_t packet[MAX_PACKET_SIZE]; int len; @@ -548,16 +560,9 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn) if (!ping_id) ++ping_id; - int ret = send_ping_request(conn, ping_id); - - if (ret == 1) { - conn->last_pinged = unix_time(); - conn->ping_id = ping_id; - } else { - if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) { - conn->status = TCP_CLIENT_DISCONNECTED; - } - } + conn->ping_request_id = conn->ping_id = ping_id; + send_ping_request(conn); + conn->last_pinged = unix_time(); } if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index e6d232ed..f57f79e6 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -58,6 +58,8 @@ typedef struct { uint64_t ping_id; uint64_t ping_response_id; + uint64_t ping_request_id; + void *net_crypto_pointer; uint32_t net_crypto_location; struct { -- cgit v1.2.3 From a5d2775c2570121c34a13d248662adb5c4770f9b Mon Sep 17 00:00:00 2001 From: notsecure Date: Fri, 11 Jul 2014 10:23:40 -0400 Subject: . --- toxcore/net_crypto.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index bc22a23e..7c0dd77b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2252,19 +2252,9 @@ static void send_crypto_packets(Net_Crypto *c) /* additional step: adjust the send rate based on the size change of the send queue */ uint32_t queue_size = num_packets_array(&conn->send_array); - if(queue_size > conn->last_queue_size + 100) { - double v1 = (double)(conn->packets_sent) / (double)(queue_size); - double v2 = (double)(conn->last_packets_sent) / (double)(conn->last_queue_size == 0 ? 1 : conn->last_queue_size); - if(v1 < v2) { - conn->packets_resent = conn->packets_sent; - conn->rate_increase = 0; - } - - conn->last_queue_size = queue_size; - conn->last_packets_sent = conn->packets_sent; - } else if(queue_size < conn->last_queue_size) { - conn->last_queue_size = queue_size; - conn->last_packets_sent = conn->packets_sent; + if(queue_size > conn->packet_send_rate && queue_size > conn->last_queue_size) { + conn->rate_increase = 0; + conn->packets_resent = conn->packets_sent; } @@ -2326,6 +2316,7 @@ static void send_crypto_packets(Net_Crypto *c) conn->dropped = dropped; conn->drop_ignore = drop_ignore_new; conn->packets_resent = 0; + conn->last_queue_size = queue_size; if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending || !conn->packets_sent) { conn->rate_increase = 0; -- cgit v1.2.3 From 21e797ac90a06232aef03e47faecc048910ba1e3 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 11 Jul 2014 13:30:10 -0400 Subject: Fixed some small TCP issues. --- toxcore/TCP_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 6ead44a5..bc1b7ff4 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -451,7 +451,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u uint8_t con_id = data[1] - NUM_RESERVED_PORTS; if (conn->connections[con_id].status != 1) - return -1; + return 0; conn->connections[con_id].status = 2; @@ -475,7 +475,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u return 0; if (conn->connections[con_id].status != 2) - return -1; + return 0; conn->connections[con_id].status = 1; -- cgit v1.2.3 From 4fa115e6d09057219e317dd49bb7b8a3bd2b45f7 Mon Sep 17 00:00:00 2001 From: Jin^eLD Date: Sun, 13 Jul 2014 20:01:06 +0300 Subject: Fixed documentation comment errors --- toxcore/tox.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'toxcore') diff --git a/toxcore/tox.h b/toxcore/tox.h index d931b9be..b44648fc 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -294,13 +294,13 @@ void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint void *), void *userdata); /* Set the function that will be executed when a message from a friend is received. - * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * message, uint32_t length, void *userdata) + * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * message, uint16_t length, void *userdata) */ void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when an action from a friend is received. - * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint32_t length, void *userdata) + * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint16_t length, void *userdata) */ void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *), void *userdata); -- cgit v1.2.3 From 0244bd596deaba90bcf9282e1709ea5f25b328ca Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 14 Jul 2014 12:21:13 -0400 Subject: Attempted fix of file transfer issue. Instead of dropping the rate to minimum just half it until it drops to the minimum. --- toxcore/net_crypto.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 7c0dd77b..bb1bbe05 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2318,8 +2318,12 @@ static void send_crypto_packets(Net_Crypto *c) conn->packets_resent = 0; conn->last_queue_size = queue_size; - if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending || !conn->packets_sent) { + if (!conn->sending || !conn->packets_sent) { conn->rate_increase = 0; + conn->packet_send_rate /= 2; + } + + if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) { conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; } -- cgit v1.2.3 From 4da8664a1521f2feeafbe8c036380c670cbc6d32 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 14 Jul 2014 16:23:19 -0400 Subject: Packets can now be sent from the connection status callback. --- toxcore/net_crypto.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index bb1bbe05..10921abb 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1149,11 +1149,11 @@ static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_i } if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { - if (conn->connection_status_callback) - conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); - clear_temp_packet(c, crypt_connection_id); conn->status = CRYPTO_CONN_ESTABLISHED; + + if (conn->connection_status_callback) + conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); } return 0; @@ -2252,7 +2252,8 @@ static void send_crypto_packets(Net_Crypto *c) /* additional step: adjust the send rate based on the size change of the send queue */ uint32_t queue_size = num_packets_array(&conn->send_array); - if(queue_size > conn->packet_send_rate && queue_size > conn->last_queue_size) { + + if (queue_size > conn->packet_send_rate && queue_size > conn->last_queue_size) { conn->rate_increase = 0; conn->packets_resent = conn->packets_sent; } -- cgit v1.2.3 From 2ea634ece28b11ef204b3669a42dde30c5e24ffe Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 14 Jul 2014 16:37:43 -0400 Subject: Set packets_left to default non zero values when creating connection. --- toxcore/net_crypto.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 10921abb..3751d0e2 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1485,6 +1485,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ set_connection_dht_public_key(c, crypt_connection_id, n_c->dht_public_key, current_time_monotonic()); conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; + conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; crypto_connection_add_source(c, crypt_connection_id, n_c->source); return crypt_connection_id; } @@ -1517,6 +1518,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key) crypto_box_keypair(conn->sessionpublic_key, conn->sessionsecret_key); conn->status = CRYPTO_CONN_COOKIE_REQUESTING; conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; + conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; return crypt_connection_id; } -- cgit v1.2.3 From 32ed095d7271a311a89d1430610e3adb4568ce2c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 14 Jul 2014 18:20:17 -0400 Subject: Change connection status before handling data packets. --- toxcore/net_crypto.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 3751d0e2..c44d09c4 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1106,6 +1106,14 @@ static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_i return -1; } + if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { + clear_temp_packet(c, crypt_connection_id); + conn->status = CRYPTO_CONN_ESTABLISHED; + + if (conn->connection_status_callback) + conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); + } + if (real_data[0] == PACKET_ID_REQUEST) { int requested = handle_request_packet(&conn->send_array, real_data, real_length); @@ -1148,14 +1156,6 @@ static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_i return -1; } - if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { - clear_temp_packet(c, crypt_connection_id); - conn->status = CRYPTO_CONN_ESTABLISHED; - - if (conn->connection_status_callback) - conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); - } - return 0; } -- cgit v1.2.3 From 59821d6a5ef5b298a66feaaca337e71e9cecab61 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 14 Jul 2014 18:36:26 -0400 Subject: Fixed file sending documentation. --- toxcore/tox.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'toxcore') diff --git a/toxcore/tox.h b/toxcore/tox.h index b44648fc..14426f31 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -495,20 +495,20 @@ uint32_t tox_get_chatlist(const Tox *tox, int *out_list, uint32_t list_size); * tox_file_data_remaining(...) can be used to know how many bytes are left to send/receive. * * If the connection breaks during file sending (The other person goes offline without pausing the sending and then comes back) - * the receiver must send a control packet with receive_send == 0 message_id = TOX_FILECONTROL_RESUME_BROKEN and the data being + * the receiver must send a control packet with send_receive == 1 message_id = TOX_FILECONTROL_RESUME_BROKEN and the data being * a uint64_t (in host byte order) containing the number of bytes received. * - * If the sender receives this packet, he must send a control packet with receive_send == 1 and control_type == TOX_FILECONTROL_ACCEPT + * If the sender receives this packet, he must send a control packet with send_receive == 0 and control_type == TOX_FILECONTROL_ACCEPT * then he must start sending file data from the position (data , uint64_t in host byte order) received in the TOX_FILECONTROL_RESUME_BROKEN packet. * * To pause a file transfer send a control packet with control_type == TOX_FILECONTROL_PAUSE. * To unpause a file transfer send a control packet with control_type == TOX_FILECONTROL_ACCEPT. * * If you receive a control packet with receive_send == 1 and control_type == TOX_FILECONTROL_PAUSE, you must stop sending filenumber until the other - * person sends a control packet with receive_send == 1 and control_type == TOX_FILECONTROL_ACCEPT with the filenumber being a paused filenumber. + * person sends a control packet with send_receive == 0 and control_type == TOX_FILECONTROL_ACCEPT with the filenumber being a paused filenumber. * * If you receive a control packet with receive_send == 0 and control_type == TOX_FILECONTROL_PAUSE, it means the sender of filenumber has paused the - * transfer and will resume it later with a control packet with receive_send == 0 and control_type == TOX_FILECONTROL_ACCEPT for that file number. + * transfer and will resume it later with a control packet with send_receive == 1 and control_type == TOX_FILECONTROL_ACCEPT for that file number. * * More to come... */ -- cgit v1.2.3 From 078d60ab929ca7ba50ba9e7de5745456c6114352 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 15 Jul 2014 12:35:57 -0400 Subject: Fixed bug with TCP. Don't increment nonce if packet was discarded instead of being sent. --- toxcore/TCP_client.c | 10 +++++----- toxcore/TCP_server.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index bc1b7ff4..2a2289b2 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -155,16 +155,16 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) return -1; - increment_nonce(con->sent_nonce); - len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); - if ((unsigned int)len == sizeof(packet)) - return 1; - if (len <= 0) return 0; + increment_nonce(con->sent_nonce); + + if ((unsigned int)len == sizeof(packet)) + return 1; + memcpy(con->last_packet, packet, length); con->last_packet_length = sizeof(packet); con->last_packet_sent = len; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 7487ad4f..6c0133dd 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -335,16 +335,16 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) return -1; - increment_nonce(con->sent_nonce); - len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); - if ((unsigned int)len == sizeof(packet)) - return 1; - if (len <= 0) return 0; + increment_nonce(con->sent_nonce); + + if ((unsigned int)len == sizeof(packet)) + return 1; + memcpy(con->last_packet, packet, length); con->last_packet_length = sizeof(packet); con->last_packet_sent = len; -- cgit v1.2.3 From ae7a11cae9e220ce0a66bc48977104495759a01a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 15 Jul 2014 13:39:32 -0400 Subject: Fixed TCP bug. Packet was being copied with a wrong, smaller length. --- toxcore/TCP_client.c | 2 +- toxcore/TCP_server.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 2a2289b2..dbcae11b 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -165,7 +165,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const if ((unsigned int)len == sizeof(packet)) return 1; - memcpy(con->last_packet, packet, length); + memcpy(con->last_packet, packet, sizeof(packet)); con->last_packet_length = sizeof(packet); con->last_packet_sent = len; return 1; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 6c0133dd..e7ef0d3a 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -345,7 +345,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const if ((unsigned int)len == sizeof(packet)) return 1; - memcpy(con->last_packet, packet, length); + memcpy(con->last_packet, packet, sizeof(packet)); con->last_packet_length = sizeof(packet); con->last_packet_sent = len; return 1; -- cgit v1.2.3 From 22d28ddf36563e2d0018fc20cafdfe61278dd67f Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 15 Jul 2014 17:40:21 -0400 Subject: added priority packet system for TCP server --- toxcore/TCP_server.c | 109 ++++++++++++++++++++++++++++++++++++++++++++------- toxcore/TCP_server.h | 10 +++++ 2 files changed, 104 insertions(+), 15 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index e7ef0d3a..8b7d5fdd 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -306,25 +306,94 @@ static int send_pending_data(TCP_Secure_Connection *con) return 0; } - if (len > left) - return -1; - con->last_packet_sent += len; return -1; } +/* return 0 if pending data was sent completely + * return -1 if it wasn't + */ +static int send_pending_data_priority(TCP_Secure_Connection *con) +{ + TCP_Priority_List *p = con->priority_queue_start; + + while(p) { + uint16_t left = p->size - p->sent; + int len = send(con->sock, p->data + p->sent, left, MSG_NOSIGNAL); + + if(len != left) { + if(len > 0) { + p->sent += len; + } + break; + } + + TCP_Priority_List *pp = p; + p = p->next; + free(pp); + } + + con->priority_queue_start = p; + if(!p) { + con->priority_queue_end = NULL; + return 0; + } + + return -1; +} + +/* return 0 on failure (only if malloc fails) + * return 1 on success + */ +static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, int sent) +{ + if(sent == size) { + return 1; + } + + if(sent <= 0) { + sent = 0; + } + + TCP_Priority_List *p = con->priority_queue_end, *new; + new = malloc(sizeof(TCP_Priority_List) + size); + if(!new) { + return 0; + } + + new->next = NULL; + new->size = size; + new->sent = sent; + memcpy(new->data, packet, size); + + if(p) { + p->next = new; + } else { + con->priority_queue_start = new; + } + + con->priority_queue_end = new; + return 1; +} + /* return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length) +static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, _Bool priority) { if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) return -1; - if (send_pending_data(con) == -1) - return 0; + _Bool sendpriority = 1; + if (send_pending_data_priority(con) == -1) { + if (priority) { + sendpriority = 0; + } else { + return 0; + } + } uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; @@ -332,6 +401,13 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); + if (priority) { + return add_priority(con, packet, sizeof(packet), sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0); + } + + if (send_pending_data(con) == -1) + return 0; + if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) return -1; @@ -459,7 +535,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const data[1] = rpid; memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES); - return write_packet_TCP_secure_connection(con, data, sizeof(data)); + return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); } /* return 1 on success. @@ -469,7 +545,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id + NUM_RESERVED_PORTS}; - return write_packet_TCP_secure_connection(con, data, sizeof(data)); + return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); } /* return 1 on success. @@ -479,7 +555,7 @@ static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id + NUM_RESERVED_PORTS}; - return write_packet_TCP_secure_connection(con, data, sizeof(data)); + return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); } /* return 0 on success. @@ -579,7 +655,7 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES); memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, - sizeof(resp_packet)); + sizeof(resp_packet), 0); } return 0; @@ -637,7 +713,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, memcpy(packet + 1, data, length); packet[0] = TCP_PACKET_ONION_RESPONSE; - if (write_packet_TCP_secure_connection(con, packet, sizeof(packet)) != 1) + if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1) return 1; return 0; @@ -682,7 +758,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint uint8_t response[1 + sizeof(uint64_t)]; response[0] = TCP_PACKET_PONG; memcpy(response + 1, data + 1, sizeof(uint64_t)); - write_packet_TCP_secure_connection(con, response, sizeof(response)); + write_packet_TCP_secure_connection(con, response, sizeof(response), 1); return 0; } @@ -752,7 +828,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint uint8_t new_data[length]; memcpy(new_data, data, length); new_data[0] = other_c_id; - int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length); + int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); if (ret == -1) return -1; @@ -1058,7 +1134,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) ++ping_id; memcpy(ping + 1, &ping_id, sizeof(uint64_t)); - int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping)); + int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1); if (ret == 1) { conn->last_pinged = unix_time(); @@ -1076,7 +1152,10 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) continue; } - send_pending_data(conn); + /* try sending queued priority packets first */ + if(send_pending_data_priority(conn) == 0) { + send_pending_data(conn); + } #ifndef TCP_SERVER_USE_EPOLL diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h index def0a978..81507acb 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h @@ -80,6 +80,14 @@ enum { TCP_STATUS_CONFIRMED, }; +typedef struct TCP_Priority_List TCP_Priority_List; + +struct TCP_Priority_List { + TCP_Priority_List *next; + uint16_t size, sent; + uint8_t data[0]; +}; + typedef struct TCP_Secure_Connection { uint8_t status; sock_t sock; @@ -98,6 +106,8 @@ typedef struct TCP_Secure_Connection { uint16_t last_packet_length; uint16_t last_packet_sent; + TCP_Priority_List *priority_queue_start, *priority_queue_end; + uint64_t identifier; uint64_t last_pinged; -- cgit v1.2.3 From 96249e8b020c76e9f80da6538ea19699612479dc Mon Sep 17 00:00:00 2001 From: notsecure Date: Tue, 15 Jul 2014 18:22:04 -0400 Subject: fixed some issues --- toxcore/TCP_server.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 8b7d5fdd..15c2b45d 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -288,7 +288,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, /* return 0 if pending data was sent completely * return -1 if it wasn't */ -static int send_pending_data(TCP_Secure_Connection *con) +static int send_pending_data_nonpriority(TCP_Secure_Connection *con) { if (con->last_packet_length == 0) { return 0; @@ -314,8 +314,13 @@ static int send_pending_data(TCP_Secure_Connection *con) /* return 0 if pending data was sent completely * return -1 if it wasn't */ -static int send_pending_data_priority(TCP_Secure_Connection *con) +static int send_pending_data(TCP_Secure_Connection *con) { + /* finish sending current non-priority packet */ + if(send_pending_data_nonpriority(con) == -1) { + return -1; + } + TCP_Priority_List *p = con->priority_queue_start; while(p) { @@ -346,16 +351,8 @@ static int send_pending_data_priority(TCP_Secure_Connection *con) /* return 0 on failure (only if malloc fails) * return 1 on success */ -static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, int sent) +static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) { - if(sent == size) { - return 1; - } - - if(sent <= 0) { - sent = 0; - } - TCP_Priority_List *p = con->priority_queue_end, *new; new = malloc(sizeof(TCP_Priority_List) + size); if(!new) { @@ -387,7 +384,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const return -1; _Bool sendpriority = 1; - if (send_pending_data_priority(con) == -1) { + if (send_pending_data(con) == -1) { if (priority) { sendpriority = 0; } else { @@ -401,15 +398,23 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); + if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) + return -1; + if (priority) { - return add_priority(con, packet, sizeof(packet), sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0); - } + len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; + if(len <= 0) { + len = 0; + } else { + increment_nonce(con->sent_nonce); + } - if (send_pending_data(con) == -1) - return 0; + if(len == sizeof(packet)) { + return 1; + } - if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) - return -1; + return add_priority(con, packet, sizeof(packet), len); + } len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); @@ -1152,10 +1157,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) continue; } - /* try sending queued priority packets first */ - if(send_pending_data_priority(conn) == 0) { - send_pending_data(conn); - } + send_pending_data(conn); #ifndef TCP_SERVER_USE_EPOLL -- cgit v1.2.3 From e04fff3eac1c1fe1bb663edd8d0b45a9798ace83 Mon Sep 17 00:00:00 2001 From: notsecure Date: Wed, 16 Jul 2014 08:33:24 -0400 Subject: applied priority queue changes to the TCP client too --- toxcore/TCP_client.c | 107 ++++++++++++++++++++++++++++++++++++++++++++------- toxcore/TCP_client.h | 2 + 2 files changed, 96 insertions(+), 13 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index dbcae11b..4aed0ff5 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -109,7 +109,7 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data /* return 0 if pending data was sent completely * return -1 if it wasn't */ -static int send_pending_data(TCP_Client_Connection *con) +static int send_pending_data_nonpriority(TCP_Client_Connection *con) { if (con->last_packet_length == 0) { return 0; @@ -127,24 +127,90 @@ static int send_pending_data(TCP_Client_Connection *con) return 0; } - if (len > left) + con->last_packet_sent += len; + return -1; +} + +/* return 0 if pending data was sent completely + * return -1 if it wasn't + */ +static int send_pending_data(TCP_Client_Connection *con) +{ + /* finish sending current non-priority packet */ + if(send_pending_data_nonpriority(con) == -1) { return -1; + } + + TCP_Priority_List *p = con->priority_queue_start; + + while(p) { + uint16_t left = p->size - p->sent; + int len = send(con->sock, p->data + p->sent, left, MSG_NOSIGNAL); + + if(len != left) { + if(len > 0) { + p->sent += len; + } + break; + } + + TCP_Priority_List *pp = p; + p = p->next; + free(pp); + } + + con->priority_queue_start = p; + if(!p) { + con->priority_queue_end = NULL; + return 0; + } - con->last_packet_sent += len; return -1; } +/* return 0 on failure (only if malloc fails) + * return 1 on success + */ +static _Bool add_priority(TCP_Client_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) +{ + TCP_Priority_List *p = con->priority_queue_end, *new; + new = malloc(sizeof(TCP_Priority_List) + size); + if(!new) { + return 0; + } + + new->next = NULL; + new->size = size; + new->sent = sent; + memcpy(new->data, packet, size); + + if(p) { + p->next = new; + } else { + con->priority_queue_start = new; + } + + con->priority_queue_end = new; + return 1; +} + /* return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length) +static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length, _Bool priority) { if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) return -1; - if (send_pending_data(con) == -1) - return 0; + _Bool sendpriority = 1; + if (send_pending_data(con) == -1) { + if (priority) { + sendpriority = 0; + } else { + return 0; + } + } uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; @@ -155,6 +221,21 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) return -1; + if (priority) { + len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; + if(len <= 0) { + len = 0; + } else { + increment_nonce(con->sent_nonce); + } + + if(len == sizeof(packet)) { + return 1; + } + + return add_priority(con, packet, sizeof(packet), len); + } + len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); if (len <= 0) @@ -180,7 +261,7 @@ int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) uint8_t packet[1 + crypto_box_PUBLICKEYBYTES]; packet[0] = TCP_PACKET_ROUTING_REQUEST; memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1); } void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, @@ -218,7 +299,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u uint8_t packet[1 + length]; packet[0] = con_id + NUM_RESERVED_PORTS; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); } /* return 1 on success. @@ -234,7 +315,7 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const packet[0] = TCP_PACKET_OOB_SEND; memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); } @@ -280,7 +361,7 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id) uint8_t packet[1 + 1]; packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; packet[1] = id; - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1); } /* return 1 on success. @@ -297,7 +378,7 @@ static int send_ping_request(TCP_Client_Connection *con) memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); int ret; - if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { + if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) { con->ping_request_id = 0; } @@ -318,7 +399,7 @@ static int send_ping_response(TCP_Client_Connection *con) memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); int ret; - if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) { + if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) { con->ping_response_id = 0; } @@ -348,7 +429,7 @@ int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t uint8_t packet[1 + length]; packet[0] = TCP_PACKET_ONION_REQUEST; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); + return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); } void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index f57f79e6..9998d20c 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -52,6 +52,8 @@ typedef struct { uint16_t last_packet_length; uint16_t last_packet_sent; + TCP_Priority_List *priority_queue_start, *priority_queue_end; + uint64_t kill_at; uint64_t last_pinged; -- cgit v1.2.3 From 13b23b1a0157785e18d7ef551a1a5a0e0006ee64 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 16 Jul 2014 18:29:00 -0400 Subject: fix memory leak --- toxcore/group_chats.c | 1 + 1 file changed, 1 insertion(+) (limited to 'toxcore') diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 53ef69d3..77fa6acd 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -820,6 +820,7 @@ void do_groupchat(Group_Chat *chat) void kill_groupchat(Group_Chat *chat) { send_data(chat, 0, 0, GROUP_CHAT_QUIT); + kill_Assoc(chat->assoc); free(chat->group); free(chat); } -- cgit v1.2.3 From bcfb1261ac4ab5f6470924f473c8c18bc5f98f01 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 16 Jul 2014 20:39:58 -0400 Subject: Fixed possible bug with TCP nonce not getting incremented. --- toxcore/TCP_client.c | 4 ++-- toxcore/TCP_server.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'toxcore') diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 02d4e7dc..45220538 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -231,10 +231,10 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const if (len <= 0) { len = 0; - } else { - increment_nonce(con->sent_nonce); } + increment_nonce(con->sent_nonce); + if (len == sizeof(packet)) { return 1; } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index f3cdd287..47e2ff27 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -411,10 +411,10 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const if (len <= 0) { len = 0; - } else { - increment_nonce(con->sent_nonce); } + increment_nonce(con->sent_nonce); + if (len == sizeof(packet)) { return 1; } -- cgit v1.2.3 From 9c84b5401cdfb621b2e77751ad869b7fea786885 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 18 Jul 2014 21:34:22 -0400 Subject: Spread packets over many TCP relays instead of just using the first one. --- toxcore/net_crypto.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index c44d09c4..18dd3b05 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -426,19 +426,22 @@ static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const ui } - //TODO: spread packets over many relays, detect and kill bad relays. + //TODO: detect and kill bad relays. uint32_t i; + unsigned int r = rand(); + for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { - if (conn->status_tcp[i] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */ - if (send_data(c->tcp_connections[i], conn->con_number_tcp[i], data, length) == 1) + if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */ + if (send_data(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->con_number_tcp[(i + r) % MAX_TCP_CONNECTIONS], + data, length) == 1) return 0; } } for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { - if (conn->status_tcp[i] == STATUS_TCP_INVISIBLE) { - if (send_oob_packet(c->tcp_connections[i], conn->dht_public_key, data, length) == 1) + if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_INVISIBLE) { + if (send_oob_packet(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->dht_public_key, data, length) == 1) return 0; } } -- cgit v1.2.3 From 8195ade7092e6261b6554ca9657eeded3c220c8d Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 19 Jul 2014 22:09:30 -0400 Subject: Removed useless function. --- toxcore/net_crypto.c | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'toxcore') diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 18dd3b05..ac2359ee 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -37,21 +37,6 @@ static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_conn return (uint32_t)crypt_connection_id >= c->crypto_connections_length; } -/* return 0 if connection is dead. - * return 1 if connection is alive. - */ -static int is_alive(uint8_t status) -{ - if (status == CRYPTO_CONN_COOKIE_REQUESTING || - status == CRYPTO_CONN_HANDSHAKE_SENT || - status == CRYPTO_CONN_NOT_CONFIRMED || - status == CRYPTO_CONN_ESTABLISHED) { - return 1; - } - - return 0; -} - /* cookie timeout in seconds */ #define COOKIE_TIMEOUT 10 #define COOKIE_DATA_LENGTH (crypto_box_PUBLICKEYBYTES * 2) -- cgit v1.2.3 From 97ad1e62b676682bb4f1950023460e326b216841 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 20 Jul 2014 20:08:00 -0400 Subject: Added addr_parse_ip() to network.h header. Removed useless semicolons. --- toxcore/network.c | 26 ++++++++++++-------------- toxcore/network.h | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) (limited to 'toxcore') diff --git a/toxcore/network.c b/toxcore/network.c index 0009a558..373fef9e 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -671,7 +671,7 @@ int ip_equal(const IP *a, const IP *b) } return 0; -}; +} /* ipport_equal * compares two IPAny_Port structures @@ -688,7 +688,7 @@ int ipport_equal(const IP_Port *a, const IP_Port *b) return 0; return ip_equal(&a->ip, &b->ip); -}; +} /* nulls out ip */ void ip_reset(IP *ip) @@ -697,7 +697,7 @@ void ip_reset(IP *ip) return; memset(ip, 0, sizeof(IP)); -}; +} /* nulls out ip, sets family according to flag */ void ip_init(IP *ip, uint8_t ipv6enabled) @@ -707,7 +707,7 @@ void ip_init(IP *ip, uint8_t ipv6enabled) memset(ip, 0, sizeof(IP)); ip->family = ipv6enabled ? AF_INET6 : AF_INET; -}; +} /* checks if ip is valid */ int ip_isset(const IP *ip) @@ -716,7 +716,7 @@ int ip_isset(const IP *ip) return 0; return (ip->family != 0); -}; +} /* checks if ip is valid */ int ipport_isset(const IP_Port *ipport) @@ -728,7 +728,7 @@ int ipport_isset(const IP_Port *ipport) return 0; return ip_isset(&ipport->ip); -}; +} /* copies an ip structure (careful about direction!) */ void ip_copy(IP *target, const IP *source) @@ -737,7 +737,7 @@ void ip_copy(IP *target, const IP *source) return; memcpy(target, source, sizeof(IP)); -}; +} /* copies an ip_port structure (careful about direction!) */ void ipport_copy(IP_Port *target, const IP_Port *source) @@ -805,7 +805,7 @@ const char *ip_ntoa(const IP *ip) /* brute force protection against lacking termination */ addresstext[sizeof(addresstext) - 1] = 0; return addresstext; -}; +} /* * addr_parse_ip @@ -820,7 +820,6 @@ const char *ip_ntoa(const IP *ip) * * returns 1 on success, 0 on failure */ - int addr_parse_ip(const char *address, IP *to) { if (!address || !to) @@ -832,7 +831,7 @@ int addr_parse_ip(const char *address, IP *to) to->family = AF_INET; to->ip4.in_addr = addr4; return 1; - }; + } struct in6_addr addr6; @@ -840,10 +839,10 @@ int addr_parse_ip(const char *address, IP *to) to->family = AF_INET6; to->ip6.in6_addr = addr6; return 1; - }; + } return 0; -}; +} /* * addr_resolve(): @@ -862,7 +861,6 @@ int addr_parse_ip(const char *address, IP *to) * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 * returns 0 on failure */ - int addr_resolve(const char *address, IP *to, IP *extra) { if (!address || !to) @@ -970,4 +968,4 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) return 0; return 1; -}; +} diff --git a/toxcore/network.h b/toxcore/network.h index 359d26c8..b902a487 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -191,6 +191,21 @@ IP_Port; */ const char *ip_ntoa(const IP *ip); +/* + * addr_parse_ip + * directly parses the input into an IP structure + * tries IPv4 first, then IPv6 + * + * input + * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) + * + * output + * IP: family and the value is set on success + * + * returns 1 on success, 0 on failure + */ +int addr_parse_ip(const char *address, IP *to); + /* ip_equal * compares two IPAny structures * unset means unequal -- cgit v1.2.3 From b63e4ad88fb93820fb740372f36d74c65b8b7b81 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 20 Jul 2014 20:50:53 -0400 Subject: LAN discovery should now work on windows machines with multiple ethernet devices. Added some code to get real adapter broadcast addresses on windows. --- configure.ac | 2 +- toxcore/LAN_discovery.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 5 deletions(-) (limited to 'toxcore') diff --git a/configure.ac b/configure.ac index 174ecbe0..d1c54c71 100644 --- a/configure.ac +++ b/configure.ac @@ -662,7 +662,7 @@ fi if test "x$WIN32" = "xyes"; then AC_CHECK_LIB(ws2_32, main, [ - WINSOCK2_LIBS="-lws2_32" + WINSOCK2_LIBS="-liphlpapi -lws2_32" AC_SUBST(WINSOCK2_LIBS) ], [ diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index e1b8534c..420c490d 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -30,11 +30,72 @@ #define MAX_INTERFACES 16 -#ifdef __linux static int broadcast_count = -1; static IP_Port broadcast_ip_port[MAX_INTERFACES]; +#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) + +#include + +static void fetch_broadcast_info(uint16_t port) +{ + broadcast_count = 0; + + IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(pAdapterInfo)); + unsigned long ulOutBufLen = sizeof(pAdapterInfo); + + if (pAdapterInfo == NULL) { + printf("Error allocating memory for pAdapterInfo\n"); + return; + } + + if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { + free(pAdapterInfo); + pAdapterInfo = malloc(ulOutBufLen); + + if (pAdapterInfo == NULL) { + printf("Error allocating memory needed to call GetAdaptersinfo\n"); + return; + } + } + + int ret; + + if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { + IP_ADAPTER_INFO *pAdapter = pAdapterInfo; + + while (pAdapter) { + IP gateway = {0}, subnet_mask = {0}; + + if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask) + && addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) { + if (gateway.family == AF_INET && subnet_mask.family == AF_INET) { + IP_Port *ip_port = &broadcast_ip_port[broadcast_count]; + ip_port->ip.family = AF_INET; + uint32_t gateway_ip = ntohl(gateway.ip4.uint32), subnet_ip = ntohl(subnet_mask.ip4.uint32); + uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1; + ip_port->ip.ip4.uint32 = htonl(broadcast_ip); + ip_port->port = port; + broadcast_count++; + printf("broadcast ip: %s\n", ip_ntoa(&ip_port->ip)); + + if (broadcast_count >= MAX_INTERFACES) { + return; + } + } + } + + pAdapter = pAdapter->Next; + } + } else { + printf("Fetching adapter info failed %i\n", ret); + } + +} + +#elif defined(__linux__) + static void fetch_broadcast_info(uint16_t port) { /* Not sure how many platforms this will run on, @@ -93,6 +154,14 @@ static void fetch_broadcast_info(uint16_t port) close(sock); } +#else //TODO: Other platforms? + +static void fetch_broadcast_info(uint16_t port) +{ + broadcast_count = 0; +} + +#endif /* Send packet to all IPv4 broadcast addresses * * return 1 if sent to at least one broadcast target. @@ -115,7 +184,6 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8 return 1; } -#endif /* __linux */ /* Return the broadcast ip. */ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) @@ -228,9 +296,8 @@ int send_LANdiscovery(uint16_t port, DHT *dht) data[0] = NET_PACKET_LAN_DISCOVERY; id_copy(data + 1, dht->self_public_key); -#ifdef __linux send_broadcasts(dht->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); -#endif + int res = -1; IP_Port ip_port; ip_port.port = port; -- cgit v1.2.3