winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / winhttp / net.c
blob0cc2bb2bef781ff7d73f42be80dd676aceaec9e3
1 /*
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 <assert.h>
21 #include <stdarg.h>
23 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ws2tcpip.h"
27 #include "winhttp.h"
28 #include "schannel.h"
30 #include "wine/debug.h"
31 #include "winhttp_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
35 static int sock_send(int fd, const void *msg, size_t len, int flags)
37 int ret;
40 if ((ret = send(fd, msg, len, flags)) == -1) WARN("send error %u\n", WSAGetLastError());
42 while(ret == -1 && WSAGetLastError() == WSAEINTR);
43 return ret;
46 static int sock_recv(int fd, void *msg, size_t len, int flags)
48 int ret;
51 if ((ret = recv(fd, msg, len, flags)) == -1) WARN("recv error %u\n", WSAGetLastError());
53 while(ret == -1 && WSAGetLastError() == WSAEINTR);
54 return ret;
57 static DWORD netconn_verify_cert( PCCERT_CONTEXT cert, WCHAR *server, DWORD security_flags, BOOL check_revocation )
59 HCERTSTORE store = cert->hCertStore;
60 BOOL ret;
61 CERT_CHAIN_PARA chainPara = { sizeof(chainPara), { 0 } };
62 PCCERT_CHAIN_CONTEXT chain;
63 char oid_server_auth[] = szOID_PKIX_KP_SERVER_AUTH;
64 char *server_auth[] = { oid_server_auth };
65 DWORD err = ERROR_SUCCESS;
67 TRACE("verifying %s\n", debugstr_w( server ));
68 chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
69 chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
70 ret = CertGetCertificateChain( NULL, cert, NULL, store, &chainPara,
71 check_revocation ? CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT : 0,
72 NULL, &chain );
73 if (ret)
75 if (chain->TrustStatus.dwErrorStatus)
77 static const DWORD supportedErrors =
78 CERT_TRUST_IS_NOT_TIME_VALID |
79 CERT_TRUST_IS_UNTRUSTED_ROOT |
80 CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
82 if (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
84 if (!(security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID))
85 err = ERROR_WINHTTP_SECURE_CERT_DATE_INVALID;
87 else if ((chain->TrustStatus.dwErrorStatus &
88 CERT_TRUST_IS_UNTRUSTED_ROOT) ||
89 (chain->TrustStatus.dwErrorStatus &
90 CERT_TRUST_IS_PARTIAL_CHAIN))
92 if (!(security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA))
93 err = ERROR_WINHTTP_SECURE_INVALID_CA;
95 else if ((chain->TrustStatus.dwErrorStatus &
96 CERT_TRUST_IS_OFFLINE_REVOCATION) ||
97 (chain->TrustStatus.dwErrorStatus &
98 CERT_TRUST_REVOCATION_STATUS_UNKNOWN))
99 err = ERROR_WINHTTP_SECURE_CERT_REV_FAILED;
100 else if (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_REVOKED)
101 err = ERROR_WINHTTP_SECURE_CERT_REVOKED;
102 else if (chain->TrustStatus.dwErrorStatus &
103 CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
105 if (!(security_flags & SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE))
106 err = ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE;
108 else if (chain->TrustStatus.dwErrorStatus & ~supportedErrors)
109 err = ERROR_WINHTTP_SECURE_INVALID_CERT;
111 if (!err)
113 CERT_CHAIN_POLICY_PARA policyPara;
114 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
115 CERT_CHAIN_POLICY_STATUS policyStatus;
116 CERT_CHAIN_CONTEXT chainCopy;
118 /* Clear chain->TrustStatus.dwErrorStatus so
119 * CertVerifyCertificateChainPolicy will verify additional checks
120 * rather than stopping with an existing, ignored error.
122 memcpy(&chainCopy, chain, sizeof(chainCopy));
123 chainCopy.TrustStatus.dwErrorStatus = 0;
124 sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
125 sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
126 sslExtraPolicyPara.pwszServerName = server;
127 sslExtraPolicyPara.fdwChecks = security_flags;
128 policyPara.cbSize = sizeof(policyPara);
129 policyPara.dwFlags = 0;
130 policyPara.pvExtraPolicyPara = &sslExtraPolicyPara;
131 ret = CertVerifyCertificateChainPolicy( CERT_CHAIN_POLICY_SSL,
132 &chainCopy, &policyPara,
133 &policyStatus );
134 /* Any error in the policy status indicates that the
135 * policy couldn't be verified.
137 if (ret && policyStatus.dwError)
139 if (policyStatus.dwError == CERT_E_CN_NO_MATCH)
140 err = ERROR_WINHTTP_SECURE_CERT_CN_INVALID;
141 else
142 err = ERROR_WINHTTP_SECURE_INVALID_CERT;
145 CertFreeCertificateChain( chain );
147 else
148 err = ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
149 TRACE("returning %08x\n", err);
150 return err;
153 static BOOL winsock_loaded;
155 void netconn_unload( void )
157 if (winsock_loaded) WSACleanup();
160 static BOOL WINAPI winsock_startup( INIT_ONCE *once, void *param, void **ctx )
162 int ret;
163 WSADATA data;
164 if (!(ret = WSAStartup( MAKEWORD(1,1), &data ))) winsock_loaded = TRUE;
165 else ERR( "WSAStartup failed: %d\n", ret );
166 return TRUE;
169 static void winsock_init(void)
171 static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
172 InitOnceExecuteOnce( &once, winsock_startup, NULL, NULL );
175 static void set_blocking( struct netconn *conn, BOOL blocking )
177 ULONG state = !blocking;
178 ioctlsocket( conn->socket, FIONBIO, &state );
181 DWORD netconn_create( struct hostdata *host, const struct sockaddr_storage *sockaddr, int timeout,
182 struct netconn **ret_conn )
184 struct netconn *conn;
185 unsigned int addr_len;
186 DWORD ret;
188 winsock_init();
190 if (!(conn = heap_alloc_zero( sizeof(*conn) ))) return ERROR_OUTOFMEMORY;
191 conn->host = host;
192 conn->sockaddr = *sockaddr;
193 if ((conn->socket = socket( sockaddr->ss_family, SOCK_STREAM, 0 )) == -1)
195 ret = WSAGetLastError();
196 WARN("unable to create socket (%u)\n", ret);
197 heap_free( conn );
198 return ret;
201 switch (conn->sockaddr.ss_family)
203 case AF_INET:
204 addr_len = sizeof(struct sockaddr_in);
205 break;
206 case AF_INET6:
207 addr_len = sizeof(struct sockaddr_in6);
208 break;
209 default:
210 ERR( "unhandled family %u\n", conn->sockaddr.ss_family );
211 heap_free( conn );
212 return ERROR_INVALID_PARAMETER;
215 if (timeout > 0) set_blocking( conn, FALSE );
217 if (!connect( conn->socket, (const struct sockaddr *)&conn->sockaddr, addr_len )) ret = ERROR_SUCCESS;
218 else
220 ret = WSAGetLastError();
221 if (ret == WSAEWOULDBLOCK || ret == WSAEINPROGRESS)
223 FD_SET set;
224 TIMEVAL timeval = { 0, timeout * 1000 };
225 int res;
227 FD_ZERO( &set );
228 FD_SET( conn->socket, &set );
229 if ((res = select( conn->socket + 1, NULL, &set, NULL, &timeval )) > 0) ret = ERROR_SUCCESS;
230 else if (!res) ret = ERROR_WINHTTP_TIMEOUT;
234 if (timeout > 0) set_blocking( conn, TRUE );
236 if (ret)
238 WARN("unable to connect to host (%u)\n", ret);
239 closesocket( conn->socket );
240 heap_free( conn );
241 return ret;
244 *ret_conn = conn;
245 return ERROR_SUCCESS;
248 void netconn_close( struct netconn *conn )
250 if (conn->secure)
252 heap_free( conn->peek_msg_mem );
253 heap_free(conn->ssl_buf);
254 heap_free(conn->extra_buf);
255 DeleteSecurityContext(&conn->ssl_ctx);
257 closesocket( conn->socket );
258 release_host( conn->host );
259 heap_free(conn);
262 DWORD netconn_secure_connect( struct netconn *conn, WCHAR *hostname, DWORD security_flags, CredHandle *cred_handle,
263 BOOL check_revocation )
265 SecBuffer out_buf = {0, SECBUFFER_TOKEN, NULL}, in_bufs[2] = {{0, SECBUFFER_TOKEN}, {0, SECBUFFER_EMPTY}};
266 SecBufferDesc out_desc = {SECBUFFER_VERSION, 1, &out_buf}, in_desc = {SECBUFFER_VERSION, 2, in_bufs};
267 BYTE *read_buf;
268 SIZE_T read_buf_size = 2048;
269 ULONG attrs = 0;
270 CtxtHandle ctx;
271 SSIZE_T size;
272 const CERT_CONTEXT *cert;
273 SECURITY_STATUS status;
274 DWORD res = ERROR_SUCCESS;
276 const DWORD isc_req_flags = ISC_REQ_ALLOCATE_MEMORY|ISC_REQ_USE_SESSION_KEY|ISC_REQ_CONFIDENTIALITY
277 |ISC_REQ_SEQUENCE_DETECT|ISC_REQ_REPLAY_DETECT|ISC_REQ_MANUAL_CRED_VALIDATION;
279 if (!(read_buf = heap_alloc( read_buf_size ))) return ERROR_OUTOFMEMORY;
281 status = InitializeSecurityContextW(cred_handle, NULL, hostname, isc_req_flags, 0, 0, NULL, 0,
282 &ctx, &out_desc, &attrs, NULL);
284 assert(status != SEC_E_OK);
286 while(status == SEC_I_CONTINUE_NEEDED || status == SEC_E_INCOMPLETE_MESSAGE) {
287 if(out_buf.cbBuffer) {
288 assert(status == SEC_I_CONTINUE_NEEDED);
290 TRACE("sending %u bytes\n", out_buf.cbBuffer);
292 size = sock_send(conn->socket, out_buf.pvBuffer, out_buf.cbBuffer, 0);
293 if(size != out_buf.cbBuffer) {
294 ERR("send failed\n");
295 res = ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
296 break;
299 FreeContextBuffer(out_buf.pvBuffer);
300 out_buf.pvBuffer = NULL;
301 out_buf.cbBuffer = 0;
304 if(status == SEC_I_CONTINUE_NEEDED) {
305 assert(in_bufs[1].cbBuffer < read_buf_size);
307 memmove(read_buf, (BYTE*)in_bufs[0].pvBuffer+in_bufs[0].cbBuffer-in_bufs[1].cbBuffer, in_bufs[1].cbBuffer);
308 in_bufs[0].cbBuffer = in_bufs[1].cbBuffer;
310 in_bufs[1].BufferType = SECBUFFER_EMPTY;
311 in_bufs[1].cbBuffer = 0;
312 in_bufs[1].pvBuffer = NULL;
315 assert(in_bufs[0].BufferType == SECBUFFER_TOKEN);
316 assert(in_bufs[1].BufferType == SECBUFFER_EMPTY);
318 if(in_bufs[0].cbBuffer + 1024 > read_buf_size) {
319 BYTE *new_read_buf;
321 new_read_buf = heap_realloc(read_buf, read_buf_size + 1024);
322 if(!new_read_buf) {
323 status = E_OUTOFMEMORY;
324 break;
327 in_bufs[0].pvBuffer = read_buf = new_read_buf;
328 read_buf_size += 1024;
331 size = sock_recv(conn->socket, read_buf+in_bufs[0].cbBuffer, read_buf_size-in_bufs[0].cbBuffer, 0);
332 if(size < 1) {
333 status = ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
334 break;
337 TRACE("recv %lu bytes\n", size);
339 in_bufs[0].cbBuffer += size;
340 in_bufs[0].pvBuffer = read_buf;
341 status = InitializeSecurityContextW(cred_handle, &ctx, hostname, isc_req_flags, 0, 0, &in_desc,
342 0, NULL, &out_desc, &attrs, NULL);
343 TRACE("InitializeSecurityContext ret %08x\n", status);
345 if(status == SEC_E_OK) {
346 if(in_bufs[1].BufferType == SECBUFFER_EXTRA)
347 FIXME("SECBUFFER_EXTRA not supported\n");
349 status = QueryContextAttributesW(&ctx, SECPKG_ATTR_STREAM_SIZES, &conn->ssl_sizes);
350 if(status != SEC_E_OK) {
351 WARN("Could not get sizes\n");
352 break;
355 status = QueryContextAttributesW(&ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&cert);
356 if(status == SEC_E_OK) {
357 res = netconn_verify_cert(cert, hostname, security_flags, check_revocation);
358 CertFreeCertificateContext(cert);
359 if(res != ERROR_SUCCESS) {
360 WARN("cert verify failed: %u\n", res);
361 break;
363 }else {
364 WARN("Could not get cert\n");
365 break;
368 conn->ssl_buf = heap_alloc(conn->ssl_sizes.cbHeader + conn->ssl_sizes.cbMaximumMessage + conn->ssl_sizes.cbTrailer);
369 if(!conn->ssl_buf) {
370 res = ERROR_OUTOFMEMORY;
371 break;
376 heap_free(read_buf);
378 if(status != SEC_E_OK || res != ERROR_SUCCESS) {
379 WARN("Failed to initialize security context: %08x\n", status);
380 heap_free(conn->ssl_buf);
381 conn->ssl_buf = NULL;
382 DeleteSecurityContext(&ctx);
383 return ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
387 TRACE("established SSL connection\n");
388 conn->secure = TRUE;
389 conn->ssl_ctx = ctx;
390 return ERROR_SUCCESS;
393 static DWORD send_ssl_chunk( struct netconn *conn, const void *msg, size_t size )
395 SecBuffer bufs[4] = {
396 {conn->ssl_sizes.cbHeader, SECBUFFER_STREAM_HEADER, conn->ssl_buf},
397 {size, SECBUFFER_DATA, conn->ssl_buf+conn->ssl_sizes.cbHeader},
398 {conn->ssl_sizes.cbTrailer, SECBUFFER_STREAM_TRAILER, conn->ssl_buf+conn->ssl_sizes.cbHeader+size},
399 {0, SECBUFFER_EMPTY, NULL}
401 SecBufferDesc buf_desc = {SECBUFFER_VERSION, ARRAY_SIZE(bufs), bufs};
402 SECURITY_STATUS res;
404 memcpy( bufs[1].pvBuffer, msg, size );
405 if ((res = EncryptMessage(&conn->ssl_ctx, 0, &buf_desc, 0)) != SEC_E_OK)
407 WARN("EncryptMessage failed: %08x\n", res);
408 return res;
411 if (sock_send( conn->socket, conn->ssl_buf, bufs[0].cbBuffer + bufs[1].cbBuffer + bufs[2].cbBuffer, 0 ) < 1)
413 WARN("send failed\n");
414 return WSAGetLastError();
417 return ERROR_SUCCESS;
420 DWORD netconn_send( struct netconn *conn, const void *msg, size_t len, int *sent )
422 if (conn->secure)
424 const BYTE *ptr = msg;
425 size_t chunk_size;
426 DWORD res;
428 *sent = 0;
429 while (len)
431 chunk_size = min( len, conn->ssl_sizes.cbMaximumMessage );
432 if ((res = send_ssl_chunk( conn, ptr, chunk_size )))
433 return res;
435 *sent += chunk_size;
436 ptr += chunk_size;
437 len -= chunk_size;
440 return ERROR_SUCCESS;
443 if ((*sent = sock_send( conn->socket, msg, len, 0 )) < 0) return WSAGetLastError();
444 return ERROR_SUCCESS;
447 static DWORD read_ssl_chunk( struct netconn *conn, void *buf, SIZE_T buf_size, SIZE_T *ret_size, BOOL *eof )
449 const SIZE_T ssl_buf_size = conn->ssl_sizes.cbHeader+conn->ssl_sizes.cbMaximumMessage+conn->ssl_sizes.cbTrailer;
450 SecBuffer bufs[4];
451 SecBufferDesc buf_desc = {SECBUFFER_VERSION, ARRAY_SIZE(bufs), bufs};
452 SSIZE_T size, buf_len;
453 unsigned int i;
454 SECURITY_STATUS res;
456 assert(conn->extra_len < ssl_buf_size);
458 if(conn->extra_len) {
459 memcpy(conn->ssl_buf, conn->extra_buf, conn->extra_len);
460 buf_len = conn->extra_len;
461 conn->extra_len = 0;
462 heap_free(conn->extra_buf);
463 conn->extra_buf = NULL;
464 }else {
465 if ((buf_len = sock_recv( conn->socket, conn->ssl_buf + conn->extra_len, ssl_buf_size - conn->extra_len, 0)) < 0)
466 return WSAGetLastError();
468 if (!buf_len)
470 *eof = TRUE;
471 return ERROR_SUCCESS;
475 *ret_size = 0;
476 *eof = FALSE;
478 do {
479 memset(bufs, 0, sizeof(bufs));
480 bufs[0].BufferType = SECBUFFER_DATA;
481 bufs[0].cbBuffer = buf_len;
482 bufs[0].pvBuffer = conn->ssl_buf;
484 switch ((res = DecryptMessage( &conn->ssl_ctx, &buf_desc, 0, NULL )))
486 case SEC_E_OK:
487 break;
489 case SEC_I_RENEGOTIATE:
490 TRACE("renegotiate\n");
491 return ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED;
493 case SEC_I_CONTEXT_EXPIRED:
494 TRACE("context expired\n");
495 *eof = TRUE;
496 return ERROR_SUCCESS;
498 case SEC_E_INCOMPLETE_MESSAGE:
499 assert(buf_len < ssl_buf_size);
501 if ((size = sock_recv( conn->socket, conn->ssl_buf + buf_len, ssl_buf_size - buf_len, 0 )) < 1)
502 return SEC_E_INCOMPLETE_MESSAGE;
504 buf_len += size;
505 continue;
507 default:
508 WARN("failed: %08x\n", res);
509 return res;
511 } while (res != SEC_E_OK);
513 for(i = 0; i < ARRAY_SIZE(bufs); i++) {
514 if(bufs[i].BufferType == SECBUFFER_DATA) {
515 size = min(buf_size, bufs[i].cbBuffer);
516 memcpy(buf, bufs[i].pvBuffer, size);
517 if(size < bufs[i].cbBuffer) {
518 assert(!conn->peek_len);
519 conn->peek_msg_mem = conn->peek_msg = heap_alloc(bufs[i].cbBuffer - size);
520 if(!conn->peek_msg)
521 return ERROR_OUTOFMEMORY;
522 conn->peek_len = bufs[i].cbBuffer-size;
523 memcpy(conn->peek_msg, (char*)bufs[i].pvBuffer+size, conn->peek_len);
526 *ret_size = size;
530 for(i = 0; i < ARRAY_SIZE(bufs); i++) {
531 if(bufs[i].BufferType == SECBUFFER_EXTRA) {
532 conn->extra_buf = heap_alloc(bufs[i].cbBuffer);
533 if(!conn->extra_buf)
534 return ERROR_OUTOFMEMORY;
536 conn->extra_len = bufs[i].cbBuffer;
537 memcpy(conn->extra_buf, bufs[i].pvBuffer, conn->extra_len);
541 return ERROR_SUCCESS;
544 DWORD netconn_recv( struct netconn *conn, void *buf, size_t len, int flags, int *recvd )
546 *recvd = 0;
547 if (!len) return ERROR_SUCCESS;
549 if (conn->secure)
551 SIZE_T size;
552 DWORD res;
553 BOOL eof;
555 if (conn->peek_msg)
557 *recvd = min( len, conn->peek_len );
558 memcpy( buf, conn->peek_msg, *recvd );
559 conn->peek_len -= *recvd;
560 conn->peek_msg += *recvd;
562 if (conn->peek_len == 0)
564 heap_free( conn->peek_msg_mem );
565 conn->peek_msg_mem = NULL;
566 conn->peek_msg = NULL;
568 /* check if we have enough data from the peek buffer */
569 if (!(flags & MSG_WAITALL) || *recvd == len) return ERROR_SUCCESS;
571 size = *recvd;
575 SIZE_T cread = 0;
576 if ((res = read_ssl_chunk( conn, (BYTE *)buf + size, len - size, &cread, &eof )))
578 WARN("read_ssl_chunk failed: %u\n", res);
579 if (!size) return res;
580 break;
582 if (eof)
584 TRACE("EOF\n");
585 break;
587 size += cread;
589 } while (!size || ((flags & MSG_WAITALL) && size < len));
591 TRACE("received %ld bytes\n", size);
592 *recvd = size;
593 return ERROR_SUCCESS;
596 if ((*recvd = sock_recv( conn->socket, buf, len, flags )) < 0) return WSAGetLastError();
597 return ERROR_SUCCESS;
600 ULONG netconn_query_data_available( struct netconn *conn )
602 return conn->secure ? conn->peek_len : 0;
605 DWORD netconn_set_timeout( struct netconn *netconn, BOOL send, int value )
607 int opt = send ? SO_SNDTIMEO : SO_RCVTIMEO;
608 if (setsockopt( netconn->socket, SOL_SOCKET, opt, (void *)&value, sizeof(value) ) == -1)
610 DWORD err = WSAGetLastError();
611 WARN("setsockopt failed (%u)\n", err );
612 return err;
614 return ERROR_SUCCESS;
617 BOOL netconn_is_alive( struct netconn *netconn )
619 int len;
620 char b;
621 DWORD err;
623 set_blocking( netconn, FALSE );
624 len = sock_recv( netconn->socket, &b, 1, MSG_PEEK );
625 err = WSAGetLastError();
626 set_blocking( netconn, TRUE );
628 return len == 1 || (len == -1 && err == WSAEWOULDBLOCK);
631 static DWORD resolve_hostname( const WCHAR *name, INTERNET_PORT port, struct sockaddr_storage *sa )
633 ADDRINFOW *res, hints;
634 int ret;
636 memset( &hints, 0, sizeof(hints) );
637 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
638 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
640 hints.ai_family = AF_INET;
642 ret = GetAddrInfoW( name, NULL, &hints, &res );
643 if (ret != 0)
645 TRACE("failed to get IPv4 address of %s, retrying with IPv6\n", debugstr_w(name));
646 hints.ai_family = AF_INET6;
647 ret = GetAddrInfoW( name, NULL, &hints, &res );
648 if (ret != 0)
650 TRACE("failed to get address of %s\n", debugstr_w(name));
651 return ERROR_WINHTTP_NAME_NOT_RESOLVED;
654 memcpy( sa, res->ai_addr, res->ai_addrlen );
655 switch (res->ai_family)
657 case AF_INET:
658 ((struct sockaddr_in *)sa)->sin_port = htons( port );
659 break;
660 case AF_INET6:
661 ((struct sockaddr_in6 *)sa)->sin6_port = htons( port );
662 break;
665 FreeAddrInfoW( res );
666 return ERROR_SUCCESS;
669 struct async_resolve
671 const WCHAR *hostname;
672 INTERNET_PORT port;
673 struct sockaddr_storage *addr;
674 DWORD result;
675 HANDLE done;
678 static void CALLBACK resolve_proc( TP_CALLBACK_INSTANCE *instance, void *ctx )
680 struct async_resolve *async = ctx;
681 async->result = resolve_hostname( async->hostname, async->port, async->addr );
682 SetEvent( async->done );
685 DWORD netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr_storage *addr, int timeout )
687 DWORD ret;
689 if (!timeout) ret = resolve_hostname( hostname, port, addr );
690 else
692 struct async_resolve async;
694 async.hostname = hostname;
695 async.port = port;
696 async.addr = addr;
697 if (!(async.done = CreateEventW( NULL, FALSE, FALSE, NULL ))) return GetLastError();
698 if (!TrySubmitThreadpoolCallback( resolve_proc, &async, NULL ))
700 CloseHandle( async.done );
701 return GetLastError();
703 if (WaitForSingleObject( async.done, timeout ) != WAIT_OBJECT_0) ret = ERROR_WINHTTP_TIMEOUT;
704 else ret = async.result;
705 CloseHandle( async.done );
708 return ret;
711 const void *netconn_get_certificate( struct netconn *conn )
713 const CERT_CONTEXT *ret;
714 SECURITY_STATUS res;
716 if (!conn->secure) return NULL;
717 res = QueryContextAttributesW(&conn->ssl_ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&ret);
718 return res == SEC_E_OK ? ret : NULL;
721 int netconn_get_cipher_strength( struct netconn *conn )
723 SecPkgContext_ConnectionInfo conn_info;
724 SECURITY_STATUS res;
726 if (!conn->secure) return 0;
727 res = QueryContextAttributesW(&conn->ssl_ctx, SECPKG_ATTR_CONNECTION_INFO, (void*)&conn_info);
728 if(res != SEC_E_OK)
729 WARN("QueryContextAttributesW failed: %08x\n", res);
730 return res == SEC_E_OK ? conn_info.dwCipherStrength : 0;