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
;
163 static unsigned int num_ssl_locks
;
165 static unsigned long ssl_thread_id(void)
167 return GetCurrentThreadId();
170 static void ssl_lock_callback(int mode
, int type
, const char *file
, int line
)
172 if (mode
& CRYPTO_LOCK
)
173 EnterCriticalSection(&ssl_locks
[type
]);
175 LeaveCriticalSection(&ssl_locks
[type
]);
178 static PCCERT_CONTEXT
X509_to_cert_context(X509
*cert
)
180 unsigned char* buffer
,*p
;
182 BOOL malloced
= FALSE
;
186 len
= pi2d_X509(cert
,&p
);
188 * SSL 0.9.7 and above malloc the buffer if it is null.
189 * however earlier version do not and so we would need to alloc the buffer.
191 * see the i2d_X509 man page for more details.
195 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
197 len
= pi2d_X509(cert
,&p
);
205 ret
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
210 HeapFree(GetProcessHeap(),0,buffer
);
215 static DWORD
netconn_verify_cert(PCCERT_CONTEXT cert
, HCERTSTORE store
,
219 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
220 PCCERT_CHAIN_CONTEXT chain
;
221 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
222 char *server_auth
[] = { oid_server_auth
};
223 DWORD err
= ERROR_SUCCESS
;
225 TRACE("verifying %s\n", debugstr_w(server
));
226 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
227 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
228 if ((ret
= CertGetCertificateChain(NULL
, cert
, NULL
, store
, &chainPara
, 0,
231 if (chain
->TrustStatus
.dwErrorStatus
)
233 if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
234 err
= ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
235 else if (chain
->TrustStatus
.dwErrorStatus
&
236 CERT_TRUST_IS_UNTRUSTED_ROOT
)
237 err
= ERROR_INTERNET_INVALID_CA
;
238 else if ((chain
->TrustStatus
.dwErrorStatus
&
239 CERT_TRUST_IS_OFFLINE_REVOCATION
) ||
240 (chain
->TrustStatus
.dwErrorStatus
&
241 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
))
242 err
= ERROR_INTERNET_SEC_CERT_NO_REV
;
243 else if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_REVOKED
)
244 err
= ERROR_INTERNET_SEC_CERT_REVOKED
;
245 else if (chain
->TrustStatus
.dwErrorStatus
&
246 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
247 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
249 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
253 CERT_CHAIN_POLICY_PARA policyPara
;
254 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
255 CERT_CHAIN_POLICY_STATUS policyStatus
;
257 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
258 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
259 sslExtraPolicyPara
.pwszServerName
= server
;
260 policyPara
.cbSize
= sizeof(policyPara
);
261 policyPara
.dwFlags
= 0;
262 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
263 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
264 chain
, &policyPara
, &policyStatus
);
265 /* Any error in the policy status indicates that the
266 * policy couldn't be verified.
268 if (ret
&& policyStatus
.dwError
)
270 if (policyStatus
.dwError
== CERT_E_CN_NO_MATCH
)
271 err
= ERROR_INTERNET_SEC_CERT_CN_INVALID
;
273 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
276 CertFreeCertificateChain(chain
);
278 TRACE("returning %08x\n", err
);
282 static int netconn_secure_verify(int preverify_ok
, X509_STORE_CTX
*ctx
)
288 ssl
= pX509_STORE_CTX_get_ex_data(ctx
,
289 pSSL_get_ex_data_X509_STORE_CTX_idx());
290 server
= pSSL_get_ex_data(ssl
, hostname_idx
);
293 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
294 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
300 PCCERT_CONTEXT endCert
= NULL
;
303 for (i
= 0; ret
&& i
< psk_num((struct stack_st
*)ctx
->chain
); i
++)
305 PCCERT_CONTEXT context
;
307 cert
= (X509
*)psk_value((struct stack_st
*)ctx
->chain
, i
);
308 if ((context
= X509_to_cert_context(cert
)))
311 ret
= CertAddCertificateContextToStore(store
, context
,
312 CERT_STORE_ADD_ALWAYS
, &endCert
);
314 ret
= CertAddCertificateContextToStore(store
, context
,
315 CERT_STORE_ADD_ALWAYS
, NULL
);
316 CertFreeCertificateContext(context
);
319 if (!endCert
) ret
= FALSE
;
322 DWORD_PTR err
= netconn_verify_cert(endCert
, store
, server
);
326 pSSL_set_ex_data(ssl
, error_idx
, (void *)err
);
330 CertFreeCertificateContext(endCert
);
331 CertCloseStore(store
, 0);
339 DWORD
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
341 connection
->useSSL
= FALSE
;
342 connection
->socketFD
= -1;
345 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
348 TRACE("using SSL connection\n");
349 EnterCriticalSection(&init_ssl_cs
);
350 if (OpenSSL_ssl_handle
) /* already initialized everything */
352 LeaveCriticalSection(&init_ssl_cs
);
353 return ERROR_SUCCESS
;
355 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
356 if (!OpenSSL_ssl_handle
)
358 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
360 LeaveCriticalSection(&init_ssl_cs
);
361 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
363 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
364 if (!OpenSSL_crypto_handle
)
366 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
368 LeaveCriticalSection(&init_ssl_cs
);
369 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
372 /* mmm nice ugly macroness */
374 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
377 ERR("failed to load symbol %s\n", #x); \
378 LeaveCriticalSection(&init_ssl_cs); \
379 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
382 DYNSSL(SSL_library_init
);
383 DYNSSL(SSL_load_error_strings
);
384 DYNSSL(SSLv23_method
);
385 DYNSSL(SSL_CTX_free
);
391 DYNSSL(SSL_shutdown
);
395 DYNSSL(SSL_get_ex_new_index
);
396 DYNSSL(SSL_get_ex_data
);
397 DYNSSL(SSL_set_ex_data
);
398 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx
);
399 DYNSSL(SSL_get_verify_result
);
400 DYNSSL(SSL_get_peer_certificate
);
401 DYNSSL(SSL_CTX_get_timeout
);
402 DYNSSL(SSL_CTX_set_timeout
);
403 DYNSSL(SSL_CTX_set_default_verify_paths
);
404 DYNSSL(SSL_CTX_set_verify
);
405 DYNSSL(X509_STORE_CTX_get_ex_data
);
408 #define DYNCRYPTO(x) \
409 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
412 ERR("failed to load symbol %s\n", #x); \
413 LeaveCriticalSection(&init_ssl_cs); \
414 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
416 DYNCRYPTO(BIO_new_fp
);
417 DYNCRYPTO(CRYPTO_num_locks
);
418 DYNCRYPTO(CRYPTO_set_id_callback
);
419 DYNCRYPTO(CRYPTO_set_locking_callback
);
420 DYNCRYPTO(ERR_free_strings
);
421 DYNCRYPTO(ERR_get_error
);
422 DYNCRYPTO(ERR_error_string
);
429 pSSL_load_error_strings();
430 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
432 meth
= pSSLv23_method();
433 ctx
= pSSL_CTX_new(meth
);
434 if (!pSSL_CTX_set_default_verify_paths(ctx
))
436 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
437 pERR_error_string(pERR_get_error(), 0));
438 LeaveCriticalSection(&init_ssl_cs
);
439 return ERROR_OUTOFMEMORY
;
441 hostname_idx
= pSSL_get_ex_new_index(0, (void *)"hostname index",
443 if (hostname_idx
== -1)
445 ERR("SSL_get_ex_new_index failed; %s\n",
446 pERR_error_string(pERR_get_error(), 0));
447 LeaveCriticalSection(&init_ssl_cs
);
448 return ERROR_OUTOFMEMORY
;
450 error_idx
= pSSL_get_ex_new_index(0, (void *)"error index",
454 ERR("SSL_get_ex_new_index failed; %s\n",
455 pERR_error_string(pERR_get_error(), 0));
456 LeaveCriticalSection(&init_ssl_cs
);
457 return ERROR_OUTOFMEMORY
;
459 pSSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, netconn_secure_verify
);
461 pCRYPTO_set_id_callback(ssl_thread_id
);
462 num_ssl_locks
= pCRYPTO_num_locks();
463 ssl_locks
= HeapAlloc(GetProcessHeap(), 0, num_ssl_locks
* sizeof(CRITICAL_SECTION
));
466 LeaveCriticalSection(&init_ssl_cs
);
467 return ERROR_OUTOFMEMORY
;
469 for (i
= 0; i
< num_ssl_locks
; i
++)
470 InitializeCriticalSection(&ssl_locks
[i
]);
471 pCRYPTO_set_locking_callback(ssl_lock_callback
);
472 LeaveCriticalSection(&init_ssl_cs
);
474 FIXME("can't use SSL, not compiled in.\n");
475 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
478 return ERROR_SUCCESS
;
481 void NETCON_unload(void)
483 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
484 if (OpenSSL_crypto_handle
)
487 wine_dlclose(OpenSSL_crypto_handle
, NULL
, 0);
489 if (OpenSSL_ssl_handle
)
493 wine_dlclose(OpenSSL_ssl_handle
, NULL
, 0);
498 for (i
= 0; i
< num_ssl_locks
; i
++) DeleteCriticalSection(&ssl_locks
[i
]);
499 HeapFree(GetProcessHeap(), 0, ssl_locks
);
504 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
506 if (connection
->socketFD
== -1)
512 /* translate a unix error code into a winsock one */
513 int sock_get_error( int err
)
515 #if !defined(__MINGW32__) && !defined (_MSC_VER)
518 case EINTR
: return WSAEINTR
;
519 case EBADF
: return WSAEBADF
;
521 case EACCES
: return WSAEACCES
;
522 case EFAULT
: return WSAEFAULT
;
523 case EINVAL
: return WSAEINVAL
;
524 case EMFILE
: return WSAEMFILE
;
525 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
526 case EINPROGRESS
: return WSAEINPROGRESS
;
527 case EALREADY
: return WSAEALREADY
;
528 case ENOTSOCK
: return WSAENOTSOCK
;
529 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
530 case EMSGSIZE
: return WSAEMSGSIZE
;
531 case EPROTOTYPE
: return WSAEPROTOTYPE
;
532 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
533 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
534 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
535 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
536 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
537 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
538 case EADDRINUSE
: return WSAEADDRINUSE
;
539 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
540 case ENETDOWN
: return WSAENETDOWN
;
541 case ENETUNREACH
: return WSAENETUNREACH
;
542 case ENETRESET
: return WSAENETRESET
;
543 case ECONNABORTED
: return WSAECONNABORTED
;
545 case ECONNRESET
: return WSAECONNRESET
;
546 case ENOBUFS
: return WSAENOBUFS
;
547 case EISCONN
: return WSAEISCONN
;
548 case ENOTCONN
: return WSAENOTCONN
;
549 case ESHUTDOWN
: return WSAESHUTDOWN
;
550 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
551 case ETIMEDOUT
: return WSAETIMEDOUT
;
552 case ECONNREFUSED
: return WSAECONNREFUSED
;
553 case ELOOP
: return WSAELOOP
;
554 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
555 case EHOSTDOWN
: return WSAEHOSTDOWN
;
556 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
557 case ENOTEMPTY
: return WSAENOTEMPTY
;
559 case EPROCLIM
: return WSAEPROCLIM
;
562 case EUSERS
: return WSAEUSERS
;
565 case EDQUOT
: return WSAEDQUOT
;
568 case ESTALE
: return WSAESTALE
;
571 case EREMOTE
: return WSAEREMOTE
;
573 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
579 /******************************************************************************
581 * Basically calls 'socket()'
583 DWORD
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
584 int type
, int protocol
)
587 if (connection
->useSSL
)
588 return ERROR_NOT_SUPPORTED
;
591 connection
->socketFD
= socket(domain
, type
, protocol
);
592 if (connection
->socketFD
== -1)
593 return sock_get_error(errno
);
595 return ERROR_SUCCESS
;
598 /******************************************************************************
600 * Basically calls 'close()' unless we should use SSL
602 DWORD
NETCON_close(WININET_NETCONNECTION
*connection
)
606 if (!NETCON_connected(connection
)) return ERROR_SUCCESS
;
609 if (connection
->useSSL
)
611 pSSL_shutdown(connection
->ssl_s
);
612 pSSL_free(connection
->ssl_s
);
613 connection
->ssl_s
= NULL
;
615 connection
->useSSL
= FALSE
;
619 result
= closesocket(connection
->socketFD
);
620 connection
->socketFD
= -1;
623 return sock_get_error(errno
);
624 return ERROR_SUCCESS
;
627 /******************************************************************************
628 * NETCON_secure_connect
629 * Initiates a secure connection over an existing plaintext connection.
631 DWORD
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPWSTR hostname
)
633 DWORD res
= ERROR_NOT_SUPPORTED
;
638 /* can't connect if we are already connected */
639 if (connection
->useSSL
)
641 ERR("already connected\n");
642 return ERROR_INTERNET_CANNOT_CONNECT
;
645 connection
->ssl_s
= pSSL_new(ctx
);
646 if (!connection
->ssl_s
)
648 ERR("SSL_new failed: %s\n",
649 pERR_error_string(pERR_get_error(), 0));
650 res
= ERROR_OUTOFMEMORY
;
654 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
656 ERR("SSL_set_fd failed: %s\n",
657 pERR_error_string(pERR_get_error(), 0));
658 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
662 if (pSSL_connect(connection
->ssl_s
) <= 0)
664 res
= (DWORD_PTR
)pSSL_get_ex_data(connection
->ssl_s
, error_idx
);
666 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
667 ERR("SSL_connect failed: %d\n", res
);
670 pSSL_set_ex_data(connection
->ssl_s
, hostname_idx
, hostname
);
671 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
674 ERR("no certificate for server %s\n", debugstr_w(hostname
));
675 /* FIXME: is this the best error? */
676 res
= ERROR_INTERNET_INVALID_CA
;
679 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
680 if (verify_res
!= X509_V_OK
)
682 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
683 /* FIXME: we should set an error and return, but we only warn at
687 connection
->useSSL
= TRUE
;
688 return ERROR_SUCCESS
;
691 if (connection
->ssl_s
)
693 pSSL_shutdown(connection
->ssl_s
);
694 pSSL_free(connection
->ssl_s
);
695 connection
->ssl_s
= NULL
;
701 /******************************************************************************
703 * Connects to the specified address.
705 DWORD
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
706 unsigned int addrlen
)
710 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
713 WARN("Unable to connect to host (%s)\n", strerror(errno
));
715 closesocket(connection
->socketFD
);
716 connection
->socketFD
= -1;
717 return sock_get_error(errno
);
720 return ERROR_SUCCESS
;
723 /******************************************************************************
725 * Basically calls 'send()' unless we should use SSL
726 * number of chars send is put in *sent
728 DWORD
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
731 if (!NETCON_connected(connection
)) return ERROR_INTERNET_CONNECTION_ABORTED
;
732 if (!connection
->useSSL
)
734 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
736 return sock_get_error(errno
);
737 return ERROR_SUCCESS
;
743 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
744 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
745 if (*sent
< 1 && len
)
746 return ERROR_INTERNET_CONNECTION_ABORTED
;
747 return ERROR_SUCCESS
;
749 return ERROR_NOT_SUPPORTED
;
754 /******************************************************************************
756 * Basically calls 'recv()' unless we should use SSL
757 * number of chars received is put in *recvd
759 DWORD
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
760 int *recvd
/* out */)
763 if (!NETCON_connected(connection
)) return ERROR_INTERNET_CONNECTION_ABORTED
;
765 return ERROR_SUCCESS
;
766 if (!connection
->useSSL
)
768 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
769 return *recvd
== -1 ? sock_get_error(errno
) : ERROR_SUCCESS
;
774 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
775 return *recvd
> 0 ? ERROR_SUCCESS
: ERROR_INTERNET_CONNECTION_ABORTED
;
777 return ERROR_NOT_SUPPORTED
;
782 /******************************************************************************
783 * NETCON_query_data_available
784 * Returns the number of bytes of peeked data plus the number of bytes of
785 * queued, but unread data.
787 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
790 if (!NETCON_connected(connection
))
793 if (!connection
->useSSL
)
797 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
800 TRACE("%d bytes of queued, but unread data\n", unread
);
801 *available
+= unread
;
808 *available
= pSSL_pending(connection
->ssl_s
);
814 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
820 if (!connection
->useSSL
)
823 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
824 r
= X509_to_cert_context(cert
);
831 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
836 /* FIXME: we should probably store the timeout in the connection to set
837 * when we do connect */
838 if (!NETCON_connected(connection
))
839 return ERROR_SUCCESS
;
841 /* value is in milliseconds, convert to struct timeval */
842 tv
.tv_sec
= value
/ 1000;
843 tv
.tv_usec
= (value
% 1000) * 1000;
845 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
846 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
851 WARN("setsockopt failed (%s)\n", strerror(errno
));
852 return sock_get_error(errno
);
855 return ERROR_SUCCESS
;