summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorGregory Mullen (grayhatter) <greg@grayhatter.com>2016-08-22 14:44:58 -0700
committerGregory Mullen (grayhatter) <greg@grayhatter.com>2016-09-06 02:22:04 -0700
commitaad1e0ad3f96786e0fb10d8dd144e5e6ebe93258 (patch)
tree963477c57148626140286ac278369ef3af60811f /toxcore/net_crypto.c
parente7d3a1a665d1204d15b00fdbe6716b43d8ef3b4a (diff)
Make friend requests stateless
Messenger is slightly twisty when it comes to sending connection status callbacks It will very likely need at the very least a partial refactor to clean it up a bit. Toxcore shouldn't need void *userdata as deep as is currently does. (amend 1) Because of the nature of toxcore connection callbacks, I decided to change this commit from statelessness for connections changes to statelessness for friend requests. It's simpler this was and doesn't include doing anything foolish in the time between commits. group fixup because grayhatter doesn't want to do it "arguably correct" is not how you write security sensitive code Clear a compiler warning about types within a function.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 940feb04..0d8fabac 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1297,7 +1297,7 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id)
1297 &kill_packet, sizeof(kill_packet)); 1297 &kill_packet, sizeof(kill_packet));
1298} 1298}
1299 1299
1300static void connection_kill(Net_Crypto *c, int crypt_connection_id) 1300static void connection_kill(Net_Crypto *c, int crypt_connection_id, void *userdata)
1301{ 1301{
1302 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1302 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1303 1303
@@ -1306,7 +1306,8 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id)
1306 } 1306 }
1307 1307
1308 if (conn->connection_status_callback) { 1308 if (conn->connection_status_callback) {
1309 conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 0); 1309 conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 0,
1310 userdata);
1310 } 1311 }
1311 1312
1312 crypto_kill(c, crypt_connection_id); 1313 crypto_kill(c, crypt_connection_id);
@@ -1370,7 +1371,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1370 } 1371 }
1371 1372
1372 if (real_data[0] == PACKET_ID_KILL) { 1373 if (real_data[0] == PACKET_ID_KILL) {
1373 connection_kill(c, crypt_connection_id); 1374 connection_kill(c, crypt_connection_id, userdata);
1374 return 0; 1375 return 0;
1375 } 1376 }
1376 1377
@@ -1379,7 +1380,8 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1379 conn->status = CRYPTO_CONN_ESTABLISHED; 1380 conn->status = CRYPTO_CONN_ESTABLISHED;
1380 1381
1381 if (conn->connection_status_callback) { 1382 if (conn->connection_status_callback) {
1382 conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); 1383 conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1,
1384 userdata);
1383 } 1385 }
1384 } 1386 }
1385 1387
@@ -1525,7 +1527,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons
1525 conn->status = CRYPTO_CONN_NOT_CONFIRMED; 1527 conn->status = CRYPTO_CONN_NOT_CONFIRMED;
1526 } else { 1528 } else {
1527 if (conn->dht_pk_callback) { 1529 if (conn->dht_pk_callback) {
1528 conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key); 1530 conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata);
1529 } 1531 }
1530 } 1532 }
1531 } else { 1533 } else {
@@ -1729,7 +1731,8 @@ void new_connection_handler(Net_Crypto *c, int (*new_connection_callback)(void *
1729 * return -1 on failure. 1731 * return -1 on failure.
1730 * return 0 on success. 1732 * return 0 on success.
1731 */ 1733 */
1732static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const uint8_t *data, uint16_t length) 1734static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const uint8_t *data, uint16_t length,
1735 void *userdata)
1733{ 1736{
1734 New_Connection n_c; 1737 New_Connection n_c;
1735 n_c.cookie = malloc(COOKIE_LENGTH); 1738 n_c.cookie = malloc(COOKIE_LENGTH);
@@ -1753,7 +1756,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const
1753 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1756 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1754 1757
1755 if (public_key_cmp(n_c.dht_public_key, conn->dht_public_key) != 0) { 1758 if (public_key_cmp(n_c.dht_public_key, conn->dht_public_key) != 0) {
1756 connection_kill(c, crypt_connection_id); 1759 connection_kill(c, crypt_connection_id, userdata);
1757 } else { 1760 } else {
1758 int ret = -1; 1761 int ret = -1;
1759 1762
@@ -1966,7 +1969,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
1966} 1969}
1967 1970
1968static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned int tcp_connections_number, 1971static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned int tcp_connections_number,
1969 const uint8_t *data, uint16_t length) 1972 const uint8_t *data, uint16_t length, void *userdata)
1970{ 1973{
1971 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) { 1974 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) {
1972 return -1; 1975 return -1;
@@ -1984,7 +1987,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in
1984 source.ip.family = TCP_FAMILY; 1987 source.ip.family = TCP_FAMILY;
1985 source.ip.ip6.uint32[0] = tcp_connections_number; 1988 source.ip.ip6.uint32[0] = tcp_connections_number;
1986 1989
1987 if (handle_new_connection_handshake(c, source, data, length) != 0) { 1990 if (handle_new_connection_handshake(c, source, data, length, userdata) != 0) {
1988 return -1; 1991 return -1;
1989 } 1992 }
1990 1993
@@ -2119,7 +2122,7 @@ static void do_tcp(Net_Crypto *c, void *userdata)
2119 * return 0 on success. 2122 * return 0 on success.
2120 */ 2123 */
2121int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, 2124int connection_status_handler(const Net_Crypto *c, int crypt_connection_id,
2122 int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id) 2125 int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata), void *object, int id)
2123{ 2126{
2124 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2127 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2125 2128
@@ -2191,7 +2194,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
2191 * return 0 on success. 2194 * return 0 on success.
2192 */ 2195 */
2193int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, 2196int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number,
2194 const uint8_t *dht_public_key), void *object, uint32_t number) 2197 const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number)
2195{ 2198{
2196 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2199 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2197 2200
@@ -2239,7 +2242,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2239 return 1; 2242 return 1;
2240 } 2243 }
2241 2244
2242 if (handle_new_connection_handshake(c, source, packet, length) != 0) { 2245 if (handle_new_connection_handshake(c, source, packet, length, userdata) != 0) {
2243 return 1; 2246 return 1;
2244 } 2247 }
2245 2248
@@ -2822,7 +2825,7 @@ Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info)
2822 return temp; 2825 return temp;
2823} 2826}
2824 2827
2825static void kill_timedout(Net_Crypto *c) 2828static void kill_timedout(Net_Crypto *c, void *userdata)
2826{ 2829{
2827 uint32_t i; 2830 uint32_t i;
2828 //uint64_t temp_time = current_time_monotonic(); 2831 //uint64_t temp_time = current_time_monotonic();
@@ -2844,7 +2847,7 @@ static void kill_timedout(Net_Crypto *c)
2844 continue; 2847 continue;
2845 } 2848 }
2846 2849
2847 connection_kill(c, i); 2850 connection_kill(c, i, userdata);
2848 } 2851 }
2849 2852
2850#if 0 2853#if 0
@@ -2868,7 +2871,7 @@ uint32_t crypto_run_interval(const Net_Crypto *c)
2868void do_net_crypto(Net_Crypto *c, void *userdata) 2871void do_net_crypto(Net_Crypto *c, void *userdata)
2869{ 2872{
2870 unix_time_update(); 2873 unix_time_update();
2871 kill_timedout(c); 2874 kill_timedout(c, userdata);
2872 do_tcp(c, userdata); 2875 do_tcp(c, userdata);
2873 send_crypto_packets(c); 2876 send_crypto_packets(c);
2874} 2877}