Skip to contentSkip to conversation

make non-blocking connect work on Windows

PATCH Applied Heiko Hund 4 participants 8 messages Jul 17, 2012 – Jul 19, 2012

lore
Share & tools
lore

Heiko Hund

author
#0 lore raw

PATCH v2 1/2 make non-blocking connect work on Windows

Instead of EINPROGRESS WinSock2 returns WSAEWOULDBLOCK if a non-blocking connect(2) cannot be completed immediately.

---
 src/openvpn/socket.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
src/openvpn/socket.c +7 -1
index 54ebce7..02e2768 100644
@@ -1165,7 +1165,13 @@ openvpn_connect (socket_descriptor_t sd,
   status = connect (sd, &remote->addr.sa, af_addr_size(remote->addr.sa.sa_family));
   if (status)
     status = openvpn_errno_socket ();
-  if (status == EINPROGRESS)
+  if (
+#ifdef WIN32
+    status == WSAEWOULDBLOCK
+#else
+    status == EINPROGRESS
+#endif
+  )
     {
       while (true)
 	{
Signature · 1 line
1.7.9
author
#1 lore raw

PATCH v2 2/2 don't treat socket related errors special anymore

WSAGetLastError() is just a wrapper for GetLastError(). So, there's no need to differentiate between socket related and other errors.

This patch removes all special handling of socket errors in favor of simplifying the codebase somewhat.

---
 src/openvpn/error.c  |    9 ++-----
 src/openvpn/error.h  |    4 ---
 src/openvpn/fdmisc.c |    2 +-
 src/openvpn/manage.c |    4 +-
 src/openvpn/proxy.c  |   10 ++++----
 src/openvpn/ps.c     |    6 ++--
 src/openvpn/socket.c |   56 +++++++++++++++++++++++++-------------------------
 src/openvpn/socket.h |    2 +-
 src/openvpn/socks.c  |   26 +++++++++++-----------
 src/openvpn/win32.c  |    6 ++--
 10 files changed, 59 insertions(+), 66 deletions(-)
src/openvpn/error.c +3 -6
index d6ad639..8396fe0 100644
@@ -221,10 +221,7 @@ void x_msg (const unsigned int flags, const char *format, ...)
     return;
 #endif
 
-  if (flags & M_ERRNO_SOCK)
-    e = openvpn_errno_socket ();
-  else
-    e = openvpn_errno ();
+  e = openvpn_errno ();
 
   /*
    * Apply muting filter.
@@ -245,7 +242,7 @@ void x_msg (const unsigned int flags, const char *format, ...)
   va_end (arglist);
   m1[ERR_BUF_SIZE - 1] = 0; /* windows vsnprintf needs this */
 
-  if ((flags & (M_ERRNO|M_ERRNO_SOCK)) && e)
+  if ((flags & M_ERRNO) && e)
     {
       openvpn_snprintf (m2, ERR_BUF_SIZE, "%s: %s (errno=%d)",
 			m1, strerror_ts (e, &gc), e);
@@ -596,7 +593,7 @@ x_check_status (int status,
 		struct link_socket *sock,
 		struct tuntap *tt)
 {
-  const int my_errno = (sock ? openvpn_errno_socket () : (int)openvpn_errno ());
+  const int my_errno = openvpn_errno ();
   const char *extended_msg = NULL;
 
   msg (x_cs_verbose_level, "%s %s returned %d",
src/openvpn/error.h +0 -4
index ed8f903..aedb7c3 100644
@@ -68,12 +68,10 @@ struct gc_arena;
 
 #ifdef WIN32
 # define openvpn_errno()             GetLastError()
-# define openvpn_errno_socket()      WSAGetLastError()
 # define openvpn_strerror(e, gc)     strerror_win32(e, gc)
   const char *strerror_win32 (DWORD errnum, struct gc_arena *gc);
 #else
 # define openvpn_errno()             errno
-# define openvpn_errno_socket()      errno
 # define openvpn_strerror(x, gc)     strerror(x)
 #endif
 
@@ -94,7 +92,6 @@ extern int x_msg_line_num;
 #define M_DEBUG           (1<<7)
 
 #define M_ERRNO           (1<<8)	 /* show errno description */
-#define M_ERRNO_SOCK      (1<<9)	 /* show socket errno description */
 
 #ifdef ENABLE_CRYPTO_OPENSSL
 #  define M_SSL             (1<<10)	 /* show SSL error */
@@ -110,7 +107,6 @@ extern int x_msg_line_num;
 
 /* flag combinations which are frequently used */
 #define M_ERR     (M_FATAL | M_ERRNO)
-#define M_SOCKERR (M_FATAL | M_ERRNO_SOCK)
 #define M_SSLERR  (M_FATAL | M_SSL)
 #define M_USAGE   (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
 #define M_CLIENT  (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
src/openvpn/fdmisc.c +1 -1
index 8dc11b1..7fe449c 100644
@@ -66,7 +66,7 @@ void
 set_nonblock (int fd)
 {
   if (!set_nonblock_action (fd))
-    msg (M_SOCKERR, "Set socket to non-blocking mode failed");
+    msg (M_ERR, "Set socket to non-blocking mode failed");
 }
 
 /* Set a file descriptor to not be passed across execs */
src/openvpn/manage.c +2 -2
index 33d8be4..3ef14e5 100644
@@ -1575,7 +1575,7 @@ man_listen (struct management *man)
        * Listen for connection
        */
       if (listen (man->connection.sd_top, 1))
-	msg (M_SOCKERR, "MANAGEMENT: listen() failed");
+	msg (M_ERR, "MANAGEMENT: listen() failed");
 
       /*
        * Set misc socket properties
@@ -1763,7 +1763,7 @@ man_process_command (struct management *man, const char *line)
 static bool
 man_io_error (struct management *man, const char *prefix)
 {
-  const int err = openvpn_errno_socket ();
+  const int err = openvpn_errno ();
 
   if (!ignore_sys_error (err))
     {
src/openvpn/proxy.c +5 -5
index 202e55e..28ce019 100644
@@ -108,7 +108,7 @@ recv_line (socket_descriptor_t sd,
       if (status == 0)
 	{
 	  if (verbose)
-	    msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_line: TCP port read timeout expired");
+	    msg (D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read timeout expired");
 	  goto error;
 	}
 
@@ -116,7 +116,7 @@ recv_line (socket_descriptor_t sd,
       if (status < 0)
 	{
 	  if (verbose)
-	    msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_line: TCP port read failed on select()");
+	    msg (D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read failed on select()");
 	  goto error;
 	}
 
@@ -127,7 +127,7 @@ recv_line (socket_descriptor_t sd,
       if (size != 1)
 	{
 	  if (verbose)
-	    msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_line: TCP port read failed on recv()");
+	    msg (D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read failed on recv()");
 	  goto error;
 	}
 
@@ -152,7 +152,7 @@ recv_line (socket_descriptor_t sd,
 	  if (!isprint(c) && !isspace(c)) /* not ascii? */
 	    {
 	      if (verbose)
-		msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_line: Non-ASCII character (%d) read on recv()", (int)c);
+		msg (D_LINK_ERRORS | M_ERRNO, "recv_line: Non-ASCII character (%d) read on recv()", (int)c);
 	      *lookahead = la;
 	      return false;
 	    }
@@ -182,7 +182,7 @@ send_line (socket_descriptor_t sd,
   const ssize_t size = send (sd, buf, strlen (buf), MSG_NOSIGNAL);
   if (size != (ssize_t) strlen (buf))
     {
-      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "send_line: TCP port write failed on send()");
+      msg (D_LINK_ERRORS | M_ERRNO, "send_line: TCP port write failed on send()");
       return false;
     }
   return true;
src/openvpn/ps.c +3 -3
index 5d056ee..6495dc7 100644
@@ -233,7 +233,7 @@ port_share_sendmsg (const socket_descriptor_t sd,
 
       status = sendmsg (sd, &mesg, MSG_NOSIGNAL);
       if (status == -1)
-	msg (M_WARN|M_ERRNO_SOCK, "PORT SHARE: sendmsg failed -- unable to communicate with background process (%d,%d,%d,%d)",
+	msg (M_WARN|M_ERRNO, "PORT SHARE: sendmsg failed -- unable to communicate with background process (%d,%d,%d,%d)",
 	     sd, sd_send, sd_null[0], sd_null[1]
 	     );
 
@@ -419,7 +419,7 @@ proxy_entry_new (struct proxy_connection **list,
   sock_addr_set (&osaddr, server_addr, server_port);
   if ((sd_server = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
     {
-      msg (M_WARN|M_ERRNO_SOCK, "PORT SHARE PROXY: cannot create socket");
+      msg (M_WARN|M_ERRNO, "PORT SHARE PROXY: cannot create socket");
       return false;
     }
   status = openvpn_connect (sd_server, &osaddr, 5, NULL);
@@ -851,7 +851,7 @@ port_share_open (const char *host,
 	}
       else
 	{
-	  msg (M_SOCKERR, "PORT SHARE: unexpected init recv_control status=%d", status);
+	  msg (M_ERR, "PORT SHARE: unexpected init recv_control status=%d", status);
 	}
     }
   else
src/openvpn/socket.c +28 -28

221 lines not on this page · raw message

src/openvpn/socket.h +1 -1
index ef21cb6..47c6e8e 100644
@@ -750,7 +750,7 @@ socket_connection_reset (const struct link_socket *sock, int status)
 	return true;
       else if (status < 0)
 	{
-	  const int err = openvpn_errno_socket ();
+	  const int err = openvpn_errno ();
 #ifdef WIN32
 	  return err == WSAECONNRESET || err == WSAECONNABORTED;
 #else
src/openvpn/socks.c +13 -13
index dd54c8d..235982e 100644
@@ -120,7 +120,7 @@ socks_username_password_auth (struct socks_proxy_info *p,
 
   if (size != strlen (to_send))
     {
-      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_username_password_auth: TCP port write failed on send()");
+      msg (D_LINK_ERRORS | M_ERRNO, "socks_username_password_auth: TCP port write failed on send()");
       return false;
     }
 
@@ -146,14 +146,14 @@ socks_username_password_auth (struct socks_proxy_info *p,
       /* timeout? */
       if (status == 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_username_password_auth: TCP port read timeout expired");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_username_password_auth: TCP port read timeout expired");
 	  return false;
 	}
 
       /* error */
       if (status < 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_username_password_auth: TCP port read failed on select()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_username_password_auth: TCP port read failed on select()");
 	  return false;
 	}
 
@@ -163,7 +163,7 @@ socks_username_password_auth (struct socks_proxy_info *p,
       /* error? */
       if (size != 1)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_username_password_auth: TCP port read failed on recv()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_username_password_auth: TCP port read failed on recv()");
 	  return false;
 	}
 
@@ -194,7 +194,7 @@ socks_handshake (struct socks_proxy_info *p,
   const ssize_t size = send (sd, "\x05\x02\x00\x02", 4, MSG_NOSIGNAL);
   if (size != 4)
     {
-      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_handshake: TCP port write failed on send()");
+      msg (D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port write failed on send()");
       return false;
     }
 
@@ -220,14 +220,14 @@ socks_handshake (struct socks_proxy_info *p,
       /* timeout? */
       if (status == 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_handshake: TCP port read timeout expired");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read timeout expired");
 	  return false;
 	}
 
       /* error */
       if (status < 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_handshake: TCP port read failed on select()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read failed on select()");
 	  return false;
 	}
 
@@ -237,7 +237,7 @@ socks_handshake (struct socks_proxy_info *p,
       /* error? */
       if (size != 1)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "socks_handshake: TCP port read failed on recv()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read failed on recv()");
 	  return false;
 	}
 
@@ -319,14 +319,14 @@ recv_socks_reply (socket_descriptor_t sd,
       /* timeout? */
       if (status == 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_socks_reply: TCP port read timeout expired");
+	  msg (D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read timeout expired");
 	  return false;
 	}
 
       /* error */
       if (status < 0)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_socks_reply: TCP port read failed on select()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read failed on select()");
 	  return false;
 	}
 
@@ -336,7 +336,7 @@ recv_socks_reply (socket_descriptor_t sd,
       /* error? */
       if (size != 1)
 	{
-	  msg (D_LINK_ERRORS | M_ERRNO_SOCK, "recv_socks_reply: TCP port read failed on recv()");
+	  msg (D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read failed on recv()");
 	  return false;
 	}
 
@@ -421,7 +421,7 @@ establish_socks_proxy_passthru (struct socks_proxy_info *p,
     const ssize_t size = send (sd, buf, 5 + len + 2, MSG_NOSIGNAL);
     if ((int)size != 5 + (int)len + 2)
       {
-	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "establish_socks_proxy_passthru: TCP port write failed on send()");
+	msg (D_LINK_ERRORS | M_ERRNO, "establish_socks_proxy_passthru: TCP port write failed on send()");
 	goto error;
       }
   }
@@ -458,7 +458,7 @@ establish_socks_proxy_udpassoc (struct socks_proxy_info *p,
 			       10, MSG_NOSIGNAL);
     if (size != 10)
       {
-	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "establish_socks_proxy_passthru: TCP port write failed on send()");
+	msg (D_LINK_ERRORS | M_ERRNO, "establish_socks_proxy_passthru: TCP port write failed on send()");
 	goto error;
       }
   }
src/openvpn/win32.c +3 -3
index e8e69dc..d00088e 100644
@@ -257,7 +257,7 @@ init_net_event_win32 (struct rw_handle *event, long network_events, socket_descr
   
   /* setup network events to change read event state */
   if (WSAEventSelect (sd, event->read, network_events) != 0)
-    msg (M_FATAL | M_ERRNO_SOCK, "Error: init_net_event_win32: WSAEventSelect call failed");
+    msg (M_FATAL | M_ERRNO, "Error: init_net_event_win32: WSAEventSelect call failed");
 }
 
 long
@@ -266,7 +266,7 @@ reset_net_event_win32 (struct rw_handle *event, socket_descriptor_t sd)
   WSANETWORKEVENTS wne;  
   if (WSAEnumNetworkEvents (sd, event->read, &wne) != 0)
     {
-      msg (M_FATAL | M_ERRNO_SOCK, "Error: reset_net_event_win32: WSAEnumNetworkEvents call failed");
+      msg (M_FATAL | M_ERRNO, "Error: reset_net_event_win32: WSAEnumNetworkEvents call failed");
       return 0; /* NOTREACHED */
     }
   else
@@ -281,7 +281,7 @@ close_net_event_win32 (struct rw_handle *event, socket_descriptor_t sd, unsigned
       if (socket_defined (sd))
 	{
 	  if (WSAEventSelect (sd, event->read, 0) != 0)
-	    msg (M_WARN | M_ERRNO_SOCK, "Warning: close_net_event_win32: WSAEventSelect call failed");
+	    msg (M_WARN | M_ERRNO, "Warning: close_net_event_win32: WSAEventSelect call failed");
 	}
       if (!ResetEvent (event->read))
 	msg (M_WARN | M_ERRNO, "Warning: ResetEvent (read) failed in close_net_event_win32");
Signature · 1 line
1.7.9
#2 lore raw

Re: [Openvpn-devel] [PATCHv2 2/2] don't treat socket related errors special anymore

Hi,

Quoted 2 lines · Heiko Hund

WSAGetLastError() is just a wrapper for GetLastError(). So, there's no need to differentiate between socket related and other errors.

ACK on current version.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#3 lore raw

Re: [Openvpn-devel] [PATCHv2 2/2] don't treat socket related errors special anymore

In reply to Heiko Hund
Quoted 7 lines · Heiko Hund

WSAGetLastError() is just a wrapper for GetLastError(). So, there's no need to differentiate between socket related and other errors.

This patch removes all special handling of socket errors in favor of simplifying the codebase somewhat.

Quoted 13 lines
---
 src/openvpn/error.c  |    9 ++-----
 src/openvpn/error.h  |    4 ---
 src/openvpn/fdmisc.c |    2 +-
 src/openvpn/manage.c |    4 +-
 src/openvpn/proxy.c  |   10 ++++----
 src/openvpn/ps.c     |    6 ++--
 src/openvpn/socket.c |   56 +++++++++++++++++++++++++-------------------------
 src/openvpn/socket.h |    2 +-
 src/openvpn/socks.c  |   26 +++++++++++-----------
 src/openvpn/win32.c  |    6 ++--
 10 files changed, 59 insertions(+), 66 deletions(-)

Applied to master.

commit 910675de28956cf8d028aed727486b64747362fb

Author: Heiko Hund <[email protected]...>
Date:   Tue Jul 17 18:25:16 2012 +0200
    don't treat socket related errors special anymore
    
    Signed-off-by: Heiko Hund <[email protected]...>
    Acked-by: Gert Doering <[email protected]...>
    Message-Id: [email protected]...
    URL: http://article.gmane.org/gmane.network.openvpn.devel/6876
    Signed-off-by: David Sommerseth <[email protected]...>

kind regards,

David Sommerseth

#4 lore raw
In reply to Heiko Hund

Hi,

Quoted 2 lines · Heiko Hund

Instead of EINPROGRESS WinSock2 returns WSAEWOULDBLOCK if a non-blocking connect(2) cannot be completed immediately.

Feature-ACK (thanks for fixing :) ), and given the preceding discussion, code-ACK as well.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw
In reply to Heiko Hund

Thanks.
I think this is the simplest solution to this issue.

Quoted 41 lines · Heiko Hund

Instead of EINPROGRESS WinSock2 returns WSAEWOULDBLOCK if a non-blocking connect(2) cannot be completed immediately.

---
 src/openvpn/socket.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
src/openvpn/socket.c +7 -2
index 54ebce7..02e2768 100644
@@ -1165,7 +1165,13 @@ openvpn_connect (socket_descriptor_t sd,
   status = connect (sd, &remote->addr.sa, af_addr_size(remote->addr.sa.sa_family));
   if (status)
     status = openvpn_errno_socket ();
-  if (status == EINPROGRESS)
+  if (
+#ifdef WIN32
+    status == WSAEWOULDBLOCK
+#else
+    status == EINPROGRESS
+#endif
+  )
     {
       while (true)
        {
--

1.7.9

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#6 lore raw

I had time to look at the code, what I don't understand is how come we work none blocking and do not test for EWOULDBLOCK at other places... If I get it right, at least after send() we can get EWOULDBLOCK even if select() returned we have free buffers.

Quoted 50 lines · Alon Bar-Lev

Thanks.
I think this is the simplest solution to this issue.

Quoted 45 lines · Heiko Hund

Instead of EINPROGRESS WinSock2 returns WSAEWOULDBLOCK if a non-blocking connect(2) cannot be completed immediately.

---
 src/openvpn/socket.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
src/openvpn/socket.c +7 -1
index 54ebce7..02e2768 100644
@@ -1165,7 +1165,13 @@ openvpn_connect (socket_descriptor_t sd,
   status = connect (sd, &remote->addr.sa,
 af_addr_size(remote->addr.sa.sa_family));
   if (status)
     status = openvpn_errno_socket ();
-  if (status == EINPROGRESS)
+  if (
+#ifdef WIN32
+    status == WSAEWOULDBLOCK
+#else
+    status == EINPROGRESS
+#endif
+  )
     {
       while (true)
        {

--
1.7.9

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#7 lore raw
In reply to Heiko Hund
Quoted 8 lines · Heiko Hund

Instead of EINPROGRESS WinSock2 returns WSAEWOULDBLOCK if a non-blocking connect(2) cannot be completed immediately.

---
 src/openvpn/socket.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

Applied to master.

commit 9081e0ad4c496a0334a21fc4e8e4f1f73a470b5a

Author: Heiko Hund <[email protected]...>
Date:   Tue Jul 17 18:19:53 2012 +0200
    make non-blocking connect work on Windows
    
    Signed-off-by: Heiko Hund <[email protected]...>
    Acked-by: Gert Doering <[email protected]...>
    Message-Id: [email protected]...
    URL: http://article.gmane.org/gmane.network.openvpn.devel/6875
    Signed-off-by: David Sommerseth <[email protected]...>

kind regards,

David Sommerseth