d3dx8: Initialize the matrix stack in D3DXCreateMatrixStack().
[wine/multimedia.git] / dlls / winhttp / net.c
blobb34305058c117aff2b5c5d99f2e47096dc373e51
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 #define DEFAULT_SEND_TIMEOUT 30
62 #define DEFAULT_RECEIVE_TIMEOUT 30
64 #ifndef HAVE_GETADDRINFO
66 /* critical section to protect non-reentrant gethostbyname() */
67 static CRITICAL_SECTION cs_gethostbyname;
68 static CRITICAL_SECTION_DEBUG critsect_debug =
70 0, 0, &cs_gethostbyname,
71 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
72 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
74 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
76 #endif
78 #ifdef SONAME_LIBSSL
80 #include <openssl/err.h>
82 static void *libssl_handle;
83 static void *libcrypto_handle;
85 static SSL_METHOD *method;
86 static SSL_CTX *ctx;
88 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
90 MAKE_FUNCPTR( SSL_library_init );
91 MAKE_FUNCPTR( SSL_load_error_strings );
92 MAKE_FUNCPTR( SSLv23_method );
93 MAKE_FUNCPTR( SSL_CTX_new );
94 MAKE_FUNCPTR( SSL_new );
95 MAKE_FUNCPTR( SSL_free );
96 MAKE_FUNCPTR( SSL_set_fd );
97 MAKE_FUNCPTR( SSL_connect );
98 MAKE_FUNCPTR( SSL_shutdown );
99 MAKE_FUNCPTR( SSL_write );
100 MAKE_FUNCPTR( SSL_read );
101 MAKE_FUNCPTR( SSL_get_verify_result );
102 MAKE_FUNCPTR( SSL_get_peer_certificate );
103 MAKE_FUNCPTR( SSL_CTX_get_timeout );
104 MAKE_FUNCPTR( SSL_CTX_set_timeout );
105 MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths );
106 MAKE_FUNCPTR( i2d_X509 );
108 MAKE_FUNCPTR( BIO_new_fp );
109 MAKE_FUNCPTR( ERR_get_error );
110 MAKE_FUNCPTR( ERR_error_string );
111 #undef MAKE_FUNCPTR
113 #endif
115 /* translate a unix error code into a winsock error code */
116 static int sock_get_error( int err )
118 #if !defined(__MINGW32__) && !defined (_MSC_VER)
119 switch (err)
121 case EINTR: return WSAEINTR;
122 case EBADF: return WSAEBADF;
123 case EPERM:
124 case EACCES: return WSAEACCES;
125 case EFAULT: return WSAEFAULT;
126 case EINVAL: return WSAEINVAL;
127 case EMFILE: return WSAEMFILE;
128 case EWOULDBLOCK: return WSAEWOULDBLOCK;
129 case EINPROGRESS: return WSAEINPROGRESS;
130 case EALREADY: return WSAEALREADY;
131 case ENOTSOCK: return WSAENOTSOCK;
132 case EDESTADDRREQ: return WSAEDESTADDRREQ;
133 case EMSGSIZE: return WSAEMSGSIZE;
134 case EPROTOTYPE: return WSAEPROTOTYPE;
135 case ENOPROTOOPT: return WSAENOPROTOOPT;
136 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
137 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
138 case EOPNOTSUPP: return WSAEOPNOTSUPP;
139 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
140 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
141 case EADDRINUSE: return WSAEADDRINUSE;
142 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
143 case ENETDOWN: return WSAENETDOWN;
144 case ENETUNREACH: return WSAENETUNREACH;
145 case ENETRESET: return WSAENETRESET;
146 case ECONNABORTED: return WSAECONNABORTED;
147 case EPIPE:
148 case ECONNRESET: return WSAECONNRESET;
149 case ENOBUFS: return WSAENOBUFS;
150 case EISCONN: return WSAEISCONN;
151 case ENOTCONN: return WSAENOTCONN;
152 case ESHUTDOWN: return WSAESHUTDOWN;
153 case ETOOMANYREFS: return WSAETOOMANYREFS;
154 case ETIMEDOUT: return WSAETIMEDOUT;
155 case ECONNREFUSED: return WSAECONNREFUSED;
156 case ELOOP: return WSAELOOP;
157 case ENAMETOOLONG: return WSAENAMETOOLONG;
158 case EHOSTDOWN: return WSAEHOSTDOWN;
159 case EHOSTUNREACH: return WSAEHOSTUNREACH;
160 case ENOTEMPTY: return WSAENOTEMPTY;
161 #ifdef EPROCLIM
162 case EPROCLIM: return WSAEPROCLIM;
163 #endif
164 #ifdef EUSERS
165 case EUSERS: return WSAEUSERS;
166 #endif
167 #ifdef EDQUOT
168 case EDQUOT: return WSAEDQUOT;
169 #endif
170 #ifdef ESTALE
171 case ESTALE: return WSAESTALE;
172 #endif
173 #ifdef EREMOTE
174 case EREMOTE: return WSAEREMOTE;
175 #endif
176 default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
178 #endif
179 return err;
182 BOOL netconn_init( netconn_t *conn, BOOL secure )
184 conn->socket = -1;
185 if (!secure) return TRUE;
187 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
188 if (libssl_handle) return TRUE;
189 if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
191 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
192 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
193 return FALSE;
195 if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
197 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
198 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
199 return FALSE;
201 #define LOAD_FUNCPTR(x) \
202 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
204 ERR("Failed to load symbol %s\n", #x); \
205 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
206 return FALSE; \
208 LOAD_FUNCPTR( SSL_library_init );
209 LOAD_FUNCPTR( SSL_load_error_strings );
210 LOAD_FUNCPTR( SSLv23_method );
211 LOAD_FUNCPTR( SSL_CTX_new );
212 LOAD_FUNCPTR( SSL_new );
213 LOAD_FUNCPTR( SSL_free );
214 LOAD_FUNCPTR( SSL_set_fd );
215 LOAD_FUNCPTR( SSL_connect );
216 LOAD_FUNCPTR( SSL_shutdown );
217 LOAD_FUNCPTR( SSL_write );
218 LOAD_FUNCPTR( SSL_read );
219 LOAD_FUNCPTR( SSL_get_verify_result );
220 LOAD_FUNCPTR( SSL_get_peer_certificate );
221 LOAD_FUNCPTR( SSL_CTX_get_timeout );
222 LOAD_FUNCPTR( SSL_CTX_set_timeout );
223 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
224 LOAD_FUNCPTR( i2d_X509 );
225 #undef LOAD_FUNCPTR
227 #define LOAD_FUNCPTR(x) \
228 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
230 ERR("Failed to load symbol %s\n", #x); \
231 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
232 return FALSE; \
234 LOAD_FUNCPTR( BIO_new_fp );
235 LOAD_FUNCPTR( ERR_get_error );
236 LOAD_FUNCPTR( ERR_error_string );
237 #undef LOAD_FUNCPTR
239 pSSL_library_init();
240 pSSL_load_error_strings();
241 pBIO_new_fp( stderr, BIO_NOCLOSE );
243 method = pSSLv23_method();
244 #else
245 WARN("SSL support not compiled in.\n");
246 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
247 return FALSE;
248 #endif
249 return TRUE;
252 BOOL netconn_connected( netconn_t *conn )
254 return (conn->socket != -1);
257 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
259 if ((conn->socket = socket( domain, type, protocol )) == -1)
261 WARN("unable to create socket (%s)\n", strerror(errno));
262 set_last_error( sock_get_error( errno ) );
263 return FALSE;
265 return TRUE;
268 BOOL netconn_close( netconn_t *conn )
270 int res;
272 #ifdef SONAME_LIBSSL
273 if (conn->secure)
275 heap_free( conn->peek_msg_mem );
276 conn->peek_msg_mem = NULL;
277 conn->peek_msg = NULL;
278 conn->peek_len = 0;
280 pSSL_shutdown( conn->ssl_conn );
281 pSSL_free( conn->ssl_conn );
283 conn->ssl_conn = NULL;
284 conn->secure = FALSE;
286 #endif
287 res = closesocket( conn->socket );
288 conn->socket = -1;
289 if (res == -1)
291 set_last_error( sock_get_error( errno ) );
292 return FALSE;
294 return TRUE;
297 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len )
299 if (connect( conn->socket, sockaddr, addr_len ) == -1)
301 WARN("unable to connect to host (%s)\n", strerror(errno));
302 set_last_error( sock_get_error( errno ) );
303 return FALSE;
305 return TRUE;
308 BOOL netconn_secure_connect( netconn_t *conn )
310 #ifdef SONAME_LIBSSL
311 X509 *cert;
312 long res;
314 ctx = pSSL_CTX_new( method );
315 if (!pSSL_CTX_set_default_verify_paths( ctx ))
317 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
318 set_last_error( ERROR_OUTOFMEMORY );
319 return FALSE;
321 if (!(conn->ssl_conn = pSSL_new( ctx )))
323 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
324 set_last_error( ERROR_OUTOFMEMORY );
325 goto fail;
327 if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
329 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
330 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
331 goto fail;
333 if (pSSL_connect( conn->ssl_conn ) <= 0)
335 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
336 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
337 goto fail;
339 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn )))
341 ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
342 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
343 goto fail;
345 if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
347 /* FIXME: we should set an error and return, but we only print an error at the moment */
348 ERR("couldn't verify server certificate (%ld)\n", res);
350 TRACE("established SSL connection\n");
351 conn->secure = TRUE;
352 return TRUE;
354 fail:
355 if (conn->ssl_conn)
357 pSSL_shutdown( conn->ssl_conn );
358 pSSL_free( conn->ssl_conn );
359 conn->ssl_conn = NULL;
361 #endif
362 return FALSE;
365 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
367 if (!netconn_connected( conn )) return FALSE;
368 if (conn->secure)
370 #ifdef SONAME_LIBSSL
371 if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
372 *sent = pSSL_write( conn->ssl_conn, msg, len );
373 if (*sent < 1 && len) return FALSE;
374 return TRUE;
375 #else
376 return FALSE;
377 #endif
379 if ((*sent = send( conn->socket, msg, len, flags )) == -1)
381 set_last_error( sock_get_error( errno ) );
382 return FALSE;
384 return TRUE;
387 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
389 *recvd = 0;
390 if (!netconn_connected( conn )) return FALSE;
391 if (!len) return TRUE;
393 if (conn->secure)
395 #ifdef SONAME_LIBSSL
396 if (flags & ~(MSG_PEEK | MSG_WAITALL))
397 FIXME("SSL_read does not support the following flags: %08x\n", flags);
399 /* this ugly hack is all for MSG_PEEK */
400 if (flags & MSG_PEEK && !conn->peek_msg)
402 if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
404 else if (flags & MSG_PEEK && conn->peek_msg)
406 if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
407 *recvd = min( len, conn->peek_len );
408 memcpy( buf, conn->peek_msg, *recvd );
409 return TRUE;
411 else if (conn->peek_msg)
413 *recvd = min( len, conn->peek_len );
414 memcpy( buf, conn->peek_msg, *recvd );
415 conn->peek_len -= *recvd;
416 conn->peek_msg += *recvd;
418 if (conn->peek_len == 0)
420 heap_free( conn->peek_msg_mem );
421 conn->peek_msg_mem = NULL;
422 conn->peek_msg = NULL;
424 /* check if we have enough data from the peek buffer */
425 if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
427 *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
428 if (flags & MSG_PEEK) /* must copy into buffer */
430 conn->peek_len = *recvd;
431 if (!*recvd)
433 heap_free( conn->peek_msg_mem );
434 conn->peek_msg_mem = NULL;
435 conn->peek_msg = NULL;
437 else memcpy( conn->peek_msg, buf, *recvd );
439 if (*recvd < 1 && len) return FALSE;
440 return TRUE;
441 #else
442 return FALSE;
443 #endif
445 if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
447 set_last_error( sock_get_error( errno ) );
448 return FALSE;
450 return TRUE;
453 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
455 #ifdef FIONREAD
456 int ret, unread;
457 #endif
458 *available = 0;
459 if (!netconn_connected( conn )) return FALSE;
461 if (conn->secure)
463 #ifdef SONAME_LIBSSL
464 if (conn->peek_msg) *available = conn->peek_len;
465 #endif
466 return TRUE;
468 #ifdef FIONREAD
469 if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
470 #endif
471 return TRUE;
474 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
476 struct pollfd pfd;
477 BOOL ret = FALSE;
478 DWORD recvd = 0;
480 if (!netconn_connected( conn )) return FALSE;
482 if (conn->secure)
484 #ifdef SONAME_LIBSSL
485 long timeout;
487 timeout = pSSL_CTX_get_timeout( ctx );
488 pSSL_CTX_set_timeout( ctx, DEFAULT_RECEIVE_TIMEOUT );
490 while (recvd < *buflen)
492 int dummy;
493 if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
495 set_last_error( ERROR_CONNECTION_ABORTED );
496 break;
498 if (buffer[recvd] == '\n')
500 ret = TRUE;
501 break;
503 if (buffer[recvd] != '\r') recvd++;
505 pSSL_CTX_set_timeout( ctx, timeout );
506 if (ret)
508 buffer[recvd++] = 0;
509 *buflen = recvd;
510 TRACE("received line %s\n", debugstr_a(buffer));
512 return ret;
513 #else
514 return FALSE;
515 #endif
518 pfd.fd = conn->socket;
519 pfd.events = POLLIN;
520 while (recvd < *buflen)
522 if (poll( &pfd, 1, DEFAULT_RECEIVE_TIMEOUT * 1000 ) > 0)
524 int res;
525 if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
527 if (res == -1) set_last_error( sock_get_error( errno ) );
528 break;
530 if (buffer[recvd] == '\n')
532 ret = TRUE;
533 break;
535 if (buffer[recvd] != '\r') recvd++;
537 else
539 set_last_error( ERROR_WINHTTP_TIMEOUT );
540 break;
543 if (ret)
545 buffer[recvd++] = 0;
546 *buflen = recvd;
547 TRACE("received line %s\n", debugstr_a(buffer));
549 return ret;
552 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
554 int res;
555 struct timeval tv;
557 /* value is in milliseconds, convert to struct timeval */
558 tv.tv_sec = value / 1000;
559 tv.tv_usec = (value % 1000) * 1000;
561 if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv, sizeof(tv) ) == -1))
563 WARN("setsockopt failed (%s)\n", strerror( errno ));
564 return sock_get_error( errno );
566 return ERROR_SUCCESS;
569 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr_in *sa )
571 char *hostname;
572 #ifdef HAVE_GETADDRINFO
573 struct addrinfo *res, hints;
574 int ret;
575 #else
576 struct hostent *he;
577 #endif
579 if (!(hostname = strdupWA( hostnameW ))) return FALSE;
581 #ifdef HAVE_GETADDRINFO
582 memset( &hints, 0, sizeof(struct addrinfo) );
583 hints.ai_family = AF_INET;
585 ret = getaddrinfo( hostname, NULL, &hints, &res );
586 heap_free( hostname );
587 if (ret != 0)
589 TRACE("failed to get address of %s (%s)\n", debugstr_a(hostname), gai_strerror(ret));
590 return FALSE;
592 memset( sa, 0, sizeof(struct sockaddr_in) );
593 memcpy( &sa->sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr) );
594 sa->sin_family = res->ai_family;
595 sa->sin_port = htons( port );
597 freeaddrinfo( res );
598 #else
599 EnterCriticalSection( &cs_gethostbyname );
601 he = gethostbyname( hostname );
602 heap_free( hostname );
603 if (!he)
605 TRACE("failed to get address of %s (%d)\n", debugstr_a(hostname), h_errno);
606 LeaveCriticalSection( &cs_gethostbyname );
607 return FALSE;
609 memset( sa, 0, sizeof(struct sockaddr_in) );
610 memcpy( (char *)&sa->sin_addr, he->h_addr, he->h_length );
611 sa->sin_family = he->h_addrtype;
612 sa->sin_port = htons( port );
614 LeaveCriticalSection( &cs_gethostbyname );
615 #endif
616 return TRUE;
619 const void *netconn_get_certificate( netconn_t *conn )
621 #ifdef SONAME_LIBSSL
622 X509 *cert;
623 unsigned char *buffer, *p;
624 int len;
625 BOOL malloc = FALSE;
626 const CERT_CONTEXT *ret;
628 if (!conn->secure) return NULL;
630 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
631 p = NULL;
632 if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
634 * SSL 0.9.7 and above malloc the buffer if it is null.
635 * however earlier version do not and so we would need to alloc the buffer.
637 * see the i2d_X509 man page for more details.
639 if (!p)
641 if (!(buffer = heap_alloc( len ))) return NULL;
642 p = buffer;
643 len = pi2d_X509( cert, &p );
645 else
647 buffer = p;
648 malloc = TRUE;
651 ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
653 if (malloc) free( buffer );
654 else heap_free( buffer );
656 return ret;
657 #else
658 return NULL;
659 #endif