winhttp: Set callback to verify hostname with peer's certificate.
[wine/multimedia.git] / dlls / winhttp / net.c
bloba86a5a6277225d9b28b8d23040a4649e9e4a15f4
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
29 #endif
30 #ifdef HAVE_SYS_IOCTL_H
31 # include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_SYS_FILIO_H
34 # include <sys/filio.h>
35 #endif
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #ifdef HAVE_OPENSSL_SSL_H
40 # include <openssl/ssl.h>
41 #undef FAR
42 #undef DSA
43 #endif
45 #include "wine/debug.h"
46 #include "wine/library.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winhttp.h"
51 #include "wincrypt.h"
53 #include "winhttp_private.h"
55 /* to avoid conflicts with the Unix socket headers */
56 #define USE_WS_PREFIX
57 #include "winsock2.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
61 #ifndef HAVE_GETADDRINFO
63 /* critical section to protect non-reentrant gethostbyname() */
64 static CRITICAL_SECTION cs_gethostbyname;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
67 0, 0, &cs_gethostbyname,
68 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
71 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
73 #endif
75 #ifdef SONAME_LIBSSL
77 #include <openssl/err.h>
79 static CRITICAL_SECTION init_ssl_cs;
80 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
82 0, 0, &init_ssl_cs,
83 { &init_ssl_cs_debug.ProcessLocksList,
84 &init_ssl_cs_debug.ProcessLocksList },
85 0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
87 static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
89 static void *libssl_handle;
90 static void *libcrypto_handle;
92 static SSL_METHOD *method;
93 static SSL_CTX *ctx;
94 static int hostname_idx;
96 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
98 MAKE_FUNCPTR( SSL_library_init );
99 MAKE_FUNCPTR( SSL_load_error_strings );
100 MAKE_FUNCPTR( SSLv23_method );
101 MAKE_FUNCPTR( SSL_CTX_free );
102 MAKE_FUNCPTR( SSL_CTX_new );
103 MAKE_FUNCPTR( SSL_new );
104 MAKE_FUNCPTR( SSL_free );
105 MAKE_FUNCPTR( SSL_set_fd );
106 MAKE_FUNCPTR( SSL_connect );
107 MAKE_FUNCPTR( SSL_shutdown );
108 MAKE_FUNCPTR( SSL_write );
109 MAKE_FUNCPTR( SSL_read );
110 MAKE_FUNCPTR( SSL_get_ex_new_index );
111 MAKE_FUNCPTR( SSL_get_ex_data );
112 MAKE_FUNCPTR( SSL_set_ex_data );
113 MAKE_FUNCPTR( SSL_get_ex_data_X509_STORE_CTX_idx );
114 MAKE_FUNCPTR( SSL_get_verify_result );
115 MAKE_FUNCPTR( SSL_get_peer_certificate );
116 MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths );
117 MAKE_FUNCPTR( SSL_CTX_set_verify );
118 MAKE_FUNCPTR( X509_STORE_CTX_get_ex_data );
120 MAKE_FUNCPTR( BIO_new_fp );
121 MAKE_FUNCPTR( CRYPTO_num_locks );
122 MAKE_FUNCPTR( CRYPTO_set_id_callback );
123 MAKE_FUNCPTR( CRYPTO_set_locking_callback );
124 MAKE_FUNCPTR( ERR_get_error );
125 MAKE_FUNCPTR( ERR_error_string );
126 MAKE_FUNCPTR( i2d_X509 );
127 #undef MAKE_FUNCPTR
129 static CRITICAL_SECTION *ssl_locks;
130 static unsigned int num_ssl_locks;
132 static unsigned long ssl_thread_id(void)
134 return GetCurrentThreadId();
137 static void ssl_lock_callback(int mode, int type, const char *file, int line)
139 if (mode & CRYPTO_LOCK)
140 EnterCriticalSection( &ssl_locks[type] );
141 else
142 LeaveCriticalSection( &ssl_locks[type] );
145 #endif
147 /* translate a unix error code into a winsock error code */
148 static int sock_get_error( int err )
150 #if !defined(__MINGW32__) && !defined (_MSC_VER)
151 switch (err)
153 case EINTR: return WSAEINTR;
154 case EBADF: return WSAEBADF;
155 case EPERM:
156 case EACCES: return WSAEACCES;
157 case EFAULT: return WSAEFAULT;
158 case EINVAL: return WSAEINVAL;
159 case EMFILE: return WSAEMFILE;
160 case EWOULDBLOCK: return WSAEWOULDBLOCK;
161 case EINPROGRESS: return WSAEINPROGRESS;
162 case EALREADY: return WSAEALREADY;
163 case ENOTSOCK: return WSAENOTSOCK;
164 case EDESTADDRREQ: return WSAEDESTADDRREQ;
165 case EMSGSIZE: return WSAEMSGSIZE;
166 case EPROTOTYPE: return WSAEPROTOTYPE;
167 case ENOPROTOOPT: return WSAENOPROTOOPT;
168 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
169 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
170 case EOPNOTSUPP: return WSAEOPNOTSUPP;
171 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
172 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
173 case EADDRINUSE: return WSAEADDRINUSE;
174 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
175 case ENETDOWN: return WSAENETDOWN;
176 case ENETUNREACH: return WSAENETUNREACH;
177 case ENETRESET: return WSAENETRESET;
178 case ECONNABORTED: return WSAECONNABORTED;
179 case EPIPE:
180 case ECONNRESET: return WSAECONNRESET;
181 case ENOBUFS: return WSAENOBUFS;
182 case EISCONN: return WSAEISCONN;
183 case ENOTCONN: return WSAENOTCONN;
184 case ESHUTDOWN: return WSAESHUTDOWN;
185 case ETOOMANYREFS: return WSAETOOMANYREFS;
186 case ETIMEDOUT: return WSAETIMEDOUT;
187 case ECONNREFUSED: return WSAECONNREFUSED;
188 case ELOOP: return WSAELOOP;
189 case ENAMETOOLONG: return WSAENAMETOOLONG;
190 case EHOSTDOWN: return WSAEHOSTDOWN;
191 case EHOSTUNREACH: return WSAEHOSTUNREACH;
192 case ENOTEMPTY: return WSAENOTEMPTY;
193 #ifdef EPROCLIM
194 case EPROCLIM: return WSAEPROCLIM;
195 #endif
196 #ifdef EUSERS
197 case EUSERS: return WSAEUSERS;
198 #endif
199 #ifdef EDQUOT
200 case EDQUOT: return WSAEDQUOT;
201 #endif
202 #ifdef ESTALE
203 case ESTALE: return WSAESTALE;
204 #endif
205 #ifdef EREMOTE
206 case EREMOTE: return WSAEREMOTE;
207 #endif
208 default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
210 #endif
211 return err;
214 #ifdef SONAME_LIBSSL
215 static int netconn_secure_verify( int preverify_ok, X509_STORE_CTX *ctx )
217 SSL *ssl;
218 WCHAR *server;
220 ssl = pX509_STORE_CTX_get_ex_data( ctx, pSSL_get_ex_data_X509_STORE_CTX_idx() );
221 server = pSSL_get_ex_data( ssl, hostname_idx );
222 FIXME("verify %s\n", debugstr_w(server));
223 return preverify_ok;
225 #endif
227 BOOL netconn_init( netconn_t *conn, BOOL secure )
229 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
230 int i;
231 #endif
233 conn->socket = -1;
234 if (!secure) return TRUE;
236 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
237 EnterCriticalSection( &init_ssl_cs );
238 if (libssl_handle)
240 LeaveCriticalSection( &init_ssl_cs );
241 return TRUE;
243 if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
245 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
246 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
247 LeaveCriticalSection( &init_ssl_cs );
248 return FALSE;
250 if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
252 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
253 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
254 LeaveCriticalSection( &init_ssl_cs );
255 return FALSE;
257 #define LOAD_FUNCPTR(x) \
258 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
260 ERR("Failed to load symbol %s\n", #x); \
261 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
262 LeaveCriticalSection( &init_ssl_cs ); \
263 return FALSE; \
265 LOAD_FUNCPTR( SSL_library_init );
266 LOAD_FUNCPTR( SSL_load_error_strings );
267 LOAD_FUNCPTR( SSLv23_method );
268 LOAD_FUNCPTR( SSL_CTX_free );
269 LOAD_FUNCPTR( SSL_CTX_new );
270 LOAD_FUNCPTR( SSL_new );
271 LOAD_FUNCPTR( SSL_free );
272 LOAD_FUNCPTR( SSL_set_fd );
273 LOAD_FUNCPTR( SSL_connect );
274 LOAD_FUNCPTR( SSL_shutdown );
275 LOAD_FUNCPTR( SSL_write );
276 LOAD_FUNCPTR( SSL_read );
277 LOAD_FUNCPTR( SSL_get_ex_new_index );
278 LOAD_FUNCPTR( SSL_get_ex_data );
279 LOAD_FUNCPTR( SSL_set_ex_data );
280 LOAD_FUNCPTR( SSL_get_ex_data_X509_STORE_CTX_idx );
281 LOAD_FUNCPTR( SSL_get_verify_result );
282 LOAD_FUNCPTR( SSL_get_peer_certificate );
283 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
284 LOAD_FUNCPTR( SSL_CTX_set_verify );
285 LOAD_FUNCPTR( X509_STORE_CTX_get_ex_data );
286 #undef LOAD_FUNCPTR
288 #define LOAD_FUNCPTR(x) \
289 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
291 ERR("Failed to load symbol %s\n", #x); \
292 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
293 LeaveCriticalSection( &init_ssl_cs ); \
294 return FALSE; \
296 LOAD_FUNCPTR( BIO_new_fp );
297 LOAD_FUNCPTR( CRYPTO_num_locks );
298 LOAD_FUNCPTR( CRYPTO_set_id_callback );
299 LOAD_FUNCPTR( CRYPTO_set_locking_callback );
300 LOAD_FUNCPTR( ERR_get_error );
301 LOAD_FUNCPTR( ERR_error_string );
302 LOAD_FUNCPTR( i2d_X509 );
303 #undef LOAD_FUNCPTR
305 pSSL_library_init();
306 pSSL_load_error_strings();
307 pBIO_new_fp( stderr, BIO_NOCLOSE );
309 method = pSSLv23_method();
310 ctx = pSSL_CTX_new( method );
311 if (!pSSL_CTX_set_default_verify_paths( ctx ))
313 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
314 set_last_error( ERROR_OUTOFMEMORY );
315 LeaveCriticalSection( &init_ssl_cs );
316 return FALSE;
318 hostname_idx = pSSL_get_ex_new_index( 0, (void *)"hostname index", NULL, NULL, NULL );
319 if (hostname_idx == -1)
321 ERR("SSL_get_ex_new_index failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
322 set_last_error( ERROR_OUTOFMEMORY );
323 LeaveCriticalSection( &init_ssl_cs );
324 return FALSE;
326 pSSL_CTX_set_verify( ctx, SSL_VERIFY_PEER, netconn_secure_verify );
328 pCRYPTO_set_id_callback(ssl_thread_id);
329 num_ssl_locks = pCRYPTO_num_locks();
330 ssl_locks = HeapAlloc(GetProcessHeap(), 0, num_ssl_locks * sizeof(CRITICAL_SECTION));
331 if (!ssl_locks)
333 set_last_error( ERROR_OUTOFMEMORY );
334 LeaveCriticalSection( &init_ssl_cs );
335 return FALSE;
337 for (i = 0; i < num_ssl_locks; i++) InitializeCriticalSection( &ssl_locks[i] );
338 pCRYPTO_set_locking_callback(ssl_lock_callback);
340 LeaveCriticalSection( &init_ssl_cs );
341 #else
342 WARN("SSL support not compiled in.\n");
343 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
344 return FALSE;
345 #endif
346 return TRUE;
349 void netconn_unload( void )
351 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
352 if (libcrypto_handle)
354 wine_dlclose( libcrypto_handle, NULL, 0 );
356 if (libssl_handle)
358 if (ctx)
359 pSSL_CTX_free( ctx );
360 wine_dlclose( libssl_handle, NULL, 0 );
362 if (ssl_locks)
364 int i;
365 for (i = 0; i < num_ssl_locks; i++) DeleteCriticalSection( &ssl_locks[i] );
366 HeapFree( GetProcessHeap(), 0, ssl_locks );
368 #endif
371 BOOL netconn_connected( netconn_t *conn )
373 return (conn->socket != -1);
376 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
378 if ((conn->socket = socket( domain, type, protocol )) == -1)
380 WARN("unable to create socket (%s)\n", strerror(errno));
381 set_last_error( sock_get_error( errno ) );
382 return FALSE;
384 return TRUE;
387 BOOL netconn_close( netconn_t *conn )
389 int res;
391 #ifdef SONAME_LIBSSL
392 if (conn->secure)
394 heap_free( conn->peek_msg_mem );
395 conn->peek_msg_mem = NULL;
396 conn->peek_msg = NULL;
397 conn->peek_len = 0;
399 pSSL_shutdown( conn->ssl_conn );
400 pSSL_free( conn->ssl_conn );
402 conn->ssl_conn = NULL;
403 conn->secure = FALSE;
405 #endif
406 res = closesocket( conn->socket );
407 conn->socket = -1;
408 if (res == -1)
410 set_last_error( sock_get_error( errno ) );
411 return FALSE;
413 return TRUE;
416 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout )
418 BOOL ret = FALSE;
419 int res = 0, state;
421 if (timeout > 0)
423 state = 1;
424 ioctlsocket( conn->socket, FIONBIO, &state );
426 if (connect( conn->socket, sockaddr, addr_len ) < 0)
428 res = sock_get_error( errno );
429 if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
431 struct pollfd pfd;
433 pfd.fd = conn->socket;
434 pfd.events = POLLOUT;
435 if (poll( &pfd, 1, timeout ) > 0)
436 ret = TRUE;
437 else
438 res = sock_get_error( errno );
441 else
442 ret = TRUE;
443 if (timeout > 0)
445 state = 0;
446 ioctlsocket( conn->socket, FIONBIO, &state );
448 if (!ret)
450 WARN("unable to connect to host (%d)\n", res);
451 set_last_error( res );
453 return ret;
456 BOOL netconn_secure_connect( netconn_t *conn, WCHAR *hostname )
458 #ifdef SONAME_LIBSSL
459 long res;
461 if (!(conn->ssl_conn = pSSL_new( ctx )))
463 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
464 set_last_error( ERROR_OUTOFMEMORY );
465 goto fail;
467 if (!pSSL_set_ex_data( conn->ssl_conn, hostname_idx, hostname ))
469 ERR("SSL_set_ex_data failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
470 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
471 goto fail;
473 if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
475 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
476 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
477 goto fail;
479 if (pSSL_connect( conn->ssl_conn ) <= 0)
481 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
482 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
483 goto fail;
485 if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
487 /* FIXME: we should set an error and return, but we only print an error at the moment */
488 ERR("couldn't verify server certificate (%ld)\n", res);
490 TRACE("established SSL connection\n");
491 conn->secure = TRUE;
492 return TRUE;
494 fail:
495 if (conn->ssl_conn)
497 pSSL_shutdown( conn->ssl_conn );
498 pSSL_free( conn->ssl_conn );
499 conn->ssl_conn = NULL;
501 #endif
502 return FALSE;
505 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
507 if (!netconn_connected( conn )) return FALSE;
508 if (conn->secure)
510 #ifdef SONAME_LIBSSL
511 if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
512 *sent = pSSL_write( conn->ssl_conn, msg, len );
513 if (*sent < 1 && len) return FALSE;
514 return TRUE;
515 #else
516 return FALSE;
517 #endif
519 if ((*sent = send( conn->socket, msg, len, flags )) == -1)
521 set_last_error( sock_get_error( errno ) );
522 return FALSE;
524 return TRUE;
527 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
529 *recvd = 0;
530 if (!netconn_connected( conn )) return FALSE;
531 if (!len) return TRUE;
533 if (conn->secure)
535 #ifdef SONAME_LIBSSL
536 if (flags & ~(MSG_PEEK | MSG_WAITALL))
537 FIXME("SSL_read does not support the following flags: %08x\n", flags);
539 /* this ugly hack is all for MSG_PEEK */
540 if (flags & MSG_PEEK && !conn->peek_msg)
542 if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
544 else if (flags & MSG_PEEK && conn->peek_msg)
546 if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
547 *recvd = min( len, conn->peek_len );
548 memcpy( buf, conn->peek_msg, *recvd );
549 return TRUE;
551 else if (conn->peek_msg)
553 *recvd = min( len, conn->peek_len );
554 memcpy( buf, conn->peek_msg, *recvd );
555 conn->peek_len -= *recvd;
556 conn->peek_msg += *recvd;
558 if (conn->peek_len == 0)
560 heap_free( conn->peek_msg_mem );
561 conn->peek_msg_mem = NULL;
562 conn->peek_msg = NULL;
564 /* check if we have enough data from the peek buffer */
565 if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
567 *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
568 if (flags & MSG_PEEK) /* must copy into buffer */
570 conn->peek_len = *recvd;
571 if (!*recvd)
573 heap_free( conn->peek_msg_mem );
574 conn->peek_msg_mem = NULL;
575 conn->peek_msg = NULL;
577 else memcpy( conn->peek_msg, buf, *recvd );
579 if (*recvd < 1 && len) return FALSE;
580 return TRUE;
581 #else
582 return FALSE;
583 #endif
585 if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
587 set_last_error( sock_get_error( errno ) );
588 return FALSE;
590 return TRUE;
593 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
595 #ifdef FIONREAD
596 int ret, unread;
597 #endif
598 *available = 0;
599 if (!netconn_connected( conn )) return FALSE;
601 if (conn->secure)
603 #ifdef SONAME_LIBSSL
604 if (conn->peek_msg) *available = conn->peek_len;
605 #endif
606 return TRUE;
608 #ifdef FIONREAD
609 if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
610 #endif
611 return TRUE;
614 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
616 struct pollfd pfd;
617 BOOL ret = FALSE;
618 DWORD recvd = 0;
620 if (!netconn_connected( conn )) return FALSE;
622 if (conn->secure)
624 #ifdef SONAME_LIBSSL
625 while (recvd < *buflen)
627 int dummy;
628 if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
630 set_last_error( ERROR_CONNECTION_ABORTED );
631 break;
633 if (buffer[recvd] == '\n')
635 ret = TRUE;
636 break;
638 if (buffer[recvd] != '\r') recvd++;
640 if (ret)
642 buffer[recvd++] = 0;
643 *buflen = recvd;
644 TRACE("received line %s\n", debugstr_a(buffer));
646 return ret;
647 #else
648 return FALSE;
649 #endif
652 pfd.fd = conn->socket;
653 pfd.events = POLLIN;
654 while (recvd < *buflen)
656 int timeout, res;
657 struct timeval tv;
658 socklen_t len = sizeof(tv);
660 if ((res = getsockopt( conn->socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &len ) != -1))
661 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
662 else
663 timeout = -1;
664 if (poll( &pfd, 1, timeout ) > 0)
666 if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
668 if (res == -1) set_last_error( sock_get_error( errno ) );
669 break;
671 if (buffer[recvd] == '\n')
673 ret = TRUE;
674 break;
676 if (buffer[recvd] != '\r') recvd++;
678 else
680 set_last_error( ERROR_WINHTTP_TIMEOUT );
681 break;
684 if (ret)
686 buffer[recvd++] = 0;
687 *buflen = recvd;
688 TRACE("received line %s\n", debugstr_a(buffer));
690 return ret;
693 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
695 int res;
696 struct timeval tv;
698 /* value is in milliseconds, convert to struct timeval */
699 tv.tv_sec = value / 1000;
700 tv.tv_usec = (value % 1000) * 1000;
702 if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
704 WARN("setsockopt failed (%s)\n", strerror( errno ));
705 return sock_get_error( errno );
707 return ERROR_SUCCESS;
710 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len )
712 char *hostname;
713 #ifdef HAVE_GETADDRINFO
714 struct addrinfo *res, hints;
715 int ret;
716 #else
717 struct hostent *he;
718 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
719 #endif
721 if (!(hostname = strdupWA( hostnameW ))) return FALSE;
723 #ifdef HAVE_GETADDRINFO
724 memset( &hints, 0, sizeof(struct addrinfo) );
725 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
726 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
728 hints.ai_family = AF_INET;
730 ret = getaddrinfo( hostname, NULL, &hints, &res );
731 if (ret != 0)
733 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(hostnameW), gai_strerror(ret));
734 hints.ai_family = AF_INET6;
735 ret = getaddrinfo( hostname, NULL, &hints, &res );
736 if (ret != 0)
738 TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW), gai_strerror(ret));
739 heap_free( hostname );
740 return FALSE;
743 heap_free( hostname );
744 if (*sa_len < res->ai_addrlen)
746 WARN("address too small\n");
747 freeaddrinfo( res );
748 return FALSE;
750 *sa_len = res->ai_addrlen;
751 memcpy( sa, res->ai_addr, res->ai_addrlen );
752 /* Copy port */
753 switch (res->ai_family)
755 case AF_INET:
756 ((struct sockaddr_in *)sa)->sin_port = htons( port );
757 break;
758 case AF_INET6:
759 ((struct sockaddr_in6 *)sa)->sin6_port = htons( port );
760 break;
763 freeaddrinfo( res );
764 return TRUE;
765 #else
766 EnterCriticalSection( &cs_gethostbyname );
768 he = gethostbyname( hostname );
769 heap_free( hostname );
770 if (!he)
772 TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW), h_errno);
773 LeaveCriticalSection( &cs_gethostbyname );
774 return FALSE;
776 if (*sa_len < sizeof(struct sockaddr_in))
778 WARN("address too small\n");
779 LeaveCriticalSection( &cs_gethostbyname );
780 return FALSE;
782 *sa_len = sizeof(struct sockaddr_in);
783 memset( sa, 0, sizeof(struct sockaddr_in) );
784 memcpy( &sin->sin_addr, he->h_addr, he->h_length );
785 sin->sin_family = he->h_addrtype;
786 sin->sin_port = htons( port );
788 LeaveCriticalSection( &cs_gethostbyname );
789 return TRUE;
790 #endif
793 const void *netconn_get_certificate( netconn_t *conn )
795 #ifdef SONAME_LIBSSL
796 X509 *cert;
797 unsigned char *buffer, *p;
798 int len;
799 BOOL malloc = FALSE;
800 const CERT_CONTEXT *ret;
802 if (!conn->secure) return NULL;
804 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
805 p = NULL;
806 if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
808 * SSL 0.9.7 and above malloc the buffer if it is null.
809 * however earlier version do not and so we would need to alloc the buffer.
811 * see the i2d_X509 man page for more details.
813 if (!p)
815 if (!(buffer = heap_alloc( len ))) return NULL;
816 p = buffer;
817 len = pi2d_X509( cert, &p );
819 else
821 buffer = p;
822 malloc = TRUE;
825 ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
827 if (malloc) free( buffer );
828 else heap_free( buffer );
830 return ret;
831 #else
832 return NULL;
833 #endif