# HG changeset patch # User Matti Hamalainen # Date 1403531976 -10800 # Node ID 6624893ad528ec220633a0e770568f54c3cd98c0 # Parent 32dcabfc2644fe9ff4f4ed7326963f0cba86e1ed Improve error handling. diff -r 32dcabfc2644 -r 6624893ad528 th_network.c --- a/th_network.c Mon Jun 23 16:14:12 2014 +0300 +++ b/th_network.c Mon Jun 23 16:59:36 2014 +0300 @@ -215,16 +215,16 @@ } -static int th_conn_proxy_wait(th_conn_t *conn) +static int th_conn_proxy_wait(th_conn_t *conn, const ssize_t want) { int status, tries; for (status = TH_CONN_NO_DATA, tries = 1; tries <= 20 && status != TH_CONN_DATA_AVAIL; tries++) { #ifdef __WIN32 - Sleep(50); + Sleep(100); #else - usleep(50000); + usleep(100000); #endif th_conn_reset(conn); switch (status = th_conn_pull(conn)) @@ -236,6 +236,8 @@ case TH_CONN_DATA_AVAIL: case TH_CONN_NO_DATA: + if (conn->total_bytes < want) + status = TH_CONN_NO_DATA; break; default: @@ -316,7 +318,7 @@ goto out; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL) goto out; ptr = (uint8_t*) conn->buf; @@ -410,7 +412,6 @@ goto out; } - // Form handshake packet th_growbuf_clear(&buf); th_growbuf_put_u8(&buf, 0x05); // Protocol version @@ -422,7 +423,7 @@ goto out; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL) goto out; ptr = (uint8_t *) conn->buf; @@ -461,7 +462,7 @@ goto out; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL) goto out; ptr = (uint8_t *) conn->buf; @@ -537,7 +538,7 @@ goto out; // Wait for SOCKS server to reply - if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) + if (th_conn_proxy_wait(conn, 3) != TH_CONN_DATA_AVAIL) goto out; ptr = (uint8_t *) conn->buf; @@ -561,8 +562,8 @@ ptr++; if (*ptr != 0) { - err = THERR_INIT_FAIL; - th_conn_err(conn, err, "wot.\n"); + err = THERR_INVALID_DATA; + th_conn_err(conn, err, "Invalid reply from SOCKS 5 server: expected 0x00, got 0x%02x.\n", *ptr); goto out; }