user32: Fix a couple of message test failures.
[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 #include "wine/debug.h"
53 #include "internet.h"
54
55 /* To avoid conflicts with the Unix socket headers. we only need it for
56  * the error codes anyway. */
57 #define USE_WS_PREFIX
58 #include "winsock2.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 #if !defined(__MINGW32__) && !defined (_MSC_VER)
207     switch (err)
208     {
209         case EINTR:             return WSAEINTR;
210         case EBADF:             return WSAEBADF;
211         case EPERM:
212         case EACCES:            return WSAEACCES;
213         case EFAULT:            return WSAEFAULT;
214         case EINVAL:            return WSAEINVAL;
215         case EMFILE:            return WSAEMFILE;
216         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
217         case EINPROGRESS:       return WSAEINPROGRESS;
218         case EALREADY:          return WSAEALREADY;
219         case ENOTSOCK:          return WSAENOTSOCK;
220         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
221         case EMSGSIZE:          return WSAEMSGSIZE;
222         case EPROTOTYPE:        return WSAEPROTOTYPE;
223         case ENOPROTOOPT:       return WSAENOPROTOOPT;
224         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
225         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
226         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
227         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
228         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
229         case EADDRINUSE:        return WSAEADDRINUSE;
230         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
231         case ENETDOWN:          return WSAENETDOWN;
232         case ENETUNREACH:       return WSAENETUNREACH;
233         case ENETRESET:         return WSAENETRESET;
234         case ECONNABORTED:      return WSAECONNABORTED;
235         case EPIPE:
236         case ECONNRESET:        return WSAECONNRESET;
237         case ENOBUFS:           return WSAENOBUFS;
238         case EISCONN:           return WSAEISCONN;
239         case ENOTCONN:          return WSAENOTCONN;
240         case ESHUTDOWN:         return WSAESHUTDOWN;
241         case ETOOMANYREFS:      return WSAETOOMANYREFS;
242         case ETIMEDOUT:         return WSAETIMEDOUT;
243         case ECONNREFUSED:      return WSAECONNREFUSED;
244         case ELOOP:             return WSAELOOP;
245         case ENAMETOOLONG:      return WSAENAMETOOLONG;
246         case EHOSTDOWN:         return WSAEHOSTDOWN;
247         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
248         case ENOTEMPTY:         return WSAENOTEMPTY;
249 #ifdef EPROCLIM
250         case EPROCLIM:          return WSAEPROCLIM;
251 #endif
252 #ifdef EUSERS
253         case EUSERS:            return WSAEUSERS;
254 #endif
255 #ifdef EDQUOT
256         case EDQUOT:            return WSAEDQUOT;
257 #endif
258 #ifdef ESTALE
259         case ESTALE:            return WSAESTALE;
260 #endif
261 #ifdef EREMOTE
262         case EREMOTE:           return WSAEREMOTE;
263 #endif
264     default: errno=err; perror("sock_set_error"); return WSAEFAULT;
265     }
266 #endif
267     return err;
268 }
269
270 /******************************************************************************
271  * NETCON_create
272  * Basically calls 'socket()'
273  */
274 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
275               int type, int protocol)
276 {
277 #ifdef SONAME_LIBSSL
278     if (connection->useSSL)
279         return FALSE;
280 #endif
281
282     connection->socketFD = socket(domain, type, protocol);
283     if (connection->socketFD == -1)
284     {
285         INTERNET_SetLastError(sock_get_error(errno));
286         return FALSE;
287     }
288     return TRUE;
289 }
290
291 /******************************************************************************
292  * NETCON_close
293  * Basically calls 'close()' unless we should use SSL
294  */
295 BOOL NETCON_close(WININET_NETCONNECTION *connection)
296 {
297     int result;
298
299     if (!NETCON_connected(connection)) return FALSE;
300
301 #ifdef SONAME_LIBSSL
302     if (connection->useSSL)
303     {
304         HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
305         connection->peek_msg = NULL;
306         connection->peek_msg_mem = NULL;
307         connection->peek_len = 0;
308
309         pSSL_shutdown(connection->ssl_s);
310         pSSL_free(connection->ssl_s);
311         connection->ssl_s = NULL;
312
313         connection->useSSL = FALSE;
314     }
315 #endif
316
317     result = closesocket(connection->socketFD);
318     connection->socketFD = -1;
319
320     if (result == -1)
321     {
322         INTERNET_SetLastError(sock_get_error(errno));
323         return FALSE;
324     }
325     return TRUE;
326 }
327 #ifdef SONAME_LIBSSL
328 static BOOL check_hostname(X509 *cert, char *hostname)
329 {
330     /* FIXME: implement */
331     return TRUE;
332 }
333 #endif
334 /******************************************************************************
335  * NETCON_secure_connect
336  * Initiates a secure connection over an existing plaintext connection.
337  */
338 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
339 {
340 #ifdef SONAME_LIBSSL
341     long verify_res;
342     X509 *cert;
343     int len;
344     char *hostname_unix;
345
346     /* can't connect if we are already connected */
347     if (connection->useSSL)
348     {
349         ERR("already connected\n");
350         return FALSE;
351     }
352
353     ctx = pSSL_CTX_new(meth);
354     if (!pSSL_CTX_set_default_verify_paths(ctx))
355     {
356         ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
357             pERR_error_string(pERR_get_error(), 0));
358         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
359         return FALSE;
360     }
361     connection->ssl_s = pSSL_new(ctx);
362     if (!connection->ssl_s)
363     {
364         ERR("SSL_new failed: %s\n",
365             pERR_error_string(pERR_get_error(), 0));
366         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
367         goto fail;
368     }
369
370     if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
371     {
372         ERR("SSL_set_fd failed: %s\n",
373             pERR_error_string(pERR_get_error(), 0));
374         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
375         goto fail;
376     }
377
378     if (pSSL_connect(connection->ssl_s) <= 0)
379     {
380         ERR("SSL_connect failed: %s\n",
381             pERR_error_string(pERR_get_error(), 0));
382         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
383         goto fail;
384     }
385     cert = pSSL_get_peer_certificate(connection->ssl_s);
386     if (!cert)
387     {
388         ERR("no certificate for server %s\n", debugstr_w(hostname));
389         /* FIXME: is this the best error? */
390         INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
391         goto fail;
392     }
393     verify_res = pSSL_get_verify_result(connection->ssl_s);
394     if (verify_res != X509_V_OK)
395     {
396         ERR("couldn't verify the security of the connection, %ld\n", verify_res);
397         /* FIXME: we should set an error and return, but we only warn at
398          * the moment */
399     }
400
401     len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
402     hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
403     if (!hostname_unix)
404     {
405         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
406         goto fail;
407     }
408     WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
409
410     if (!check_hostname(cert, hostname_unix))
411     {
412         HeapFree(GetProcessHeap(), 0, hostname_unix);
413         INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
414         goto fail;
415     }
416
417     HeapFree(GetProcessHeap(), 0, hostname_unix);
418     connection->useSSL = TRUE;
419     return TRUE;
420
421 fail:
422     if (connection->ssl_s)
423     {
424         pSSL_shutdown(connection->ssl_s);
425         pSSL_free(connection->ssl_s);
426         connection->ssl_s = NULL;
427     }
428 #endif
429     return FALSE;
430 }
431
432 /******************************************************************************
433  * NETCON_connect
434  * Connects to the specified address.
435  */
436 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
437                     unsigned int addrlen)
438 {
439     int result;
440
441     if (!NETCON_connected(connection)) return FALSE;
442
443     result = connect(connection->socketFD, serv_addr, addrlen);
444     if (result == -1)
445     {
446         WARN("Unable to connect to host (%s)\n", strerror(errno));
447         INTERNET_SetLastError(sock_get_error(errno));
448
449         closesocket(connection->socketFD);
450         connection->socketFD = -1;
451         return FALSE;
452     }
453
454     return TRUE;
455 }
456
457 /******************************************************************************
458  * NETCON_send
459  * Basically calls 'send()' unless we should use SSL
460  * number of chars send is put in *sent
461  */
462 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
463                 int *sent /* out */)
464 {
465     if (!NETCON_connected(connection)) return FALSE;
466     if (!connection->useSSL)
467     {
468         *sent = send(connection->socketFD, msg, len, flags);
469         if (*sent == -1)
470         {
471             INTERNET_SetLastError(sock_get_error(errno));
472             return FALSE;
473         }
474         return TRUE;
475     }
476     else
477     {
478 #ifdef SONAME_LIBSSL
479         if (flags)
480             FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
481         *sent = pSSL_write(connection->ssl_s, msg, len);
482         if (*sent < 1 && len)
483             return FALSE;
484         return TRUE;
485 #else
486         return FALSE;
487 #endif
488     }
489 }
490
491 /******************************************************************************
492  * NETCON_recv
493  * Basically calls 'recv()' unless we should use SSL
494  * number of chars received is put in *recvd
495  */
496 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
497                 int *recvd /* out */)
498 {
499     *recvd = 0;
500     if (!NETCON_connected(connection)) return FALSE;
501     if (!len)
502         return TRUE;
503     if (!connection->useSSL)
504     {
505         *recvd = recv(connection->socketFD, buf, len, flags);
506         if (*recvd == -1)
507         {
508             INTERNET_SetLastError(sock_get_error(errno));
509             return FALSE;
510         }
511         return TRUE;
512     }
513     else
514     {
515 #ifdef SONAME_LIBSSL
516         if (flags & ~(MSG_PEEK|MSG_WAITALL))
517             FIXME("SSL_read does not support the following flag: %08x\n", flags);
518
519         /* this ugly hack is all for MSG_PEEK. eww gross */
520         if (flags & MSG_PEEK && !connection->peek_msg)
521         {
522             connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
523         }
524         else if (flags & MSG_PEEK && connection->peek_msg)
525         {
526             if (len < connection->peek_len)
527                 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
528             *recvd = min(len, connection->peek_len);
529             memcpy(buf, connection->peek_msg, *recvd);
530             return TRUE;
531         }
532         else if (connection->peek_msg)
533         {
534             *recvd = min(len, connection->peek_len);
535             memcpy(buf, connection->peek_msg, *recvd);
536             connection->peek_len -= *recvd;
537             connection->peek_msg += *recvd;
538             if (connection->peek_len == 0)
539             {
540                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
541                 connection->peek_msg_mem = NULL;
542                 connection->peek_msg = NULL;
543             }
544             /* check if we got enough data from the peek buffer */
545             if (!(flags & MSG_WAITALL) || (*recvd == len))
546                 return TRUE;
547             /* otherwise, fall through */
548         }
549         *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
550         if (flags & MSG_PEEK) /* must copy stuff into buffer */
551         {
552             connection->peek_len = *recvd;
553             if (!*recvd)
554             {
555                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
556                 connection->peek_msg_mem = NULL;
557                 connection->peek_msg = NULL;
558             }
559             else
560                 memcpy(connection->peek_msg, buf, *recvd);
561         }
562         if (*recvd < 1 && len)
563             return FALSE;
564         return TRUE;
565 #else
566         return FALSE;
567 #endif
568     }
569 }
570
571 /******************************************************************************
572  * NETCON_query_data_available
573  * Returns the number of bytes of peeked data plus the number of bytes of
574  * queued, but unread data.
575  */
576 BOOL NETCON_query_data_available(WININET_NETCONNECTION *connection, DWORD *available)
577 {
578     *available = 0;
579     if (!NETCON_connected(connection))
580         return FALSE;
581
582 #ifdef SONAME_LIBSSL
583     if (connection->peek_msg) *available = connection->peek_len;
584 #endif
585
586 #ifdef FIONREAD
587     if (!connection->useSSL)
588     {
589         int unread;
590         int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
591         if (!retval)
592         {
593             TRACE("%d bytes of queued, but unread data\n", unread);
594             *available += unread;
595         }
596     }
597 #endif
598     return TRUE;
599 }
600
601 /******************************************************************************
602  * NETCON_getNextLine
603  */
604 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
605 {
606
607     TRACE("\n");
608
609     if (!NETCON_connected(connection)) return FALSE;
610
611     if (!connection->useSSL)
612     {
613         struct timeval tv;
614         fd_set infd;
615         BOOL bSuccess = FALSE;
616         DWORD nRecv = 0;
617
618         FD_ZERO(&infd);
619         FD_SET(connection->socketFD, &infd);
620         tv.tv_sec=RESPONSE_TIMEOUT;
621         tv.tv_usec=0;
622
623         while (nRecv < *dwBuffer)
624         {
625             if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
626             {
627                 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
628                 {
629                     INTERNET_SetLastError(sock_get_error(errno));
630                     goto lend;
631                 }
632
633                 if (lpszBuffer[nRecv] == '\n')
634                 {
635                     bSuccess = TRUE;
636                     break;
637                 }
638                 if (lpszBuffer[nRecv] != '\r')
639                     nRecv++;
640             }
641             else
642             {
643                 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
644                 goto lend;
645             }
646         }
647
648     lend:             /* FIXME: don't use labels */
649         if (bSuccess)
650         {
651             lpszBuffer[nRecv++] = '\0';
652             *dwBuffer = nRecv;
653             TRACE(":%u %s\n", nRecv, lpszBuffer);
654             return TRUE;
655         }
656         else
657         {
658             return FALSE;
659         }
660     }
661     else
662     {
663 #ifdef SONAME_LIBSSL
664         long prev_timeout;
665         DWORD nRecv = 0;
666         BOOL success = TRUE;
667
668         prev_timeout = pSSL_CTX_get_timeout(ctx);
669         pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
670
671         while (nRecv < *dwBuffer)
672         {
673             int recv = 1;
674             if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
675             {
676                 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
677                 success = FALSE;
678             }
679
680             if (lpszBuffer[nRecv] == '\n')
681             {
682                 success = TRUE;
683                 break;
684             }
685             if (lpszBuffer[nRecv] != '\r')
686                 nRecv++;
687         }
688
689         pSSL_CTX_set_timeout(ctx, prev_timeout);
690         if (success)
691         {
692             lpszBuffer[nRecv++] = '\0';
693             *dwBuffer = nRecv;
694             TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
695             return TRUE;
696         }
697         return FALSE;
698 #else
699         return FALSE;
700 #endif
701     }
702 }
703
704
705 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
706 {
707 #ifdef SONAME_LIBSSL
708     X509* cert;
709     unsigned char* buffer,*p;
710     INT len;
711     BOOL malloced = FALSE;
712     LPCVOID r = NULL;
713
714     if (!connection->useSSL)
715         return NULL;
716
717     cert = pSSL_get_peer_certificate(connection->ssl_s);
718     p = NULL;
719     len = pi2d_X509(cert,&p);
720     /*
721      * SSL 0.9.7 and above malloc the buffer if it is null. 
722      * however earlier version do not and so we would need to alloc the buffer.
723      *
724      * see the i2d_X509 man page for more details.
725      */
726     if (!p)
727     {
728         buffer = HeapAlloc(GetProcessHeap(),0,len);
729         p = buffer;
730         len = pi2d_X509(cert,&p);
731     }
732     else
733     {
734         buffer = p;
735         malloced = TRUE;
736     }
737
738     r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
739
740     if (malloced)
741         free(buffer);
742     else
743         HeapFree(GetProcessHeap(),0,buffer);
744
745     return r;
746 #else
747     return NULL;
748 #endif
749 }
750
751 DWORD NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
752 {
753     int result;
754     struct timeval tv;
755
756     /* FIXME: we should probably store the timeout in the connection to set
757      * when we do connect */
758     if (!NETCON_connected(connection))
759         return ERROR_SUCCESS;
760
761     /* value is in milliseconds, convert to struct timeval */
762     tv.tv_sec = value / 1000;
763     tv.tv_usec = (value % 1000) * 1000;
764
765     result = setsockopt(connection->socketFD, SOL_SOCKET,
766                         send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
767                         sizeof(tv));
768
769     if (result == -1)
770     {
771         WARN("setsockopt failed (%s)\n", strerror(errno));
772         return sock_get_error(errno);
773     }
774
775     return ERROR_SUCCESS;
776 }