diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-08-17 10:52:04 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-08-18 00:01:53 +0100 |
commit | a5e35180c7b42d30c82825cd67c8118ce048f65a (patch) | |
tree | b74e8ece4f78a988ab0a1300cc08bd35fcd63662 /toxcore/net_crypto.c | |
parent | db22522741cccdeba657776844538c71cf8e8e7a (diff) |
Make tox_callback_friend_name stateless.
See #27 and #40 for details.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index f8a85adf..381b0a50 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -191,7 +191,8 @@ static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, ui | |||
191 | 191 | ||
192 | /* Handle the cookie request packet (for raw UDP) | 192 | /* Handle the cookie request packet (for raw UDP) |
193 | */ | 193 | */ |
194 | static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length) | 194 | static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, |
195 | void *userdata) | ||
195 | { | 196 | { |
196 | Net_Crypto *c = object; | 197 | Net_Crypto *c = object; |
197 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; | 198 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; |
@@ -1223,7 +1224,7 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id) | |||
1223 | * return 0 on success. | 1224 | * return 0 on success. |
1224 | */ | 1225 | */ |
1225 | static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, | 1226 | static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, |
1226 | _Bool udp) | 1227 | _Bool udp, void *userdata) |
1227 | { | 1228 | { |
1228 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) | 1229 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) |
1229 | return -1; | 1230 | return -1; |
@@ -1320,7 +1321,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1320 | 1321 | ||
1321 | if (conn->connection_data_callback) | 1322 | if (conn->connection_data_callback) |
1322 | conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, | 1323 | conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, |
1323 | dt.length); | 1324 | dt.length, userdata); |
1324 | 1325 | ||
1325 | /* conn might get killed in callback. */ | 1326 | /* conn might get killed in callback. */ |
1326 | conn = get_crypto_connection(c, crypt_connection_id); | 1327 | conn = get_crypto_connection(c, crypt_connection_id); |
@@ -1360,7 +1361,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1360 | * return 0 on success. | 1361 | * return 0 on success. |
1361 | */ | 1362 | */ |
1362 | static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, | 1363 | static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, |
1363 | _Bool udp) | 1364 | _Bool udp, void *userdata) |
1364 | { | 1365 | { |
1365 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1366 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) |
1366 | return -1; | 1367 | return -1; |
@@ -1425,7 +1426,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons | |||
1425 | 1426 | ||
1426 | case NET_PACKET_CRYPTO_DATA: { | 1427 | case NET_PACKET_CRYPTO_DATA: { |
1427 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { | 1428 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { |
1428 | return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp); | 1429 | return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp, userdata); |
1429 | } else { | 1430 | } else { |
1430 | return -1; | 1431 | return -1; |
1431 | } | 1432 | } |
@@ -1805,7 +1806,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1805 | } | 1806 | } |
1806 | 1807 | ||
1807 | 1808 | ||
1808 | static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length) | 1809 | static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length, void *userdata) |
1809 | { | 1810 | { |
1810 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1811 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) |
1811 | return -1; | 1812 | return -1; |
@@ -1822,7 +1823,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t | |||
1822 | } | 1823 | } |
1823 | 1824 | ||
1824 | pthread_mutex_unlock(&c->tcp_mutex); | 1825 | pthread_mutex_unlock(&c->tcp_mutex); |
1825 | int ret = handle_packet_connection(c, id, data, length, 0); | 1826 | int ret = handle_packet_connection(c, id, data, length, 0, userdata); |
1826 | pthread_mutex_lock(&c->tcp_mutex); | 1827 | pthread_mutex_lock(&c->tcp_mutex); |
1827 | 1828 | ||
1828 | if (ret != 0) | 1829 | if (ret != 0) |
@@ -1937,10 +1938,10 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u | |||
1937 | return ret; | 1938 | return ret; |
1938 | } | 1939 | } |
1939 | 1940 | ||
1940 | static void do_tcp(Net_Crypto *c) | 1941 | static void do_tcp(Net_Crypto *c, void *userdata) |
1941 | { | 1942 | { |
1942 | pthread_mutex_lock(&c->tcp_mutex); | 1943 | pthread_mutex_lock(&c->tcp_mutex); |
1943 | do_tcp_connections(c->tcp_c); | 1944 | do_tcp_connections(c->tcp_c, userdata); |
1944 | pthread_mutex_unlock(&c->tcp_mutex); | 1945 | pthread_mutex_unlock(&c->tcp_mutex); |
1945 | 1946 | ||
1946 | uint32_t i; | 1947 | uint32_t i; |
@@ -2001,7 +2002,7 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | |||
2001 | * return 0 on success. | 2002 | * return 0 on success. |
2002 | */ | 2003 | */ |
2003 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 2004 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, |
2004 | int id, uint8_t *data, uint16_t length), void *object, int id) | 2005 | int id, uint8_t *data, uint16_t length, void *userdata), void *object, int id) |
2005 | { | 2006 | { |
2006 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2007 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2007 | 2008 | ||
@@ -2081,7 +2082,7 @@ static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port) | |||
2081 | * Crypto data packets. | 2082 | * Crypto data packets. |
2082 | * | 2083 | * |
2083 | */ | 2084 | */ |
2084 | static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet, uint16_t length) | 2085 | static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
2085 | { | 2086 | { |
2086 | if (length <= CRYPTO_MIN_PACKET_SIZE || length > MAX_CRYPTO_PACKET_SIZE) | 2087 | if (length <= CRYPTO_MIN_PACKET_SIZE || length > MAX_CRYPTO_PACKET_SIZE) |
2087 | return 1; | 2088 | return 1; |
@@ -2099,7 +2100,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet | |||
2099 | return 0; | 2100 | return 0; |
2100 | } | 2101 | } |
2101 | 2102 | ||
2102 | if (handle_packet_connection(c, crypt_connection_id, packet, length, 1) != 0) | 2103 | if (handle_packet_connection(c, crypt_connection_id, packet, length, 1, userdata) != 0) |
2103 | return 1; | 2104 | return 1; |
2104 | 2105 | ||
2105 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2106 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
@@ -2685,11 +2686,11 @@ uint32_t crypto_run_interval(const Net_Crypto *c) | |||
2685 | } | 2686 | } |
2686 | 2687 | ||
2687 | /* Main loop. */ | 2688 | /* Main loop. */ |
2688 | void do_net_crypto(Net_Crypto *c) | 2689 | void do_net_crypto(Net_Crypto *c, void *userdata) |
2689 | { | 2690 | { |
2690 | unix_time_update(); | 2691 | unix_time_update(); |
2691 | kill_timedout(c); | 2692 | kill_timedout(c); |
2692 | do_tcp(c); | 2693 | do_tcp(c, userdata); |
2693 | send_crypto_packets(c); | 2694 | send_crypto_packets(c); |
2694 | } | 2695 | } |
2695 | 2696 | ||