changeset 604:37ab4725e4f9

In preparation for SOCKS 5 support, move some structs etc. into network.c from the header file and rename them to socks4 specific.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 20 May 2014 02:10:52 +0300
parents 0a30bf8db004
children ad00f2bbb615
files network.c network.h
diffstat 2 files changed, 35 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/network.c	Tue May 20 01:29:41 2014 +0300
+++ b/network.c	Tue May 20 02:10:52 2014 +0300
@@ -6,8 +6,26 @@
 #include "network.h"
 #include <errno.h>
 
+struct nn_socks4_t
+{
+    uint8_t version;
+    uint8_t command;
+    in_port_t port;
+    in_addr_t addr;
+} __attribute__((__packed__));
+
+struct nn_socks4_res_t
+{
+    uint8_t nb;
+    uint8_t result;
+    in_port_t port;
+    in_addr_t addr;
+} __attribute__((__packed__));
+
+
 static BOOL nn_network_inited = FALSE;
 
+
 static const char *nn_proxy_types[] =
 {
     "none",
@@ -222,32 +240,31 @@
     // Proxy-specific setup
     if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A)
     {
-        struct nn_socks_t *socksh;
+        struct nn_socks4_t *socksh;
         size_t bufsiz;
         char *ptr, *buf;
         int tries, status = -1;
 
-        nn_conn_msg(conn, "Initializing proxy negotiation.\n");
+        nn_conn_msg(conn, "Initializing SOCKS 4/a proxy negotiation.\n");
 
-        bufsiz = sizeof(struct nn_socks_t) + strlen(conn->proxy.userid) + 1;
+        bufsiz = sizeof(struct nn_socks4_t) + strlen(conn->proxy.userid) + 1;
         if (conn->proxy.type == NN_PROXY_SOCKS4A)
             bufsiz += strlen(conn->host) + 1;
 
-        ptr = buf = th_malloc(bufsiz);
-        if (buf == NULL)
+        if ((ptr = buf = th_malloc(bufsiz)) == NULL)
         {
             conn->err = -1;
             nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz);
             goto error;
         }
 
-        // Create SOCKS 4/4A request
-        socksh = (struct nn_socks_t *) buf;
+        // Create SOCKS 5 handshake
+        socksh = (struct nn_socks4_t *) buf;
         socksh->version = 4;
-        socksh->command = SOCKS_CMD_CONNECT;
+        socksh->command = NN_PROXY_CMD_CONNECT;
         socksh->port    = htons(port);
         socksh->addr    = (conn->proxy.type == NN_PROXY_SOCKS4A) ?  htonl(0x00000032) : conn->addr.s_addr;
-        ptr += sizeof(struct nn_socks_t);
+        ptr += sizeof(struct nn_socks4_t);
 
         strcpy(ptr, conn->proxy.userid);
 
@@ -282,7 +299,7 @@
         // Check results
         if (status == 0)
         {
-            struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf);
+            struct nn_socks4_res_t *res = (struct nn_socks4_res_t *) &(conn->buf);
             if (res->nb != 0)
             {
                 nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb);
--- a/network.h	Tue May 20 01:29:41 2014 +0300
+++ b/network.h	Tue May 20 02:10:52 2014 +0300
@@ -52,25 +52,18 @@
 
 enum
 {
-    SOCKS_CMD_CONNECT = 1,
-    SOCKS_CMD_BIND = 2
+    NN_PROXY_AUTH_NONE = 0,
+//    NN_PROXY_AUTH_GSSAPI = 1,     // Not supported
+    NN_PROXY_AUTH_USER = 2,
 };
 
-struct nn_socks_t
+enum
 {
-    uint8_t version;
-    uint8_t command;
-    in_port_t port;
-    in_addr_t addr;
-} __attribute__((__packed__));
+    NN_PROXY_CMD_CONNECT = 1,
+    NN_PROXY_CMD_BIND = 2,
+    NN_PROXY_CMD_ASSOC_UDP = 3,
+};
 
-struct nn_socks_res_t
-{
-    uint8_t nb;
-    uint8_t result;
-    in_port_t port;
-    in_addr_t addr;
-} __attribute__((__packed__));
 
 typedef struct _nn_conn_t
 {