From 565d73713aa75f0d5437f0152ee7cb22f3113c59 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 16 Apr 2015 14:17:57 -0400 Subject: Store TCP relays tied to friend and reconnect to some when reconnecting. --- toxcore/Messenger.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index fefc3b17..bd44115d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2185,10 +2185,10 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) break; - int i; + int j; - for (i = 0; i < n; i++) { - add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].public_key); + for (j = 0; j < n; j++) { + friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key); } break; -- cgit v1.2.3 From 92a708578f9aa4c2cff2d3a2de08e28ee61cda1b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 16 Apr 2015 20:39:09 -0400 Subject: Properly share connected relays and connect to the friend correctly with them. --- toxcore/Messenger.c | 13 +++++++++++++ toxcore/Messenger.h | 2 +- toxcore/TCP_connection.c | 4 ++-- toxcore/TCP_connection.h | 6 +++--- 4 files changed, 19 insertions(+), 6 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index bd44115d..3233a466 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -755,11 +755,23 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t static int send_relays(const Messenger *m, int32_t friendnumber) { + if (friend_not_valid(m, friendnumber)) + return 0; + Node_format nodes[MAX_SHARED_RELAYS]; uint8_t data[1024]; int n, length; n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS); + + unsigned int i; + + for (i = 0; i < n; ++i) { + /* Associated the relays being sent with this connection. + On receiving the peer will do the same which will establish the connection. */ + friend_add_tcp_relay(m->fr_c, m->friendlist[friendnumber].friendcon_id, nodes[i].ip_port, nodes[i].public_key); + } + length = pack_nodes(data, sizeof(data), nodes, n); int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0); @@ -1914,6 +1926,7 @@ static int handle_status(void *object, int i, uint8_t status) m->friendlist[i].userstatus_sent = 0; m->friendlist[i].statusmessage_sent = 0; m->friendlist[i].user_istyping_sent = 0; + m->friendlist[i].share_relays_lastsent = 0; } else { /* Went offline. */ if (m->friendlist[i].status == FRIEND_ONLINE) { set_friend_status(m, i, FRIEND_CONFIRMED); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 63fb2820..e17bbb42 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -63,7 +63,7 @@ enum { #define PACKET_ID_LOSSY_GROUPCHAT 199 /* Max number of tcp relays sent to friends */ -#define MAX_SHARED_RELAYS 16 +#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) /* All packets starting with a byte in this range can be used for anything. */ #define PACKET_ID_LOSSLESS_RANGE_START 160 diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 49a213a7..ab18daac 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -901,10 +901,10 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ */ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num) { - unsigned int i, copied = 0; + unsigned int i, copied = 0, r = rand(); for (i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); if (!tcp_con) { continue; diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 98f52f8d..07d7820c 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -34,14 +34,14 @@ #define TCP_CONNECTIONS_STATUS_REGISTERED 1 #define TCP_CONNECTIONS_STATUS_ONLINE 2 -#define MAX_FRIEND_TCP_CONNECTIONS 4 +#define MAX_FRIEND_TCP_CONNECTIONS 6 /* Time until connection to friend gets killed (if it doesn't get locked withing that time) */ #define TCP_CONNECTION_ANNOUNCE_TIMEOUT (TCP_CONNECTION_TIMEOUT) /* The amount of recommended connections for each friend - NOTE: Must be equal or smaller than MAX_FRIEND_TCP_CONNECTIONS */ -#define RECOMMENDED_FRIEND_TCP_CONNECTIONS 3 + NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */ +#define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2) typedef struct { uint8_t status; -- cgit v1.2.3 From 453548f18149594af977dc63c8a1924d8a6bb2e6 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 18 Apr 2015 11:15:59 -0400 Subject: Some messenger fixes. --- toxcore/Messenger.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3233a466..1c9510ab 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -493,10 +493,6 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con uint32_t msg_id = ++m->friendlist[friendnumber].message_id; - if (msg_id == 0) { - msg_id = ++m->friendlist[friendnumber].message_id; // Otherwise, false error - } - add_receipt(m, friendnumber, packet_num, msg_id); if (message_id) @@ -1362,7 +1358,7 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin if (m->friendlist[friendnumber].status != FRIEND_ONLINE) return -2; - if (filenumber > MAX_CONCURRENT_FILE_PIPES) + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) return -3; struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[filenumber]; -- cgit v1.2.3 From 3bd4f5902cb4dd4301ba9a8433f073fe31d6d2b5 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 21 Apr 2015 20:12:24 -0400 Subject: Move the send tcp relay packet from Messenger to friend connection. --- toxcore/Messenger.c | 52 ------------------------------------ toxcore/Messenger.h | 10 +------ toxcore/friend_connection.c | 64 +++++++++++++++++++++++++++++++++++++++++---- toxcore/friend_connection.h | 9 +++++++ 4 files changed, 69 insertions(+), 66 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 1c9510ab..efb66961 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -749,37 +749,6 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0); } -static int send_relays(const Messenger *m, int32_t friendnumber) -{ - if (friend_not_valid(m, friendnumber)) - return 0; - - Node_format nodes[MAX_SHARED_RELAYS]; - uint8_t data[1024]; - int n, length; - - n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS); - - unsigned int i; - - for (i = 0; i < n; ++i) { - /* Associated the relays being sent with this connection. - On receiving the peer will do the same which will establish the connection. */ - friend_add_tcp_relay(m->fr_c, m->friendlist[friendnumber].friendcon_id, nodes[i].ip_port, nodes[i].public_key); - } - - length = pack_nodes(data, sizeof(data), nodes, n); - - int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0); - - if (ret == 1) - m->friendlist[friendnumber].share_relays_lastsent = unix_time(); - - return ret; -} - - - static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) { if (friend_not_valid(m, friendnumber)) @@ -1922,7 +1891,6 @@ static int handle_status(void *object, int i, uint8_t status) m->friendlist[i].userstatus_sent = 0; m->friendlist[i].statusmessage_sent = 0; m->friendlist[i].user_istyping_sent = 0; - m->friendlist[i].share_relays_lastsent = 0; } else { /* Went offline. */ if (m->friendlist[i].status == FRIEND_ONLINE) { set_friend_status(m, i, FRIEND_CONFIRMED); @@ -2187,22 +2155,6 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) break; } - case PACKET_ID_SHARE_RELAYS: { - Node_format nodes[MAX_SHARED_RELAYS]; - int n; - - if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) - break; - - int j; - - for (j = 0; j < n; j++) { - friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key); - } - - break; - } - default: { handle_custom_lossless_packet(object, i, temp, len); break; @@ -2260,10 +2212,6 @@ void do_friends(Messenger *m) m->friendlist[i].user_istyping_sent = 1; } - if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) { - send_relays(m, i); - } - check_friend_tcp_udp(m, i); do_receipts(m, i); do_reqchunk_filecb(m, i); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e17bbb42..c9573639 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -42,8 +42,7 @@ enum { MESSAGE_ACTION }; -/* NOTE: Packet ids below 20 must never be used. */ -#define PACKET_ID_SHARE_RELAYS 23 +/* NOTE: Packet ids below 24 must never be used. */ #define PACKET_ID_ONLINE 24 #define PACKET_ID_OFFLINE 25 #define PACKET_ID_NICKNAME 48 @@ -62,9 +61,6 @@ enum { #define PACKET_ID_MESSAGE_GROUPCHAT 99 #define PACKET_ID_LOSSY_GROUPCHAT 199 -/* Max number of tcp relays sent to friends */ -#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) - /* All packets starting with a byte in this range can be used for anything. */ #define PACKET_ID_LOSSLESS_RANGE_START 160 #define PACKET_ID_LOSSLESS_RANGE_SIZE 32 @@ -110,9 +106,6 @@ enum { /* Default start timeout in seconds between friend requests. */ #define FRIENDREQUEST_TIMEOUT 5; -/* Interval between the sending of tcp relay information */ -#define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60) - enum { CONNECTION_NONE, CONNECTION_TCP, @@ -199,7 +192,6 @@ typedef struct { uint32_t message_id; // a semi-unique id used in read receipts. uint32_t friendrequest_nospam; // The nospam number used in the friend request. uint64_t last_seen_time; - uint64_t share_relays_lastsent; uint8_t last_connection_udp_tcp; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; unsigned int num_sending_files; diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 0f171100..892048ad 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -198,6 +198,43 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_ } } +static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) +{ + Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + + if (!friend_con) + return 0; + + Node_format nodes[MAX_SHARED_RELAYS]; + uint8_t data[1024]; + int n, length; + + n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); + + unsigned int i; + + for (i = 0; i < n; ++i) { + /* Associated the relays being sent with this connection. + On receiving the peer will do the same which will establish the connection. */ + friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); + } + + length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); + + if (length <= 0) + return 0; + + data[0] = PACKET_ID_SHARE_RELAYS; + ++length; + + if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) { + friend_con->share_relays_lastsent = unix_time(); + return 1; + } + + return 0; +} + /* callback for recv TCP relay nodes. */ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) { @@ -293,6 +330,7 @@ static int handle_status(void *object, int number, uint8_t status) call_cb = 1; friend_con->status = FRIENDCONN_STATUS_CONNECTED; friend_con->ping_lastrecv = unix_time(); + friend_con->share_relays_lastsent = 0; onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); } else { /* Went offline. */ if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { @@ -326,18 +364,30 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt Friend_Connections *fr_c = object; Friend_Conn *friend_con = get_conn(fr_c, number); + if (!friend_con) + return -1; + if (data[0] == PACKET_ID_FRIEND_REQUESTS) { if (fr_c->fr_request_callback) fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length); return 0; - } + } else if (data[0] == PACKET_ID_ALIVE) { + friend_con->ping_lastrecv = unix_time(); + return 0; + } else if (data[0] == PACKET_ID_SHARE_RELAYS) { + Node_format nodes[MAX_SHARED_RELAYS]; + int n; - if (!friend_con) - return -1; + if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1) + return -1; + + int j; + + for (j = 0; j < n; j++) { + friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); + } - if (data[0] == PACKET_ID_ALIVE) { - friend_con->ping_lastrecv = unix_time(); return 0; } @@ -745,6 +795,10 @@ void do_friend_connections(Friend_Connections *fr_c) send_ping(fr_c, i); } + if (friend_con->share_relays_lastsent + SHARE_RELAYS_INTERVAL < temp_time) { + send_relays(fr_c, i); + } + if (friend_con->ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { /* If we stopped receiving ping packets, kill it. */ crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id); diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 641d2872..60b62646 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -36,6 +36,7 @@ #define GROUPCHAT_CALLBACK_INDEX 1 #define PACKET_ID_ALIVE 16 +#define PACKET_ID_SHARE_RELAYS 17 #define PACKET_ID_FRIEND_REQUESTS 18 /* Interval between the sending of ping packets. */ @@ -49,6 +50,13 @@ #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) +/* Max number of tcp relays sent to friends */ +#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) + +/* Interval between the sending of tcp relay information */ +#define SHARE_RELAYS_INTERVAL (5 * 60) + + enum { FRIENDCONN_STATUS_NONE, FRIENDCONN_STATUS_CONNECTING, @@ -68,6 +76,7 @@ typedef struct { int crypt_connection_id; uint64_t ping_lastrecv, ping_lastsent; + uint64_t share_relays_lastsent; struct { int (*status_callback)(void *object, int id, uint8_t status); -- cgit v1.2.3 From 3e256e429f6b2d3b8d3b49c1beac62f3b2f138ee Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 24 Apr 2015 11:09:42 -0400 Subject: Fixed a couple possible issues with the friend connection callback. A TCP callback could be received when only a UDP connection was present. Increased a UDP timeout to make it less likely to time out and switch to TCP. --- toxcore/Messenger.c | 19 ++++++++++++++++--- toxcore/Messenger.h | 3 ++- toxcore/TCP_connection.c | 13 +++++++++++++ toxcore/TCP_connection.h | 5 +++++ toxcore/net_crypto.c | 30 +++++++++++++++++++----------- toxcore/net_crypto.h | 8 ++++---- 6 files changed, 59 insertions(+), 19 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index efb66961..fd17ab98 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -433,14 +433,19 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) return -1; if (m->friendlist[friendnumber].status == FRIEND_ONLINE) { - uint8_t direct_connected = 0; + _Bool direct_connected = 0; + unsigned int num_online_relays = 0; crypto_connection_status(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id), &direct_connected); + m->friendlist[friendnumber].friendcon_id), &direct_connected, &num_online_relays); if (direct_connected) { return CONNECTION_UDP; } else { - return CONNECTION_TCP; + if (num_online_relays) { + return CONNECTION_TCP; + } else { + return CONNECTION_UNKNOWN; + } } } else { return CONNECTION_NONE; @@ -851,6 +856,14 @@ static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber) if (ret == -1) return; + if (ret == CONNECTION_UNKNOWN) { + if (last_connection_udp_tcp == CONNECTION_UDP) { + return; + } else { + ret = CONNECTION_TCP; + } + } + if (last_connection_udp_tcp != ret) { if (m->friend_connectionstatuschange) m->friend_connectionstatuschange(m, friendnumber, ret, m->friend_connectionstatuschange_userdata); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index c9573639..5221e639 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -109,7 +109,8 @@ enum { enum { CONNECTION_NONE, CONNECTION_TCP, - CONNECTION_UDP + CONNECTION_UDP, + CONNECTION_UNKNOWN }; /* USERSTATUS - diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 7b478611..fe39dc52 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -1124,6 +1124,19 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ } } +/* return number of online tcp relays tied to the connection on success. + * return 0 on failure. + */ +unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int connections_number) +{ + TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); + + if (!con_to) + return 0; + + return online_tcp_connection_from_conn(con_to); +} + /* Copy a maximum of max_num TCP relays we are connected to to tcp_relays. * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. * diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 3f7b616d..29fbdee0 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -186,6 +186,11 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number); */ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number, _Bool status); +/* return number of online tcp relays tied to the connection on success. + * return 0 on failure. + */ +unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int connections_number); + /* Add a TCP relay tied to a connection. * * NOTE: This can only be used during the tcp_oob_callback. diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b6b1050a..ab0fb03a 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -404,8 +404,8 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t //TODO: on bad networks, direct connections might not last indefinitely. if (conn->ip_port.ip.family != 0) { - uint8_t direct_connected = 0; - crypto_connection_status(c, crypt_connection_id, &direct_connected); + _Bool direct_connected = 0; + crypto_connection_status(c, crypt_connection_id, &direct_connected, NULL); if (direct_connected) { if ((uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) { @@ -1446,7 +1446,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, conn->ip_port = source; } - conn->direct_lastrecv_time = current_time_monotonic(); + conn->direct_lastrecv_time = unix_time(); return 0; } else if (source.ip.family == TCP_FAMILY) { if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) @@ -1646,7 +1646,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port) return -1; if (!ipport_equal(&ip_port, &conn->ip_port)) { - if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > current_time_monotonic()) { + if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > unix_time()) { if (LAN_ip(ip_port.ip) == 0 && LAN_ip(conn->ip_port.ip) == 0 && conn->ip_port.port == ip_port.port) return -1; } @@ -1810,8 +1810,8 @@ static void do_tcp(Net_Crypto *c) return; if (conn->status == CRYPTO_CONN_ESTABLISHED) { - uint8_t direct_connected = 0; - crypto_connection_status(c, i, &direct_connected); + _Bool direct_connected = 0; + crypto_connection_status(c, i, &direct_connected, NULL); if (direct_connected) { pthread_mutex_lock(&c->tcp_mutex); @@ -1966,7 +1966,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet return -1; pthread_mutex_lock(&conn->mutex); - conn->direct_lastrecv_time = current_time_monotonic(); + conn->direct_lastrecv_time = unix_time(); pthread_mutex_unlock(&conn->mutex); return 0; } @@ -2290,18 +2290,26 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) /* return one of CRYPTO_CONN_* values indicating the state of the connection. * * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. + * sets online_tcp_relays to the number of connected tcp relays this connection has. */ -unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) +unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected, + unsigned int *online_tcp_relays) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == 0) return CRYPTO_CONN_NO_CONNECTION; - *direct_connected = 0; + if (direct_connected) { + *direct_connected = 0; - if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > current_time_monotonic()) - *direct_connected = 1; + if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_time) > unix_time()) + *direct_connected = 1; + } + + if (online_tcp_relays) { + *online_tcp_relays = tcp_connection_to_online_tcp_relays(c->tcp_c, conn->connection_number_tcp); + } return conn->status; } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index f6dd4702..9eb5e2d3 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -60,7 +60,7 @@ #define MAX_NUM_SENDPACKET_TRIES 8 /* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ -#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 2) +#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 1000) #define PACKET_ID_PADDING 0 /* Denotes padding */ #define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */ @@ -360,13 +360,13 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u */ int crypto_kill(Net_Crypto *c, int crypt_connection_id); - /* return one of CRYPTO_CONN_* values indicating the state of the connection. * * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. + * sets online_tcp_relays to the number of connected tcp relays this connection has. */ -unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected); - +unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected, + unsigned int *online_tcp_relays); /* Generate our public and private keys. * Only call this function the first time the program starts. -- cgit v1.2.3 From 69e3e5f3a4510e4883edecc78b5556d38cb61318 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 27 Apr 2015 16:13:04 -0400 Subject: Move LAN discovery from Messenger to friend_connection. --- toxcore/LAN_discovery.c | 5 +++++ toxcore/LAN_discovery.h | 3 +++ toxcore/Messenger.c | 11 ----------- toxcore/Messenger.h | 2 -- toxcore/friend_connection.c | 13 +++++++++++++ toxcore/friend_connection.h | 2 ++ 6 files changed, 23 insertions(+), 13 deletions(-) (limited to 'toxcore/Messenger.c') diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index bc020d87..dbce762e 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -336,3 +336,8 @@ void LANdiscovery_init(DHT *dht) { networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); } + +void LANdiscovery_kill(DHT *dht) +{ + networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL); +} diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 5dffc3ad..5243bd93 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -37,6 +37,9 @@ int send_LANdiscovery(uint16_t port, DHT *dht); /* Sets up packet handlers. */ void LANdiscovery_init(DHT *dht); +/* Clear packet handlers. */ +void LANdiscovery_kill(DHT *dht); + /* checks if a given IP isn't routable * * return 0 if ip is a LAN ip. diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index fd17ab98..a7e0a9fe 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1765,15 +1765,6 @@ static int friend_already_added(const uint8_t *real_pk, void *data) return -1; } -/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ -static void LANdiscovery(Messenger *m) -{ - if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { - send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->dht); - m->last_LANdiscovery = unix_time(); - } -} - /* Run this at startup. */ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) { @@ -1842,7 +1833,6 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) m->options = *options; friendreq_init(&(m->fr), m->fr_c); - LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); set_filter_function(&(m->fr), &friend_already_added, m); @@ -2308,7 +2298,6 @@ void do_messenger(Messenger *m) do_onion_client(m->onion_c); do_friend_connections(m->fr_c); do_friends(m); - LANdiscovery(m); connection_status_cb(m); #ifdef LOGGING diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 5221e639..6943475f 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -234,8 +234,6 @@ struct Messenger { uint32_t numonline_friends; - uint64_t last_LANdiscovery; - #define NUM_SAVED_TCP_RELAYS 8 uint8_t has_added_relays; // If the first connection has occurred in do_messenger Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 5182ce28..c13ca949 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -758,10 +758,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c) temp->onion_c = onion_c; new_connection_handler(temp->net_crypto, &handle_new_connections, temp); + LANdiscovery_init(temp->dht); return temp; } +/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ +static void LANdiscovery(Friend_Connections *fr_c) +{ + if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { + send_LANdiscovery(htons(TOX_PORT_DEFAULT), fr_c->dht); + fr_c->last_LANdiscovery = unix_time(); + } +} + /* main friend_connections loop. */ void do_friend_connections(Friend_Connections *fr_c) { @@ -809,6 +819,8 @@ void do_friend_connections(Friend_Connections *fr_c) } } } + + LANdiscovery(fr_c); } /* Free everything related with friend_connections. */ @@ -823,5 +835,6 @@ void kill_friend_connections(Friend_Connections *fr_c) kill_friend_connection(fr_c, i); } + LANdiscovery_kill(fr_c->dht); free(fr_c); } diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 60b62646..baca4b76 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -109,6 +109,8 @@ typedef struct { int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len); void *fr_request_object; + + uint64_t last_LANdiscovery; } Friend_Connections; /* return friendcon_id corresponding to the real public key on success. -- cgit v1.2.3