From 60ed415a5a36ac253a343f403e1bbfb620ae571a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 17 May 2014 14:56:20 -0400 Subject: Added a function to get the DHT public key stored in net_crypto. The DHT public keys in onion_client/net_crypto are synced and set to whichever is more recent. Tox peers now try to connect directly even when already connected with a TCP relay. --- toxcore/Messenger.c | 11 ++++++++++- toxcore/net_crypto.c | 30 +++++++++++++++++++++++++++++- toxcore/net_crypto.h | 7 +++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 2c7dcdee..b0d5a2e5 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2200,13 +2200,22 @@ void do_friends(Messenger *m) } friend_new_connection(m, i, m->friendlist[i].client_id); - uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; + } + if (m->friendlist[i].crypt_connection_id != -1) { + uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; uint64_t timestamp = onion_getfriend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key); + if (timestamp != 0) { set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key, timestamp); } + timestamp = get_connection_dht_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key); + + if (timestamp != 0) { + onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key, timestamp); + } + uint8_t direct_connected; unsigned int status = crypto_connection_status(m->net_crypto, m->friendlist[i].crypt_connection_id, &direct_connected); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 2f01bc83..8230a41d 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -408,8 +408,16 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data, //TODO: on bad networks, direct connections might not last indefinitely. if (conn->ip_port.ip.family != 0) { - if ((uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) + uint8_t direct_connected = 0; + crypto_connection_status(c, crypt_connection_id, &direct_connected); + + if (direct_connected && (uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) return 0; + + if (length < 96 + || data[0] == NET_PACKET_CRYPTO_HS) //TODO: a better way of sending packets directly to confirm the others ip. + sendpacket(c->dht->net, conn->ip_port, data, length); + } //TODO: spread packets over many relays, detect and kill bad relays. @@ -1488,6 +1496,26 @@ static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id) return 0; } +/* Copy friends DHT public key into dht_key. + * + * return 0 on failure (no key copied). + * return timestamp on success (key copied). + */ +uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) +{ + Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + + if (conn == 0) + return 0; + + if (conn->dht_public_key_set == 0) + return 0; + + memcpy(dht_public_key, conn->dht_public_key, crypto_box_PUBLICKEYBYTES); + return conn->dht_public_key_timestamp; +} + + /* Set the DHT public key of the crypto connection. * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to * the other peer. diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index dbbb295c..e41e7e1d 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -186,6 +186,13 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c); */ int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key); +/* Copy friends DHT public key into dht_key. + * + * return 0 on failure (no key copied). + * return timestamp on success (key copied). + */ +uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key); + /* Set the DHT public key of the crypto connection. * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to * the other peer. -- cgit v1.2.3