@@ -1585,8 +1585,23 @@ PQconnectPoll(PGconn *conn)
1585
1585
conn -> raddr .salen = addr_cur -> ai_addrlen ;
1586
1586
1587
1587
/* Open a socket */
1588
- conn -> sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1589
- if (conn -> sock < 0 )
1588
+ {
1589
+ /*
1590
+ * While we use 'pgsocket' as the socket type in the
1591
+ * backend, we use 'int' for libpq socket values.
1592
+ * This requires us to map PGINVALID_SOCKET to -1
1593
+ * on Windows.
1594
+ * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1595
+ */
1596
+ pgsocket sock = socket (addr_cur -> ai_family , SOCK_STREAM , 0 );
1597
+ #ifdef WIN32
1598
+ if (sock == PGINVALID_SOCKET )
1599
+ conn -> sock = -1 ;
1600
+ else
1601
+ #endif
1602
+ conn -> sock = sock ;
1603
+ }
1604
+ if (conn -> sock == -1 )
1590
1605
{
1591
1606
/*
1592
1607
* ignore socket() failure if we have more addresses
@@ -3123,7 +3138,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3123
3138
char * errbuf , int errbufsize )
3124
3139
{
3125
3140
int save_errno = SOCK_ERRNO ;
3126
- int tmpsock = -1 ;
3141
+ pgsocket tmpsock = PGINVALID_SOCKET ;
3127
3142
char sebuf [256 ];
3128
3143
int maxlen ;
3129
3144
struct
@@ -3136,7 +3151,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3136
3151
* We need to open a temporary connection to the postmaster. Do this with
3137
3152
* only kernel calls.
3138
3153
*/
3139
- if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) < 0 )
3154
+ if ((tmpsock = socket (raddr -> addr .ss_family , SOCK_STREAM , 0 )) == PGINVALID_SOCKET )
3140
3155
{
3141
3156
strlcpy (errbuf , "PQcancel() -- socket() failed: " , errbufsize );
3142
3157
goto cancel_errReturn ;
@@ -3207,7 +3222,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
3207
3222
maxlen );
3208
3223
strcat (errbuf , "\n" );
3209
3224
}
3210
- if (tmpsock >= 0 )
3225
+ if (tmpsock != PGINVALID_SOCKET )
3211
3226
closesocket (tmpsock );
3212
3227
SOCK_ERRNO_SET (save_errno );
3213
3228
return FALSE;
@@ -4620,6 +4635,15 @@ PQerrorMessage(const PGconn *conn)
4620
4635
return conn -> errorMessage .data ;
4621
4636
}
4622
4637
4638
+ /*
4639
+ * In Windows, socket values are unsigned, and an invalid socket value
4640
+ * (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
4641
+ * warning). Ideally we would return an unsigned value for PQsocket() on
4642
+ * Windows, but that would cause the function's return value to differ from
4643
+ * Unix, so we just return -1 for invalid sockets.
4644
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
4645
+ * http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
4646
+ */
4623
4647
int
4624
4648
PQsocket (const PGconn * conn )
4625
4649
{
0 commit comments