summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/tox_test.c95
-rw-r--r--other/apidsl/tox.in.h5
-rw-r--r--toxcore/Messenger.c29
-rw-r--r--toxcore/Messenger.h3
-rw-r--r--toxcore/tox.c13
-rw-r--r--toxcore/tox.h6
6 files changed, 148 insertions, 3 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c
index 0dab2e69..2e2d4538 100644
--- a/auto_tests/tox_test.c
+++ b/auto_tests/tox_test.c
@@ -809,6 +809,100 @@ loop_top:
809} 809}
810END_TEST 810END_TEST
811 811
812#define TCP_RELAY_PORT 33445
813
814START_TEST(test_many_clients_tcp)
815{
816 long long unsigned int cur_time = time(NULL);
817 Tox *toxes[NUM_TOXES];
818 uint32_t i, j;
819 uint32_t to_comp = 974536;
820
821 for (i = 0; i < NUM_TOXES; ++i) {
822 struct Tox_Options opts;
823 tox_options_default(&opts);
824
825 if (i == 0) {
826 opts.tcp_port = TCP_RELAY_PORT;
827 } else {
828 opts.udp_enabled = 0;
829 }
830
831 toxes[i] = tox_new(&opts, 0, 0, 0);
832 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
833 tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp);
834 uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
835 tox_self_get_dht_id(toxes[0], dpk);
836 tox_add_tcp_relay(toxes[i], "::1", TCP_RELAY_PORT, dpk, 0);
837 tox_bootstrap(toxes[i], "::1", 33445, dpk, 0);
838 }
839
840 {
841 TOX_ERR_GET_PORT error;
842 ck_assert_msg(tox_self_get_udp_port(toxes[0], &error) == 33445, "First Tox instance did not bind to udp port 33445.\n");
843 ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error");
844 ck_assert_msg(tox_self_get_tcp_port(toxes[0], &error) == TCP_RELAY_PORT,
845 "First Tox instance did not bind to tcp port %u.\n", TCP_RELAY_PORT);
846 ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error");
847 }
848
849 struct {
850 uint16_t tox1;
851 uint16_t tox2;
852 } pairs[NUM_FRIENDS];
853
854 uint8_t address[TOX_ADDRESS_SIZE];
855
856 for (i = 0; i < NUM_FRIENDS; ++i) {
857loop_top:
858 pairs[i].tox1 = rand() % NUM_TOXES;
859 pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES;
860
861 for (j = 0; j < i; ++j) {
862 if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2)
863 goto loop_top;
864 }
865
866 tox_self_get_address(toxes[pairs[i].tox1], address);
867
868 TOX_ERR_FRIEND_ADD test;
869 uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (uint8_t *)"Gentoo", 7, &test);
870
871 if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) {
872 goto loop_top;
873 }
874
875 ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test);
876 }
877
878 while (1) {
879 uint16_t counter = 0;
880
881 for (i = 0; i < NUM_TOXES; ++i) {
882 for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j)
883 if (tox_friend_get_connection_status(toxes[i], j, 0) == TOX_CONNECTION_TCP)
884 ++counter;
885 }
886
887 if (counter == NUM_FRIENDS * 2) {
888 break;
889 }
890
891 for (i = 0; i < NUM_TOXES; ++i) {
892 tox_iterate(toxes[i]);
893 }
894
895 c_sleep(50);
896 }
897
898 for (i = 0; i < NUM_TOXES; ++i) {
899 tox_kill(toxes[i]);
900 }
901
902 printf("test_many_clients_tcp succeeded, took %llu seconds\n", time(NULL) - cur_time);
903}
904END_TEST
905
812#define NUM_GROUP_TOX 32 906#define NUM_GROUP_TOX 32
813 907
814void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) 908void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
@@ -996,6 +1090,7 @@ Suite *tox_suite(void)
996 DEFTESTCASE(one); 1090 DEFTESTCASE(one);
997 DEFTESTCASE_SLOW(few_clients, 50); 1091 DEFTESTCASE_SLOW(few_clients, 50);
998 DEFTESTCASE_SLOW(many_clients, 150); 1092 DEFTESTCASE_SLOW(many_clients, 150);
1093 DEFTESTCASE_SLOW(many_clients_tcp, 150);
999 DEFTESTCASE_SLOW(many_group, 100); 1094 DEFTESTCASE_SLOW(many_group, 100);
1000 return s; 1095 return s;
1001} 1096}
diff --git a/other/apidsl/tox.in.h b/other/apidsl/tox.in.h
index 84a4a03e..dd47df23 100644
--- a/other/apidsl/tox.in.h
+++ b/other/apidsl/tox.in.h
@@ -411,6 +411,11 @@ static class options {
411 * The end port of the inclusive port range to attempt to use. 411 * The end port of the inclusive port range to attempt to use.
412 */ 412 */
413 uint16_t end_port; 413 uint16_t end_port;
414
415 /**
416 * The port to use for the TCP server. If 0, the tcp server is disabled.
417 */
418 uint16_t tcp_port;
414 } 419 }
415 420
416 421
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 5b7a7f61..359708ee 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1807,6 +1807,27 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
1807 return NULL; 1807 return NULL;
1808 } 1808 }
1809 1809
1810 if (options->tcp_server_port) {
1811 m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_public_key,
1812 m->dht->self_secret_key, m->onion);
1813
1814 if (m->tcp_server == NULL) {
1815 kill_friend_connections(m->fr_c);
1816 kill_onion(m->onion);
1817 kill_onion_announce(m->onion_a);
1818 kill_onion_client(m->onion_c);
1819 kill_DHT(m->dht);
1820 kill_net_crypto(m->net_crypto);
1821 kill_networking(m->net);
1822 free(m);
1823
1824 if (error)
1825 *error = MESSENGER_ERROR_TCP_SERVER;
1826
1827 return NULL;
1828 }
1829 }
1830
1810 m->options = *options; 1831 m->options = *options;
1811 friendreq_init(&(m->fr), m->fr_c); 1832 friendreq_init(&(m->fr), m->fr_c);
1812 set_nospam(&(m->fr), random_int()); 1833 set_nospam(&(m->fr), random_int());
@@ -1826,6 +1847,10 @@ void kill_messenger(Messenger *m)
1826 1847
1827 uint32_t i; 1848 uint32_t i;
1828 1849
1850 if (m->tcp_server) {
1851 kill_TCP_server(m->tcp_server);
1852 }
1853
1829 kill_friend_connections(m->fr_c); 1854 kill_friend_connections(m->fr_c);
1830 kill_onion(m->onion); 1855 kill_onion(m->onion);
1831 kill_onion_announce(m->onion_a); 1856 kill_onion_announce(m->onion_a);
@@ -2270,6 +2295,10 @@ void do_messenger(Messenger *m)
2270 do_DHT(m->dht); 2295 do_DHT(m->dht);
2271 } 2296 }
2272 2297
2298 if (m->tcp_server) {
2299 do_TCP_server(m->tcp_server);
2300 }
2301
2273 do_net_crypto(m->net_crypto); 2302 do_net_crypto(m->net_crypto);
2274 do_onion_client(m->onion_c); 2303 do_onion_client(m->onion_c);
2275 do_friend_connections(m->fr_c); 2304 do_friend_connections(m->fr_c);
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 8ab6b6ed..26704dd1 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -70,6 +70,7 @@ typedef struct {
70 uint8_t udp_disabled; 70 uint8_t udp_disabled;
71 TCP_Proxy_Info proxy_info; 71 TCP_Proxy_Info proxy_info;
72 uint16_t port_range[2]; 72 uint16_t port_range[2];
73 uint16_t tcp_server_port;
73} Messenger_Options; 74} Messenger_Options;
74 75
75 76
@@ -219,6 +220,7 @@ struct Messenger {
219 220
220 Friend_Connections *fr_c; 221 Friend_Connections *fr_c;
221 222
223 TCP_Server *tcp_server;
222 Friend_Requests fr; 224 Friend_Requests fr;
223 uint8_t name[MAX_NAME_LENGTH]; 225 uint8_t name[MAX_NAME_LENGTH];
224 uint16_t name_length; 226 uint16_t name_length;
@@ -727,6 +729,7 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
727enum { 729enum {
728 MESSENGER_ERROR_NONE, 730 MESSENGER_ERROR_NONE,
729 MESSENGER_ERROR_PORT, 731 MESSENGER_ERROR_PORT,
732 MESSENGER_ERROR_TCP_SERVER,
730 MESSENGER_ERROR_OTHER 733 MESSENGER_ERROR_OTHER
731}; 734};
732 735
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 848f81c7..92318cf9 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -152,6 +152,7 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng
152 m_options.udp_disabled = !options->udp_enabled; 152 m_options.udp_disabled = !options->udp_enabled;
153 m_options.port_range[0] = options->start_port; 153 m_options.port_range[0] = options->start_port;
154 m_options.port_range[1] = options->end_port; 154 m_options.port_range[1] = options->end_port;
155 m_options.tcp_server_port = options->tcp_port;
155 156
156 switch (options->proxy_type) { 157 switch (options->proxy_type) {
157 case TOX_PROXY_TYPE_HTTP: 158 case TOX_PROXY_TYPE_HTTP:
@@ -1205,9 +1206,15 @@ uint16_t tox_self_get_udp_port(const Tox *tox, TOX_ERR_GET_PORT *error)
1205 1206
1206uint16_t tox_self_get_tcp_port(const Tox *tox, TOX_ERR_GET_PORT *error) 1207uint16_t tox_self_get_tcp_port(const Tox *tox, TOX_ERR_GET_PORT *error)
1207{ 1208{
1208 /* TCP server not yet implemented in clients. */ 1209 const Messenger *m = tox;
1209 SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND); 1210
1210 return 0; 1211 if (m->tcp_server) {
1212 SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK);
1213 return m->options.tcp_server_port;
1214 } else {
1215 SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND);
1216 return 0;
1217 }
1211} 1218}
1212 1219
1213#include "tox_old_code.h" 1220#include "tox_old_code.h"
diff --git a/toxcore/tox.h b/toxcore/tox.h
index abd2f051..4afdf7c3 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -426,6 +426,12 @@ struct Tox_Options {
426 */ 426 */
427 uint16_t end_port; 427 uint16_t end_port;
428 428
429
430 /**
431 * The port to use for the TCP server. If 0, the tcp server is disabled.
432 */
433 uint16_t tcp_port;
434
429}; 435};
430 436
431 437