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/net_crypto.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'toxcore/net_crypto.c') 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. -- cgit v1.2.3