Error 56 on FreeBSD
Brought to you by:
delius
I am trying to use tsocks on FreeBSD 6 with
Thunderbird. Whenever I try to connect to a pop3
server, I receive the following message:
15:23:06 libtsocks(93180): Error 56 attempting to
connect to SOCKS server (Socket is already connected)
It appears this has been fixed for MacOS, Darwin and
NetBSD (as far as google tells me). So I hope it can be
done for FreeBSD as well.
Is there some debugging work I can do to help resolve
this issue?
Logged In: YES
user_id=1130924
Using tsocks with ssh -D works fine, so this problem only
seems to appear with some socks servers. Unfortunately I do
not have any details on the socks server causing problems.
Logged In: YES
user_id=2163712
Originator: NO
You need to probably modify procedure "connect_server" in tsocks.c of version 1.8beta5. I got it to work by changed it to...
static int connect_server(struct connreq *conn) {
int rc;
/* Connect this socket to the socks server */
show_msg(MSGDEBUG, "Connecting to %s port %d\n",
inet_ntoa(conn->serveraddr.sin_addr), ntohs(conn->serveraddr.sin_port));
rc = realconnect(conn->sockid, (CONNECT_SOCKARG) &(conn->serveraddr),
sizeof(conn->serveraddr));
show_msg(MSGDEBUG, "Connect returned %d, errno is %d\n", rc, errno);
if (rc) {
if (errno != EINPROGRESS) {
if (errno == EISCONN) {
rc = 0;
show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n", conn->sockid);
conn->state = CONNECTED;
} else {
show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
"server (%s)\n", errno, strerror(errno));
conn->state = FAILED;
}
} else {
show_msg(MSGDEBUG, "Connection in progress\n");
conn->state = CONNECTING;
}
} else {
show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n", conn->sockid);
conn->state = CONNECTED;
}
return((rc ? errno : 0));
}
Logged In: YES
user_id=2163712
Originator: NO
Better yet, consider applying the following 101-line patch (which I do not know how to attach to this tracker):
diff -ur tsocks-1.8beta5/tsocks.c tsocks-1.8/tsocks.c
--- tsocks-1.8beta5/tsocks.c 2008-08-08 14:28:40.134118960 -0400
+++ tsocks-1.8/tsocks.c 2008-08-08 15:08:27.266219720 -0400
@@ -775,6 +775,20 @@
conn->state);
switch(conn->state) {
case UNSTARTED:
+#ifdef TOGGLE_NONBLOCKING
+ /* BEGIN RESTORATIVE FIX for necessary functionality present in TSOCKS 1.8 beta 2 but removed in TSOCKS 1.8 beta 3 */
+ /* Get the flags of the socket (in case it's non blocking) */
+ if ((conn->sockflags = fcntl(conn->sockid, F_GETFL)) == -1) {
+ conn->sockflags = 0;
+ }
+
+ /* If the flags show the socket as non blocking, set it */
+ /* to blocking for our connection to the socks server */
+ if ((conn->sockflags & O_NONBLOCK) != 0) {
+ fcntl(conn->sockid, F_SETFL, conn->sockflags & (~(O_NONBLOCK)));
+ }
+ /* BREAK RESTORATIVE FIX */
+#endif
case CONNECTING:
rc = connect_server(conn);
break;
@@ -827,6 +841,22 @@
case GOTV5CONNECT:
rc = read_socksv5_connect(conn);
break;
+ case FAILED:
+ case DONE:
+#ifdef TOGGLE_NONBLOCKING
+ /* CONTINUE RESTORATIVE FIX for necessary functionality present in TSOCKS 1.8 beta 2 but removed in TSOCKS 1.8 beta 3 */
+ /* If the socket was in non blocking mode, restore that */
+ if ((conn->sockflags & O_NONBLOCK) != 0) {
+ fcntl(conn->sockid, F_SETFL, conn->sockflags);
+ }
+
+// if (rc != 0) {
+// errno = conn->err;
+// return(-1);
+// }
+ /* END RESTORATIVE FIX */
+#endif
+ break;
}
conn->err = errno;
@@ -854,9 +884,19 @@
show_msg(MSGDEBUG, "Connect returned %d, errno is %d\n", rc, errno);
if (rc) {
if (errno != EINPROGRESS) {
- show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
- "server (%s)\n", errno, strerror(errno));
- conn->state = FAILED;
+ /* BEGIN FIX by Lindberg Williams pertaining to non blocking sockets */
+ if (errno == EISCONN) {
+ rc = 0;
+ show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n", conn->sockid);
+ conn->state = CONNECTED;
+ } else {
+ /* BREAK FIX */
+ show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
+ "server (%s)\n", errno, strerror(errno));
+ conn->state = FAILED;
+ /* CONTINUE FIX by Lindberg Williams pertaining to non blocking sockets */
+ }
+ /* END FIX */
} else {
show_msg(MSGDEBUG, "Connection in progress\n");
conn->state = CONNECTING;
@@ -1177,6 +1217,9 @@
}
#endif
+ /* Below is the code discarded in the version transition */
+ /* from TSOCKS beta 2 to TSOCKS beta 3 pertaining to non */
+ /* blocking sockets. */
#if 0
/* Get the flags of the socket, (incase its non blocking */
if ((sockflags = fcntl(sockid, F_GETFL)) == -1) {
diff -ur tsocks-1.8beta5/tsocks.h tsocks-1.8/tsocks.h
--- tsocks-1.8beta5/tsocks.h 2008-08-08 14:28:44.125512176 -0400
+++ tsocks-1.8/tsocks.h 2008-08-08 14:41:30.916942160 -0400
@@ -4,6 +4,8 @@
#define _TSOCKS_H 1
+#define TOGGLE_NONBLOCKING 1
+
#include <parser.h>
/* Structure representing a socks connection request */
@@ -27,6 +29,9 @@
struct connreq {
/* Information about the socket and target */
int sockid;
+#ifdef TOGGLE_NONBLOCKING
+ int sockflags;
+#endif
struct sockaddr_in connaddr;
struct sockaddr_in serveraddr;
Logged In: YES
user_id=2163712
Originator: NO
This earlier patch has a flaw that fails to restore non-blocking sockets to their original state after changing them to non-blocking. Please disregard it. I have created and updated a separate thread for the issue that this earlier patch was intended to address.
Logged In: YES
user_id=2163712
Originator: NO
The following is a revised and properly wrapped 106-line patch (metacharacters aside):
diff -ur tsocks-1.8beta5/tsocks.c tsocks-1.8/tsocks.c
--- tsocks-1.8beta5/tsocks.c 2008-08-24 08:47:43.156401152 -0400
+++ tsocks-1.8/tsocks.c 2008-08-24 08:48:26.968740664 -0400
@@ -99,6 +99,9 @@
static int read_socksv4_req(struct connreq *conn);
static int read_socksv5_connect(struct connreq *conn);
static int read_socksv5_auth(struct connreq *conn);
+#ifdef TOGGLE_NONBLOCKING
+static int toggling_nonblocking = 0;
+#endif
void _init(void) {
#ifdef USE_OLD_DLSYM
@@ -698,6 +701,17 @@
"%d which is a connection request of status %d\n",
conn->sockid, conn->state);
kill_socks_request(conn);
+ #ifdef TOGGLE_NONBLOCKING
+ /* Most likely the calling procedure fails to unset
+ * O_NONBLOCK or fails to recognize the errno
+ * EINPROGRESS as a form of success when connect
+ * returns -1 */
+ /* Toggling of the non blocking socket state will be
+ * enabled to avert this improper response to certain
+ * connect failures in future connects of the running
+ * process */
+ toggling_nonblocking = (conn->state != FAILED && conn->state != DONE);
+ #endif
}
return(rc);
@@ -775,6 +789,24 @@
conn->state);
switch(conn->state) {
case UNSTARTED:
+ #ifdef TOGGLE_NONBLOCKING
+ if (toggling_nonblocking) {
+ /* BEGIN RESTORATIVE FIX for necessary functionality present
+ in TSOCKS 1.8 beta 2 but removed in TSOCKS 1.8 beta 3 */
+ /* Get the flags of the socket (in case it's non blocking) */
+ if ((conn->sockflags = fcntl(conn->sockid, F_GETFL)) == -1) {
+ conn->sockflags = 0;
+ }
+
+ /* If the flags show the socket as non blocking, set it */
+ /* to blocking for our connection to the socks server */
+ if ((conn->sockflags & O_NONBLOCK) != 0) {
+ fcntl(conn->sockid, F_SETFL, conn->sockflags &
+ (~(O_NONBLOCK)));
+ }
+ /* BREAK RESTORATIVE FIX */
+ }
+ #endif
case CONNECTING:
rc = connect_server(conn);
break;
@@ -831,6 +863,17 @@
conn->err = errno;
}
+#ifdef TOGGLE_NONBLOCKING
+ if (toggling_nonblocking) {
+ /* CONTINUE RESTORATIVE FIX for necessary functionality present in
+ TSOCKS 1.8 beta 2 but removed in TSOCKS 1.8 beta 3 */
+ /* If the socket was in non blocking mode, restore that */
+ if ((conn->sockflags & O_NONBLOCK) != 0) {
+ fcntl(conn->sockid, F_SETFL, conn->sockflags);
+ }
+ /* END RESTORATIVE FIX */
+ }
+#endif
if (i == 20)
show_msg(MSGERR, "Ooops, state loop while handling request %d\n",
@@ -854,9 +897,16 @@
show_msg(MSGDEBUG, "Connect returned %d, errno is %d\n", rc, errno);
if (rc) {
if (errno != EINPROGRESS) {
- show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
- "server (%s)\n", errno, strerror(errno));
- conn->state = FAILED;
+ if (errno == EISCONN) {
+ rc = 0;
+ show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n",
+ conn->sockid);
+ conn->state = CONNECTED;
+ } else {
+ show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
+ "server (%s)\n", errno, strerror(errno));
+ conn->state = FAILED;
+ }
} else {
show_msg(MSGDEBUG, "Connection in progress\n");
conn->state = CONNECTING;
@@ -1177,6 +1227,9 @@
}
#endif
+ /* Below is the code discarded in the version transition */
+ /* from TSOCKS beta 2 to TSOCKS beta 3 pertaining to non */
+ /* blocking sockets. */
#if 0
/* Get the flags of the socket, (incase its non blocking */
if ((sockflags = fcntl(sockid, F_GETFL)) == -1) {
diff -ur tsocks-1.8beta5/tsocks.h tsocks-1.8/tsocks.h
--- tsocks-1.8beta5/tsocks.h 2008-08-24 08:47:43.156401152 -0400
+++ tsocks-1.8/tsocks.h 2008-08-24 08:48:26.968740664 -0400
@@ -4,6 +4,8 @@
#define _TSOCKS_H 1
+#define TOGGLE_NONBLOCKING 1
+
#include <parser.h>
/* Structure representing a socks connection request */
@@ -27,6 +29,9 @@
struct connreq {
/* Information about the socket and target */
int sockid;
+#ifdef TOGGLE_NONBLOCKING
+ int sockflags;
+#endif
struct sockaddr_in connaddr;
struct sockaddr_in serveraddr;
I am getting the same error with tor-0.2.3.25 and mplayer as client with libtsocks.so used with LD_PRELOAD