changeset 117:bfae4758fa6f

More work on network module.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 21 Jun 2014 23:31:54 +0300
parents bc540c5bfaf9
children ca33c6c80176
files th_network.c th_network.h
diffstat 2 files changed, 45 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/th_network.c	Sat Jun 21 23:19:57 2014 +0300
+++ b/th_network.c	Sat Jun 21 23:31:54 2014 +0300
@@ -196,11 +196,8 @@
 
 static int th_conn_proxy_send(th_conn_t *conn, th_growbuf_t *buf)
 {
-    int ret;
     th_conn_reset(conn);
-    ret = th_conn_send_buf(conn, (void *) buf->data, buf->len);
-    th_free(buf);
-    return ret;
+    return th_conn_send_growbuf(conn, buf);
 }
 
 
@@ -211,11 +208,10 @@
     int err = THERR_INIT_FAIL;
 
     (void) host;
-
+    th_growbuf_init(&buf, 128);
     th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 4/a proxy negotiation.\n");
 
     // Create SOCKS 4 handshake
-    th_growbuf_init(&buf, 128);
     th_growbuf_put_u8(&buf, 4);  // Protocol version
     th_growbuf_put_u8(&buf, TH_PROXY_CMD_CONNECT); // Command
     th_growbuf_put_u16_be(&buf, port);
@@ -230,11 +226,11 @@
 
     // Send request
     if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK)
-        return err;
+        goto out;
 
     // Wait for SOCKS server to reply
     if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL)
-        return err;
+        goto out;
 
     ptr = (uint8_t*) conn->buf;
     if (*ptr != 0)
@@ -243,7 +239,7 @@
         th_conn_err(conn, err,
             "Invalid SOCKS 4 server reply, does not begin with NUL byte (%d).\n",
             *ptr);
-        return err;
+        goto out;
     }
 
     ptr++;
@@ -260,10 +256,14 @@
 
         err = THERR_INIT_FAIL;
         th_conn_err(conn, err, "SOCKS 4 setup failed, 0x%02x: %s.\n", *ptr, s);
-        return err;
+        goto out;
     }
 
-    return THERR_OK;
+    err = THERR_OK;
+
+out:
+    th_growbuf_free(&buf);
+    return err;
 }
 
 
@@ -274,6 +274,7 @@
     uint8_t *ptr;
     int avail, auth, err = THERR_INIT_FAIL;
 
+    th_growbuf_init(&buf, 256);
     th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 5 proxy negotiation.\n");
 
     switch (conn->proxy.auth_type)
@@ -315,7 +316,7 @@
 
 
     // Form handshake packet
-    th_growbuf_init(&buf, 0);
+    th_growbuf_clear(&buf);
     th_growbuf_put_u8(&buf, 0x05);   // Protocol version
     th_growbuf_put_u8(&buf, avail);  // # of available auth methods
     th_growbuf_put_u8(&buf, auth);
@@ -326,7 +327,7 @@
 
     // Wait for SOCKS server to reply
     if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL)
-        return THERR_INIT_FAIL;
+        goto out;
 
     ptr = (uint8_t *) conn->buf;
     if (*ptr != 0x05)
@@ -350,7 +351,7 @@
     if (auth == TH_SOCKS5_AUTH_USER)
     {
         // Attempt user/pass authentication (RFC 1929)
-        th_growbuf_init(&buf, 256);
+        th_growbuf_clear(&buf);
         
         th_growbuf_put_u8(&buf, 0x01);
         th_growbuf_put_u8(&buf, userid_len);
@@ -364,7 +365,7 @@
 
         // Wait for SOCKS server to reply
         if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL)
-            return THERR_INIT_FAIL;
+            goto out;
 
         ptr = (uint8_t *) conn->buf;
         if (*ptr != 0x01)
@@ -396,7 +397,7 @@
 
 #if 0
     // Form client connection request packet
-    th_growbuf_init(&buf, 128);
+    th_growbuf_clear(&buf);
 
     th_growbuf_put_u8(&buf, 0x05); // Protocol version
     th_growbuf_put_u8(&buf, cmd);
@@ -411,39 +412,41 @@
 
     // Wait for SOCKS server to reply
     if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL)
-        return FALSE;
+        goto out;
 
     ptr = (uint8_t *) conn->buf;
     if (*ptr != 0x05)
     {
-        th_conn_err(conn, "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr);
-        return FALSE;
-    }
-    ptr++;
-    if (*ptr != 0)
-    {
-        int err = 0;
-        if (*ptr >= 0 && *ptr < sizeof())
-            th_conn_err(conn, err, "%s.\n", th_socks5_results_msgs[*ptr]);
-        else
-            th_conn_err(conn, err ".Unknown\n");
-
+        err = THERR_INVALID_DATA;
+        th_conn_err(conn, err,
+            "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr);
         goto out;
     }
     ptr++;
     if (*ptr != 0)
     {
-        int err = asdf;
-        th_conn_err(conn, err, ".\n");
+        err = THERR_INIT_FAIL;
+        if (*ptr >= 0 && *ptr < sizeof())
+            th_conn_err(conn, err, "%s.\n", th_socks5_results_msgs[*ptr]);
+        else
+            th_conn_err(conn, err ".Unknown\n");
+        goto out;
+    }
+    ptr++;
+    if (*ptr != 0)
+    {
+        err = THERR_INIT_FAIL;
+        th_conn_err(conn, err, "wot.\n");
         goto out;
     }
 
 #endif
 
+    err = THERR_OK;
+
 out:
     th_growbuf_free(&buf);
-
-    return THERR_OK;
+    return err;
 }
 
 
@@ -600,6 +603,14 @@
 }
 
 
+int th_conn_send_growbuf(th_conn_t *conn, th_growbuf_t *buf)
+{
+    int ret = th_conn_send_buf(conn, buf->data, buf->len);
+    th_growbuf_clear(buf);
+    return ret;
+}
+
+
 void th_conn_reset(th_conn_t *conn)
 {
     if (conn != NULL)
--- a/th_network.h	Sat Jun 21 23:19:57 2014 +0300
+++ b/th_network.h	Sat Jun 21 23:31:54 2014 +0300
@@ -137,6 +137,7 @@
 
 int         th_conn_pull(th_conn_t *);
 int         th_conn_send_buf(th_conn_t *, const void *buf, const size_t len);
+int         th_conn_send_growbuf(th_conn_t *, th_growbuf_t *buf);
 BOOL        th_conn_check(th_conn_t *);