@@ -1626,8 +1626,23 @@ PQconnectPoll(PGconn *conn)
1626
1626
conn -> raddr .salen = addr_cur -> ai_addrlen ;
1627
1627
1628
1628
/* Open a socket */
1629
- conn -> sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1630
- if (conn -> sock < 0 )
1629
+ {
1630
+ /*
1631
+ * While we use 'pgsocket' as the socket type in the
1632
+ * backend, we use 'int' for libpq socket values.
1633
+ * This requires us to map PGINVALID_SOCKET to -1
1634
+ * on Windows.
1635
+ * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1636
+ */
1637
+ pgsocket sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1638
+ #ifdef WIN32
1639
+ if (sock == PGINVALID_SOCKET )
1640
+ conn -> sock = -1 ;
1641
+ else
1642
+ #endif
1643
+ conn -> sock = sock ;
1644
+ }
1645
+ if (conn -> sock == -1 )
1631
1646
{
1632
1647
/*
1633
1648
* ignore socket() failure if we have more addresses
@@ -3173,7 +3188,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3173
3188
char * errbuf , int errbufsize )
3174
3189
{
3175
3190
int save_errno = SOCK_ERRNO ;
3176
- int tmpsock = -1 ;
3191
+ pgsocket tmpsock = PGINVALID_SOCKET ;
3177
3192
char sebuf [256 ];
3178
3193
int maxlen ;
3179
3194
struct
@@ -3186,7 +3201,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3186
3201
* We need to open a temporary connection to the postmaster. Do this with
3187
3202
* only kernel calls.
3188
3203
*/
3189
- if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) < 0 )
3204
+ if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) == PGINVALID_SOCKET )
3190
3205
{
3191
3206
strlcpy (errbuf , "PQcancel() -- socket() failed: " , errbufsize );
3192
3207
goto cancel_errReturn ;
@@ -3257,7 +3272,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3257
3272
maxlen );
3258
3273
strcat (errbuf , "\n" );
3259
3274
}
3260
- if (tmpsock >= 0 )
3275
+ if (tmpsock != PGINVALID_SOCKET )
3261
3276
closesocket (tmpsock );
3262
3277
SOCK_ERRNO_SET (save_errno );
3263
3278
return FALSE;
@@ -5246,6 +5261,15 @@ PQerrorMessage(const PGconn *conn)
5246
5261
return conn -> errorMessage .data ;
5247
5262
}
5248
5263
5264
+ /*
5265
+ * In Windows, socket values are unsigned, and an invalid socket value
5266
+ * (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
5267
+ * warning). Ideally we would return an unsigned value for PQsocket() on
5268
+ * Windows, but that would cause the function's return value to differ from
5269
+ * Unix, so we just return -1 for invalid sockets.
5270
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
5271
+ * http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
5272
+ */
5249
5273
int
5250
5274
PQsocket (const PGconn * conn )
5251
5275
{
0 commit comments