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.c54
1 files changed, 42 insertions, 12 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 7607015e..57ee20b8 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2443,7 +2443,7 @@ int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const ui
2443 * 2443 *
2444 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) 2444 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
2445 */ 2445 */
2446int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) 2446int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
2447{ 2447{
2448 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 2448 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
2449 return -1; 2449 return -1;
@@ -2454,13 +2454,24 @@ int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const u
2454 if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) 2454 if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
2455 return -1; 2455 return -1;
2456 2456
2457 pthread_mutex_lock(&c->connections_mutex);
2458 ++c->connection_use_counter;
2459 pthread_mutex_unlock(&c->connections_mutex);
2460
2457 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2461 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2458 2462
2459 if (conn == 0) 2463 int ret = -1;
2460 return -1;
2461 2464
2462 return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data, 2465 if (conn) {
2463 length); 2466 ret = send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data,
2467 length);
2468 }
2469
2470 pthread_mutex_lock(&c->connections_mutex);
2471 --c->connection_use_counter;
2472 pthread_mutex_unlock(&c->connections_mutex);
2473
2474 return ret;
2464} 2475}
2465 2476
2466/* Kill a crypto connection. 2477/* Kill a crypto connection.
@@ -2470,17 +2481,32 @@ int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const u
2470 */ 2481 */
2471int crypto_kill(Net_Crypto *c, int crypt_connection_id) 2482int crypto_kill(Net_Crypto *c, int crypt_connection_id)
2472{ 2483{
2484 while (1) { /* TODO: is this really the best way to do this? */
2485 pthread_mutex_lock(&c->connections_mutex);
2486
2487 if (!c->connection_use_counter) {
2488 break;
2489 }
2490
2491 pthread_mutex_unlock(&c->connections_mutex);
2492 }
2493
2473 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2494 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2474 2495
2475 if (conn == 0) 2496 int ret = -1;
2476 return -1;
2477 2497
2478 if (conn->status == CRYPTO_CONN_ESTABLISHED) 2498 if (conn) {
2479 send_kill_packet(c, crypt_connection_id); 2499 if (conn->status == CRYPTO_CONN_ESTABLISHED)
2500 send_kill_packet(c, crypt_connection_id);
2480 2501
2481 disconnect_peer_tcp(c, crypt_connection_id); 2502 disconnect_peer_tcp(c, crypt_connection_id);
2482 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 2503 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id);
2483 return wipe_crypto_connection(c, crypt_connection_id); 2504 ret = wipe_crypto_connection(c, crypt_connection_id);
2505 }
2506
2507 pthread_mutex_unlock(&c->connections_mutex);
2508
2509 return ret;
2484} 2510}
2485 2511
2486/* return one of CRYPTO_CONN_* values indicating the state of the connection. 2512/* return one of CRYPTO_CONN_* values indicating the state of the connection.
@@ -2553,6 +2579,8 @@ Net_Crypto *new_net_crypto(DHT *dht)
2553 networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); 2579 networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp);
2554 2580
2555 bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8); 2581 bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8);
2582
2583 pthread_mutex_init(&temp->connections_mutex, NULL);
2556 return temp; 2584 return temp;
2557} 2585}
2558 2586
@@ -2626,6 +2654,8 @@ void kill_net_crypto(Net_Crypto *c)
2626 kill_TCP_connection(c->tcp_connections[i]); 2654 kill_TCP_connection(c->tcp_connections[i]);
2627 } 2655 }
2628 2656
2657 pthread_mutex_destroy(&c->connections_mutex);
2658
2629 bs_list_free(&c->ip_port_list); 2659 bs_list_free(&c->ip_port_list);
2630 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL); 2660 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL);
2631 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); 2661 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL);