2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #define NONAMELESSUNION
28 #if defined(__MINGW32__) || defined (_MSC_VER)
32 #include <sys/types.h>
36 #ifdef HAVE_SYS_POLL_H
37 # include <sys/poll.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
45 #ifdef HAVE_SYS_FILIO_H
46 # include <sys/filio.h>
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
58 #ifdef HAVE_NETINET_IN_H
59 # include <netinet/in.h>
61 #ifdef HAVE_OPENSSL_SSL_H
62 # include <openssl/ssl.h>
73 #include "wine/library.h"
80 #include "wine/debug.h"
83 /* To avoid conflicts with the Unix socket headers. we only need it for
84 * the error codes anyway. */
88 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
91 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
94 * This should use winsock - To use winsock the functions will have to change a bit
95 * as they are designed for unix sockets.
96 * SSL stuff should use crypt32.dll
101 #include <openssl/err.h>
103 static CRITICAL_SECTION init_ssl_cs
;
104 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug
=
107 { &init_ssl_cs_debug
.ProcessLocksList
,
108 &init_ssl_cs_debug
.ProcessLocksList
},
109 0, 0, { (DWORD_PTR
)(__FILE__
": init_ssl_cs") }
111 static CRITICAL_SECTION init_ssl_cs
= { &init_ssl_cs_debug
, -1, 0, 0, 0, 0 };
113 static void *OpenSSL_ssl_handle
;
114 static void *OpenSSL_crypto_handle
;
116 static SSL_METHOD
*meth
;
118 static int hostname_idx
;
119 static int error_idx
;
121 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
123 /* OpenSSL functions that we use */
124 MAKE_FUNCPTR(SSL_library_init
);
125 MAKE_FUNCPTR(SSL_load_error_strings
);
126 MAKE_FUNCPTR(SSLv23_method
);
127 MAKE_FUNCPTR(SSL_CTX_free
);
128 MAKE_FUNCPTR(SSL_CTX_new
);
129 MAKE_FUNCPTR(SSL_new
);
130 MAKE_FUNCPTR(SSL_free
);
131 MAKE_FUNCPTR(SSL_set_fd
);
132 MAKE_FUNCPTR(SSL_connect
);
133 MAKE_FUNCPTR(SSL_shutdown
);
134 MAKE_FUNCPTR(SSL_write
);
135 MAKE_FUNCPTR(SSL_read
);
136 MAKE_FUNCPTR(SSL_pending
);
137 MAKE_FUNCPTR(SSL_get_ex_new_index
);
138 MAKE_FUNCPTR(SSL_get_ex_data
);
139 MAKE_FUNCPTR(SSL_set_ex_data
);
140 MAKE_FUNCPTR(SSL_get_ex_data_X509_STORE_CTX_idx
);
141 MAKE_FUNCPTR(SSL_get_verify_result
);
142 MAKE_FUNCPTR(SSL_get_peer_certificate
);
143 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
144 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
145 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
146 MAKE_FUNCPTR(SSL_CTX_set_verify
);
147 MAKE_FUNCPTR(X509_STORE_CTX_get_ex_data
);
149 /* OpenSSL's libcrypto functions that we use */
150 MAKE_FUNCPTR(BIO_new_fp
);
151 MAKE_FUNCPTR(CRYPTO_num_locks
);
152 MAKE_FUNCPTR(CRYPTO_set_id_callback
);
153 MAKE_FUNCPTR(CRYPTO_set_locking_callback
);
154 MAKE_FUNCPTR(ERR_free_strings
);
155 MAKE_FUNCPTR(ERR_get_error
);
156 MAKE_FUNCPTR(ERR_error_string
);
157 MAKE_FUNCPTR(i2d_X509
);
158 MAKE_FUNCPTR(sk_num
);
159 MAKE_FUNCPTR(sk_value
);
162 static CRITICAL_SECTION
*ssl_locks
;
164 static unsigned long ssl_thread_id(void)
166 return GetCurrentThreadId();
169 static void ssl_lock_callback(int mode
, int type
, const char *file
, int line
)
171 if (mode
& CRYPTO_LOCK
)
172 EnterCriticalSection(&ssl_locks
[type
]);
174 LeaveCriticalSection(&ssl_locks
[type
]);
177 static PCCERT_CONTEXT
X509_to_cert_context(X509
*cert
)
179 unsigned char* buffer
,*p
;
181 BOOL malloced
= FALSE
;
185 len
= pi2d_X509(cert
,&p
);
187 * SSL 0.9.7 and above malloc the buffer if it is null.
188 * however earlier version do not and so we would need to alloc the buffer.
190 * see the i2d_X509 man page for more details.
194 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
196 len
= pi2d_X509(cert
,&p
);
204 ret
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
209 HeapFree(GetProcessHeap(),0,buffer
);
214 static DWORD
netconn_verify_cert(PCCERT_CONTEXT cert
, HCERTSTORE store
,
218 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
219 PCCERT_CHAIN_CONTEXT chain
;
220 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
221 char *server_auth
[] = { oid_server_auth
};
222 DWORD err
= ERROR_SUCCESS
;
224 TRACE("verifying %s\n", debugstr_w(server
));
225 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
226 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
227 if ((ret
= CertGetCertificateChain(NULL
, cert
, NULL
, store
, &chainPara
, 0,
230 if (chain
->TrustStatus
.dwErrorStatus
)
232 if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
233 err
= ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
234 else if (chain
->TrustStatus
.dwErrorStatus
&
235 CERT_TRUST_IS_UNTRUSTED_ROOT
)
236 err
= ERROR_INTERNET_INVALID_CA
;
237 else if ((chain
->TrustStatus
.dwErrorStatus
&
238 CERT_TRUST_IS_OFFLINE_REVOCATION
) ||
239 (chain
->TrustStatus
.dwErrorStatus
&
240 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
))
241 err
= ERROR_INTERNET_SEC_CERT_NO_REV
;
242 else if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_REVOKED
)
243 err
= ERROR_INTERNET_SEC_CERT_REVOKED
;
244 else if (chain
->TrustStatus
.dwErrorStatus
&
245 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
246 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
248 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
252 CERT_CHAIN_POLICY_PARA policyPara
;
253 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
254 CERT_CHAIN_POLICY_STATUS policyStatus
;
256 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
257 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
258 sslExtraPolicyPara
.pwszServerName
= server
;
259 policyPara
.cbSize
= sizeof(policyPara
);
260 policyPara
.dwFlags
= 0;
261 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
262 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
263 chain
, &policyPara
, &policyStatus
);
264 /* Any error in the policy status indicates that the
265 * policy couldn't be verified.
267 if (ret
&& policyStatus
.dwError
)
269 if (policyStatus
.dwError
== CERT_E_CN_NO_MATCH
)
270 err
= ERROR_INTERNET_SEC_CERT_CN_INVALID
;
272 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
275 CertFreeCertificateChain(chain
);
277 TRACE("returning %08x\n", err
);
281 static int netconn_secure_verify(int preverify_ok
, X509_STORE_CTX
*ctx
)
287 ssl
= pX509_STORE_CTX_get_ex_data(ctx
,
288 pSSL_get_ex_data_X509_STORE_CTX_idx());
289 server
= pSSL_get_ex_data(ssl
, hostname_idx
);
292 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
293 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
299 PCCERT_CONTEXT endCert
= NULL
;
302 for (i
= 0; ret
&& i
< psk_num((struct stack_st
*)ctx
->chain
); i
++)
304 PCCERT_CONTEXT context
;
306 cert
= (X509
*)psk_value((struct stack_st
*)ctx
->chain
, i
);
307 if ((context
= X509_to_cert_context(cert
)))
310 ret
= CertAddCertificateContextToStore(store
, context
,
311 CERT_STORE_ADD_ALWAYS
, &endCert
);
313 ret
= CertAddCertificateContextToStore(store
, context
,
314 CERT_STORE_ADD_ALWAYS
, NULL
);
315 CertFreeCertificateContext(context
);
318 if (!endCert
) ret
= FALSE
;
321 DWORD_PTR err
= netconn_verify_cert(endCert
, store
, server
);
325 pSSL_set_ex_data(ssl
, error_idx
, (void *)err
);
329 CertFreeCertificateContext(endCert
);
330 CertCloseStore(store
, 0);
338 DWORD
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
340 connection
->useSSL
= FALSE
;
341 connection
->socketFD
= -1;
344 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
347 TRACE("using SSL connection\n");
348 EnterCriticalSection(&init_ssl_cs
);
349 if (OpenSSL_ssl_handle
) /* already initialized everything */
351 LeaveCriticalSection(&init_ssl_cs
);
352 return ERROR_SUCCESS
;
354 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
355 if (!OpenSSL_ssl_handle
)
357 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
359 LeaveCriticalSection(&init_ssl_cs
);
360 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
362 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
363 if (!OpenSSL_crypto_handle
)
365 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
367 LeaveCriticalSection(&init_ssl_cs
);
368 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
371 /* mmm nice ugly macroness */
373 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
376 ERR("failed to load symbol %s\n", #x); \
377 LeaveCriticalSection(&init_ssl_cs); \
378 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
381 DYNSSL(SSL_library_init
);
382 DYNSSL(SSL_load_error_strings
);
383 DYNSSL(SSLv23_method
);
384 DYNSSL(SSL_CTX_free
);
390 DYNSSL(SSL_shutdown
);
394 DYNSSL(SSL_get_ex_new_index
);
395 DYNSSL(SSL_get_ex_data
);
396 DYNSSL(SSL_set_ex_data
);
397 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx
);
398 DYNSSL(SSL_get_verify_result
);
399 DYNSSL(SSL_get_peer_certificate
);
400 DYNSSL(SSL_CTX_get_timeout
);
401 DYNSSL(SSL_CTX_set_timeout
);
402 DYNSSL(SSL_CTX_set_default_verify_paths
);
403 DYNSSL(SSL_CTX_set_verify
);
404 DYNSSL(X509_STORE_CTX_get_ex_data
);
407 #define DYNCRYPTO(x) \
408 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
411 ERR("failed to load symbol %s\n", #x); \
412 LeaveCriticalSection(&init_ssl_cs); \
413 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
415 DYNCRYPTO(BIO_new_fp
);
416 DYNCRYPTO(CRYPTO_num_locks
);
417 DYNCRYPTO(CRYPTO_set_id_callback
);
418 DYNCRYPTO(CRYPTO_set_locking_callback
);
419 DYNCRYPTO(ERR_free_strings
);
420 DYNCRYPTO(ERR_get_error
);
421 DYNCRYPTO(ERR_error_string
);
428 pSSL_load_error_strings();
429 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
431 meth
= pSSLv23_method();
432 ctx
= pSSL_CTX_new(meth
);
433 if (!pSSL_CTX_set_default_verify_paths(ctx
))
435 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
436 pERR_error_string(pERR_get_error(), 0));
437 LeaveCriticalSection(&init_ssl_cs
);
438 return ERROR_OUTOFMEMORY
;
440 hostname_idx
= pSSL_get_ex_new_index(0, (void *)"hostname index",
442 if (hostname_idx
== -1)
444 ERR("SSL_get_ex_new_index failed; %s\n",
445 pERR_error_string(pERR_get_error(), 0));
446 LeaveCriticalSection(&init_ssl_cs
);
447 return ERROR_OUTOFMEMORY
;
449 error_idx
= pSSL_get_ex_new_index(0, (void *)"error index",
453 ERR("SSL_get_ex_new_index failed; %s\n",
454 pERR_error_string(pERR_get_error(), 0));
455 LeaveCriticalSection(&init_ssl_cs
);
456 return ERROR_OUTOFMEMORY
;
458 pSSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, netconn_secure_verify
);
460 pCRYPTO_set_id_callback(ssl_thread_id
);
461 ssl_locks
= HeapAlloc(GetProcessHeap(), 0,
462 pCRYPTO_num_locks() * sizeof(CRITICAL_SECTION
));
465 LeaveCriticalSection(&init_ssl_cs
);
466 return ERROR_OUTOFMEMORY
;
468 for (i
= 0; i
< pCRYPTO_num_locks(); i
++)
469 InitializeCriticalSection(&ssl_locks
[i
]);
470 pCRYPTO_set_locking_callback(ssl_lock_callback
);
471 LeaveCriticalSection(&init_ssl_cs
);
473 FIXME("can't use SSL, not compiled in.\n");
474 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
477 return ERROR_SUCCESS
;
480 void NETCON_unload(void)
482 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
483 if (OpenSSL_crypto_handle
)
486 wine_dlclose(OpenSSL_crypto_handle
, NULL
, 0);
488 if (OpenSSL_ssl_handle
)
492 wine_dlclose(OpenSSL_ssl_handle
, NULL
, 0);
497 for (i
= 0; i
< pCRYPTO_num_locks(); i
++) DeleteCriticalSection(&ssl_locks
[i
]);
498 HeapFree(GetProcessHeap(), 0, ssl_locks
);
503 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
505 if (connection
->socketFD
== -1)
511 /* translate a unix error code into a winsock one */
512 int sock_get_error( int err
)
514 #if !defined(__MINGW32__) && !defined (_MSC_VER)
517 case EINTR
: return WSAEINTR
;
518 case EBADF
: return WSAEBADF
;
520 case EACCES
: return WSAEACCES
;
521 case EFAULT
: return WSAEFAULT
;
522 case EINVAL
: return WSAEINVAL
;
523 case EMFILE
: return WSAEMFILE
;
524 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
525 case EINPROGRESS
: return WSAEINPROGRESS
;
526 case EALREADY
: return WSAEALREADY
;
527 case ENOTSOCK
: return WSAENOTSOCK
;
528 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
529 case EMSGSIZE
: return WSAEMSGSIZE
;
530 case EPROTOTYPE
: return WSAEPROTOTYPE
;
531 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
532 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
533 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
534 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
535 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
536 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
537 case EADDRINUSE
: return WSAEADDRINUSE
;
538 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
539 case ENETDOWN
: return WSAENETDOWN
;
540 case ENETUNREACH
: return WSAENETUNREACH
;
541 case ENETRESET
: return WSAENETRESET
;
542 case ECONNABORTED
: return WSAECONNABORTED
;
544 case ECONNRESET
: return WSAECONNRESET
;
545 case ENOBUFS
: return WSAENOBUFS
;
546 case EISCONN
: return WSAEISCONN
;
547 case ENOTCONN
: return WSAENOTCONN
;
548 case ESHUTDOWN
: return WSAESHUTDOWN
;
549 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
550 case ETIMEDOUT
: return WSAETIMEDOUT
;
551 case ECONNREFUSED
: return WSAECONNREFUSED
;
552 case ELOOP
: return WSAELOOP
;
553 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
554 case EHOSTDOWN
: return WSAEHOSTDOWN
;
555 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
556 case ENOTEMPTY
: return WSAENOTEMPTY
;
558 case EPROCLIM
: return WSAEPROCLIM
;
561 case EUSERS
: return WSAEUSERS
;
564 case EDQUOT
: return WSAEDQUOT
;
567 case ESTALE
: return WSAESTALE
;
570 case EREMOTE
: return WSAEREMOTE
;
572 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
578 /******************************************************************************
580 * Basically calls 'socket()'
582 DWORD
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
583 int type
, int protocol
)
586 if (connection
->useSSL
)
587 return ERROR_NOT_SUPPORTED
;
590 connection
->socketFD
= socket(domain
, type
, protocol
);
591 if (connection
->socketFD
== -1)
592 return sock_get_error(errno
);
594 return ERROR_SUCCESS
;
597 /******************************************************************************
599 * Basically calls 'close()' unless we should use SSL
601 DWORD
NETCON_close(WININET_NETCONNECTION
*connection
)
605 if (!NETCON_connected(connection
)) return ERROR_SUCCESS
;
608 if (connection
->useSSL
)
610 pSSL_shutdown(connection
->ssl_s
);
611 pSSL_free(connection
->ssl_s
);
612 connection
->ssl_s
= NULL
;
614 connection
->useSSL
= FALSE
;
618 result
= closesocket(connection
->socketFD
);
619 connection
->socketFD
= -1;
622 return sock_get_error(errno
);
623 return ERROR_SUCCESS
;
626 /******************************************************************************
627 * NETCON_secure_connect
628 * Initiates a secure connection over an existing plaintext connection.
630 DWORD
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPWSTR hostname
)
632 DWORD res
= ERROR_NOT_SUPPORTED
;
637 /* can't connect if we are already connected */
638 if (connection
->useSSL
)
640 ERR("already connected\n");
641 return ERROR_INTERNET_CANNOT_CONNECT
;
644 connection
->ssl_s
= pSSL_new(ctx
);
645 if (!connection
->ssl_s
)
647 ERR("SSL_new failed: %s\n",
648 pERR_error_string(pERR_get_error(), 0));
649 res
= ERROR_OUTOFMEMORY
;
653 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
655 ERR("SSL_set_fd failed: %s\n",
656 pERR_error_string(pERR_get_error(), 0));
657 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
661 if (pSSL_connect(connection
->ssl_s
) <= 0)
663 res
= (DWORD_PTR
)pSSL_get_ex_data(connection
->ssl_s
, error_idx
);
665 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
666 ERR("SSL_connect failed: %d\n", res
);
669 pSSL_set_ex_data(connection
->ssl_s
, hostname_idx
, hostname
);
670 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
673 ERR("no certificate for server %s\n", debugstr_w(hostname
));
674 /* FIXME: is this the best error? */
675 res
= ERROR_INTERNET_INVALID_CA
;
678 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
679 if (verify_res
!= X509_V_OK
)
681 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
682 /* FIXME: we should set an error and return, but we only warn at
686 connection
->useSSL
= TRUE
;
687 return ERROR_SUCCESS
;
690 if (connection
->ssl_s
)
692 pSSL_shutdown(connection
->ssl_s
);
693 pSSL_free(connection
->ssl_s
);
694 connection
->ssl_s
= NULL
;
700 /******************************************************************************
702 * Connects to the specified address.
704 DWORD
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
705 unsigned int addrlen
)
709 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
712 WARN("Unable to connect to host (%s)\n", strerror(errno
));
714 closesocket(connection
->socketFD
);
715 connection
->socketFD
= -1;
716 return sock_get_error(errno
);
719 return ERROR_SUCCESS
;
722 /******************************************************************************
724 * Basically calls 'send()' unless we should use SSL
725 * number of chars send is put in *sent
727 DWORD
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
730 if (!NETCON_connected(connection
)) return ERROR_INTERNET_CONNECTION_ABORTED
;
731 if (!connection
->useSSL
)
733 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
735 return sock_get_error(errno
);
736 return ERROR_SUCCESS
;
742 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
743 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
744 if (*sent
< 1 && len
)
745 return ERROR_INTERNET_CONNECTION_ABORTED
;
746 return ERROR_SUCCESS
;
748 return ERROR_NOT_SUPPORTED
;
753 /******************************************************************************
755 * Basically calls 'recv()' unless we should use SSL
756 * number of chars received is put in *recvd
758 DWORD
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
759 int *recvd
/* out */)
762 if (!NETCON_connected(connection
)) return ERROR_INTERNET_CONNECTION_ABORTED
;
764 return ERROR_SUCCESS
;
765 if (!connection
->useSSL
)
767 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
768 return *recvd
== -1 ? sock_get_error(errno
) : ERROR_SUCCESS
;
773 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
774 return *recvd
> 0 ? ERROR_SUCCESS
: ERROR_INTERNET_CONNECTION_ABORTED
;
776 return ERROR_NOT_SUPPORTED
;
781 /******************************************************************************
782 * NETCON_query_data_available
783 * Returns the number of bytes of peeked data plus the number of bytes of
784 * queued, but unread data.
786 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
789 if (!NETCON_connected(connection
))
792 if (!connection
->useSSL
)
796 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
799 TRACE("%d bytes of queued, but unread data\n", unread
);
800 *available
+= unread
;
807 *available
= pSSL_pending(connection
->ssl_s
);
813 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
819 if (!connection
->useSSL
)
822 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
823 r
= X509_to_cert_context(cert
);
830 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
835 /* FIXME: we should probably store the timeout in the connection to set
836 * when we do connect */
837 if (!NETCON_connected(connection
))
838 return ERROR_SUCCESS
;
840 /* value is in milliseconds, convert to struct timeval */
841 tv
.tv_sec
= value
/ 1000;
842 tv
.tv_usec
= (value
% 1000) * 1000;
844 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
845 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
850 WARN("setsockopt failed (%s)\n", strerror(errno
));
851 return sock_get_error(errno
);
854 return ERROR_SUCCESS
;