From 2740099da010846fdb38883ae74cff31b014ca61 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 12 Jun 2014 11:15:20 -0400 Subject: [PATCH] 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. --- configure.ac | 16 +++++++--------- toxcore/Makefile.inc | 6 ++++-- toxcore/Messenger.c | 1 + toxcore/net_crypto.c | 11 +++++++++-- toxcore/net_crypto.h | 3 +++ 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/configure.ac b/configure.ac index e3b9a77b..0dd88da4 100644 --- a/configure.ac +++ b/configure.ac @@ -459,15 +459,13 @@ if (test "x$WIN32" != "xyes") && (test "x$MACH" != "xyes") && (test "x$DISABLE_R ) fi -if test "x$BUILD_AV" = "xyes"; then - AX_PTHREAD( - [], - [ - AC_MSG_WARN([disabling AV support: required pthread library not found]) - BUILD_AV="no" - ] - ) -fi + +AX_PTHREAD( + [], + [ + AC_MSG_ERROR([required library pthread was not found on your system]) + ] +) if test "x$BUILD_AV" = "xyes"; then PKG_CHECK_MODULES([OPUS], [opus], diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 3cfdd73b..8e39f96e 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -50,7 +50,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ libtoxcore_la_CFLAGS = -I$(top_srcdir) \ -I$(top_srcdir)/toxcore \ $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) + $(NACL_CFLAGS) \ + $(PTHREAD_CFLAGS) libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \ $(EXTRA_LT_LDFLAGS) \ @@ -62,4 +63,5 @@ libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \ libtoxcore_la_LIBADD = $(LIBSODIUM_LIBS) \ $(NACL_OBJECTS) \ - $(NAC_LIBS) + $(NAC_LIBS) \ + $(PTHREAD_LIBS) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 653a2ee1..05065e62 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2184,6 +2184,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) for (i = 0; i < n; i++) { add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id); } + break; } 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 if (conn == 0) return -1; + pthread_mutex_lock(&conn->mutex); uint8_t packet[1 + sizeof(uint16_t) + length + crypto_box_MACBYTES]; packet[0] = NET_PACKET_CRYPTO_DATA; memcpy(packet + 1, conn->sent_nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t)); int len = encrypt_data_symmetric(conn->shared_key, conn->sent_nonce, data, length, packet + 1 + sizeof(uint16_t)); - if (len + 1 + sizeof(uint16_t) != sizeof(packet)) + if (len + 1 + sizeof(uint16_t) != sizeof(packet)) { + pthread_mutex_unlock(&conn->mutex); return -1; + } increment_nonce(conn->sent_nonce); - return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); + int ret = send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); + pthread_mutex_unlock(&conn->mutex); + return ret; } /* 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) memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); int id = c->crypto_connections_length; + pthread_mutex_init(&c->crypto_connections[id].mutex, NULL); ++c->crypto_connections_length; return id; } @@ -1261,6 +1267,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) return -1; uint32_t i; + pthread_mutex_destroy(&c->crypto_connections[crypt_connection_id].mutex); memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); for (i = c->crypto_connections_length; i != 0; --i) { diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index b1108ae1..ee790f24 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -26,6 +26,7 @@ #include "DHT.h" #include "TCP_client.h" +#include #define CRYPTO_CONN_NO_CONNECTION 0 #define CRYPTO_CONN_COOKIE_REQUESTING 1 //send cookie request packets @@ -155,6 +156,8 @@ typedef struct { Node_format tcp_relays[MAX_TCP_RELAYS_PEER]; uint16_t num_tcp_relays; + + pthread_mutex_t mutex; } Crypto_Connection; typedef struct {