shdocvw: Added WebBrowser::Resizable property implementation.
[wine] / dlls / wininet / netconnection.c
1 /*
2  * Wininet - networking layer. Uses unix sockets or OpenSSL.
3  *
4  * Copyright 2002 TransGaming Technologies Inc.
5  *
6  * David Hammerton
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
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 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <errno.h>
44
45 #include "wine/library.h"
46 #include "windef.h"
47 #include "winbase.h"
48 #include "wininet.h"
49 #include "winerror.h"
50 #include "wincrypt.h"
51
52 /* To avoid conflicts with the Unix socket headers. we only need it for
53  * the error codes anyway. */
54 #define USE_WS_PREFIX
55 #include "winsock2.h"
56
57 #include "wine/debug.h"
58 #include "internet.h"
59
60 #define RESPONSE_TIMEOUT        30            /* FROM internet.c */
61
62
63 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
64
65 /* FIXME!!!!!!
66  *    This should use winsock - To use winsock the functions will have to change a bit
67  *        as they are designed for unix sockets.
68  *    SSL stuff should use crypt32.dll
69  */
70
71 #ifdef SONAME_LIBSSL
72
73 #include <openssl/err.h>
74
75 static void *OpenSSL_ssl_handle;
76 static void *OpenSSL_crypto_handle;
77
78 static SSL_METHOD *meth;
79 static SSL_CTX *ctx;
80
81 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
82
83 /* OpenSSL functions that we use */
84 MAKE_FUNCPTR(SSL_library_init);
85 MAKE_FUNCPTR(SSL_load_error_strings);
86 MAKE_FUNCPTR(SSLv23_method);
87 MAKE_FUNCPTR(SSL_CTX_new);
88 MAKE_FUNCPTR(SSL_new);
89 MAKE_FUNCPTR(SSL_free);
90 MAKE_FUNCPTR(SSL_set_fd);
91 MAKE_FUNCPTR(SSL_connect);
92 MAKE_FUNCPTR(SSL_shutdown);
93 MAKE_FUNCPTR(SSL_write);
94 MAKE_FUNCPTR(SSL_read);
95 MAKE_FUNCPTR(SSL_get_verify_result);
96 MAKE_FUNCPTR(SSL_get_peer_certificate);
97 MAKE_FUNCPTR(SSL_CTX_get_timeout);
98 MAKE_FUNCPTR(SSL_CTX_set_timeout);
99 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
100 MAKE_FUNCPTR(i2d_X509);
101
102 /* OpenSSL's libcrypto functions that we use */
103 MAKE_FUNCPTR(BIO_new_fp);
104 MAKE_FUNCPTR(ERR_get_error);
105 MAKE_FUNCPTR(ERR_error_string);
106 #undef MAKE_FUNCPTR
107
108 #endif
109
110 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
111 {
112     connection->useSSL = FALSE;
113     connection->socketFD = -1;
114     if (useSSL)
115     {
116 #ifdef SONAME_LIBSSL
117         TRACE("using SSL connection\n");
118         if (OpenSSL_ssl_handle) /* already initialized everything */
119             return TRUE;
120         OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
121         if (!OpenSSL_ssl_handle)
122         {
123             ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
124                 SONAME_LIBSSL);
125             INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
126             return FALSE;
127         }
128         OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
129         if (!OpenSSL_crypto_handle)
130         {
131             ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
132                 SONAME_LIBCRYPTO);
133             INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
134             return FALSE;
135         }
136
137         /* mmm nice ugly macroness */
138 #define DYNSSL(x) \
139     p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
140     if (!p##x) \
141     { \
142         ERR("failed to load symbol %s\n", #x); \
143         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
144         return FALSE; \
145     }
146
147         DYNSSL(SSL_library_init);
148         DYNSSL(SSL_load_error_strings);
149         DYNSSL(SSLv23_method);
150         DYNSSL(SSL_CTX_new);
151         DYNSSL(SSL_new);
152         DYNSSL(SSL_free);
153         DYNSSL(SSL_set_fd);
154         DYNSSL(SSL_connect);
155         DYNSSL(SSL_shutdown);
156         DYNSSL(SSL_write);
157         DYNSSL(SSL_read);
158         DYNSSL(SSL_get_verify_result);
159         DYNSSL(SSL_get_peer_certificate);
160         DYNSSL(SSL_CTX_get_timeout);
161         DYNSSL(SSL_CTX_set_timeout);
162         DYNSSL(SSL_CTX_set_default_verify_paths);
163         DYNSSL(i2d_X509);
164 #undef DYNSSL
165
166 #define DYNCRYPTO(x) \
167     p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
168     if (!p##x) \
169     { \
170         ERR("failed to load symbol %s\n", #x); \
171         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
172         return FALSE; \
173     }
174         DYNCRYPTO(BIO_new_fp);
175         DYNCRYPTO(ERR_get_error);
176         DYNCRYPTO(ERR_error_string);
177 #undef DYNCRYPTO
178
179         pSSL_library_init();
180         pSSL_load_error_strings();
181         pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
182
183         meth = pSSLv23_method();
184         connection->peek_msg = NULL;
185         connection->peek_msg_mem = NULL;
186 #else
187         FIXME("can't use SSL, not compiled in.\n");
188         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
189         return FALSE;
190 #endif
191     }
192     return TRUE;
193 }
194
195 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
196 {
197     if (connection->socketFD == -1)
198         return FALSE;
199     else
200         return TRUE;
201 }
202
203 /* translate a unix error code into a winsock one */
204 static int sock_get_error( int err )
205 {
206     switch (err)
207     {
208         case EINTR:             return WSAEINTR;
209         case EBADF:             return WSAEBADF;
210         case EPERM:
211         case EACCES:            return WSAEACCES;
212         case EFAULT:            return WSAEFAULT;
213         case EINVAL:            return WSAEINVAL;
214         case EMFILE:            return WSAEMFILE;
215         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
216         case EINPROGRESS:       return WSAEINPROGRESS;
217         case EALREADY:          return WSAEALREADY;
218         case ENOTSOCK:          return WSAENOTSOCK;
219         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
220         case EMSGSIZE:          return WSAEMSGSIZE;
221         case EPROTOTYPE:        return WSAEPROTOTYPE;
222         case ENOPROTOOPT:       return WSAENOPROTOOPT;
223         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
224         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
225         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
226         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
227         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
228         case EADDRINUSE:        return WSAEADDRINUSE;
229         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
230         case ENETDOWN:          return WSAENETDOWN;
231         case ENETUNREACH:       return WSAENETUNREACH;
232         case ENETRESET:         return WSAENETRESET;
233         case ECONNABORTED:      return WSAECONNABORTED;
234         case EPIPE:
235         case ECONNRESET:        return WSAECONNRESET;
236         case ENOBUFS:           return WSAENOBUFS;
237         case EISCONN:           return WSAEISCONN;
238         case ENOTCONN:          return WSAENOTCONN;
239         case ESHUTDOWN:         return WSAESHUTDOWN;
240         case ETOOMANYREFS:      return WSAETOOMANYREFS;
241         case ETIMEDOUT:         return WSAETIMEDOUT;
242         case ECONNREFUSED:      return WSAECONNREFUSED;
243         case ELOOP:             return WSAELOOP;
244         case ENAMETOOLONG:      return WSAENAMETOOLONG;
245         case EHOSTDOWN:         return WSAEHOSTDOWN;
246         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
247         case ENOTEMPTY:         return WSAENOTEMPTY;
248 #ifdef EPROCLIM
249         case EPROCLIM:          return WSAEPROCLIM;
250 #endif
251 #ifdef EUSERS
252         case EUSERS:            return WSAEUSERS;
253 #endif
254 #ifdef EDQUOT
255         case EDQUOT:            return WSAEDQUOT;
256 #endif
257 #ifdef ESTALE
258         case ESTALE:            return WSAESTALE;
259 #endif
260 #ifdef EREMOTE
261         case EREMOTE:           return WSAEREMOTE;
262 #endif
263     default: errno=err; perror("sock_set_error"); return WSAEFAULT;
264     }
265 }
266
267 /******************************************************************************
268  * NETCON_create
269  * Basically calls 'socket()'
270  */
271 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
272               int type, int protocol)
273 {
274 #ifdef SONAME_LIBSSL
275     if (connection->useSSL)
276         return FALSE;
277 #endif
278
279     connection->socketFD = socket(domain, type, protocol);
280     if (connection->socketFD == -1)
281     {
282         INTERNET_SetLastError(sock_get_error(errno));
283         return FALSE;
284     }
285     return TRUE;
286 }
287
288 /******************************************************************************
289  * NETCON_close
290  * Basically calls 'close()' unless we should use SSL
291  */
292 BOOL NETCON_close(WININET_NETCONNECTION *connection)
293 {
294     int result;
295
296     if (!NETCON_connected(connection)) return FALSE;
297
298 #ifdef SONAME_LIBSSL
299     if (connection->useSSL)
300     {
301         HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
302         connection->peek_msg = NULL;
303         connection->peek_msg_mem = NULL;
304         connection->peek_len = 0;
305
306         pSSL_shutdown(connection->ssl_s);
307         pSSL_free(connection->ssl_s);
308         connection->ssl_s = NULL;
309
310         connection->useSSL = FALSE;
311     }
312 #endif
313
314     result = closesocket(connection->socketFD);
315     connection->socketFD = -1;
316
317     if (result == -1)
318     {
319         INTERNET_SetLastError(sock_get_error(errno));
320         return FALSE;
321     }
322     return TRUE;
323 }
324 #ifdef SONAME_LIBSSL
325 static BOOL check_hostname(X509 *cert, char *hostname)
326 {
327     /* FIXME: implement */
328     return TRUE;
329 }
330 #endif
331 /******************************************************************************
332  * NETCON_secure_connect
333  * Initiates a secure connection over an existing plaintext connection.
334  */
335 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
336 {
337 #ifdef SONAME_LIBSSL
338     long verify_res;
339     X509 *cert;
340     int len;
341     char *hostname_unix;
342
343     /* can't connect if we are already connected */
344     if (connection->useSSL)
345     {
346         ERR("already connected\n");
347         return FALSE;
348     }
349
350     ctx = pSSL_CTX_new(meth);
351     if (!pSSL_CTX_set_default_verify_paths(ctx))
352     {
353         ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
354             pERR_error_string(pERR_get_error(), 0));
355         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
356         return FALSE;
357     }
358     connection->ssl_s = pSSL_new(ctx);
359     if (!connection->ssl_s)
360     {
361         ERR("SSL_new failed: %s\n",
362             pERR_error_string(pERR_get_error(), 0));
363         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
364         goto fail;
365     }
366
367     if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
368     {
369         ERR("SSL_set_fd failed: %s\n",
370             pERR_error_string(pERR_get_error(), 0));
371         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
372         goto fail;
373     }
374
375     if (pSSL_connect(connection->ssl_s) <= 0)
376     {
377         ERR("SSL_connect failed: %s\n",
378             pERR_error_string(pERR_get_error(), 0));
379         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
380         goto fail;
381     }
382     cert = pSSL_get_peer_certificate(connection->ssl_s);
383     if (!cert)
384     {
385         ERR("no certificate for server %s\n", debugstr_w(hostname));
386         /* FIXME: is this the best error? */
387         INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
388         goto fail;
389     }
390     verify_res = pSSL_get_verify_result(connection->ssl_s);
391     if (verify_res != X509_V_OK)
392     {
393         ERR("couldn't verify the security of the connection, %ld\n", verify_res);
394         /* FIXME: we should set an error and return, but we only warn at
395          * the moment */
396     }
397
398     len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
399     hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
400     if (!hostname_unix)
401     {
402         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
403         goto fail;
404     }
405     WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
406
407     if (!check_hostname(cert, hostname_unix))
408     {
409         HeapFree(GetProcessHeap(), 0, hostname_unix);
410         INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
411         goto fail;
412     }
413
414     HeapFree(GetProcessHeap(), 0, hostname_unix);
415     connection->useSSL = TRUE;
416     return TRUE;
417
418 fail:
419     if (connection->ssl_s)
420     {
421         pSSL_shutdown(connection->ssl_s);
422         pSSL_free(connection->ssl_s);
423         connection->ssl_s = NULL;
424     }
425 #endif
426     return FALSE;
427 }
428
429 /******************************************************************************
430  * NETCON_connect
431  * Connects to the specified address.
432  */
433 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
434                     unsigned int addrlen)
435 {
436     int result;
437
438     if (!NETCON_connected(connection)) return FALSE;
439
440     result = connect(connection->socketFD, serv_addr, addrlen);
441     if (result == -1)
442     {
443         WARN("Unable to connect to host (%s)\n", strerror(errno));
444         INTERNET_SetLastError(sock_get_error(errno));
445
446         closesocket(connection->socketFD);
447         connection->socketFD = -1;
448         return FALSE;
449     }
450
451     return TRUE;
452 }
453
454 /******************************************************************************
455  * NETCON_send
456  * Basically calls 'send()' unless we should use SSL
457  * number of chars send is put in *sent
458  */
459 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
460                 int *sent /* out */)
461 {
462     if (!NETCON_connected(connection)) return FALSE;
463     if (!connection->useSSL)
464     {
465         *sent = send(connection->socketFD, msg, len, flags);
466         if (*sent == -1)
467         {
468             INTERNET_SetLastError(sock_get_error(errno));
469             return FALSE;
470         }
471         return TRUE;
472     }
473     else
474     {
475 #ifdef SONAME_LIBSSL
476         if (flags)
477             FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
478         *sent = pSSL_write(connection->ssl_s, msg, len);
479         if (*sent < 1 && len)
480             return FALSE;
481         return TRUE;
482 #else
483         return FALSE;
484 #endif
485     }
486 }
487
488 /******************************************************************************
489  * NETCON_recv
490  * Basically calls 'recv()' unless we should use SSL
491  * number of chars received is put in *recvd
492  */
493 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
494                 int *recvd /* out */)
495 {
496     *recvd = 0;
497     if (!NETCON_connected(connection)) return FALSE;
498     if (!len)
499         return TRUE;
500     if (!connection->useSSL)
501     {
502         *recvd = recv(connection->socketFD, buf, len, flags);
503         if (*recvd == -1)
504         {
505             INTERNET_SetLastError(sock_get_error(errno));
506             return FALSE;
507         }
508         return TRUE;
509     }
510     else
511     {
512 #ifdef SONAME_LIBSSL
513         if (flags & ~(MSG_PEEK|MSG_WAITALL))
514             FIXME("SSL_read does not support the following flag: %08x\n", flags);
515
516         /* this ugly hack is all for MSG_PEEK. eww gross */
517         if (flags & MSG_PEEK && !connection->peek_msg)
518         {
519             connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
520         }
521         else if (flags & MSG_PEEK && connection->peek_msg)
522         {
523             if (len < connection->peek_len)
524                 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
525             *recvd = min(len, connection->peek_len);
526             memcpy(buf, connection->peek_msg, *recvd);
527             return TRUE;
528         }
529         else if (connection->peek_msg)
530         {
531             *recvd = min(len, connection->peek_len);
532             memcpy(buf, connection->peek_msg, *recvd);
533             connection->peek_len -= *recvd;
534             connection->peek_msg += *recvd;
535             if (connection->peek_len == 0)
536             {
537                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
538                 connection->peek_msg_mem = NULL;
539                 connection->peek_msg = NULL;
540             }
541             /* check if we got enough data from the peek buffer */
542             if (!(flags & MSG_WAITALL) || (*recvd == len))
543                 return TRUE;
544             /* otherwise, fall through */
545         }
546         *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
547         if (flags & MSG_PEEK) /* must copy stuff into buffer */
548         {
549             connection->peek_len = *recvd;
550             if (!*recvd)
551             {
552                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
553                 connection->peek_msg_mem = NULL;
554                 connection->peek_msg = NULL;
555             }
556             else
557                 memcpy(connection->peek_msg, buf, *recvd);
558         }
559         if (*recvd < 1 && len)
560             return FALSE;
561         return TRUE;
562 #else
563         return FALSE;
564 #endif
565     }
566 }
567
568 /******************************************************************************
569  * NETCON_query_data_available
570  * Returns the number of bytes of peeked data plus the number of bytes of
571  * queued, but unread data.
572  */
573 BOOL NETCON_query_data_available(WININET_NETCONNECTION *connection, DWORD *available)
574 {
575     *available = 0;
576     if (!NETCON_connected(connection))
577         return FALSE;
578
579 #ifdef SONAME_LIBSSL
580     if (connection->peek_msg) *available = connection->peek_len;
581 #endif
582
583 #ifdef FIONREAD
584     if (!connection->useSSL)
585     {
586         int unread;
587         int retval = ioctl(connection->socketFD, FIONREAD, &unread);
588         if (!retval)
589         {
590             TRACE("%d bytes of queued, but unread data\n", unread);
591             *available += unread;
592         }
593     }
594 #endif
595     return TRUE;
596 }
597
598 /******************************************************************************
599  * NETCON_getNextLine
600  */
601 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
602 {
603
604     TRACE("\n");
605
606     if (!NETCON_connected(connection)) return FALSE;
607
608     if (!connection->useSSL)
609     {
610         struct timeval tv;
611         fd_set infd;
612         BOOL bSuccess = FALSE;
613         DWORD nRecv = 0;
614
615         FD_ZERO(&infd);
616         FD_SET(connection->socketFD, &infd);
617         tv.tv_sec=RESPONSE_TIMEOUT;
618         tv.tv_usec=0;
619
620         while (nRecv < *dwBuffer)
621         {
622             if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
623             {
624                 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
625                 {
626                     INTERNET_SetLastError(sock_get_error(errno));
627                     goto lend;
628                 }
629
630                 if (lpszBuffer[nRecv] == '\n')
631                 {
632                     bSuccess = TRUE;
633                     break;
634                 }
635                 if (lpszBuffer[nRecv] != '\r')
636                     nRecv++;
637             }
638             else
639             {
640                 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
641                 goto lend;
642             }
643         }
644
645     lend:             /* FIXME: don't use labels */
646         if (bSuccess)
647         {
648             lpszBuffer[nRecv++] = '\0';
649             *dwBuffer = nRecv;
650             TRACE(":%u %s\n", nRecv, lpszBuffer);
651             return TRUE;
652         }
653         else
654         {
655             return FALSE;
656         }
657     }
658     else
659     {
660 #ifdef SONAME_LIBSSL
661         long prev_timeout;
662         DWORD nRecv = 0;
663         BOOL success = TRUE;
664
665         prev_timeout = pSSL_CTX_get_timeout(ctx);
666         pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
667
668         while (nRecv < *dwBuffer)
669         {
670             int recv = 1;
671             if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
672             {
673                 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
674                 success = FALSE;
675             }
676
677             if (lpszBuffer[nRecv] == '\n')
678             {
679                 success = TRUE;
680                 break;
681             }
682             if (lpszBuffer[nRecv] != '\r')
683                 nRecv++;
684         }
685
686         pSSL_CTX_set_timeout(ctx, prev_timeout);
687         if (success)
688         {
689             lpszBuffer[nRecv++] = '\0';
690             *dwBuffer = nRecv;
691             TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
692             return TRUE;
693         }
694         return FALSE;
695 #else
696         return FALSE;
697 #endif
698     }
699 }
700
701
702 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
703 {
704 #ifdef SONAME_LIBSSL
705     X509* cert;
706     unsigned char* buffer,*p;
707     INT len;
708     BOOL malloced = FALSE;
709     LPCVOID r = NULL;
710
711     if (!connection->useSSL)
712         return NULL;
713
714     cert = pSSL_get_peer_certificate(connection->ssl_s);
715     p = NULL;
716     len = pi2d_X509(cert,&p);
717     /*
718      * SSL 0.9.7 and above malloc the buffer if it is null. 
719      * however earlier version do not and so we would need to alloc the buffer.
720      *
721      * see the i2d_X509 man page for more details.
722      */
723     if (!p)
724     {
725         buffer = HeapAlloc(GetProcessHeap(),0,len);
726         p = buffer;
727         len = pi2d_X509(cert,&p);
728     }
729     else
730     {
731         buffer = p;
732         malloced = TRUE;
733     }
734
735     r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
736
737     if (malloced)
738         free(buffer);
739     else
740         HeapFree(GetProcessHeap(),0,buffer);
741
742     return r;
743 #else
744     return NULL;
745 #endif
746 }
747
748 BOOL NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
749 {
750     int result;
751     struct timeval tv;
752
753     /* FIXME: we should probably store the timeout in the connection to set
754      * when we do connect */
755     if (!NETCON_connected(connection))
756         return TRUE;
757
758     /* value is in milliseconds, convert to struct timeval */
759     tv.tv_sec = value / 1000;
760     tv.tv_usec = (value % 1000) * 1000;
761
762     result = setsockopt(connection->socketFD, SOL_SOCKET,
763                         send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
764                         sizeof(tv));
765
766     if (result == -1)
767     {
768         WARN("setsockopt failed (%s)\n", strerror(errno));
769         INTERNET_SetLastError(sock_get_error(errno));
770         return FALSE;
771     }
772
773     return TRUE;
774 }