From 377da127a1e19f96f75da8b4c59eee6a947cab3b Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 13 Dec 2014 23:44:51 -0500 Subject: Added HTTP proxy support --- INSTALL | 4 +- toxcore/TCP_client.c | 101 +++++++++++++++++++++++++++++++++++++++++++++------ toxcore/TCP_client.h | 6 ++- toxcore/network.c | 35 ++++++++++++++++++ toxcore/network.h | 20 ++++++++++ toxcore/tox.c | 1 + toxcore/tox.h | 1 + 7 files changed, 153 insertions(+), 15 deletions(-) diff --git a/INSTALL b/INSTALL index 007e9396..20998407 100644 --- a/INSTALL +++ b/INSTALL @@ -12,8 +12,8 @@ without warranty of any kind. Basic Installation ================== - Briefly, the shell commands `./configure; make; make install' should -configure, build, and install this package. The following + Briefly, the shell command `./configure && make && make install' +should configure, build, and install this package. The following more-detailed instructions are generic; see the `README' file for instructions specific to this package. Some packages provide this `INSTALL' file but do not implement all of the features documented diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 23ab28c0..dfb4005c 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -66,8 +66,67 @@ static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_i return 1; } +/* return 1 on success. + * return 0 on failure. + */ +static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_conn) +{ + char one[] = "CONNECT "; + char two[] = " HTTP/1.1\nHost: "; + char three[] = "\r\n\r\n"; + + char ip[INET6_ADDRSTRLEN]; + if (!ip_parse_addr(&TCP_conn->ip_port.ip, ip, sizeof(ip))) { + return 0; + } + const uint16_t port = ntohs(TCP_conn->ip_port.port); + const int written = sprintf(TCP_conn->last_packet, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, ip, port, three); + if (written < 0) { + return 0; + } + + TCP_conn->last_packet_length = written; + TCP_conn->last_packet_sent = 0; + + return 1; +} -static void socks5_generate_handshake(TCP_Client_Connection *TCP_conn) +/* return 1 on success. + * return 0 if no data received. + * return -1 on failure (connection refused). + */ +static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn) +{ + char success[] = "200"; + uint8_t data[16]; // draining works the best if the length is a power of 2 + + int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data) - 1); + + if (ret == -1) { + return 0; + } + + data[sizeof(data) - 1] = '\0'; + + if (strstr(data, success)) { + // drain all data + // instead of drainining it byte by byte do it in bigger chunks + // decrementing to 1 + size_t step = sizeof(data); + do { + if (ret <= 0) { + step = step % 2 == 0 ? step/2 : 1; + } + ret = read_TCP_packet(TCP_conn->sock, data, step); + } while (ret > 0 || step != 1); + + return 1; + } + + return -1; +} + +static void proxy_socks5_generate_handshake(TCP_Client_Connection *TCP_conn) { TCP_conn->last_packet[0] = 5; /* SOCKSv5 */ TCP_conn->last_packet[1] = 1; /* number of authentication methods supported */ @@ -95,7 +154,7 @@ static int socks5_read_handshake_response(TCP_Client_Connection *TCP_conn) return -1; } -static void socks5_generate_connection_request(TCP_Client_Connection *TCP_conn) +static void proxy_socks5_generate_connection_request(TCP_Client_Connection *TCP_conn) { TCP_conn->last_packet[0] = 5; /* SOCKSv5 */ TCP_conn->last_packet[1] = 1; /* command code: establish a TCP/IP stream connection */ @@ -125,7 +184,7 @@ static void socks5_generate_connection_request(TCP_Client_Connection *TCP_conn) * return 0 if no data received. * return -1 on failure (connection refused). */ -static int socks5_read_connection_response(TCP_Client_Connection *TCP_conn) +static int proxy_socks5_read_connection_response(TCP_Client_Connection *TCP_conn) { if (TCP_conn->ip_port.ip.family == AF_INET) { uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; @@ -578,10 +637,14 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public encrypt_precompute(temp->public_key, self_secret_key, temp->shared_key); temp->ip_port = ip_port; - if (proxy_info) { - temp->status = TCP_CLIENT_PROXY_CONNECTING; + if (proxy_info && proxy_info->is_http) { + temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING; + temp->proxy_info = *proxy_info; + proxy_http_generate_connection_request(temp); + } else if (proxy_info && !proxy_info->is_http) { + temp->status = TCP_CLIENT_PROXY_SOCKS5_CONNECTING; temp->proxy_info = *proxy_info; - socks5_generate_handshake(temp); + proxy_socks5_generate_handshake(temp); } else { temp->status = TCP_CLIENT_CONNECTING; @@ -783,7 +846,23 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection) return; } - if (TCP_connection->status == TCP_CLIENT_PROXY_CONNECTING) { + if (TCP_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { + if (send_pending_data(TCP_connection) == 0) { + int ret = proxy_http_read_connection_response(TCP_connection); + + if (ret == -1) { + TCP_connection->kill_at = 0; + TCP_connection->status = TCP_CLIENT_DISCONNECTED; + } + + if (ret == 1) { + generate_handshake(TCP_connection); + TCP_connection->status = TCP_CLIENT_CONNECTING; + } + } + } + + if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { if (send_pending_data(TCP_connection) == 0) { int ret = socks5_read_handshake_response(TCP_connection); @@ -793,15 +872,15 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection) } if (ret == 1) { - socks5_generate_connection_request(TCP_connection); - TCP_connection->status = TCP_CLIENT_PROXY_UNCONFIRMED; + proxy_socks5_generate_connection_request(TCP_connection); + TCP_connection->status = TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED; } } } - if (TCP_connection->status == TCP_CLIENT_PROXY_UNCONFIRMED) { + if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { if (send_pending_data(TCP_connection) == 0) { - int ret = socks5_read_connection_response(TCP_connection); + int ret = proxy_socks5_read_connection_response(TCP_connection); if (ret == -1) { TCP_connection->kill_at = 0; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 83cb48ba..8a1aeec1 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -31,12 +31,14 @@ typedef struct { IP_Port ip_port; + int is_http; /* HTTP proxy on true, SOCKS5 on false */ } TCP_Proxy_Info; enum { TCP_CLIENT_NO_STATUS, - TCP_CLIENT_PROXY_CONNECTING, - TCP_CLIENT_PROXY_UNCONFIRMED, + TCP_CLIENT_PROXY_HTTP_CONNECTING, + TCP_CLIENT_PROXY_SOCKS5_CONNECTING, + TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED, TCP_CLIENT_CONNECTING, TCP_CLIENT_UNCONFIRMED, TCP_CLIENT_CONFIRMED, diff --git a/toxcore/network.c b/toxcore/network.c index 35ef5221..5539de6b 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -784,6 +784,9 @@ void ipport_unpack(IP_Port *target, const uint8_t *data) /* ip_ntoa * converts ip into a string * uses a static buffer, so mustn't used multiple times in the same output + * + * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]" + * writes error message into the buffer on error */ /* there would be INET6_ADDRSTRLEN, but it might be too short for the error message */ static char addresstext[96]; @@ -815,6 +818,38 @@ const char *ip_ntoa(const IP *ip) return addresstext; } +/* + * ip_parse_addr + * parses IP structure into an address string + * + * input + * ip: ip of AF_INET or AF_INET6 families + * length: length of the address buffer + * Must be at least INET_ADDRSTRLEN for AF_INET + * and INET6_ADDRSTRLEN for AF_INET6 + * + * output + * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) + * + * returns 1 on success, 0 on failure + */ +int ip_parse_addr(const IP *ip, char *address, size_t length) +{ + if (!address || !ip) { + return 0; + } + + if (ip->family == AF_INET) { + struct in_addr *addr = (struct in_addr *)&ip->ip4; + return inet_ntop(ip->family, addr, address, length) != NULL; + } else if (ip->family == AF_INET6) { + struct in6_addr *addr = (struct in6_addr *)&ip->ip6; + return inet_ntop(ip->family, addr, address, length) != NULL; + } + + return 0; +} + /* * addr_parse_ip * directly parses the input into an IP structure diff --git a/toxcore/network.h b/toxcore/network.h index ccda7ae9..5f9af490 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -180,9 +180,29 @@ IP_Port; /* ip_ntoa * converts ip into a string * uses a static buffer, so mustn't used multiple times in the same output + * + * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]" + * writes error message into the buffer on error */ const char *ip_ntoa(const IP *ip); +/* + * ip_parse_addr + * parses IP structure into an address string + * + * input + * ip: ip of AF_INET or AF_INET6 families + * length: length of the address buffer + * Must be at least INET_ADDRSTRLEN for AF_INET + * and INET6_ADDRSTRLEN for AF_INET6 + * + * output + * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) + * + * returns 1 on success, 0 on failure + */ +int ip_parse_addr(const IP *ip, char *address, size_t length); + /* * addr_parse_ip * directly parses the input into an IP structure diff --git a/toxcore/tox.c b/toxcore/tox.c index 66587011..3f3a05b0 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1021,6 +1021,7 @@ Tox *tox_new(Tox_Options *options) m_options.ipv6enabled = options->ipv6enabled; m_options.udp_disabled = options->udp_disabled; m_options.proxy_enabled = options->proxy_enabled; + m_options.proxy_info.is_http = options->proxy_is_http; if (m_options.proxy_enabled) { ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); diff --git a/toxcore/tox.h b/toxcore/tox.h index 3b72eede..f2c102ab 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -892,6 +892,7 @@ typedef struct { 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. */ + uint8_t proxy_is_http; /*1: HTTP proxy, 0: SOCKS5 proxy*/ } Tox_Options; /* -- cgit v1.2.3 From e9bf38499e42eec2b01ef5024f0b7092539bc11c Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 21 Dec 2014 19:59:00 -0500 Subject: Some refactoring of proxy code --- toxcore/Messenger.c | 6 +---- toxcore/Messenger.h | 1 - toxcore/TCP_client.c | 59 ++++++++++++++++++++++++++++++++----------------- toxcore/TCP_client.h | 20 +++++++++++++++-- toxcore/net_crypto.c | 12 ++-------- toxcore/net_crypto.h | 1 - toxcore/tox.c | 62 ++++++++++++++++++++++++++++++++++++++++++++-------- toxcore/tox.h | 26 ++++++++++++++++------ 8 files changed, 132 insertions(+), 55 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 7d93b2da..465795d9 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1595,11 +1595,7 @@ Messenger *new_messenger(Messenger_Options *options) return NULL; } - if (options->proxy_enabled) { - m->net_crypto = new_net_crypto(m->dht, &options->proxy_info); - } else { - m->net_crypto = new_net_crypto(m->dht, 0); - } + m->net_crypto = new_net_crypto(m->dht, &options->proxy_info); if (m->net_crypto == NULL) { kill_networking(m->net); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index ae6f54c6..3851edf7 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -75,7 +75,6 @@ typedef struct { uint8_t ipv6enabled; uint8_t udp_disabled; - uint8_t proxy_enabled; TCP_Proxy_Info proxy_info; } Messenger_Options; diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index dfb4005c..0b08283f 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -37,8 +37,16 @@ */ static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) { - if (proxy_info) - ip_port = proxy_info->ip_port; + 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; + } struct sockaddr_storage addr = {0}; size_t addrsize; @@ -80,7 +88,7 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_con return 0; } const uint16_t port = ntohs(TCP_conn->ip_port.port); - const int written = sprintf(TCP_conn->last_packet, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, ip, port, three); + const int written = snprintf(TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, ip, port, three); if (written < 0) { return 0; } @@ -605,8 +613,16 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public uint8_t family = ip_port.ip.family; - if (proxy_info) - family = proxy_info->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; + } sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); @@ -636,23 +652,26 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public memcpy(temp->self_public_key, self_public_key, crypto_box_PUBLICKEYBYTES); encrypt_precompute(temp->public_key, self_secret_key, temp->shared_key); temp->ip_port = ip_port; + temp->proxy_info = *proxy_info; - if (proxy_info && proxy_info->is_http) { - temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING; - temp->proxy_info = *proxy_info; - proxy_http_generate_connection_request(temp); - } else if (proxy_info && !proxy_info->is_http) { - temp->status = TCP_CLIENT_PROXY_SOCKS5_CONNECTING; - temp->proxy_info = *proxy_info; - proxy_socks5_generate_handshake(temp); - } else { - temp->status = TCP_CLIENT_CONNECTING; + switch (proxy_info->proxy_type) { + case TCP_PROXY_HTTP: + temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING; + proxy_http_generate_connection_request(temp); + break; + case TCP_PROXY_SOCKS5: + temp->status = TCP_CLIENT_PROXY_SOCKS5_CONNECTING; + proxy_socks5_generate_handshake(temp); + break; + case TCP_PROXY_NONE: + temp->status = TCP_CLIENT_CONNECTING; - if (generate_handshake(temp) == -1) { - kill_sock(sock); - free(temp); - return NULL; - } + if (generate_handshake(temp) == -1) { + kill_sock(sock); + free(temp); + return NULL; + } + break; } temp->kill_at = unix_time() + TCP_CONNECTION_TIMEOUT; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 8a1aeec1..4c82e786 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -29,9 +29,25 @@ #define TCP_CONNECTION_TIMEOUT 10 -typedef struct { +typedef enum { + TCP_PROXY_NONE, + TCP_PROXY_HTTP, + TCP_PROXY_SOCKS5 +} TCP_PROXY_TYPE; + +typedef struct { + IP_Port ip_port; +} TCP_Proxy_HTTP; + +typedef struct { IP_Port ip_port; - int is_http; /* HTTP proxy on true, SOCKS5 on false */ +} 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/net_crypto.c b/toxcore/net_crypto.c index be2f967d..c6660f26 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1999,13 +1999,8 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key) for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { if (c->tcp_connections_new[i] == NULL) { - if (c->proxy_set) { - c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key, + c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key, &c->proxy_info); - } else { - c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key, - 0); - } return 0; } @@ -2738,10 +2733,7 @@ Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info) bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8); - if (proxy_info) { - temp->proxy_info = *proxy_info; - temp->proxy_set = 1; - } + temp->proxy_info = *proxy_info; return temp; } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 0726207b..4340d4b9 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -213,7 +213,6 @@ typedef struct { int (*tcp_onion_callback)(void *object, const uint8_t *data, uint16_t length); void *tcp_onion_callback_object; - uint8_t proxy_set; TCP_Proxy_Info proxy_info; } Net_Crypto; diff --git a/toxcore/tox.c b/toxcore/tox.c index 3f3a05b0..72817c80 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1020,19 +1020,63 @@ Tox *tox_new(Tox_Options *options) } else { m_options.ipv6enabled = options->ipv6enabled; m_options.udp_disabled = options->udp_disabled; - m_options.proxy_enabled = options->proxy_enabled; - m_options.proxy_info.is_http = options->proxy_is_http; - if (m_options.proxy_enabled) { - ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); + switch (options->proxy_type) { + 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; + } - if (m_options.ipv6enabled) - m_options.proxy_info.ip_port.ip.family = AF_UNSPEC; + TCP_Proxy_HTTP* m_proxy = (TCP_Proxy_HTTP*)m_options.proxy_info.proxy; + Tox_Proxy_HTTP* tox_proxy = (Tox_Proxy_HTTP*)options->proxy; - if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL)) - return NULL; + ip_init(&m_proxy->ip_port.ip, m_options.ipv6enabled); - m_options.proxy_info.ip_port.port = htons(options->proxy_port); + 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: { + 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: { + m_options.proxy_info.proxy_type = TCP_PROXY_NONE; + + break; + } } } diff --git a/toxcore/tox.h b/toxcore/tox.h index f2c102ab..94e4d5c6 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -873,6 +873,22 @@ int tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8_ */ int tox_isconnected(const Tox *tox); +typedef enum { + TOX_PROXY_NONE, + TOX_PROXY_HTTP, + 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: @@ -885,14 +901,10 @@ typedef struct { /* 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.*/ + Disabling udp support is necessary when using 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. */ - uint8_t proxy_is_http; /*1: HTTP proxy, 0: SOCKS5 proxy*/ + uint8_t proxy_type; // a value from TOX_PROXY_TYPE + void* proxy; // pointer to the corresponding proxy type struct } Tox_Options; /* -- cgit v1.2.3 From 35602e12c007aec38f65c860cad5f03953c4596d Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sun, 21 Dec 2014 20:59:50 -0500 Subject: Further refactoring of proxy code --- toxcore/TCP_client.c | 22 ++++--------------- toxcore/TCP_client.h | 10 --------- toxcore/tox.c | 62 ++++++++++++---------------------------------------- toxcore/tox.h | 15 +++---------- 4 files changed, 21 insertions(+), 88 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; - } + break; + case TOX_PROXY_NONE: + m_options.proxy_info.proxy_type = TCP_PROXY_NONE; + break; + } - m_proxy->ip_port.port = htons(tox_proxy->port); + if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { + ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); - break; - } + if (m_options.ipv6enabled) + m_options.proxy_info.ip_port.ip.family = AF_UNSPEC; - case TOX_PROXY_NONE: { - m_options.proxy_info.proxy_type = TCP_PROXY_NONE; + if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL)) + return NULL; - break; - } + 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; /* -- cgit v1.2.3