Further refactoring of proxy code

This commit is contained in:
Maxim Biro 2014-12-21 20:59:50 -05:00
parent e9bf38499e
commit 35602e12c0
4 changed files with 22 additions and 89 deletions

View File

@ -37,15 +37,8 @@
*/ */
static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info)
{ {
switch (proxy_info->proxy_type) { if (proxy_info->proxy_type != TCP_PROXY_NONE) {
case TCP_PROXY_HTTP: ip_port =proxy_info->ip_port;
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;
} }
struct sockaddr_storage addr = {0}; 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; uint8_t family = ip_port.ip.family;
switch (proxy_info->proxy_type) { if (proxy_info->proxy_type != TCP_PROXY_NONE) {
case TCP_PROXY_HTTP: family = proxy_info->ip_port.ip.family;
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;
} }
sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP);

View File

@ -37,17 +37,7 @@ typedef enum {
typedef struct { typedef struct {
IP_Port ip_port; 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 uint8_t proxy_type; // a value from TCP_PROXY_TYPE
void* proxy; // pointer to the corresponding proxy type struct
} TCP_Proxy_Info; } TCP_Proxy_Info;
enum { enum {

View File

@ -1022,61 +1022,27 @@ Tox *tox_new(Tox_Options *options)
m_options.udp_disabled = options->udp_disabled; m_options.udp_disabled = options->udp_disabled;
switch (options->proxy_type) { 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_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; break;
} case TOX_PROXY_SOCKS5:
case TOX_PROXY_SOCKS5: {
m_options.proxy_info.proxy_type = TCP_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; break;
} case TOX_PROXY_NONE:
case TOX_PROXY_NONE: {
m_options.proxy_info.proxy_type = TCP_PROXY_NONE; m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
break; 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);
} }
} }

View File

@ -879,16 +879,6 @@ typedef enum {
TOX_PROXY_SOCKS5 TOX_PROXY_SOCKS5
} TOX_PROXY_TYPE; } 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 { typedef struct {
/* /*
* The type of UDP socket created depends on ipv6enabled: * 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. This will force Tox to use TCP only which may slow things down.
Disabling udp support is necessary when using proxies or Tor.*/ Disabling udp support is necessary when using proxies or Tor.*/
uint8_t udp_disabled; uint8_t udp_disabled;
uint8_t proxy_type; // a value from TOX_PROXY_TYPE uint8_t proxy_type; /* a value from TOX_PROXY_TYPE */
void* proxy; // pointer to the corresponding proxy type struct 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; } Tox_Options;
/* /*