changeset 131:6624893ad528

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 23 Jun 2014 16:59:36 +0300
parents 32dcabfc2644
children 11fa2bf90251
files th_network.c
diffstat 1 files changed, 11 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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;
     }