Get rid of HEAP_strdupWtoA calls.
[wine/multimedia.git] / dlls / wininet / netconnection.c
blob1605b101708b894e21c29d38c720aa6fb1f96cc5
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>
40 #include "wine/library.h"
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wininet.h"
44 #include "winerror.h"
46 #include "wine/debug.h"
47 #include "internet.h"
49 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
52 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
54 /* FIXME!!!!!!
55 * This should use winsock - To use winsock the funtions will have to change a bit
56 * as they are designed for unix sockets.
57 * SSL stuff should use crypt32.dll
60 #ifdef HAVE_OPENSSL_SSL_H
62 #ifndef SONAME_LIBSSL
63 #define SONAME_LIBSSL "libssl.so"
64 #endif
65 #ifndef SONAME_LIBCRYPTO
66 #define SONAME_LIBCRYPTO "libcrypto.so"
67 #endif
69 static void *OpenSSL_ssl_handle;
70 static void *OpenSSL_crypto_handle;
72 static SSL_METHOD *meth;
73 static SSL_CTX *ctx;
75 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
77 /* OpenSSL funtions that we use */
78 MAKE_FUNCPTR(SSL_library_init);
79 MAKE_FUNCPTR(SSL_load_error_strings);
80 MAKE_FUNCPTR(SSLv23_method);
81 MAKE_FUNCPTR(SSL_CTX_new);
82 MAKE_FUNCPTR(SSL_new);
83 MAKE_FUNCPTR(SSL_set_bio);
84 MAKE_FUNCPTR(SSL_connect);
85 MAKE_FUNCPTR(SSL_write);
86 MAKE_FUNCPTR(SSL_read);
87 MAKE_FUNCPTR(SSL_CTX_get_timeout);
88 MAKE_FUNCPTR(SSL_CTX_set_timeout);
90 /* OpenSSL's libcrypto functions that we use */
91 MAKE_FUNCPTR(BIO_new_socket);
92 MAKE_FUNCPTR(BIO_new_fp);
93 #undef MAKE_FUNCPTR
95 #endif
97 void NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
99 connection->useSSL = useSSL;
100 connection->socketFD = -1;
101 if (connection->useSSL)
103 #ifdef HAVE_OPENSSL_SSL_H
104 TRACE("using SSL connection\n");
105 connection->ssl_sock = -1;
106 if (OpenSSL_ssl_handle) /* already initilzed everything */
107 return;
108 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
109 if (!OpenSSL_ssl_handle)
111 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
112 SONAME_LIBSSL);
113 connection->useSSL = FALSE;
114 return;
116 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
117 if (!OpenSSL_crypto_handle)
119 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
120 SONAME_LIBCRYPTO);
121 connection->useSSL = FALSE;
122 return;
125 /* mmm nice ugly macroness */
126 #define DYNSSL(x) \
127 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
128 if (!p##x) \
130 ERR("failed to load symbol %s\n", #x); \
131 connection->useSSL = FALSE; \
132 return; \
135 DYNSSL(SSL_library_init);
136 DYNSSL(SSL_load_error_strings);
137 DYNSSL(SSLv23_method);
138 DYNSSL(SSL_CTX_new);
139 DYNSSL(SSL_new);
140 DYNSSL(SSL_set_bio);
141 DYNSSL(SSL_connect);
142 DYNSSL(SSL_write);
143 DYNSSL(SSL_read);
144 DYNSSL(SSL_CTX_get_timeout);
145 DYNSSL(SSL_CTX_set_timeout);
146 #undef DYNSSL
148 #define DYNCRYPTO(x) \
149 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
150 if (!p##x) \
152 ERR("failed to load symbol %s\n", #x); \
153 connection->useSSL = FALSE; \
154 return; \
156 DYNCRYPTO(BIO_new_fp);
157 DYNCRYPTO(BIO_new_socket);
158 #undef DYNCRYPTO
160 pSSL_library_init();
161 pSSL_load_error_strings();
162 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
164 meth = pSSLv23_method();
165 /* FIXME: SECURITY PROBLEM! WE ARN'T VERIFYING THE HOSTS CERTIFICATES OR ANYTHING */
166 #else
167 FIXME("can't use SSL, not compiled in.\n");
168 connection->useSSL = FALSE;
169 #endif
173 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
175 if (!connection->useSSL)
177 if (connection->socketFD == -1)
178 return FALSE;
179 return TRUE;
181 else
183 #ifdef HAVE_OPENSSL_SSL_H
184 if (connection->ssl_sock == -1)
185 return FALSE;
186 return TRUE;
187 #else
188 return FALSE;
189 #endif
193 /******************************************************************************
194 * NETCON_create
195 * Basically calls 'socket()' unless useSSL is supplised,
196 * in which case we do other things.
198 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
199 int type, int protocol)
201 if (!connection->useSSL)
203 connection->socketFD = socket(domain, type, protocol);
204 if (connection->socketFD == -1)
205 return FALSE;
206 return TRUE;
208 else
210 #ifdef HAVE_OPENSSL_SSL_H
211 connection->ssl_sock = socket(domain, type, protocol);
212 return TRUE;
213 #else
214 return FALSE;
215 #endif
219 /******************************************************************************
220 * NETCON_close
221 * Basically calls 'close()' unless we should use SSL
223 BOOL NETCON_close(WININET_NETCONNECTION *connection)
225 if (!NETCON_connected(connection)) return FALSE;
226 if (!connection->useSSL)
228 int result;
229 result = closesocket(connection->socketFD);
230 connection->socketFD = -1;
231 if (result == -1)
232 return FALSE;
233 return TRUE;
235 else
237 #ifdef HAVE_OPENSSL_SSL_H
238 closesocket(connection->ssl_sock);
239 connection->ssl_sock = -1;
240 /* FIXME should we call SSL_shutdown here?? Probably on whatever is the
241 * opposite of NETCON_init.... */
242 return TRUE;
243 #else
244 return FALSE;
245 #endif
249 /******************************************************************************
250 * NETCON_connect
251 * Basically calls 'connect()' unless we should use SSL
253 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
254 unsigned int addrlen)
256 if (!NETCON_connected(connection)) return FALSE;
257 if (!connection->useSSL)
259 int result;
260 result = connect(connection->socketFD, serv_addr, addrlen);
261 if (result == -1)
263 closesocket(connection->socketFD);
264 connection->socketFD = -1;
265 return FALSE;
267 return TRUE;
269 else
271 #ifdef HAVE_OPENSSL_SSL_H
272 BIO *sbio;
274 ctx = pSSL_CTX_new(meth);
275 connection->ssl_s = pSSL_new(ctx);
277 if (connect(connection->ssl_sock, serv_addr, addrlen) == -1)
278 return FALSE;
280 sbio = pBIO_new_socket(connection->ssl_sock, BIO_NOCLOSE);
281 pSSL_set_bio(connection->ssl_s, sbio, sbio);
282 if (pSSL_connect(connection->ssl_s) <= 0)
284 ERR("ssl couldn't connect\n");
285 return FALSE;
287 return TRUE;
288 #else
289 return FALSE;
290 #endif
294 /******************************************************************************
295 * NETCON_send
296 * Basically calls 'send()' unless we should use SSL
297 * number of chars send is put in *sent
299 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
300 int *sent /* out */)
302 if (!NETCON_connected(connection)) return FALSE;
303 if (!connection->useSSL)
305 *sent = send(connection->socketFD, msg, len, flags);
306 if (*sent == -1)
307 return FALSE;
308 return TRUE;
310 else
312 #ifdef HAVE_OPENSSL_SSL_H
313 if (flags)
314 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
315 *sent = pSSL_write(connection->ssl_s, msg, len);
316 if (*sent < 1 && len)
317 return FALSE;
318 return TRUE;
319 #else
320 return FALSE;
321 #endif
325 /******************************************************************************
326 * NETCON_recv
327 * Basically calls 'recv()' unless we should use SSL
328 * number of chars received is put in *recvd
330 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
331 int *recvd /* out */)
333 if (!NETCON_connected(connection)) return FALSE;
334 if (!connection->useSSL)
336 *recvd = recv(connection->socketFD, buf, len, flags);
337 if (*recvd == -1)
338 return FALSE;
339 return TRUE;
341 else
343 #ifdef HAVE_OPENSSL_SSL_H
344 static char *peek_msg = NULL;
345 static char *peek_msg_mem = NULL;
347 if (flags & (~MSG_PEEK))
348 FIXME("SSL_read does not support the following flag: %08x\n", flags);
350 /* this ugly hack is all for MSG_PEEK. eww gross */
351 if (flags & MSG_PEEK && !peek_msg)
353 peek_msg = peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
355 else if (flags & MSG_PEEK && peek_msg)
357 if (len < strlen(peek_msg))
358 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
359 strncpy(buf, peek_msg, len);
360 *recvd = (strlen(peek_msg) <= len ? strlen(peek_msg) : len);
361 return TRUE;
363 else if (peek_msg)
365 strncpy(buf, peek_msg, len);
366 peek_msg += *recvd = min(len, strlen(peek_msg));
367 if (*peek_msg == '\0' || *(peek_msg - 1) == '\0')
369 HeapFree(GetProcessHeap(), 0, peek_msg_mem);
370 peek_msg_mem = NULL;
371 peek_msg = NULL;
373 return TRUE;
375 *recvd = pSSL_read(connection->ssl_s, buf, len);
376 if (flags & MSG_PEEK) /* must copy stuff into buffer */
378 if (!*recvd)
380 HeapFree(GetProcessHeap(), 0, peek_msg_mem);
381 peek_msg_mem = NULL;
382 peek_msg = NULL;
384 else
386 strncpy(peek_msg, buf, *recvd);
387 peek_msg[*recvd] = '\0';
390 if (*recvd < 1 && len)
391 return FALSE;
392 return TRUE;
393 #else
394 return FALSE;
395 #endif
399 /******************************************************************************
400 * NETCON_getNextLine
402 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
405 TRACE("\n");
407 if (!NETCON_connected(connection)) return FALSE;
409 if (!connection->useSSL)
411 struct timeval tv;
412 fd_set infd;
413 BOOL bSuccess = FALSE;
414 DWORD nRecv = 0;
416 FD_ZERO(&infd);
417 FD_SET(connection->socketFD, &infd);
418 tv.tv_sec=RESPONSE_TIMEOUT;
419 tv.tv_usec=0;
421 while (nRecv < *dwBuffer)
423 if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
425 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
427 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED); /* fixme: right error? */
428 goto lend;
431 if (lpszBuffer[nRecv] == '\n')
433 bSuccess = TRUE;
434 break;
436 if (lpszBuffer[nRecv] != '\r')
437 nRecv++;
439 else
441 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
442 goto lend;
446 lend: /* FIXME: don't use labels */
447 if (bSuccess)
449 lpszBuffer[nRecv++] = '\0';
450 *dwBuffer = nRecv;
451 TRACE(":%lu %s\n", nRecv, lpszBuffer);
452 return TRUE;
454 else
456 return FALSE;
459 else
461 #ifdef HAVE_OPENSSL_SSL_H
462 long prev_timeout;
463 DWORD nRecv = 0;
464 BOOL success = TRUE;
466 prev_timeout = pSSL_CTX_get_timeout(ctx);
467 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
469 while (nRecv < *dwBuffer)
471 int recv = 1;
472 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
474 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
475 success = FALSE;
478 if (lpszBuffer[nRecv] == '\n')
480 success = TRUE;
481 break;
483 if (lpszBuffer[nRecv] != '\r')
484 nRecv++;
487 pSSL_CTX_set_timeout(ctx, prev_timeout);
488 if (success)
490 lpszBuffer[nRecv++] = '\0';
491 *dwBuffer = nRecv;
492 TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
493 return TRUE;
495 return FALSE;
496 #else
497 return FALSE;
498 #endif