Skip to content

Commit 8f95184

Browse files
committed
Provide errno-translation wrappers around bind() and listen() on Windows.
Fix Windows builds to report something useful rather than "could not bind IPv4 socket: No error" when bind() fails. Back-patch of commits d1b7d48 and 22989a8. Discussion: <4065.1452450340@sss.pgh.pa.us>
1 parent e5882f2 commit 8f95184

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/backend/port/win32/socket.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
*/
2828
int pgwin32_noblock = 0;
2929

30+
/* Undef the macros defined in win32.h, so we can access system functions */
3031
#undef socket
32+
#undef bind
33+
#undef listen
3134
#undef accept
3235
#undef connect
3336
#undef select
@@ -261,6 +264,27 @@ pgwin32_socket(int af, int type, int protocol)
261264
return s;
262265
}
263266

267+
int
268+
pgwin32_bind(SOCKET s, struct sockaddr * addr, int addrlen)
269+
{
270+
int res;
271+
272+
res = bind(s, addr, addrlen);
273+
if (res < 0)
274+
TranslateSocketError();
275+
return res;
276+
}
277+
278+
int
279+
pgwin32_listen(SOCKET s, int backlog)
280+
{
281+
int res;
282+
283+
res = listen(s, backlog);
284+
if (res < 0)
285+
TranslateSocketError();
286+
return res;
287+
}
264288

265289
SOCKET
266290
pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen)

src/include/port/win32.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,17 @@ void pg_queue_signal(int signum);
363363
/* In backend/port/win32/socket.c */
364364
#ifndef FRONTEND
365365
#define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
366+
#define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)
367+
#define listen(s, backlog) pgwin32_listen(s, backlog)
366368
#define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
367369
#define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
368370
#define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
369371
#define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
370372
#define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
371373

372374
SOCKET pgwin32_socket(int af, int type, int protocol);
375+
int pgwin32_bind(SOCKET s, struct sockaddr * addr, int addrlen);
376+
int pgwin32_listen(SOCKET s, int backlog);
373377
SOCKET pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
374378
int pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
375379
int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);

0 commit comments

Comments
 (0)