summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c55
1 files changed, 41 insertions, 14 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 48bf5164..63012ce6 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1238,7 +1238,11 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons
1238 1238
1239 conn->status = CRYPTO_CONN_NOT_CONFIRMED; 1239 conn->status = CRYPTO_CONN_NOT_CONFIRMED;
1240 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ 1240 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */
1241 set_connection_dht_public_key(c, crypt_connection_id, dht_public_key, current_time_monotonic()); 1241 set_connection_dht_public_key(c, crypt_connection_id, dht_public_key);
1242
1243 if (conn->dht_pk_callback)
1244 conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key);
1245
1242 } else { 1246 } else {
1243 return -1; 1247 return -1;
1244 } 1248 }
@@ -1473,7 +1477,11 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const
1473 if (create_send_handshake(c, crypt_connection_id, n_c.cookie, n_c.dht_public_key) == 0) { 1477 if (create_send_handshake(c, crypt_connection_id, n_c.cookie, n_c.dht_public_key) == 0) {
1474 conn->status = CRYPTO_CONN_NOT_CONFIRMED; 1478 conn->status = CRYPTO_CONN_NOT_CONFIRMED;
1475 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ 1479 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */
1476 set_connection_dht_public_key(c, crypt_connection_id, n_c.dht_public_key, current_time_monotonic()); 1480 set_connection_dht_public_key(c, crypt_connection_id, n_c.dht_public_key);
1481
1482 if (conn->dht_pk_callback)
1483 conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, n_c.dht_public_key);
1484
1477 ret = 0; 1485 ret = 0;
1478 } 1486 }
1479 } 1487 }
@@ -1522,7 +1530,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
1522 1530
1523 conn->status = CRYPTO_CONN_NOT_CONFIRMED; 1531 conn->status = CRYPTO_CONN_NOT_CONFIRMED;
1524 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ 1532 /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */
1525 set_connection_dht_public_key(c, crypt_connection_id, n_c->dht_public_key, current_time_monotonic()); 1533 set_connection_dht_public_key(c, crypt_connection_id, n_c->dht_public_key);
1526 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 1534 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
1527 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; 1535 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
1528 crypto_connection_add_source(c, crypt_connection_id, n_c->source); 1536 crypto_connection_add_source(c, crypt_connection_id, n_c->source);
@@ -1618,9 +1626,9 @@ static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1618/* Copy friends DHT public key into dht_key. 1626/* Copy friends DHT public key into dht_key.
1619 * 1627 *
1620 * return 0 on failure (no key copied). 1628 * return 0 on failure (no key copied).
1621 * return timestamp on success (key copied). 1629 * return 1 on success (key copied).
1622 */ 1630 */
1623uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) 1631unsigned int get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key)
1624{ 1632{
1625 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1633 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1626 1634
@@ -1631,28 +1639,22 @@ uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, ui
1631 return 0; 1639 return 0;
1632 1640
1633 memcpy(dht_public_key, conn->dht_public_key, crypto_box_PUBLICKEYBYTES); 1641 memcpy(dht_public_key, conn->dht_public_key, crypto_box_PUBLICKEYBYTES);
1634 return conn->dht_public_key_timestamp; 1642 return 1;
1635} 1643}
1636 1644
1637 1645
1638/* Set the DHT public key of the crypto connection. 1646/* Set the DHT public key of the crypto connection.
1639 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
1640 * the other peer.
1641 * 1647 *
1642 * return -1 on failure. 1648 * return -1 on failure.
1643 * return 0 on success. 1649 * return 0 on success.
1644 */ 1650 */
1645int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, 1651int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key)
1646 uint64_t timestamp)
1647{ 1652{
1648 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1653 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1649 1654
1650 if (conn == 0) 1655 if (conn == 0)
1651 return -1; 1656 return -1;
1652 1657
1653 if (timestamp <= conn->dht_public_key_timestamp)
1654 return -1;
1655
1656 if (conn->dht_public_key_set == 1 && memcmp(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) 1658 if (conn->dht_public_key_set == 1 && memcmp(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0)
1657 return -1; 1659 return -1;
1658 1660
@@ -1662,7 +1664,6 @@ int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const
1662 1664
1663 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); 1665 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES);
1664 conn->dht_public_key_set = 1; 1666 conn->dht_public_key_set = 1;
1665 conn->dht_public_key_timestamp = timestamp;
1666 1667
1667 if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) { 1668 if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) {
1668 conn->cookie_request_number = random_64b(); 1669 conn->cookie_request_number = random_64b();
@@ -1692,6 +1693,9 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port)
1692 if (conn == 0) 1693 if (conn == 0)
1693 return -1; 1694 return -1;
1694 1695
1696 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
1697 return -1;
1698
1695 if (!ipport_equal(&ip_port, &conn->ip_port)) { 1699 if (!ipport_equal(&ip_port, &conn->ip_port)) {
1696 if (bs_list_add(&c->ip_port_list, &ip_port, crypt_connection_id)) { 1700 if (bs_list_add(&c->ip_port_list, &ip_port, crypt_connection_id)) {
1697 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 1701 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id);
@@ -2243,6 +2247,29 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
2243 return 0; 2247 return 0;
2244} 2248}
2245 2249
2250
2251/* Set the function for this friend that will be callbacked with object and number
2252 * when that friend gives us his DHT temporary public key.
2253 *
2254 * object and number will be passed as argument to this function.
2255 *
2256 * return -1 on failure.
2257 * return 0 on success.
2258 */
2259int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number,
2260 const uint8_t *dht_public_key), void *object, uint32_t number)
2261{
2262 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2263
2264 if (conn == 0)
2265 return -1;
2266
2267 conn->dht_pk_callback = function;
2268 conn->dht_pk_callback_object = object;
2269 conn->dht_pk_callback_number = number;
2270 return 0;
2271}
2272
2246/* Get the crypto connection id from the ip_port. 2273/* Get the crypto connection id from the ip_port.
2247 * 2274 *
2248 * return -1 on failure. 2275 * return -1 on failure.