From ef78169842514d6f8507caebb7f791b9227812bf Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 14 Aug 2014 18:34:20 -0400 Subject: [PATCH] Added disabling of UDP and basic SOCKS5 proxy support to public API. tox_new() now takes a Tox_Options struct as argument. If a NULL pointer is passed to that struct, defaults are used. --- auto_tests/tox_test.c | 8 ++++---- testing/nTox.c | 4 ++-- testing/tox_sync.c | 2 +- toxcore/onion_client.c | 3 +++ toxcore/tox.c | 28 ++++++++++++++++++++++++---- toxcore/tox.h | 35 +++++++++++++++++++++++++++-------- 6 files changed, 61 insertions(+), 19 deletions(-) diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index f765fcd3..04dd3c22 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -124,9 +124,9 @@ void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *dat START_TEST(test_few_clients) { long long unsigned int con_time, cur_time = time(NULL); - Tox *tox1 = tox_new(TOX_ENABLE_IPV6_DEFAULT); - Tox *tox2 = tox_new(TOX_ENABLE_IPV6_DEFAULT); - Tox *tox3 = tox_new(TOX_ENABLE_IPV6_DEFAULT); + Tox *tox1 = tox_new(0); + Tox *tox2 = tox_new(0); + Tox *tox3 = tox_new(0); ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); uint32_t to_compare = 974536; tox_callback_friend_request(tox2, accept_friend_request, &to_compare); @@ -302,7 +302,7 @@ START_TEST(test_many_clients) uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES; ++i) { - toxes[i] = tox_new(TOX_ENABLE_IPV6_DEFAULT); + toxes[i] = tox_new(0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); } diff --git a/testing/nTox.c b/testing/nTox.c index 971a2571..53cbbb5d 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1225,7 +1225,7 @@ int main(int argc, char *argv[]) if (!strcmp(argv[argc - 2], "-f")) filename = argv[argc - 1]; - m = tox_new(ipv6enabled); + m = tox_new(0); if ( !m ) { fputs("Failed to allocate Messenger datastructure", stderr); @@ -1258,7 +1258,6 @@ int main(int argc, char *argv[]) uint16_t port = htons(atoi(argv[argvoffset + 2])); unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); int res = tox_bootstrap_from_address(m, argv[argvoffset + 1], ipv6enabled, port, binary_string); - free(binary_string); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); @@ -1319,6 +1318,7 @@ int main(int argc, char *argv[]) } } + free(binary_string); tox_kill(m); endwin(); return 0; diff --git a/testing/tox_sync.c b/testing/tox_sync.c index 523f2c56..206e613f 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c @@ -214,7 +214,7 @@ int main(int argc, char *argv[]) exit(0); } - Tox *tox = tox_new(ipv6enabled); + Tox *tox = tox_new(0); tox_callback_file_data(tox, write_file, NULL); tox_callback_file_control(tox, file_print_control, NULL); tox_callback_file_send_request(tox, file_request_accept, NULL); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 424711d5..24f41e6f 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -40,6 +40,9 @@ */ int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id) { + if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) + return -1; + unsigned int i; for (i = 0; i < MAX_PATH_NODES; ++i) { diff --git a/toxcore/tox.c b/toxcore/tox.c index d352f672..4b9cff8e 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -815,12 +815,32 @@ uint32_t tox_do_interval(Tox *tox) * return allocated instance of tox on success. * return 0 if there are problems. */ -Tox *tox_new(uint8_t ipv6enabled) +Tox *tox_new(Tox_Options *options) { LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL); - Messenger_Options options = {0}; - options.ipv6enabled = ipv6enabled; - return new_messenger(&options); + Messenger_Options m_options = {0}; + + if (options == NULL) { + m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + } else { + m_options.ipv6enabled = options->ipv6enabled; + m_options.udp_disabled = options->udp_disabled; + m_options.proxy_enabled = options->proxy_enabled; + + if (m_options.proxy_enabled) { + ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); + + if (m_options.ipv6enabled) + m_options.proxy_info.ip_port.ip.family = AF_UNSPEC; + + if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL)) + return NULL; + + m_options.proxy_info.ip_port.port = htons(options->proxy_port); + } + } + + return new_messenger(&m_options); } /* Run this before closing shop. diff --git a/toxcore/tox.h b/toxcore/tox.h index 8caa01e0..6cad6b10 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -622,20 +622,39 @@ int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enable */ int tox_isconnected(const Tox *tox); +typedef struct { + /* + * The type of UDP socket created depends on ipv6enabled: + * If set to 0 (zero), creates an IPv4 socket which subsequently only allows + * IPv4 communication + * If set to anything else (default), creates an IPv6 socket which allows both IPv4 AND + * IPv6 communication + */ + uint8_t ipv6enabled; + + /* Set to 1 to disable udp support. (default: 0) + This will force Tox to use TCP only which may slow things down. + Disabling udp support is necessary when using anonymous proxies or Tor.*/ + uint8_t udp_disabled; + + /* Enable proxy support. (only basic TCP socks5 proxy currently supported.) (default: 0 (disabled))*/ + uint8_t proxy_enabled; + char proxy_address[256]; /* Proxy ip or domain in NULL terminated string format. */ + uint16_t proxy_port; /* Proxy port: in host byte order. */ +} Tox_Options; + /* * Run this function at startup. * - * Initializes a tox structure - * The type of communication socket depends on ipv6enabled: - * If set to 0 (zero), creates an IPv4 socket which subsequently only allows - * IPv4 communication - * If set to anything else, creates an IPv6 socket which allows both IPv4 AND - * IPv6 communication + * Options are some options that can be passed to the Tox instance (see above struct). * + * If options is NULL, tox_new() will use default settings. + * + * Initializes a tox structure * return allocated instance of tox on success. - * return 0 if there are problems. + * return NULL on failure. */ -Tox *tox_new(uint8_t ipv6enabled); +Tox *tox_new(Tox_Options *options); /* Run this before closing shop. * Free all datastructures. */