@@ -1480,8 +1480,23 @@ PQconnectPoll(PGconn *conn)
1480
1480
conn -> raddr .salen = addr_cur -> ai_addrlen ;
1481
1481
1482
1482
/* Open a socket */
1483
- conn -> sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1484
- if (conn -> sock < 0 )
1483
+ {
1484
+ /*
1485
+ * While we use 'pgsocket' as the socket type in the
1486
+ * backend, we use 'int' for libpq socket values.
1487
+ * This requires us to map PGINVALID_SOCKET to -1
1488
+ * on Windows.
1489
+ * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1490
+ */
1491
+ pgsocket sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1492
+ #ifdef WIN32
1493
+ if (sock == PGINVALID_SOCKET )
1494
+ conn -> sock = -1 ;
1495
+ else
1496
+ #endif
1497
+ conn -> sock = sock ;
1498
+ }
1499
+ if (conn -> sock == -1 )
1485
1500
{
1486
1501
/*
1487
1502
* ignore socket() failure if we have more addresses
@@ -2892,7 +2907,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
2892
2907
char * errbuf , int errbufsize )
2893
2908
{
2894
2909
int save_errno = SOCK_ERRNO ;
2895
- int tmpsock = -1 ;
2910
+ pgsocket tmpsock = PGINVALID_SOCKET ;
2896
2911
char sebuf [256 ];
2897
2912
int maxlen ;
2898
2913
struct
@@ -2905,7 +2920,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
2905
2920
* We need to open a temporary connection to the postmaster. Do this with
2906
2921
* only kernel calls.
2907
2922
*/
2908
- if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) < 0 )
2923
+ if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) == PGINVALID_SOCKET )
2909
2924
{
2910
2925
strlcpy (errbuf , "PQcancel() -- socket() failed: " , errbufsize );
2911
2926
goto cancel_errReturn ;
@@ -2976,7 +2991,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
2976
2991
maxlen );
2977
2992
strcat (errbuf , "\n" );
2978
2993
}
2979
- if (tmpsock >= 0 )
2994
+ if (tmpsock != PGINVALID_SOCKET )
2980
2995
closesocket (tmpsock );
2981
2996
SOCK_ERRNO_SET (save_errno );
2982
2997
return FALSE;
@@ -4389,6 +4404,15 @@ PQerrorMessage(const PGconn *conn)
4389
4404
return conn -> errorMessage .data ;
4390
4405
}
4391
4406
4407
+ /*
4408
+ * In Windows, socket values are unsigned, and an invalid socket value
4409
+ * (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
4410
+ * warning). Ideally we would return an unsigned value for PQsocket() on
4411
+ * Windows, but that would cause the function's return value to differ from
4412
+ * Unix, so we just return -1 for invalid sockets.
4413
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
4414
+ * http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
4415
+ */
4392
4416
int
4393
4417
PQsocket (const PGconn * conn )
4394
4418
{
0 commit comments