Fixed bug with auto tests and cleaned up the code a bit.

This commit is contained in:
irungentoo 2014-12-26 20:12:51 -05:00
parent fe57a72659
commit c2e394c5c2
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
5 changed files with 51 additions and 29 deletions

View File

@ -38,10 +38,11 @@
static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info)
{
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
ip_port =proxy_info->ip_port;
ip_port = proxy_info->ip_port;
}
struct sockaddr_storage addr = {0};
size_t addrsize;
if (ip_port.ip.family == AF_INET) {
@ -77,11 +78,15 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_con
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 = snprintf(TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, ip, port, three);
const int written = snprintf((char *)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;
}
@ -107,19 +112,16 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn)
return 0;
}
data[sizeof(data) - 1] = '\0';
data[sizeof(data) - 1] = 0;
if (strstr(data, success)) {
if (strstr((char *)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);
unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock);
if (data_left) {
uint8_t temp_data[data_left];
read_TCP_packet(TCP_conn->sock, temp_data, data_left);
}
return 1;
}
@ -606,6 +608,12 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
uint8_t family = ip_port.ip.family;
TCP_Proxy_Info default_proxyinfo;
if (proxy_info == NULL) {
default_proxyinfo.proxy_type = TCP_PROXY_NONE;
proxy_info = &default_proxyinfo;
}
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
family = proxy_info->ip_port.ip.family;
}
@ -645,10 +653,12 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
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;
@ -657,6 +667,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
free(temp);
return NULL;
}
break;
}

View File

@ -178,14 +178,10 @@ static int del_accepted(TCP_Server *TCP_server, int index)
return 0;
}
/* Read the next two bytes in TCP stream then convert them to
* length (host byte order).
*
* return length on success
* return 0 if nothing has been read from socket.
* return ~0 on failure.
/* return the amount of data in the tcp recv buffer.
* return 0 on failure.
*/
uint16_t read_TCP_length(sock_t sock)
unsigned int TCP_socket_data_recv_buffer(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
unsigned long count = 0;
@ -195,7 +191,21 @@ uint16_t read_TCP_length(sock_t sock)
ioctl(sock, FIONREAD, &count);
#endif
if ((unsigned int)count >= sizeof(uint16_t)) {
return count;
}
/* Read the next two bytes in TCP stream then convert them to
* length (host byte order).
*
* return length on success
* return 0 if nothing has been read from socket.
* return ~0 on failure.
*/
uint16_t read_TCP_length(sock_t sock)
{
unsigned int count = TCP_socket_data_recv_buffer(sock);
if (count >= sizeof(uint16_t)) {
uint16_t length;
int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), MSG_NOSIGNAL);
@ -223,13 +233,7 @@ uint16_t read_TCP_length(sock_t sock)
*/
int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
unsigned long count = 0;
ioctlsocket(sock, FIONREAD, &count);
#else
int count = 0;
ioctl(sock, FIONREAD, &count);
#endif
unsigned int count = TCP_socket_data_recv_buffer(sock);
if (count >= length) {
int len = recv(sock, data, length, MSG_NOSIGNAL);

View File

@ -154,6 +154,11 @@ void do_TCP_server(TCP_Server *TCP_server);
*/
void kill_TCP_server(TCP_Server *TCP_server);
/* return the amount of data in the tcp recv buffer.
* return 0 on failure.
*/
unsigned int TCP_socket_data_recv_buffer(sock_t sock);
/* Read the next two bytes in TCP stream then convert them to
* length (host byte order).
*

View File

@ -2030,7 +2030,7 @@ 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) {
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);
&c->proxy_info);
return 0;
}

View File

@ -1025,9 +1025,11 @@ Tox *tox_new(Tox_Options *options)
case TOX_PROXY_HTTP:
m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
break;
case TOX_PROXY_SOCKS5:
m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
break;
case TOX_PROXY_NONE:
m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
break;