From 35602e12c007aec38f65c860cad5f03953c4596d Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 21 Dec 2014 20:59:50 -0500 Subject: [PATCH] Further refactoring of proxy code --- toxcore/TCP_client.c | 22 +++------------ toxcore/TCP_client.h | 10 ------- toxcore/tox.c | 64 +++++++++++--------------------------------- toxcore/tox.h | 15 +++-------- 4 files changed, 22 insertions(+), 89 deletions(-) diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 0b08283f..aee309ae 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -37,15 +37,8 @@ */ static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) { - switch (proxy_info->proxy_type) { - case TCP_PROXY_HTTP: - ip_port = ((TCP_Proxy_HTTP*)proxy_info->proxy)->ip_port; - break; - case TCP_PROXY_SOCKS5: - ip_port = ((TCP_Proxy_SOCKS5*)proxy_info->proxy)->ip_port; - break; - case TCP_PROXY_NONE: - break; + if (proxy_info->proxy_type != TCP_PROXY_NONE) { + ip_port =proxy_info->ip_port; } struct sockaddr_storage addr = {0}; @@ -613,15 +606,8 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public uint8_t family = ip_port.ip.family; - switch (proxy_info->proxy_type) { - case TCP_PROXY_HTTP: - family = ((TCP_Proxy_HTTP*)proxy_info->proxy)->ip_port.ip.family; - break; - case TCP_PROXY_SOCKS5: - family = ((TCP_Proxy_SOCKS5*)proxy_info->proxy)->ip_port.ip.family; - break; - case TCP_PROXY_NONE: - break; + if (proxy_info->proxy_type != TCP_PROXY_NONE) { + family = proxy_info->ip_port.ip.family; } sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 4c82e786..e37a4ee0 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -37,17 +37,7 @@ typedef enum { typedef struct { IP_Port ip_port; -} TCP_Proxy_HTTP; - -typedef struct { - IP_Port ip_port; -} TCP_Proxy_SOCKS5; - - - -typedef struct { uint8_t proxy_type; // a value from TCP_PROXY_TYPE - void* proxy; // pointer to the corresponding proxy type struct } TCP_Proxy_Info; enum { diff --git a/toxcore/tox.c b/toxcore/tox.c index 72817c80..b5b6a26b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1022,61 +1022,27 @@ Tox *tox_new(Tox_Options *options) m_options.udp_disabled = options->udp_disabled; switch (options->proxy_type) { - case TOX_PROXY_HTTP: { + case TOX_PROXY_HTTP: m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; - m_options.proxy_info.proxy = malloc(sizeof(TCP_Proxy_HTTP)); - if (!m_options.proxy_info.proxy) { - return NULL; - } - - TCP_Proxy_HTTP* m_proxy = (TCP_Proxy_HTTP*)m_options.proxy_info.proxy; - Tox_Proxy_HTTP* tox_proxy = (Tox_Proxy_HTTP*)options->proxy; - - ip_init(&m_proxy->ip_port.ip, m_options.ipv6enabled); - - if (m_options.ipv6enabled) { - m_proxy->ip_port.ip.family = AF_UNSPEC; - } - - if (!addr_resolve_or_parse_ip(tox_proxy->address, &m_proxy->ip_port.ip, NULL)) { - return NULL; - } - - m_proxy->ip_port.port = htons(tox_proxy->port); - break; - } - - case TOX_PROXY_SOCKS5: { + case TOX_PROXY_SOCKS5: m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5; - m_options.proxy_info.proxy = malloc(sizeof(TCP_Proxy_SOCKS5)); - if (!m_options.proxy_info.proxy) { - return NULL; - } - - TCP_Proxy_SOCKS5* m_proxy = (TCP_Proxy_SOCKS5*)m_options.proxy_info.proxy; - Tox_Proxy_SOCKS5* tox_proxy = (Tox_Proxy_SOCKS5*)options->proxy; - - ip_init(&m_proxy->ip_port.ip, m_options.ipv6enabled); - - if (m_options.ipv6enabled) { - m_proxy->ip_port.ip.family = AF_UNSPEC; - } - - if (!addr_resolve_or_parse_ip(tox_proxy->address, &m_proxy->ip_port.ip, NULL)) { - return NULL; - } - - m_proxy->ip_port.port = htons(tox_proxy->port); - break; - } - - case TOX_PROXY_NONE: { + case TOX_PROXY_NONE: m_options.proxy_info.proxy_type = TCP_PROXY_NONE; - break; - } + } + + if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { + 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); } } diff --git a/toxcore/tox.h b/toxcore/tox.h index 94e4d5c6..c5a5d952 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -879,16 +879,6 @@ typedef enum { TOX_PROXY_SOCKS5 } TOX_PROXY_TYPE; -typedef struct { - char address[256]; /* Proxy ip or domain in NULL terminated string format. */ - uint16_t port; /* Proxy port: in host byte order. */ -} Tox_Proxy_HTTP; - -typedef struct { - char address[256]; /* Proxy ip or domain in NULL terminated string format. */ - uint16_t port; /* Proxy port: in host byte order. */ -} Tox_Proxy_SOCKS5; - typedef struct { /* * The type of UDP socket created depends on ipv6enabled: @@ -903,8 +893,9 @@ typedef struct { This will force Tox to use TCP only which may slow things down. Disabling udp support is necessary when using proxies or Tor.*/ uint8_t udp_disabled; - uint8_t proxy_type; // a value from TOX_PROXY_TYPE - void* proxy; // pointer to the corresponding proxy type struct + uint8_t proxy_type; /* a value from TOX_PROXY_TYPE */ + 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; /*