Skip to contentSkip to conversation

make non-blocking connect work on Windows

PATCH Superseded Heiko Hund 2 participants 7 messages Jul 13, 2012 – Jul 16, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Heiko Hund

author
#0 lore raw

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

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

---
 configure.ac         |    2 ++
 src/openvpn/socket.c |    2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
configure.ac +2 -0
index d3d974d..ba5dce1 100644
@@ -617,6 +617,7 @@ m4_define(
 	[setsockopt getsockopt getsockname poll]dnl
 )
 if test "${WIN32}" = "yes"; then
+	AC_DEFINE([CONNECT_IN_PROGRESS], [WSAEWOULDBLOCK], [errno for incomplete non-blocking connect(2)])
 	m4_foreach(
 		[F],
 		m4_split(SOCKET_FUNCS SOCKET_OPT_FUNCS),
@@ -624,6 +625,7 @@ if test "${WIN32}" = "yes"; then
 			AC_DEFINE([UF], [1], [Win32 builtin])
 	)
 else
+	AC_DEFINE([CONNECT_IN_PROGRESS], [EINPROGRESS], [errno for incomplete non-blocking connect(2)])
 	AC_CHECK_FUNCS(
 		SOCKET_FUNCS,
 		,
src/openvpn/socket.c +1 -1
index 54ebce7..b2d6bba 100644
@@ -1165,7 +1165,7 @@ 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 (status == CONNECT_IN_PROGRESS)
     {
       while (true)
 	{
Signature · 1 line
1.7.9.5
#1 lore raw

Hello,

In my projects I always compare to the two values EWOULDBLOCK and EINPROGRESS and it works without much conditionals.

So simply do:
---
if (status == EINPROGRESS || status == EWOULDBLOCK)
---

Alon.

Quoted 61 lines · Heiko Hund

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

---
 configure.ac         |    2 ++
 src/openvpn/socket.c |    2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
configure.ac +1 -0
index d3d974d..ba5dce1 100644
@@ -617,6 +617,7 @@ m4_define(
        [setsockopt getsockopt getsockname poll]dnl
 )
 if test "${WIN32}" = "yes"; then
+       AC_DEFINE([CONNECT_IN_PROGRESS], [WSAEWOULDBLOCK], [errno for
 incomplete non-blocking connect(2)])
        m4_foreach(
                [F],

m4_split(SOCKET_FUNCS SOCKET_OPT_FUNCS),

configure.ac +1 -0
@@ -624,6 +625,7 @@ if test "${WIN32}" = "yes"; then
                        AC_DEFINE([UF], [1], [Win32 builtin])
        )
 else
+       AC_DEFINE([CONNECT_IN_PROGRESS], [EINPROGRESS], [errno for
 incomplete non-blocking connect(2)])
        AC_CHECK_FUNCS(
                SOCKET_FUNCS,
                ,
src/openvpn/socket.c +1 -1
index 54ebce7..b2d6bba 100644
@@ -1165,7 +1165,7 @@ 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 (status == CONNECT_IN_PROGRESS)
     {
       while (true)
        {

--
1.7.9.5

------------------------------------------------------------------------------
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

author
#2 lore raw

Hi Alon

Quoted 6 lines · On Friday 13 July 2012 20:00:49 Alon Bar-Lev wrote:

In my projects I always compare to the two values EWOULDBLOCK and EINPROGRESS and it works without much conditionals.

So simply do:
---
if (status == EINPROGRESS || status == EWOULDBLOCK)

While this surly compiles with mingw, I don't think it works as intended, as the numerical values of the constants differ:

	#define EWOULDBLOCK 140       (errno.h)
	#define WSAEWOULDBLOCK 10035L (winerror.h)

The error returned from WinSock2 is really 10035 in case of a unfinished non- blocking connect.

Heiko

Signature · 2 lines
Heiko Hund | Sr. Software Engineer | Tel +49-721-25516-237 | Fax -200
SOPHOS NSG | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany
#3 lore raw
Quoted 10 lines · Heiko Hund

Hi Alon

Quoted 6 lines · On Friday 13 July 2012 20:00:49 Alon Bar-Lev wrote:

In my projects I always compare to the two values EWOULDBLOCK and EINPROGRESS and it works without much conditionals.

So simply do:
---
if (status == EINPROGRESS || status == EWOULDBLOCK)

Quoted 9 lines

While this surly compiles with mingw, I don't think it works as intended, as the numerical values of the constants differ:

        #define EWOULDBLOCK 140       (errno.h)
        #define WSAEWOULDBLOCK 10035L (winerror.h)

The error returned from WinSock2 is really 10035 in case of a unfinished non- blocking connect.

Hi,

That's correct.... and applies to all winsock errors in this case:
---
#define WSAEWOULDBLOCK 10035L
#define WSAEINPROGRESS 10036L
---

It depends if you take error from errno or from GetLastError()/WSAGetLastError().
If we do not use errno, we can have autoconf to define emulation for errno as WSA constants.

Alon.

author
#4 lore raw
Quoted 18 lines · On Monday 16 July 2012 12:12:24 Alon Bar-Lev wrote:
Quoted 9 lines · Heiko Hund
Quoted 7 lines · On Friday 13 July 2012 20:00:49 Alon Bar-Lev wrote:

In my projects I always compare to the two values EWOULDBLOCK and EINPROGRESS and it works without much conditionals.

So simply do:
---

if (status == EINPROGRESS || status == EWOULDBLOCK)

Quoted 7 lines

While this surly compiles with mingw, I don't think it works as intended, as the numerical values of the constants differ:

        #define EWOULDBLOCK 140       (errno.h)
        #define WSAEWOULDBLOCK 10035L (winerror.h)

The error returned from WinSock2 is really 10035 in case of a unfinished non- blocking connect.

Quoted 8 lines

That's correct.... and applies to all winsock errors in this case:
---
#define WSAEWOULDBLOCK 10035L
#define WSAEINPROGRESS 10036L
---

It depends if you take error from errno or from
GetLastError()/WSAGetLastError().

I wonder if GetLastError() works reliably with WinSock2 operations as msdn.microsoft.com/en-us/library/windows/desktop/ms737828%28v=vs.85%29.aspx and msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx do explicitly state to use WSAGetLastError(). Maybe it only breaks with multithreaded applications?!

Heiko

Signature · 2 lines
Heiko Hund | Sr. Software Engineer | Tel +49-721-25516-237 | Fax -200
SOPHOS NSG | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany
#5 lore raw
Quoted 28 lines · Heiko Hund
Quoted 18 lines · On Monday 16 July 2012 12:12:24 Alon Bar-Lev wrote:
Quoted 9 lines · Heiko Hund
Quoted 7 lines · On Friday 13 July 2012 20:00:49 Alon Bar-Lev wrote:

In my projects I always compare to the two values EWOULDBLOCK and EINPROGRESS and it works without much conditionals.

So simply do:
---

if (status == EINPROGRESS || status == EWOULDBLOCK)

Quoted 7 lines

While this surly compiles with mingw, I don't think it works as intended, as the numerical values of the constants differ:

        #define EWOULDBLOCK 140       (errno.h)
        #define WSAEWOULDBLOCK 10035L (winerror.h)

The error returned from WinSock2 is really 10035 in case of a unfinished non- blocking connect.

Quoted 8 lines

That's correct.... and applies to all winsock errors in this case:
---
#define WSAEWOULDBLOCK 10035L
#define WSAEINPROGRESS 10036L
---

It depends if you take error from errno or from
GetLastError()/WSAGetLastError().

Quoted 7 lines

I wonder if GetLastError() works reliably with WinSock2 operations as msdn.microsoft.com/en-us/library/windows/desktop/ms737828%28v=vs.85%29.aspx and msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx do explicitly state to use WSAGetLastError(). Maybe it only breaks with multithreaded applications?!

Starting at windows nt 4, winsock2 there is no difference between WSAGetLastError() and GetLastError().
As for legacy, when there could be several providers, people had to use WSAGetLastError().
But there is no really difference... if you think code will be more consistence with WSA prefix, that's ok.

Alon

author
#6 lore raw
Quoted 10 lines · On Monday 16 July 2012 13:02:11 Alon Bar-Lev wrote:
Quoted 6 lines · Heiko Hund

I wonder if GetLastError() works reliably with WinSock2 operations as msdn.microsoft.com/en-us/library/windows/desktop/ms737828%28v=vs.85%29.aspx and msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx do explicitly state to use WSAGetLastError(). Maybe it only breaks with multithreaded applications?!

Starting at windows nt 4, winsock2 there is no difference between WSAGetLastError() and GetLastError().

After looking at the disassembly of ws2_32.dll on my Windows 7, I can confirm this. Thanks for the hint.

WSAGetLastError() simply returns GetLastError(), which in turn means GetLastError() itself returns WSAEWOULDBLOCK (10035). So, we can't simply check for EWOULDBLOCK (140). Is the proposed AC_DEFINE approach something you agree with?

With all that in mind we can now get rid of openvpn_errno_socket() as openvpn_errno() will do the exact same thing. I'll send a removal patch soon.

Heiko

Signature · 2 lines
Heiko Hund | Sr. Software Engineer | Tel +49-721-25516-237 | Fax -200
SOPHOS NSG | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany