@@ -1632,8 +1632,23 @@ PQconnectPoll(PGconn *conn)
1632
1632
conn -> raddr .salen = addr_cur -> ai_addrlen ;
1633
1633
1634
1634
/* Open a socket */
1635
- conn -> sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1636
- if (conn -> sock < 0 )
1635
+ {
1636
+ /*
1637
+ * While we use 'pgsocket' as the socket type in the
1638
+ * backend, we use 'int' for libpq socket values.
1639
+ * This requires us to map PGINVALID_SOCKET to -1
1640
+ * on Windows.
1641
+ * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1642
+ */
1643
+ pgsocket sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1644
+ #ifdef WIN32
1645
+ if (sock == PGINVALID_SOCKET )
1646
+ conn -> sock = -1 ;
1647
+ else
1648
+ #endif
1649
+ conn -> sock = sock ;
1650
+ }
1651
+ if (conn -> sock == -1 )
1637
1652
{
1638
1653
/*
1639
1654
* ignore socket() failure if we have more addresses
@@ -3136,7 +3151,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3136
3151
char * errbuf , int errbufsize )
3137
3152
{
3138
3153
int save_errno = SOCK_ERRNO ;
3139
- int tmpsock = -1 ;
3154
+ pgsocket tmpsock = PGINVALID_SOCKET ;
3140
3155
char sebuf [256 ];
3141
3156
int maxlen ;
3142
3157
struct
@@ -3149,7 +3164,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3149
3164
* We need to open a temporary connection to the postmaster. Do this with
3150
3165
* only kernel calls.
3151
3166
*/
3152
- if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) < 0 )
3167
+ if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) == PGINVALID_SOCKET )
3153
3168
{
3154
3169
strlcpy (errbuf , "PQcancel() -- socket() failed: " , errbufsize );
3155
3170
goto cancel_errReturn ;
@@ -3220,7 +3235,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3220
3235
maxlen );
3221
3236
strcat (errbuf , "\n" );
3222
3237
}
3223
- if (tmpsock >= 0 )
3238
+ if (tmpsock != PGINVALID_SOCKET )
3224
3239
closesocket (tmpsock );
3225
3240
SOCK_ERRNO_SET (save_errno );
3226
3241
return FALSE;
@@ -5300,6 +5315,15 @@ PQerrorMessage(const PGconn *conn)
5300
5315
return conn -> errorMessage .data ;
5301
5316
}
5302
5317
5318
+ /*
5319
+ * In Windows, socket values are unsigned, and an invalid socket value
5320
+ * (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
5321
+ * warning). Ideally we would return an unsigned value for PQsocket() on
5322
+ * Windows, but that would cause the function's return value to differ from
5323
+ * Unix, so we just return -1 for invalid sockets.
5324
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
5325
+ * http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
5326
+ */
5303
5327
int
5304
5328
PQsocket (const PGconn * conn )
5305
5329
{
0 commit comments