summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-05-17 14:56:20 -0400
committerirungentoo <irungentoo@gmail.com>2014-05-17 14:56:20 -0400
commit60ed415a5a36ac253a343f403e1bbfb620ae571a (patch)
tree33e61ba78df6d84bb1ef298741eefc6b75eda0af
parent80e4e5663fac23c4f57f58264caff6aa04a27713 (diff)
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.
-rw-r--r--toxcore/Messenger.c11
-rw-r--r--toxcore/net_crypto.c30
-rw-r--r--toxcore/net_crypto.h7
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)
2200 } 2200 }
2201 2201
2202 friend_new_connection(m, i, m->friendlist[i].client_id); 2202 friend_new_connection(m, i, m->friendlist[i].client_id);
2203 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; 2203 }
2204 2204
2205 if (m->friendlist[i].crypt_connection_id != -1) {
2206 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES];
2205 uint64_t timestamp = onion_getfriend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key); 2207 uint64_t timestamp = onion_getfriend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key);
2208
2206 if (timestamp != 0) { 2209 if (timestamp != 0) {
2207 set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key, timestamp); 2210 set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key, timestamp);
2208 } 2211 }
2209 2212
2213 timestamp = get_connection_dht_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key);
2214
2215 if (timestamp != 0) {
2216 onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key, timestamp);
2217 }
2218
2210 uint8_t direct_connected; 2219 uint8_t direct_connected;
2211 unsigned int status = crypto_connection_status(m->net_crypto, m->friendlist[i].crypt_connection_id, &direct_connected); 2220 unsigned int status = crypto_connection_status(m->net_crypto, m->friendlist[i].crypt_connection_id, &direct_connected);
2212 2221
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,
408 408
409 //TODO: on bad networks, direct connections might not last indefinitely. 409 //TODO: on bad networks, direct connections might not last indefinitely.
410 if (conn->ip_port.ip.family != 0) { 410 if (conn->ip_port.ip.family != 0) {
411 if ((uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length) 411 uint8_t direct_connected = 0;
412 crypto_connection_status(c, crypt_connection_id, &direct_connected);
413
414 if (direct_connected && (uint32_t)sendpacket(c->dht->net, conn->ip_port, data, length) == length)
412 return 0; 415 return 0;
416
417 if (length < 96
418 || data[0] == NET_PACKET_CRYPTO_HS) //TODO: a better way of sending packets directly to confirm the others ip.
419 sendpacket(c->dht->net, conn->ip_port, data, length);
420
413 } 421 }
414 422
415 //TODO: spread packets over many relays, detect and kill bad relays. 423 //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)
1488 return 0; 1496 return 0;
1489} 1497}
1490 1498
1499/* Copy friends DHT public key into dht_key.
1500 *
1501 * return 0 on failure (no key copied).
1502 * return timestamp on success (key copied).
1503 */
1504uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key)
1505{
1506 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1507
1508 if (conn == 0)
1509 return 0;
1510
1511 if (conn->dht_public_key_set == 0)
1512 return 0;
1513
1514 memcpy(dht_public_key, conn->dht_public_key, crypto_box_PUBLICKEYBYTES);
1515 return conn->dht_public_key_timestamp;
1516}
1517
1518
1491/* Set the DHT public key of the crypto connection. 1519/* Set the DHT public key of the crypto connection.
1492 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to 1520 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
1493 * the other peer. 1521 * 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);
186 */ 186 */
187int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key); 187int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key);
188 188
189/* Copy friends DHT public key into dht_key.
190 *
191 * return 0 on failure (no key copied).
192 * return timestamp on success (key copied).
193 */
194uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key);
195
189/* Set the DHT public key of the crypto connection. 196/* Set the DHT public key of the crypto connection.
190 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to 197 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
191 * the other peer. 198 * the other peer.