Skip to content

Commit 125ad53

Browse files
committed
Improve TranslateSocketError() to handle more Windows error codes.
The coverage was rather lean for cases that bind() or listen() might return. Add entries for everything that there's a direct equivalent for in the set of Unix errnos that elog.c has heard of.
1 parent e545281 commit 125ad53

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/backend/port/win32/socket.c

+44-11
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,38 @@ int pgwin32_noblock = 0;
4444

4545
/*
4646
* Convert the last socket error code into errno
47+
*
48+
* Note: where there is a direct correspondence between a WSAxxx error code
49+
* and a Berkeley error symbol, this mapping is actually a no-op, because
50+
* in win32.h we redefine the network-related Berkeley error symbols to have
51+
* the values of their WSAxxx counterparts. The point of the switch is
52+
* mostly to translate near-miss error codes into something that's sensible
53+
* in the Berkeley universe.
4754
*/
4855
static void
4956
TranslateSocketError(void)
5057
{
5158
switch (WSAGetLastError())
5259
{
53-
case WSANOTINITIALISED:
54-
case WSAENETDOWN:
55-
case WSAEINPROGRESS:
5660
case WSAEINVAL:
57-
case WSAESOCKTNOSUPPORT:
58-
case WSAEFAULT:
61+
case WSANOTINITIALISED:
5962
case WSAEINVALIDPROVIDER:
6063
case WSAEINVALIDPROCTABLE:
61-
case WSAEMSGSIZE:
64+
case WSAEDESTADDRREQ:
6265
errno = EINVAL;
6366
break;
67+
case WSAEINPROGRESS:
68+
errno = EINPROGRESS;
69+
break;
70+
case WSAEFAULT:
71+
errno = EFAULT;
72+
break;
73+
case WSAEISCONN:
74+
errno = EISCONN;
75+
break;
76+
case WSAEMSGSIZE:
77+
errno = EMSGSIZE;
78+
break;
6479
case WSAEAFNOSUPPORT:
6580
errno = EAFNOSUPPORT;
6681
break;
@@ -72,16 +87,23 @@ TranslateSocketError(void)
7287
break;
7388
case WSAEPROTONOSUPPORT:
7489
case WSAEPROTOTYPE:
90+
case WSAESOCKTNOSUPPORT:
7591
errno = EPROTONOSUPPORT;
7692
break;
93+
case WSAECONNABORTED:
94+
errno = ECONNABORTED;
95+
break;
7796
case WSAECONNREFUSED:
7897
errno = ECONNREFUSED;
7998
break;
99+
case WSAECONNRESET:
100+
errno = ECONNRESET;
101+
break;
80102
case WSAEINTR:
81103
errno = EINTR;
82104
break;
83105
case WSAENOTSOCK:
84-
errno = EBADFD;
106+
errno = ENOTSOCK;
85107
break;
86108
case WSAEOPNOTSUPP:
87109
errno = EOPNOTSUPP;
@@ -92,13 +114,24 @@ TranslateSocketError(void)
92114
case WSAEACCES:
93115
errno = EACCES;
94116
break;
95-
case WSAENOTCONN:
117+
case WSAEADDRINUSE:
118+
errno = EADDRINUSE;
119+
break;
120+
case WSAEADDRNOTAVAIL:
121+
errno = EADDRNOTAVAIL;
122+
break;
123+
case WSAEHOSTUNREACH:
124+
case WSAEHOSTDOWN:
125+
case WSAHOST_NOT_FOUND:
126+
case WSAENETDOWN:
127+
case WSAENETUNREACH:
96128
case WSAENETRESET:
97-
case WSAECONNRESET:
129+
errno = EHOSTUNREACH;
130+
break;
131+
case WSAENOTCONN:
98132
case WSAESHUTDOWN:
99-
case WSAECONNABORTED:
100133
case WSAEDISCON:
101-
errno = ECONNREFUSED; /* ENOTCONN? */
134+
errno = ENOTCONN;
102135
break;
103136
default:
104137
ereport(NOTICE,

src/include/port/win32.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,32 @@ typedef int pid_t;
285285
#define EAFNOSUPPORT WSAEAFNOSUPPORT
286286
#undef EWOULDBLOCK
287287
#define EWOULDBLOCK WSAEWOULDBLOCK
288+
#undef ECONNABORTED
289+
#define ECONNABORTED WSAECONNABORTED
288290
#undef ECONNRESET
289291
#define ECONNRESET WSAECONNRESET
290292
#undef EINPROGRESS
291293
#define EINPROGRESS WSAEINPROGRESS
294+
#undef EISCONN
295+
#define EISCONN WSAEISCONN
292296
#undef ENOBUFS
293297
#define ENOBUFS WSAENOBUFS
294298
#undef EPROTONOSUPPORT
295299
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
296300
#undef ECONNREFUSED
297301
#define ECONNREFUSED WSAECONNREFUSED
298-
#undef EBADFD
299-
#define EBADFD WSAENOTSOCK
302+
#undef ENOTSOCK
303+
#define ENOTSOCK WSAENOTSOCK
300304
#undef EOPNOTSUPP
301305
#define EOPNOTSUPP WSAEOPNOTSUPP
306+
#undef EADDRINUSE
307+
#define EADDRINUSE WSAEADDRINUSE
308+
#undef EADDRNOTAVAIL
309+
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
310+
#undef EHOSTUNREACH
311+
#define EHOSTUNREACH WSAEHOSTUNREACH
312+
#undef ENOTCONN
313+
#define ENOTCONN WSAENOTCONN
302314

303315
/*
304316
* Extended locale functions with gratuitous underscore prefixes.

0 commit comments

Comments
 (0)