msacm: Fix acmMetrics(ACM_METRIC_DRIVER_PRIORITY) return on error.
[wine/multimedia.git] / dlls / wininet / netconnection.c
blobf488d695b193a5cbd3878d76f776d15698a03432
1 /*
2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
6 * David Hammerton
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
42 #include "wine/library.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wininet.h"
46 #include "winerror.h"
48 /* To avoid conflicts with the Unix socket headers. we only need it for
49 * the error codes anyway. */
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
53 #include "wine/debug.h"
54 #include "internet.h"
56 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
59 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
61 /* FIXME!!!!!!
62 * This should use winsock - To use winsock the functions will have to change a bit
63 * as they are designed for unix sockets.
64 * SSL stuff should use crypt32.dll
67 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
69 #include <openssl/err.h>
71 #ifndef SONAME_LIBSSL
72 #define SONAME_LIBSSL "libssl.so"
73 #endif
74 #ifndef SONAME_LIBCRYPTO
75 #define SONAME_LIBCRYPTO "libcrypto.so"
76 #endif
78 static void *OpenSSL_ssl_handle;
79 static void *OpenSSL_crypto_handle;
81 static SSL_METHOD *meth;
82 static SSL_CTX *ctx;
84 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
86 /* OpenSSL functions that we use */
87 MAKE_FUNCPTR(SSL_library_init);
88 MAKE_FUNCPTR(SSL_load_error_strings);
89 MAKE_FUNCPTR(SSLv23_method);
90 MAKE_FUNCPTR(SSL_CTX_new);
91 MAKE_FUNCPTR(SSL_new);
92 MAKE_FUNCPTR(SSL_free);
93 MAKE_FUNCPTR(SSL_set_fd);
94 MAKE_FUNCPTR(SSL_connect);
95 MAKE_FUNCPTR(SSL_shutdown);
96 MAKE_FUNCPTR(SSL_write);
97 MAKE_FUNCPTR(SSL_read);
98 MAKE_FUNCPTR(SSL_get_verify_result);
99 MAKE_FUNCPTR(SSL_get_peer_certificate);
100 MAKE_FUNCPTR(SSL_CTX_get_timeout);
101 MAKE_FUNCPTR(SSL_CTX_set_timeout);
102 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
104 /* OpenSSL's libcrypto functions that we use */
105 MAKE_FUNCPTR(BIO_new_fp);
106 MAKE_FUNCPTR(ERR_get_error);
107 MAKE_FUNCPTR(ERR_error_string);
108 #undef MAKE_FUNCPTR
110 #endif
112 void NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
114 connection->useSSL = FALSE;
115 connection->socketFD = -1;
116 if (useSSL)
118 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
119 TRACE("using SSL connection\n");
120 if (OpenSSL_ssl_handle) /* already initilzed everything */
121 return;
122 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
123 if (!OpenSSL_ssl_handle)
125 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
126 SONAME_LIBSSL);
127 connection->useSSL = FALSE;
128 return;
130 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
131 if (!OpenSSL_crypto_handle)
133 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
134 SONAME_LIBCRYPTO);
135 connection->useSSL = FALSE;
136 return;
139 /* mmm nice ugly macroness */
140 #define DYNSSL(x) \
141 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
142 if (!p##x) \
144 ERR("failed to load symbol %s\n", #x); \
145 connection->useSSL = FALSE; \
146 return; \
149 DYNSSL(SSL_library_init);
150 DYNSSL(SSL_load_error_strings);
151 DYNSSL(SSLv23_method);
152 DYNSSL(SSL_CTX_new);
153 DYNSSL(SSL_new);
154 DYNSSL(SSL_free);
155 DYNSSL(SSL_set_fd);
156 DYNSSL(SSL_connect);
157 DYNSSL(SSL_shutdown);
158 DYNSSL(SSL_write);
159 DYNSSL(SSL_read);
160 DYNSSL(SSL_get_verify_result);
161 DYNSSL(SSL_get_peer_certificate);
162 DYNSSL(SSL_CTX_get_timeout);
163 DYNSSL(SSL_CTX_set_timeout);
164 DYNSSL(SSL_CTX_set_default_verify_paths);
165 #undef DYNSSL
167 #define DYNCRYPTO(x) \
168 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
169 if (!p##x) \
171 ERR("failed to load symbol %s\n", #x); \
172 connection->useSSL = FALSE; \
173 return; \
175 DYNCRYPTO(BIO_new_fp);
176 DYNCRYPTO(ERR_get_error);
177 DYNCRYPTO(ERR_error_string);
178 #undef DYNCRYPTO
180 pSSL_library_init();
181 pSSL_load_error_strings();
182 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
184 meth = pSSLv23_method();
185 connection->peek_msg = NULL;
186 connection->peek_msg_mem = NULL;
187 #else
188 FIXME("can't use SSL, not compiled in.\n");
189 connection->useSSL = FALSE;
190 #endif
194 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
196 if (connection->socketFD == -1)
197 return FALSE;
198 else
199 return TRUE;
202 /* translate a unix error code into a winsock one */
203 static int sock_get_error( int err )
205 switch (err)
207 case EINTR: return WSAEINTR;
208 case EBADF: return WSAEBADF;
209 case EPERM:
210 case EACCES: return WSAEACCES;
211 case EFAULT: return WSAEFAULT;
212 case EINVAL: return WSAEINVAL;
213 case EMFILE: return WSAEMFILE;
214 case EWOULDBLOCK: return WSAEWOULDBLOCK;
215 case EINPROGRESS: return WSAEINPROGRESS;
216 case EALREADY: return WSAEALREADY;
217 case ENOTSOCK: return WSAENOTSOCK;
218 case EDESTADDRREQ: return WSAEDESTADDRREQ;
219 case EMSGSIZE: return WSAEMSGSIZE;
220 case EPROTOTYPE: return WSAEPROTOTYPE;
221 case ENOPROTOOPT: return WSAENOPROTOOPT;
222 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
223 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
224 case EOPNOTSUPP: return WSAEOPNOTSUPP;
225 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
226 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
227 case EADDRINUSE: return WSAEADDRINUSE;
228 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
229 case ENETDOWN: return WSAENETDOWN;
230 case ENETUNREACH: return WSAENETUNREACH;
231 case ENETRESET: return WSAENETRESET;
232 case ECONNABORTED: return WSAECONNABORTED;
233 case EPIPE:
234 case ECONNRESET: return WSAECONNRESET;
235 case ENOBUFS: return WSAENOBUFS;
236 case EISCONN: return WSAEISCONN;
237 case ENOTCONN: return WSAENOTCONN;
238 case ESHUTDOWN: return WSAESHUTDOWN;
239 case ETOOMANYREFS: return WSAETOOMANYREFS;
240 case ETIMEDOUT: return WSAETIMEDOUT;
241 case ECONNREFUSED: return WSAECONNREFUSED;
242 case ELOOP: return WSAELOOP;
243 case ENAMETOOLONG: return WSAENAMETOOLONG;
244 case EHOSTDOWN: return WSAEHOSTDOWN;
245 case EHOSTUNREACH: return WSAEHOSTUNREACH;
246 case ENOTEMPTY: return WSAENOTEMPTY;
247 #ifdef EPROCLIM
248 case EPROCLIM: return WSAEPROCLIM;
249 #endif
250 #ifdef EUSERS
251 case EUSERS: return WSAEUSERS;
252 #endif
253 #ifdef EDQUOT
254 case EDQUOT: return WSAEDQUOT;
255 #endif
256 #ifdef ESTALE
257 case ESTALE: return WSAESTALE;
258 #endif
259 #ifdef EREMOTE
260 case EREMOTE: return WSAEREMOTE;
261 #endif
262 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
266 /******************************************************************************
267 * NETCON_create
268 * Basically calls 'socket()'
270 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
271 int type, int protocol)
273 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
274 if (connection->useSSL)
275 return FALSE;
276 #endif
278 connection->socketFD = socket(domain, type, protocol);
279 if (connection->socketFD == -1)
281 INTERNET_SetLastError(sock_get_error(errno));
282 return FALSE;
284 return TRUE;
287 /******************************************************************************
288 * NETCON_close
289 * Basically calls 'close()' unless we should use SSL
291 BOOL NETCON_close(WININET_NETCONNECTION *connection)
293 int result;
295 if (!NETCON_connected(connection)) return FALSE;
297 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
298 if (connection->useSSL)
300 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
301 connection->peek_msg = NULL;
302 connection->peek_msg_mem = NULL;
304 pSSL_shutdown(connection->ssl_s);
305 pSSL_free(connection->ssl_s);
306 connection->ssl_s = NULL;
308 connection->useSSL = FALSE;
310 #endif
312 result = closesocket(connection->socketFD);
313 connection->socketFD = -1;
315 if (result == -1)
317 INTERNET_SetLastError(sock_get_error(errno));
318 return FALSE;
320 return TRUE;
322 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
323 static BOOL check_hostname(X509 *cert, char *hostname)
325 /* FIXME: implement */
326 return TRUE;
328 #endif
329 /******************************************************************************
330 * NETCON_secure_connect
331 * Initiates a secure connection over an existing plaintext connection.
333 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
335 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
336 long verify_res;
337 X509 *cert;
338 int len;
339 char *hostname_unix;
341 /* can't connect if we are already connected */
342 if (connection->useSSL)
344 ERR("already connected\n");
345 return FALSE;
348 ctx = pSSL_CTX_new(meth);
349 if (!pSSL_CTX_set_default_verify_paths(ctx))
351 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
352 pERR_error_string(pERR_get_error(), 0));
353 return FALSE;
355 connection->ssl_s = pSSL_new(ctx);
356 if (!connection->ssl_s)
358 ERR("SSL_new failed: %s\n",
359 pERR_error_string(pERR_get_error(), 0));
360 goto fail;
363 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
365 ERR("SSL_set_fd failed: %s\n",
366 pERR_error_string(pERR_get_error(), 0));
367 goto fail;
370 if (pSSL_connect(connection->ssl_s) <= 0)
372 ERR("SSL_connect failed: %s\n",
373 pERR_error_string(pERR_get_error(), 0));
374 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
375 goto fail;
377 cert = pSSL_get_peer_certificate(connection->ssl_s);
378 if (!cert)
380 ERR("no certificate for server %s\n", debugstr_w(hostname));
381 /* FIXME: is this the best error? */
382 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
383 goto fail;
385 verify_res = pSSL_get_verify_result(connection->ssl_s);
386 if (verify_res != X509_V_OK)
388 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
389 /* FIXME: we should set an error and return, but we only warn at
390 * the moment */
393 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
394 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
395 if (!hostname_unix)
397 INTERNET_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
398 goto fail;
400 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
402 if (!check_hostname(cert, hostname_unix))
404 HeapFree(GetProcessHeap(), 0, hostname_unix);
405 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
406 goto fail;
409 HeapFree(GetProcessHeap(), 0, hostname_unix);
410 connection->useSSL = TRUE;
411 return TRUE;
413 fail:
414 if (connection->ssl_s)
416 pSSL_shutdown(connection->ssl_s);
417 pSSL_free(connection->ssl_s);
418 connection->ssl_s = NULL;
420 #endif
421 return FALSE;
424 /******************************************************************************
425 * NETCON_connect
426 * Connects to the specified address.
428 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
429 unsigned int addrlen)
431 int result;
433 if (!NETCON_connected(connection)) return FALSE;
435 result = connect(connection->socketFD, serv_addr, addrlen);
436 if (result == -1)
438 WARN("Unable to connect to host (%s)\n", strerror(errno));
439 INTERNET_SetLastError(sock_get_error(errno));
441 closesocket(connection->socketFD);
442 connection->socketFD = -1;
443 return FALSE;
446 return TRUE;
449 /******************************************************************************
450 * NETCON_send
451 * Basically calls 'send()' unless we should use SSL
452 * number of chars send is put in *sent
454 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
455 int *sent /* out */)
457 if (!NETCON_connected(connection)) return FALSE;
458 if (!connection->useSSL)
460 *sent = send(connection->socketFD, msg, len, flags);
461 if (*sent == -1)
463 INTERNET_SetLastError(sock_get_error(errno));
464 return FALSE;
466 return TRUE;
468 else
470 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
471 if (flags)
472 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
473 *sent = pSSL_write(connection->ssl_s, msg, len);
474 if (*sent < 1 && len)
475 return FALSE;
476 return TRUE;
477 #else
478 return FALSE;
479 #endif
483 /******************************************************************************
484 * NETCON_recv
485 * Basically calls 'recv()' unless we should use SSL
486 * number of chars received is put in *recvd
488 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
489 int *recvd /* out */)
491 if (!NETCON_connected(connection)) return FALSE;
492 if (!connection->useSSL)
494 *recvd = recv(connection->socketFD, buf, len, flags);
495 if (*recvd == -1)
497 INTERNET_SetLastError(sock_get_error(errno));
498 return FALSE;
500 return TRUE;
502 else
504 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
505 if (flags & (~MSG_PEEK))
506 FIXME("SSL_read does not support the following flag: %08x\n", flags);
508 /* this ugly hack is all for MSG_PEEK. eww gross */
509 if (flags & MSG_PEEK && !connection->peek_msg)
511 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
513 else if (flags & MSG_PEEK && connection->peek_msg)
515 size_t peek_msg_len = strlen(connection->peek_msg);
516 if (len < peek_msg_len)
517 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
518 memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
519 *recvd = min(len, peek_msg_len);
520 return TRUE;
522 else if (connection->peek_msg)
524 size_t peek_msg_len = strlen(connection->peek_msg);
525 memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
526 connection->peek_msg += *recvd = min(len, peek_msg_len);
527 if (*connection->peek_msg == '\0' || *(connection->peek_msg - 1) == '\0')
529 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
530 connection->peek_msg_mem = NULL;
531 connection->peek_msg = NULL;
533 return TRUE;
535 *recvd = pSSL_read(connection->ssl_s, buf, len);
536 if (flags & MSG_PEEK) /* must copy stuff into buffer */
538 if (!*recvd)
540 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
541 connection->peek_msg_mem = NULL;
542 connection->peek_msg = NULL;
544 else
546 memcpy(connection->peek_msg, buf, *recvd);
547 connection->peek_msg[*recvd] = '\0';
550 if (*recvd < 1 && len)
551 return FALSE;
552 return TRUE;
553 #else
554 return FALSE;
555 #endif
559 /******************************************************************************
560 * NETCON_getNextLine
562 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
565 TRACE("\n");
567 if (!NETCON_connected(connection)) return FALSE;
569 if (!connection->useSSL)
571 struct timeval tv;
572 fd_set infd;
573 BOOL bSuccess = FALSE;
574 DWORD nRecv = 0;
576 FD_ZERO(&infd);
577 FD_SET(connection->socketFD, &infd);
578 tv.tv_sec=RESPONSE_TIMEOUT;
579 tv.tv_usec=0;
581 while (nRecv < *dwBuffer)
583 if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
585 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
587 INTERNET_SetLastError(sock_get_error(errno));
588 goto lend;
591 if (lpszBuffer[nRecv] == '\n')
593 bSuccess = TRUE;
594 break;
596 if (lpszBuffer[nRecv] != '\r')
597 nRecv++;
599 else
601 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
602 goto lend;
606 lend: /* FIXME: don't use labels */
607 if (bSuccess)
609 lpszBuffer[nRecv++] = '\0';
610 *dwBuffer = nRecv;
611 TRACE(":%lu %s\n", nRecv, lpszBuffer);
612 return TRUE;
614 else
616 return FALSE;
619 else
621 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
622 long prev_timeout;
623 DWORD nRecv = 0;
624 BOOL success = TRUE;
626 prev_timeout = pSSL_CTX_get_timeout(ctx);
627 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
629 while (nRecv < *dwBuffer)
631 int recv = 1;
632 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
634 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
635 success = FALSE;
638 if (lpszBuffer[nRecv] == '\n')
640 success = TRUE;
641 break;
643 if (lpszBuffer[nRecv] != '\r')
644 nRecv++;
647 pSSL_CTX_set_timeout(ctx, prev_timeout);
648 if (success)
650 lpszBuffer[nRecv++] = '\0';
651 *dwBuffer = nRecv;
652 TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
653 return TRUE;
655 return FALSE;
656 #else
657 return FALSE;
658 #endif