summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-08-11 20:47:56 -0400
committerirungentoo <irungentoo@gmail.com>2014-08-11 20:47:56 -0400
commite6f21d0524a951f2193fbcbe94f6210df2dbfbe0 (patch)
tree73846b10172807d28943d02e444590e1be928c41
parent5a22fbf4bfe01990be0759fb29d8090cda1f8956 (diff)
Added functions to receive and send onion packets via TCP to net_crypto.c
-rw-r--r--toxcore/net_crypto.c40
-rw-r--r--toxcore/net_crypto.h15
2 files changed, 55 insertions, 0 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 8b5de632..98b84e14 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1794,6 +1794,17 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8
1794 return 0; 1794 return 0;
1795} 1795}
1796 1796
1797static int tcp_onion_callback(void *object, const uint8_t *data, uint16_t length)
1798{
1799 Net_Crypto *c = object;
1800
1801 if (c->tcp_onion_callback)
1802 return c->tcp_onion_callback(c->tcp_onion_callback_object, data, length);
1803
1804 return 1;
1805}
1806
1807
1797/* Check if tcp connection to public key can be created. 1808/* Check if tcp connection to public key can be created.
1798 * 1809 *
1799 * return -1 if it can't. 1810 * return -1 if it can't.
@@ -1913,6 +1924,34 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
1913 return -1; 1924 return -1;
1914} 1925}
1915 1926
1927/* Send an onion packet via a random connected TCP relay.
1928 *
1929 * return 0 on success.
1930 * return -1 on failure.
1931 */
1932int send_tcp_onion_request(const Net_Crypto *c, const uint8_t *data, uint16_t length)
1933{
1934 unsigned int i, r = rand();
1935
1936 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1937 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) {
1938 if (send_onion_request(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], data, length) == 1)
1939 return 0;
1940 }
1941 }
1942
1943 return -1;
1944}
1945
1946/* Set the function to be called when an onion response packet is recieved by one of the TCP connections.
1947 */
1948void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
1949 uint16_t length), void *object)
1950{
1951 c->tcp_onion_callback = tcp_onion_callback;
1952 c->tcp_onion_callback_object = object;
1953}
1954
1916/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 1955/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
1917 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 1956 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
1918 * 1957 *
@@ -1991,6 +2030,7 @@ static int add_tcp_connected(Net_Crypto *c, TCP_Client_Connection *tcp_con)
1991 routing_status_handler(tcp_con, tcp_status_callback, tcp_con); 2030 routing_status_handler(tcp_con, tcp_status_callback, tcp_con);
1992 routing_data_handler(tcp_con, tcp_data_callback, tcp_con); 2031 routing_data_handler(tcp_con, tcp_data_callback, tcp_con);
1993 oob_data_handler(tcp_con, tcp_oob_callback, tcp_con); 2032 oob_data_handler(tcp_con, tcp_oob_callback, tcp_con);
2033 onion_response_handler(tcp_con, tcp_onion_callback, c);
1994 c->tcp_connections[tcp_num] = tcp_con; 2034 c->tcp_connections[tcp_num] = tcp_con;
1995 return 0; 2035 return 0;
1996} 2036}
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 07e9ef6a..b8236a0e 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -198,6 +198,9 @@ typedef struct {
198 uint32_t current_sleep_time; 198 uint32_t current_sleep_time;
199 199
200 BS_LIST ip_port_list; 200 BS_LIST ip_port_list;
201
202 int (*tcp_onion_callback)(void *object, const uint8_t *data, uint16_t length);
203 void *tcp_onion_callback_object;
201} Net_Crypto; 204} Net_Crypto;
202 205
203 206
@@ -321,6 +324,18 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
321 */ 324 */
322int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key); 325int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
323 326
327/* Set the function to be called when an onion response packet is recieved by one of the TCP connections.
328 */
329void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
330 uint16_t length), void *object);
331
332/* Send an onion packet via a random connected TCP relay.
333 *
334 * return 0 on success.
335 * return -1 on failure.
336 */
337int send_tcp_onion_request(const Net_Crypto *c, const uint8_t *data, uint16_t length);
338
324/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 339/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
325 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 340 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
326 * 341 *