summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-06-12 11:15:20 -0400
committerirungentoo <irungentoo@gmail.com>2014-06-12 11:15:20 -0400
commit2740099da010846fdb38883ae74cff31b014ca61 (patch)
tree8c50566344c486d7e3d0ae2196cac123ec56e75f /toxcore/net_crypto.c
parent4f2674eb0f5b5d12177bb3638dbe60ceed0d5e57 (diff)
pthread is now a core dependency instead of just a toxav dependency.
Fixed possible thread bug with sending A/V packets. TODO: eventually make toxcore thread safe.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index bb38af26..78a6121c 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -728,16 +728,21 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat
728 if (conn == 0) 728 if (conn == 0)
729 return -1; 729 return -1;
730 730
731 pthread_mutex_lock(&conn->mutex);
731 uint8_t packet[1 + sizeof(uint16_t) + length + crypto_box_MACBYTES]; 732 uint8_t packet[1 + sizeof(uint16_t) + length + crypto_box_MACBYTES];
732 packet[0] = NET_PACKET_CRYPTO_DATA; 733 packet[0] = NET_PACKET_CRYPTO_DATA;
733 memcpy(packet + 1, conn->sent_nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t)); 734 memcpy(packet + 1, conn->sent_nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t));
734 int len = encrypt_data_symmetric(conn->shared_key, conn->sent_nonce, data, length, packet + 1 + sizeof(uint16_t)); 735 int len = encrypt_data_symmetric(conn->shared_key, conn->sent_nonce, data, length, packet + 1 + sizeof(uint16_t));
735 736
736 if (len + 1 + sizeof(uint16_t) != sizeof(packet)) 737 if (len + 1 + sizeof(uint16_t) != sizeof(packet)) {
738 pthread_mutex_unlock(&conn->mutex);
737 return -1; 739 return -1;
740 }
738 741
739 increment_nonce(conn->sent_nonce); 742 increment_nonce(conn->sent_nonce);
740 return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); 743 int ret = send_packet_to(c, crypt_connection_id, packet, sizeof(packet));
744 pthread_mutex_unlock(&conn->mutex);
745 return ret;
741} 746}
742 747
743/* Creates and sends a data packet with buffer_start and num to the peer using the fastest route. 748/* Creates and sends a data packet with buffer_start and num to the peer using the fastest route.
@@ -1246,6 +1251,7 @@ static int create_crypto_connection(Net_Crypto *c)
1246 1251
1247 memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); 1252 memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection));
1248 int id = c->crypto_connections_length; 1253 int id = c->crypto_connections_length;
1254 pthread_mutex_init(&c->crypto_connections[id].mutex, NULL);
1249 ++c->crypto_connections_length; 1255 ++c->crypto_connections_length;
1250 return id; 1256 return id;
1251} 1257}
@@ -1261,6 +1267,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id)
1261 return -1; 1267 return -1;
1262 1268
1263 uint32_t i; 1269 uint32_t i;
1270 pthread_mutex_destroy(&c->crypto_connections[crypt_connection_id].mutex);
1264 memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); 1271 memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection));
1265 1272
1266 for (i = c->crypto_connections_length; i != 0; --i) { 1273 for (i = c->crypto_connections_length; i != 0; --i) {