changeset 359:8f3c102db611

Fix SOCKS handling.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jun 2011 10:11:45 +0300
parents 98a3c525266b
children b465a17ffa47
files libnnchat.c nnchat.c
diffstat 2 files changed, 14 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Thu Jun 23 09:00:14 2011 +0300
+++ b/libnnchat.c	Thu Jun 23 10:11:45 2011 +0300
@@ -187,10 +187,13 @@
     /* Proxy-specific setup */
     if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) {
         struct nn_socks_t *socksh;
-        size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1 + strlen(conn->host) + 1;
+        size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1;
         char *ptr, *buf;
         int tries, status = -1;
         
+        if (conn->proxy.type == NN_PROXY_SOCKS4A)
+            bufsiz += strlen(conn->host) + 1;
+        
         ptr = buf = th_malloc(bufsiz);
         if (buf == NULL) {
             conn->err = -1;
@@ -211,9 +214,11 @@
         ptr += sizeof(struct nn_socks_t);
 
         strcpy(ptr, userid);
-        ptr += strlen(userid) + 1;
         
-        strcpy(ptr, conn->host);
+        if (conn->proxy.type == NN_PROXY_SOCKS4A) {
+            ptr += strlen(userid) + 1;
+            strcpy(ptr, conn->host);
+        }
         
         /* Send request */
         if (!nn_conn_send_buf(conn, buf, bufsiz)) {
@@ -224,10 +229,9 @@
         th_free(buf);
         
         /* Wait for SOCKS server to reply */
-        for (tries = 0; tries < 20; tries++) {
+        for (status = tries = 1; tries <= 20 && status > 0; tries++) {
             usleep(500);
             status = nn_conn_pull(conn);
-            if (status <= 0) break;
         }
         
         /* Check results */
@@ -251,7 +255,7 @@
             nn_conn_msg(conn, "SOCKS connection established!\n");
         }
         else if (status < 0) {
-            nn_conn_err(conn, "Proxy negotiation failed with network error: %d\n", status);
+            nn_conn_err(conn, "Proxy negotiation failed at try %d with network error: %d\n", tries, status);
             goto error;
         }
         else {
@@ -320,7 +324,7 @@
     fd_set tmpfds;
 
     if (conn == NULL)
-        return -1;
+        return -10;
 
     conn->ptr = conn->buf;
 
@@ -338,7 +342,7 @@
         }
     } else
     if (FD_ISSET(conn->socket, &tmpfds)) {
-        conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE, 0);
+        conn->got = recv(conn->socket, conn->buf, NN_CONNBUF_SIZE, 0);
         if (conn->got < 0) {
             conn->err = nn_get_socket_errno();
             nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err));
--- a/nnchat.c	Thu Jun 23 09:00:14 2011 +0300
+++ b/nnchat.c	Thu Jun 23 10:11:45 2011 +0300
@@ -535,12 +535,10 @@
 void errorMsgV(const char *fmt, va_list ap)
 {
     char *tmp;
-    va_list ap2;
 
-    va_copy(ap2, ap);
-    printMsgV(chatWindows[0], LOG_STAMP | LOG_WINDOW | LOG_FILE, fmt, ap);
+    tmp = th_strdup_vprintf(fmt, ap);
 
-    tmp = th_strdup_vprintf(fmt, ap2);
+    printMsg(NULL, "%s", tmp);
     
     if (errorMessages != NULL) {
         char *tmp2 = th_strdup_printf("%s%s", errorMessages, tmp);