summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_client.c37
-rw-r--r--toxcore/TCP_server.c34
-rw-r--r--toxcore/TCP_server.h5
-rw-r--r--toxcore/net_crypto.c2
-rw-r--r--toxcore/tox.c2
5 files changed, 51 insertions, 29 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index aee309ae..61a2ff1e 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -38,10 +38,11 @@
38static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) 38static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info)
39{ 39{
40 if (proxy_info->proxy_type != TCP_PROXY_NONE) { 40 if (proxy_info->proxy_type != TCP_PROXY_NONE) {
41 ip_port =proxy_info->ip_port; 41 ip_port = proxy_info->ip_port;
42 } 42 }
43 43
44 struct sockaddr_storage addr = {0}; 44 struct sockaddr_storage addr = {0};
45
45 size_t addrsize; 46 size_t addrsize;
46 47
47 if (ip_port.ip.family == AF_INET) { 48 if (ip_port.ip.family == AF_INET) {
@@ -77,11 +78,15 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_con
77 char three[] = "\r\n\r\n"; 78 char three[] = "\r\n\r\n";
78 79
79 char ip[INET6_ADDRSTRLEN]; 80 char ip[INET6_ADDRSTRLEN];
81
80 if (!ip_parse_addr(&TCP_conn->ip_port.ip, ip, sizeof(ip))) { 82 if (!ip_parse_addr(&TCP_conn->ip_port.ip, ip, sizeof(ip))) {
81 return 0; 83 return 0;
82 } 84 }
85
83 const uint16_t port = ntohs(TCP_conn->ip_port.port); 86 const uint16_t port = ntohs(TCP_conn->ip_port.port);
84 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); 87 const int written = snprintf((char *)TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two,
88 ip, port, three);
89
85 if (written < 0) { 90 if (written < 0) {
86 return 0; 91 return 0;
87 } 92 }
@@ -107,19 +112,16 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn)
107 return 0; 112 return 0;
108 } 113 }
109 114
110 data[sizeof(data) - 1] = '\0'; 115 data[sizeof(data) - 1] = 0;
111 116
112 if (strstr(data, success)) { 117 if (strstr((char *)data, success)) {
113 // drain all data 118 // drain all data
114 // instead of drainining it byte by byte do it in bigger chunks 119 unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock);
115 // decrementing to 1 120
116 size_t step = sizeof(data); 121 if (data_left) {
117 do { 122 uint8_t temp_data[data_left];
118 if (ret <= 0) { 123 read_TCP_packet(TCP_conn->sock, temp_data, data_left);
119 step = step % 2 == 0 ? step/2 : 1; 124 }
120 }
121 ret = read_TCP_packet(TCP_conn->sock, data, step);
122 } while (ret > 0 || step != 1);
123 125
124 return 1; 126 return 1;
125 } 127 }
@@ -606,6 +608,12 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
606 608
607 uint8_t family = ip_port.ip.family; 609 uint8_t family = ip_port.ip.family;
608 610
611 TCP_Proxy_Info default_proxyinfo;
612 if (proxy_info == NULL) {
613 default_proxyinfo.proxy_type = TCP_PROXY_NONE;
614 proxy_info = &default_proxyinfo;
615 }
616
609 if (proxy_info->proxy_type != TCP_PROXY_NONE) { 617 if (proxy_info->proxy_type != TCP_PROXY_NONE) {
610 family = proxy_info->ip_port.ip.family; 618 family = proxy_info->ip_port.ip.family;
611 } 619 }
@@ -645,10 +653,12 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
645 temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING; 653 temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING;
646 proxy_http_generate_connection_request(temp); 654 proxy_http_generate_connection_request(temp);
647 break; 655 break;
656
648 case TCP_PROXY_SOCKS5: 657 case TCP_PROXY_SOCKS5:
649 temp->status = TCP_CLIENT_PROXY_SOCKS5_CONNECTING; 658 temp->status = TCP_CLIENT_PROXY_SOCKS5_CONNECTING;
650 proxy_socks5_generate_handshake(temp); 659 proxy_socks5_generate_handshake(temp);
651 break; 660 break;
661
652 case TCP_PROXY_NONE: 662 case TCP_PROXY_NONE:
653 temp->status = TCP_CLIENT_CONNECTING; 663 temp->status = TCP_CLIENT_CONNECTING;
654 664
@@ -657,6 +667,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
657 free(temp); 667 free(temp);
658 return NULL; 668 return NULL;
659 } 669 }
670
660 break; 671 break;
661 } 672 }
662 673
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index c7bccab7..250d6c44 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -178,14 +178,10 @@ static int del_accepted(TCP_Server *TCP_server, int index)
178 return 0; 178 return 0;
179} 179}
180 180
181/* Read the next two bytes in TCP stream then convert them to 181/* return the amount of data in the tcp recv buffer.
182 * length (host byte order). 182 * return 0 on failure.
183 *
184 * return length on success
185 * return 0 if nothing has been read from socket.
186 * return ~0 on failure.
187 */ 183 */
188uint16_t read_TCP_length(sock_t sock) 184unsigned int TCP_socket_data_recv_buffer(sock_t sock)
189{ 185{
190#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 186#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
191 unsigned long count = 0; 187 unsigned long count = 0;
@@ -195,7 +191,21 @@ uint16_t read_TCP_length(sock_t sock)
195 ioctl(sock, FIONREAD, &count); 191 ioctl(sock, FIONREAD, &count);
196#endif 192#endif
197 193
198 if ((unsigned int)count >= sizeof(uint16_t)) { 194 return count;
195}
196
197/* Read the next two bytes in TCP stream then convert them to
198 * length (host byte order).
199 *
200 * return length on success
201 * return 0 if nothing has been read from socket.
202 * return ~0 on failure.
203 */
204uint16_t read_TCP_length(sock_t sock)
205{
206 unsigned int count = TCP_socket_data_recv_buffer(sock);
207
208 if (count >= sizeof(uint16_t)) {
199 uint16_t length; 209 uint16_t length;
200 int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), MSG_NOSIGNAL); 210 int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), MSG_NOSIGNAL);
201 211
@@ -223,13 +233,7 @@ uint16_t read_TCP_length(sock_t sock)
223 */ 233 */
224int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length) 234int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
225{ 235{
226#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 236 unsigned int count = TCP_socket_data_recv_buffer(sock);
227 unsigned long count = 0;
228 ioctlsocket(sock, FIONREAD, &count);
229#else
230 int count = 0;
231 ioctl(sock, FIONREAD, &count);
232#endif
233 237
234 if (count >= length) { 238 if (count >= length) {
235 int len = recv(sock, data, length, MSG_NOSIGNAL); 239 int len = recv(sock, data, length, MSG_NOSIGNAL);
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 3a6a868c..727c4b4e 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -154,6 +154,11 @@ void do_TCP_server(TCP_Server *TCP_server);
154 */ 154 */
155void kill_TCP_server(TCP_Server *TCP_server); 155void kill_TCP_server(TCP_Server *TCP_server);
156 156
157/* return the amount of data in the tcp recv buffer.
158 * return 0 on failure.
159 */
160unsigned int TCP_socket_data_recv_buffer(sock_t sock);
161
157/* Read the next two bytes in TCP stream then convert them to 162/* Read the next two bytes in TCP stream then convert them to
158 * length (host byte order). 163 * length (host byte order).
159 * 164 *
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index c5872068..8af3cbbc 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2030,7 +2030,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
2030 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 2030 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2031 if (c->tcp_connections_new[i] == NULL) { 2031 if (c->tcp_connections_new[i] == NULL) {
2032 c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key, 2032 c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key,
2033 &c->proxy_info); 2033 &c->proxy_info);
2034 2034
2035 return 0; 2035 return 0;
2036 } 2036 }
diff --git a/toxcore/tox.c b/toxcore/tox.c
index d7f231b5..a62bc884 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -1025,9 +1025,11 @@ Tox *tox_new(Tox_Options *options)
1025 case TOX_PROXY_HTTP: 1025 case TOX_PROXY_HTTP:
1026 m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; 1026 m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
1027 break; 1027 break;
1028
1028 case TOX_PROXY_SOCKS5: 1029 case TOX_PROXY_SOCKS5:
1029 m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5; 1030 m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
1030 break; 1031 break;
1032
1031 case TOX_PROXY_NONE: 1033 case TOX_PROXY_NONE:
1032 m_options.proxy_info.proxy_type = TCP_PROXY_NONE; 1034 m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
1033 break; 1035 break;