2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 * Copyright 2013 Jacek Caban for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "winhttp_private.h"
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
28 #ifdef HAVE_SYS_IOCTL_H
29 # include <sys/ioctl.h>
31 #ifdef HAVE_SYS_FILIO_H
32 # include <sys/filio.h>
38 #ifndef HAVE_GETADDRINFO
40 /* critical section to protect non-reentrant gethostbyname() */
41 static CRITICAL_SECTION cs_gethostbyname
;
42 static CRITICAL_SECTION_DEBUG critsect_debug
=
44 0, 0, &cs_gethostbyname
,
45 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
46 0, 0, { (DWORD_PTR
)(__FILE__
": cs_gethostbyname") }
48 static CRITICAL_SECTION cs_gethostbyname
= { &critsect_debug
, -1, 0, 0, 0, 0 };
52 /* translate a unix error code into a winsock error code */
54 static int sock_get_error( int err
)
56 #if !defined(__MINGW32__) && !defined (_MSC_VER)
59 case EINTR
: return WSAEINTR
;
60 case EBADF
: return WSAEBADF
;
62 case EACCES
: return WSAEACCES
;
63 case EFAULT
: return WSAEFAULT
;
64 case EINVAL
: return WSAEINVAL
;
65 case EMFILE
: return WSAEMFILE
;
66 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
67 case EINPROGRESS
: return WSAEINPROGRESS
;
68 case EALREADY
: return WSAEALREADY
;
69 case ENOTSOCK
: return WSAENOTSOCK
;
70 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
71 case EMSGSIZE
: return WSAEMSGSIZE
;
72 case EPROTOTYPE
: return WSAEPROTOTYPE
;
73 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
74 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
75 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
76 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
77 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
78 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
79 case EADDRINUSE
: return WSAEADDRINUSE
;
80 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
81 case ENETDOWN
: return WSAENETDOWN
;
82 case ENETUNREACH
: return WSAENETUNREACH
;
83 case ENETRESET
: return WSAENETRESET
;
84 case ECONNABORTED
: return WSAECONNABORTED
;
86 case ECONNRESET
: return WSAECONNRESET
;
87 case ENOBUFS
: return WSAENOBUFS
;
88 case EISCONN
: return WSAEISCONN
;
89 case ENOTCONN
: return WSAENOTCONN
;
90 case ESHUTDOWN
: return WSAESHUTDOWN
;
91 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
92 case ETIMEDOUT
: return WSAETIMEDOUT
;
93 case ECONNREFUSED
: return WSAECONNREFUSED
;
94 case ELOOP
: return WSAELOOP
;
95 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
96 case EHOSTDOWN
: return WSAEHOSTDOWN
;
97 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
98 case ENOTEMPTY
: return WSAENOTEMPTY
;
100 case EPROCLIM
: return WSAEPROCLIM
;
103 case EUSERS
: return WSAEUSERS
;
106 case EDQUOT
: return WSAEDQUOT
;
109 case ESTALE
: return WSAESTALE
;
112 case EREMOTE
: return WSAEREMOTE
;
114 default: errno
= err
; perror( "sock_set_error" ); return WSAEFAULT
;
120 #define sock_get_error(x) WSAGetLastError()
122 static inline int unix_ioctl(int filedes
, long request
, void *arg
)
124 return ioctlsocket(filedes
, request
, arg
);
126 #define ioctlsocket unix_ioctl
129 static int sock_send(int fd
, const void *msg
, size_t len
, int flags
)
134 if ((ret
= send(fd
, msg
, len
, flags
)) == -1) WARN("send error %s\n", strerror(errno
));
136 while(ret
== -1 && errno
== EINTR
);
140 static int sock_recv(int fd
, void *msg
, size_t len
, int flags
)
145 if ((ret
= recv(fd
, msg
, len
, flags
)) == -1) WARN("recv error %s\n", strerror(errno
));
147 while(ret
== -1 && errno
== EINTR
);
151 static DWORD
netconn_verify_cert( PCCERT_CONTEXT cert
, WCHAR
*server
, DWORD security_flags
)
153 HCERTSTORE store
= cert
->hCertStore
;
155 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
156 PCCERT_CHAIN_CONTEXT chain
;
157 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
158 char *server_auth
[] = { oid_server_auth
};
159 DWORD err
= ERROR_SUCCESS
;
161 TRACE("verifying %s\n", debugstr_w( server
));
162 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
163 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
164 if ((ret
= CertGetCertificateChain( NULL
, cert
, NULL
, store
, &chainPara
,
165 CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
,
168 if (chain
->TrustStatus
.dwErrorStatus
)
170 static const DWORD supportedErrors
=
171 CERT_TRUST_IS_NOT_TIME_VALID
|
172 CERT_TRUST_IS_UNTRUSTED_ROOT
|
173 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
175 if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
177 if (!(security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
))
178 err
= ERROR_WINHTTP_SECURE_CERT_DATE_INVALID
;
180 else if (chain
->TrustStatus
.dwErrorStatus
&
181 CERT_TRUST_IS_UNTRUSTED_ROOT
)
183 if (!(security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
))
184 err
= ERROR_WINHTTP_SECURE_INVALID_CA
;
186 else if ((chain
->TrustStatus
.dwErrorStatus
&
187 CERT_TRUST_IS_OFFLINE_REVOCATION
) ||
188 (chain
->TrustStatus
.dwErrorStatus
&
189 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
))
190 err
= ERROR_WINHTTP_SECURE_CERT_REV_FAILED
;
191 else if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_REVOKED
)
192 err
= ERROR_WINHTTP_SECURE_CERT_REVOKED
;
193 else if (chain
->TrustStatus
.dwErrorStatus
&
194 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
196 if (!(security_flags
& SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
))
197 err
= ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE
;
199 else if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
)
200 err
= ERROR_WINHTTP_SECURE_INVALID_CERT
;
204 CERT_CHAIN_POLICY_PARA policyPara
;
205 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
206 CERT_CHAIN_POLICY_STATUS policyStatus
;
207 CERT_CHAIN_CONTEXT chainCopy
;
209 /* Clear chain->TrustStatus.dwErrorStatus so
210 * CertVerifyCertificateChainPolicy will verify additional checks
211 * rather than stopping with an existing, ignored error.
213 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
214 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
215 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
216 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
217 sslExtraPolicyPara
.pwszServerName
= server
;
218 sslExtraPolicyPara
.fdwChecks
= security_flags
;
219 policyPara
.cbSize
= sizeof(policyPara
);
220 policyPara
.dwFlags
= 0;
221 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
222 ret
= CertVerifyCertificateChainPolicy( CERT_CHAIN_POLICY_SSL
,
223 &chainCopy
, &policyPara
,
225 /* Any error in the policy status indicates that the
226 * policy couldn't be verified.
228 if (ret
&& policyStatus
.dwError
)
230 if (policyStatus
.dwError
== CERT_E_CN_NO_MATCH
)
231 err
= ERROR_WINHTTP_SECURE_CERT_CN_INVALID
;
233 err
= ERROR_WINHTTP_SECURE_INVALID_CERT
;
236 CertFreeCertificateChain( chain
);
239 err
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
240 TRACE("returning %08x\n", err
);
245 static BOOL winsock_initialized
= FALSE
;
246 BOOL
netconn_init_winsock()
250 if (!winsock_initialized
)
252 error
= WSAStartup(MAKEWORD(1, 1), &wsaData
);
255 ERR("WSAStartup failed: %d\n", error
);
259 winsock_initialized
= TRUE
;
261 return winsock_initialized
;
266 void netconn_unload( void )
268 #ifndef HAVE_GETADDRINFO
269 DeleteCriticalSection(&cs_gethostbyname
);
272 if(winsock_initialized
)
277 netconn_t
*netconn_create( hostdata_t
*host
, const struct sockaddr_storage
*sockaddr
, int timeout
)
280 unsigned int addr_len
;
285 conn
= heap_alloc_zero(sizeof(*conn
));
286 if (!conn
) return NULL
;
288 conn
->sockaddr
= *sockaddr
;
289 if ((conn
->socket
= socket( sockaddr
->ss_family
, SOCK_STREAM
, 0 )) == -1)
291 WARN("unable to create socket (%s)\n", strerror(errno
));
292 set_last_error( sock_get_error( errno
) );
297 switch (conn
->sockaddr
.ss_family
)
300 addr_len
= sizeof(struct sockaddr_in
);
303 addr_len
= sizeof(struct sockaddr_in6
);
312 ioctlsocket( conn
->socket
, FIONBIO
, &state
);
318 if (connect( conn
->socket
, (const struct sockaddr
*)&conn
->sockaddr
, addr_len
) < 0)
320 res
= sock_get_error( errno
);
321 if (res
== WSAEWOULDBLOCK
|| res
== WSAEINPROGRESS
)
324 /* ReactOS: use select instead of poll */
329 FD_SET(conn
->socket
, &outfd
);
332 tv
.tv_usec
= timeout
* 1000;
337 if (select( 0, NULL
, &outfd
, NULL
, &tv
) > 0)
341 pfd
.fd
= conn
->socket
;
342 pfd
.events
= POLLOUT
;
346 if (poll( &pfd
, 1, timeout
) > 0)
354 res
= sock_get_error( errno
);
355 if (res
!= WSAEINTR
) break;
359 if (res
!= WSAEINTR
) break;
370 ioctlsocket( conn
->socket
, FIONBIO
, &state
);
374 WARN("unable to connect to host (%d)\n", res
);
375 set_last_error( res
);
376 netconn_close( conn
);
382 BOOL
netconn_close( netconn_t
*conn
)
388 heap_free( conn
->peek_msg_mem
);
389 heap_free(conn
->ssl_buf
);
390 heap_free(conn
->extra_buf
);
391 DeleteSecurityContext(&conn
->ssl_ctx
);
393 res
= closesocket( conn
->socket
);
394 release_host( conn
->host
);
398 set_last_error( sock_get_error( errno
) );
404 BOOL
netconn_secure_connect( netconn_t
*conn
, WCHAR
*hostname
, DWORD security_flags
, CredHandle
*cred_handle
)
406 SecBuffer out_buf
= {0, SECBUFFER_TOKEN
, NULL
}, in_bufs
[2] = {{0, SECBUFFER_TOKEN
}, {0, SECBUFFER_EMPTY
}};
407 SecBufferDesc out_desc
= {SECBUFFER_VERSION
, 1, &out_buf
}, in_desc
= {SECBUFFER_VERSION
, 2, in_bufs
};
409 SIZE_T read_buf_size
= 2048;
413 const CERT_CONTEXT
*cert
;
414 SECURITY_STATUS status
;
415 DWORD res
= ERROR_SUCCESS
;
417 const DWORD isc_req_flags
= ISC_REQ_ALLOCATE_MEMORY
|ISC_REQ_USE_SESSION_KEY
|ISC_REQ_CONFIDENTIALITY
418 |ISC_REQ_SEQUENCE_DETECT
|ISC_REQ_REPLAY_DETECT
|ISC_REQ_MANUAL_CRED_VALIDATION
;
420 read_buf
= heap_alloc(read_buf_size
);
424 status
= InitializeSecurityContextW(cred_handle
, NULL
, hostname
, isc_req_flags
, 0, 0, NULL
, 0,
425 &ctx
, &out_desc
, &attrs
, NULL
);
427 assert(status
!= SEC_E_OK
);
429 while(status
== SEC_I_CONTINUE_NEEDED
|| status
== SEC_E_INCOMPLETE_MESSAGE
) {
430 if(out_buf
.cbBuffer
) {
431 assert(status
== SEC_I_CONTINUE_NEEDED
);
433 TRACE("sending %u bytes\n", out_buf
.cbBuffer
);
435 size
= sock_send(conn
->socket
, out_buf
.pvBuffer
, out_buf
.cbBuffer
, 0);
436 if(size
!= out_buf
.cbBuffer
) {
437 ERR("send failed\n");
438 res
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
442 FreeContextBuffer(out_buf
.pvBuffer
);
443 out_buf
.pvBuffer
= NULL
;
444 out_buf
.cbBuffer
= 0;
447 if(status
== SEC_I_CONTINUE_NEEDED
) {
448 assert(in_bufs
[1].cbBuffer
< read_buf_size
);
450 memmove(read_buf
, (BYTE
*)in_bufs
[0].pvBuffer
+in_bufs
[0].cbBuffer
-in_bufs
[1].cbBuffer
, in_bufs
[1].cbBuffer
);
451 in_bufs
[0].cbBuffer
= in_bufs
[1].cbBuffer
;
453 in_bufs
[1].BufferType
= SECBUFFER_EMPTY
;
454 in_bufs
[1].cbBuffer
= 0;
455 in_bufs
[1].pvBuffer
= NULL
;
458 assert(in_bufs
[0].BufferType
== SECBUFFER_TOKEN
);
459 assert(in_bufs
[1].BufferType
== SECBUFFER_EMPTY
);
461 if(in_bufs
[0].cbBuffer
+ 1024 > read_buf_size
) {
464 new_read_buf
= heap_realloc(read_buf
, read_buf_size
+ 1024);
466 status
= E_OUTOFMEMORY
;
470 in_bufs
[0].pvBuffer
= read_buf
= new_read_buf
;
471 read_buf_size
+= 1024;
474 size
= sock_recv(conn
->socket
, read_buf
+in_bufs
[0].cbBuffer
, read_buf_size
-in_bufs
[0].cbBuffer
, 0);
476 status
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
480 TRACE("recv %lu bytes\n", size
);
482 in_bufs
[0].cbBuffer
+= size
;
483 in_bufs
[0].pvBuffer
= read_buf
;
484 status
= InitializeSecurityContextW(cred_handle
, &ctx
, hostname
, isc_req_flags
, 0, 0, &in_desc
,
485 0, NULL
, &out_desc
, &attrs
, NULL
);
486 TRACE("InitializeSecurityContext ret %08x\n", status
);
488 if(status
== SEC_E_OK
) {
489 if(in_bufs
[1].BufferType
== SECBUFFER_EXTRA
)
490 FIXME("SECBUFFER_EXTRA not supported\n");
492 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_STREAM_SIZES
, &conn
->ssl_sizes
);
493 if(status
!= SEC_E_OK
) {
494 WARN("Could not get sizes\n");
498 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&cert
);
499 if(status
== SEC_E_OK
) {
500 res
= netconn_verify_cert(cert
, hostname
, security_flags
);
501 CertFreeCertificateContext(cert
);
502 if(res
!= ERROR_SUCCESS
) {
503 WARN("cert verify failed: %u\n", res
);
507 WARN("Could not get cert\n");
511 conn
->ssl_buf
= heap_alloc(conn
->ssl_sizes
.cbHeader
+ conn
->ssl_sizes
.cbMaximumMessage
+ conn
->ssl_sizes
.cbTrailer
);
513 res
= GetLastError();
521 if(status
!= SEC_E_OK
|| res
!= ERROR_SUCCESS
) {
522 WARN("Failed to initialize security context failed: %08x\n", status
);
523 heap_free(conn
->ssl_buf
);
524 conn
->ssl_buf
= NULL
;
525 DeleteSecurityContext(&ctx
);
526 set_last_error(res
? res
: ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
531 TRACE("established SSL connection\n");
537 static BOOL
send_ssl_chunk(netconn_t
*conn
, const void *msg
, size_t size
)
539 SecBuffer bufs
[4] = {
540 {conn
->ssl_sizes
.cbHeader
, SECBUFFER_STREAM_HEADER
, conn
->ssl_buf
},
541 {size
, SECBUFFER_DATA
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
},
542 {conn
->ssl_sizes
.cbTrailer
, SECBUFFER_STREAM_TRAILER
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
+size
},
543 {0, SECBUFFER_EMPTY
, NULL
}
545 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, sizeof(bufs
)/sizeof(*bufs
), bufs
};
548 memcpy(bufs
[1].pvBuffer
, msg
, size
);
549 res
= EncryptMessage(&conn
->ssl_ctx
, 0, &buf_desc
, 0);
550 if(res
!= SEC_E_OK
) {
551 WARN("EncryptMessage failed\n");
555 if(sock_send(conn
->socket
, conn
->ssl_buf
, bufs
[0].cbBuffer
+bufs
[1].cbBuffer
+bufs
[2].cbBuffer
, 0) < 1) {
556 WARN("send failed\n");
563 BOOL
netconn_send( netconn_t
*conn
, const void *msg
, size_t len
, int *sent
)
567 const BYTE
*ptr
= msg
;
573 chunk_size
= min(len
, conn
->ssl_sizes
.cbMaximumMessage
);
574 if(!send_ssl_chunk(conn
, ptr
, chunk_size
))
584 if ((*sent
= sock_send( conn
->socket
, msg
, len
, 0 )) == -1)
586 set_last_error( sock_get_error( errno
) );
592 static BOOL
read_ssl_chunk(netconn_t
*conn
, void *buf
, SIZE_T buf_size
, SIZE_T
*ret_size
, BOOL
*eof
)
594 const SIZE_T ssl_buf_size
= conn
->ssl_sizes
.cbHeader
+conn
->ssl_sizes
.cbMaximumMessage
+conn
->ssl_sizes
.cbTrailer
;
596 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, sizeof(bufs
)/sizeof(*bufs
), bufs
};
597 SSIZE_T size
, buf_len
;
601 assert(conn
->extra_len
< ssl_buf_size
);
603 if(conn
->extra_len
) {
604 memcpy(conn
->ssl_buf
, conn
->extra_buf
, conn
->extra_len
);
605 buf_len
= conn
->extra_len
;
607 heap_free(conn
->extra_buf
);
608 conn
->extra_buf
= NULL
;
610 buf_len
= sock_recv(conn
->socket
, conn
->ssl_buf
+conn
->extra_len
, ssl_buf_size
-conn
->extra_len
, 0);
624 memset(bufs
, 0, sizeof(bufs
));
625 bufs
[0].BufferType
= SECBUFFER_DATA
;
626 bufs
[0].cbBuffer
= buf_len
;
627 bufs
[0].pvBuffer
= conn
->ssl_buf
;
629 res
= DecryptMessage(&conn
->ssl_ctx
, &buf_desc
, 0, NULL
);
633 case SEC_I_CONTEXT_EXPIRED
:
634 TRACE("context expired\n");
637 case SEC_E_INCOMPLETE_MESSAGE
:
638 assert(buf_len
< ssl_buf_size
);
640 size
= sock_recv(conn
->socket
, conn
->ssl_buf
+buf_len
, ssl_buf_size
-buf_len
, 0);
647 WARN("failed: %08x\n", res
);
650 } while(res
!= SEC_E_OK
);
652 for(i
=0; i
< sizeof(bufs
)/sizeof(*bufs
); i
++) {
653 if(bufs
[i
].BufferType
== SECBUFFER_DATA
) {
654 size
= min(buf_size
, bufs
[i
].cbBuffer
);
655 memcpy(buf
, bufs
[i
].pvBuffer
, size
);
656 if(size
< bufs
[i
].cbBuffer
) {
657 assert(!conn
->peek_len
);
658 conn
->peek_msg_mem
= conn
->peek_msg
= heap_alloc(bufs
[i
].cbBuffer
- size
);
661 conn
->peek_len
= bufs
[i
].cbBuffer
-size
;
662 memcpy(conn
->peek_msg
, (char*)bufs
[i
].pvBuffer
+size
, conn
->peek_len
);
669 for(i
=0; i
< sizeof(bufs
)/sizeof(*bufs
); i
++) {
670 if(bufs
[i
].BufferType
== SECBUFFER_EXTRA
) {
671 conn
->extra_buf
= heap_alloc(bufs
[i
].cbBuffer
);
675 conn
->extra_len
= bufs
[i
].cbBuffer
;
676 memcpy(conn
->extra_buf
, bufs
[i
].pvBuffer
, conn
->extra_len
);
683 BOOL
netconn_recv( netconn_t
*conn
, void *buf
, size_t len
, int flags
, int *recvd
)
686 if (!len
) return TRUE
;
695 *recvd
= min( len
, conn
->peek_len
);
696 memcpy( buf
, conn
->peek_msg
, *recvd
);
697 conn
->peek_len
-= *recvd
;
698 conn
->peek_msg
+= *recvd
;
700 if (conn
->peek_len
== 0)
702 heap_free( conn
->peek_msg_mem
);
703 conn
->peek_msg_mem
= NULL
;
704 conn
->peek_msg
= NULL
;
706 /* check if we have enough data from the peek buffer */
707 if (!(flags
& MSG_WAITALL
) || *recvd
== len
) return TRUE
;
712 res
= read_ssl_chunk(conn
, (BYTE
*)buf
+size
, len
-size
, &cread
, &eof
);
714 WARN("read_ssl_chunk failed\n");
726 }while(!size
|| ((flags
& MSG_WAITALL
) && size
< len
));
728 TRACE("received %ld bytes\n", size
);
732 if ((*recvd
= sock_recv( conn
->socket
, buf
, len
, flags
)) == -1)
734 set_last_error( sock_get_error( errno
) );
740 ULONG
netconn_query_data_available( netconn_t
*conn
)
742 return conn
->secure
? conn
->peek_len
: 0;
745 DWORD
netconn_set_timeout( netconn_t
*netconn
, BOOL send
, int value
)
749 /* value is in milliseconds, convert to struct timeval */
750 tv
.tv_sec
= value
/ 1000;
751 tv
.tv_usec
= (value
% 1000) * 1000;
753 if (setsockopt( netconn
->socket
, SOL_SOCKET
, send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
, sizeof(tv
) ) == -1)
755 WARN("setsockopt failed (%s)\n", strerror( errno
));
756 return sock_get_error( errno
);
758 return ERROR_SUCCESS
;
761 BOOL
netconn_is_alive( netconn_t
*netconn
)
767 len
= recv( netconn
->socket
, &b
, 1, MSG_PEEK
| MSG_DONTWAIT
);
768 return len
== 1 || (len
== -1 && errno
== EWOULDBLOCK
);
769 #elif defined(__MINGW32__) || defined(_MSC_VER)
775 if(!ioctlsocket(netconn
->socket
, FIONBIO
, &mode
))
778 len
= recv(netconn
->socket
, &b
, 1, MSG_PEEK
);
781 if(!ioctlsocket(netconn
->socket
, FIONBIO
, &mode
))
784 return len
== 1 || (len
== -1 && WSAGetLastError() == WSAEWOULDBLOCK
);
786 FIXME("not supported on this platform\n");
791 static DWORD
resolve_hostname( const WCHAR
*hostnameW
, INTERNET_PORT port
, struct sockaddr_storage
*sa
)
794 #ifdef HAVE_GETADDRINFO
795 struct addrinfo
*res
, hints
;
799 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
802 if (!(hostname
= strdupWA( hostnameW
))) return ERROR_OUTOFMEMORY
;
804 #ifdef HAVE_GETADDRINFO
805 memset( &hints
, 0, sizeof(struct addrinfo
) );
806 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
807 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
809 hints
.ai_family
= AF_INET
;
811 ret
= getaddrinfo( hostname
, NULL
, &hints
, &res
);
814 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(hostnameW
), gai_strerror(ret
));
815 hints
.ai_family
= AF_INET6
;
816 ret
= getaddrinfo( hostname
, NULL
, &hints
, &res
);
819 TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW
), gai_strerror(ret
));
820 heap_free( hostname
);
821 return ERROR_WINHTTP_NAME_NOT_RESOLVED
;
824 heap_free( hostname
);
825 memcpy( sa
, res
->ai_addr
, res
->ai_addrlen
);
827 switch (res
->ai_family
)
830 ((struct sockaddr_in
*)sa
)->sin_port
= htons( port
);
833 ((struct sockaddr_in6
*)sa
)->sin6_port
= htons( port
);
838 return ERROR_SUCCESS
;
840 EnterCriticalSection( &cs_gethostbyname
);
842 he
= gethostbyname( hostname
);
843 heap_free( hostname
);
846 TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW
), h_errno
);
847 LeaveCriticalSection( &cs_gethostbyname
);
848 return ERROR_WINHTTP_NAME_NOT_RESOLVED
;
850 memset( sa
, 0, sizeof(struct sockaddr_in
) );
851 memcpy( &sin
->sin_addr
, he
->h_addr
, he
->h_length
);
852 sin
->sin_family
= he
->h_addrtype
;
853 sin
->sin_port
= htons( port
);
855 LeaveCriticalSection( &cs_gethostbyname
);
856 return ERROR_SUCCESS
;
862 const WCHAR
*hostname
;
864 struct sockaddr_storage
*sa
;
867 static DWORD CALLBACK
resolve_proc( LPVOID arg
)
869 struct resolve_args
*ra
= arg
;
870 return resolve_hostname( ra
->hostname
, ra
->port
, ra
->sa
);
873 BOOL
netconn_resolve( WCHAR
*hostname
, INTERNET_PORT port
, struct sockaddr_storage
*sa
, int timeout
)
881 struct resolve_args ra
;
883 ra
.hostname
= hostname
;
887 thread
= CreateThread( NULL
, 0, resolve_proc
, &ra
, 0, NULL
);
888 if (!thread
) return FALSE
;
890 status
= WaitForSingleObject( thread
, timeout
);
891 if (status
== WAIT_OBJECT_0
) GetExitCodeThread( thread
, &ret
);
892 else ret
= ERROR_WINHTTP_TIMEOUT
;
893 CloseHandle( thread
);
895 else ret
= resolve_hostname( hostname
, port
, sa
);
899 set_last_error( ret
);
905 const void *netconn_get_certificate( netconn_t
*conn
)
907 const CERT_CONTEXT
*ret
;
910 if (!conn
->secure
) return NULL
;
911 res
= QueryContextAttributesW(&conn
->ssl_ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&ret
);
912 return res
== SEC_E_OK
? ret
: NULL
;
915 int netconn_get_cipher_strength( netconn_t
*conn
)
917 SecPkgContext_ConnectionInfo conn_info
;
920 if (!conn
->secure
) return 0;
921 res
= QueryContextAttributesW(&conn
->ssl_ctx
, SECPKG_ATTR_CONNECTION_INFO
, (void*)&conn_info
);
923 WARN("QueryContextAttributesW failed: %08x\n", res
);
924 return res
== SEC_E_OK
? conn_info
.dwCipherStrength
: 0;