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