Skip to content

Commit

Permalink
Fix Windows error translation to make GnuTLS happy
Browse files Browse the repository at this point in the history
It really wants a read/write on a non-blocking socket to return EAGAIN,
which is kind of a pain because what we actually get is WSAEINPROGRESS. And
if we translate *that* to EAGAIN then our check for a pending connect()
will break. So ditch neterrno() completely and have a special case for
the result of connect, and then openconnect__win32_neterrno() is for
GnuTLS only.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 10, 2014
1 parent 950a24c commit b5df97e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
21 changes: 11 additions & 10 deletions compat.c
Expand Up @@ -193,11 +193,21 @@ int openconnect__inet_aton(const char *cp, struct in_addr *addr)
#endif

#ifdef _WIN32
void openconnect__win32_sock_init()
{
WSADATA data;
if (WSAStartup (MAKEWORD(1, 1), &data) != 0) {
fprintf(stderr, _("ERROR: Cannot initialize sockets\n"));
exit(1);
}
}

#ifdef OPENCONNECT_GNUTLS
int openconnect__win32_neterrno()
{
switch (WSAGetLastError()) {
case WSAEINTR: return EINTR;
case WSAEWOULDBLOCK: return EWOULDBLOCK;
case WSAEWOULDBLOCK: return EAGAIN;
case WSAEINPROGRESS: return EINPROGRESS;
case WSAEALREADY: return EALREADY;
case WSAENOTSOCK: return ENOTSOCK;
Expand Down Expand Up @@ -232,15 +242,6 @@ int openconnect__win32_neterrno()
}
}

void openconnect__win32_sock_init()
{
WSADATA data;
if (WSAStartup (MAKEWORD(1, 1), &data) != 0) {
fprintf(stderr, _("ERROR: Cannot initialize sockets\n"));
exit(1);
}
}
#ifdef OPENCONNECT_GNUTLS
int openconnect__win32_sock_poll(gnutls_transport_ptr_t ptr, unsigned int ms)
{
fd_set rfds;
Expand Down
4 changes: 1 addition & 3 deletions openconnect-internal.h
Expand Up @@ -386,17 +386,15 @@ static inline int set_fd_cloexec(int fd)

#ifdef _WIN32
#define pipe(fds) _pipe(fds, 4096, O_BINARY)
#define neterrno openconnect__win32_neterrno
int openconnect__win32_neterrno();
void openconnect__win32_sock_init();
#ifdef OPENCONNECT_GNUTLS
int openconnect__win32_neterrno();
int openconnect__win32_sock_poll(gnutls_transport_ptr_t ptr, unsigned int ms);
ssize_t openconnect__win32_sock_read(gnutls_transport_ptr_t ptr, void *data, size_t size);
ssize_t openconnect__win32_sock_write(gnutls_transport_ptr_t ptr, const void *data, size_t size);
#endif
int dumb_socketpair(int socks[2], int make_overlapped);
#else
#define neterrno() errno
#define closesocket close
#ifndef O_BINARY
#define O_BINARY 0
Expand Down
10 changes: 9 additions & 1 deletion ssl.c
Expand Up @@ -47,6 +47,14 @@
#define AI_NUMERICSERV 0
#endif

static inline int connect_pending()
{
#ifdef _WIN32
return WSAGetLastError() == WSAEWOULDBLOCK;
#else
return errno == EINPROGRESS;
#endif
}
static int cancellable_connect(struct openconnect_info *vpninfo, int sockfd,
const struct sockaddr *addr, socklen_t addrlen)
{
Expand All @@ -59,7 +67,7 @@ static int cancellable_connect(struct openconnect_info *vpninfo, int sockfd,
if (vpninfo->protect_socket)
vpninfo->protect_socket(vpninfo->cbdata, sockfd);

if (connect(sockfd, addr, addrlen) < 0 && neterrno() != EINPROGRESS)
if (connect(sockfd, addr, addrlen) < 0 && !connect_pending())
return -1;

do {
Expand Down

0 comments on commit b5df97e

Please sign in to comment.