2011-07-03 05:50:11 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-03 05:50:11 +04:00
|
|
|
* Network Transport Layer
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-08-03 05:34:23 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
2013-09-05 15:44:12 +04:00
|
|
|
#include <assert.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-15 05:46:51 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-11-27 05:15:48 +04:00
|
|
|
#include <winpr/synch.h>
|
2012-12-14 09:25:48 +04:00
|
|
|
#include <winpr/print.h>
|
2014-02-05 20:54:42 +04:00
|
|
|
#include <winpr/stream.h>
|
2014-12-12 17:08:39 +03:00
|
|
|
#include <winpr/winsock.h>
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-08-19 20:26:39 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-12-14 09:25:48 +04:00
|
|
|
#include <freerdp/error.h>
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <freerdp/utils/ringbuffer.h>
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <openssl/bio.h>
|
2011-07-03 12:37:36 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2011-07-03 11:53:55 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
#ifndef _WIN32
|
2011-08-16 23:35:29 +04:00
|
|
|
#include <netdb.h>
|
2011-08-16 01:05:48 +04:00
|
|
|
#include <sys/socket.h>
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif /* _WIN32 */
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
2011-08-16 01:05:48 +04:00
|
|
|
#endif
|
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#include "tpkt.h"
|
2011-08-01 20:19:39 +04:00
|
|
|
#include "fastpath.h"
|
2011-07-03 06:59:07 +04:00
|
|
|
#include "transport.h"
|
2013-12-21 03:26:07 +04:00
|
|
|
#include "rdp.h"
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core.transport")
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#define BUFFER_SIZE 16384
|
|
|
|
|
2013-03-27 21:03:41 +04:00
|
|
|
static void* transport_client_thread(void* arg);
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* transport_send_stream_init(rdpTransport* transport, int size)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
2013-05-16 02:05:40 +04:00
|
|
|
wStream* s;
|
|
|
|
s = StreamPool_Take(transport->ReceivePool, size);
|
2013-05-02 02:15:55 +04:00
|
|
|
Stream_EnsureCapacity(s, size);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2011-07-10 23:34:43 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
BOOL transport_attach(rdpTransport* transport, int sockfd)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
BIO* socketBio;
|
|
|
|
BIO* bufferedBio;
|
2015-02-13 16:41:47 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
socketBio = BIO_new(BIO_s_simple_socket());
|
2015-02-13 16:41:47 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
if (!socketBio)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
BIO_set_fd(socketBio, sockfd, BIO_CLOSE);
|
|
|
|
|
|
|
|
bufferedBio = BIO_new(BIO_s_buffered_socket());
|
|
|
|
|
|
|
|
if (!bufferedBio)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
bufferedBio = BIO_push(bufferedBio, socketBio);
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
transport->frontBio = bufferedBio;
|
2015-02-14 18:14:13 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_connect_rdp(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-04 03:27:02 +04:00
|
|
|
/* RDP encryption */
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_connect_tls(rdpTransport* transport)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
2015-02-13 00:22:25 +03:00
|
|
|
int tlsStatus;
|
|
|
|
rdpTls* tls = NULL;
|
2015-02-11 19:57:02 +03:00
|
|
|
rdpContext* context = transport->context;
|
|
|
|
rdpSettings* settings = transport->settings;
|
2014-03-25 23:19:52 +04:00
|
|
|
|
2015-02-11 22:27:29 +03:00
|
|
|
if (transport->GatewayEnabled)
|
2013-10-11 14:12:50 +04:00
|
|
|
{
|
2015-02-13 17:27:54 +03:00
|
|
|
tls = transport->tls = tls_new(settings);
|
2013-10-24 22:13:41 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TSG_TLS;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-13 17:27:54 +03:00
|
|
|
tls = transport->tls = tls_new(settings);
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->tls = tls;
|
2015-02-11 22:27:29 +03:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
tls->hostname = settings->ServerHostname;
|
|
|
|
tls->port = settings->ServerPort;
|
2012-11-15 08:21:00 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tls->port == 0)
|
|
|
|
tls->port = 3389;
|
2013-12-07 07:15:45 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
tls->isGatewayTransport = FALSE;
|
2015-02-15 18:06:17 +03:00
|
|
|
tlsStatus = tls_connect(tls, transport->frontBio);
|
2014-04-02 00:23:27 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tlsStatus < 1)
|
2012-11-15 08:21:00 +04:00
|
|
|
{
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tlsStatus < 0)
|
2014-04-02 00:23:27 +04:00
|
|
|
{
|
|
|
|
if (!freerdp_get_last_error(context))
|
|
|
|
freerdp_set_last_error(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!freerdp_get_last_error(context))
|
|
|
|
freerdp_set_last_error(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
|
|
|
}
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->frontBio = tls->bio;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->frontBio)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "unable to prepend a filtering TLS bio");
|
2012-11-15 08:21:00 +04:00
|
|
|
return FALSE;
|
2012-04-10 23:15:36 +04:00
|
|
|
}
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_connect_nla(rdpTransport* transport)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
2015-02-16 00:04:59 +03:00
|
|
|
rdpContext* context = transport->context;
|
|
|
|
rdpSettings* settings = context->settings;
|
|
|
|
freerdp* instance = context->instance;
|
|
|
|
rdpRdp* rdp = context->rdp;
|
2013-11-08 02:37:58 +04:00
|
|
|
|
2012-11-15 05:46:51 +04:00
|
|
|
if (!transport_connect_tls(transport))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (!settings->Authentication)
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-18 21:07:52 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
rdp->nla = nla_new(instance, transport, settings);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (!rdp->nla)
|
|
|
|
return FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
transport_set_nla_mode(transport, TRUE);
|
2011-07-07 21:37:48 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (settings->AuthenticationServiceClass)
|
|
|
|
{
|
|
|
|
rdp->nla->ServicePrincipalName =
|
|
|
|
nla_make_spn(settings->AuthenticationServiceClass, settings->ServerHostname);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (!rdp->nla->ServicePrincipalName)
|
|
|
|
return FALSE;
|
2014-02-12 09:43:02 +04:00
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla_client_begin(rdp->nla) < 0)
|
2011-07-07 21:37:48 +04:00
|
|
|
{
|
2015-02-16 00:04:59 +03:00
|
|
|
if (!freerdp_get_last_error(context))
|
|
|
|
freerdp_set_last_error(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2015-02-16 00:04:59 +03:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-07 21:37:48 +04:00
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
rdp_client_transition_to_state(rdp, CONNECTION_STATE_NLA);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
BOOL transport_connect(rdpTransport* transport, const char* hostname, UINT16 port, int timeout)
|
2012-03-26 20:20:38 +04:00
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
int sockfd;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL status = FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
rdpSettings* settings = transport->settings;
|
2015-02-11 19:57:02 +03:00
|
|
|
|
2013-11-01 18:24:19 +04:00
|
|
|
transport->async = settings->AsyncTransport;
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2014-03-24 22:44:18 +04:00
|
|
|
if (transport->GatewayEnabled)
|
2012-03-26 20:20:38 +04:00
|
|
|
{
|
2015-03-17 21:54:16 +03:00
|
|
|
/* New RDP 8 gateway test. */
|
|
|
|
transport->rdg = rdg_new(transport);
|
|
|
|
if (!transport->rdg)
|
|
|
|
{
|
2015-02-13 22:26:02 +03:00
|
|
|
return FALSE;
|
2015-03-17 21:54:16 +03:00
|
|
|
}
|
|
|
|
status = rdg_connect(transport->rdg, hostname, port, timeout);
|
2015-03-18 23:13:32 +03:00
|
|
|
if (status)
|
2015-03-17 21:54:16 +03:00
|
|
|
{
|
2015-03-18 23:13:32 +03:00
|
|
|
transport->frontBio = transport->rdg->frontBio;
|
|
|
|
BIO_set_nonblock(transport->frontBio, 0);
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
|
|
|
|
status = TRUE;
|
2015-03-17 21:54:16 +03:00
|
|
|
}
|
2015-03-18 23:13:32 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (transport->rdg->state != RDG_CLIENT_STATE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
transport->tsg = tsg_new(transport);
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
if (!transport->tsg)
|
|
|
|
return FALSE;
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
if (!tsg_connect(transport->tsg, hostname, port, timeout))
|
|
|
|
return FALSE;
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
transport->frontBio = transport->tsg->bio;
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
status = TRUE;
|
|
|
|
}
|
2012-03-26 20:20:38 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
sockfd = freerdp_tcp_connect(settings, hostname, port, timeout);
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
if (sockfd < 1)
|
2015-02-13 22:26:02 +03:00
|
|
|
return FALSE;
|
2015-02-11 19:57:02 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
transport_attach(transport, sockfd);
|
2015-02-13 22:26:02 +03:00
|
|
|
|
|
|
|
status = TRUE;
|
2012-03-26 20:20:38 +04:00
|
|
|
}
|
|
|
|
|
2013-11-01 18:24:19 +04:00
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
if (transport->async)
|
|
|
|
{
|
|
|
|
transport->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
transport->thread = CreateThread(NULL, 0,
|
2015-01-28 23:37:20 +03:00
|
|
|
(LPTHREAD_START_ROUTINE) transport_client_thread, transport, 0, NULL);
|
2013-11-01 18:24:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 20:20:38 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_rdp(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
|
|
|
/* RDP encryption */
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_tls(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2015-02-11 22:27:29 +03:00
|
|
|
rdpSettings* settings = transport->settings;
|
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (!transport->tls)
|
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (!tls_accept(transport->tls, transport->frontBio, settings->CertificateFile, settings->PrivateKeyFile))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->frontBio = transport->tls->bio;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_nla(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2015-02-11 22:27:29 +03:00
|
|
|
rdpSettings* settings = transport->settings;
|
|
|
|
freerdp* instance = (freerdp*) settings->instance;
|
2013-11-08 02:37:58 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (!transport->tls)
|
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (!tls_accept(transport->tls, transport->frontBio, settings->CertificateFile, settings->PrivateKeyFile))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->frontBio = transport->tls->bio;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
/* Network Level Authentication */
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (!settings->Authentication)
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (!transport->nla)
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
2015-02-15 19:10:14 +03:00
|
|
|
transport->nla = nla_new(instance, transport, settings);
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, TRUE);
|
|
|
|
}
|
2012-02-14 07:27:59 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla_authenticate(transport->nla) < 0)
|
2012-02-14 07:27:59 +04:00
|
|
|
{
|
2015-02-02 19:50:56 +03:00
|
|
|
WLog_ERR(TAG, "client authentication failure");
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_free(transport->nla);
|
|
|
|
transport->nla = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-14 07:27:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
/* don't free nla module yet, we need to copy the credentials from it first */
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
int transport_read_layer(rdpTransport* transport, BYTE* data, int bytes)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
|
|
|
int read = 0;
|
|
|
|
int status = -1;
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2014-05-30 21:08:00 +04:00
|
|
|
if (!transport->frontBio)
|
|
|
|
{
|
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-30 23:06:07 +04:00
|
|
|
|
2012-12-09 03:27:08 +04:00
|
|
|
while (read < bytes)
|
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
status = BIO_read(transport->frontBio, data + read, bytes - read);
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status <= 0)
|
2012-12-09 03:27:08 +04:00
|
|
|
{
|
2014-05-30 19:34:04 +04:00
|
|
|
if (!transport->frontBio || !BIO_should_retry(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
/* something unexpected happened, let's close */
|
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* non blocking will survive a partial read */
|
|
|
|
if (!transport->blocking)
|
|
|
|
return read;
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
/* blocking means that we can't continue until we have read the number of requested bytes */
|
|
|
|
if (BIO_wait_read(transport->frontBio, 100) < 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "error when selecting for read");
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
continue;
|
2012-12-09 03:27:08 +04:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(data + read, bytes - read);
|
|
|
|
#endif
|
|
|
|
read += status;
|
2012-12-09 03:27:08 +04:00
|
|
|
}
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2012-12-09 03:27:08 +04:00
|
|
|
return read;
|
2012-12-08 07:18:50 +04:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tries to read toRead bytes from the specified transport
|
|
|
|
*
|
|
|
|
* Try to read toRead bytes from the transport to the stream.
|
|
|
|
* In case it was not possible to read toRead bytes 0 is returned. The stream is always advanced by the
|
|
|
|
* number of bytes read.
|
|
|
|
*
|
2014-07-28 15:12:01 +04:00
|
|
|
* The function assumes that the stream has enough capacity to hold the data.
|
2014-07-22 21:14:43 +04:00
|
|
|
*
|
|
|
|
* @param[in] transport rdpTransport
|
|
|
|
* @param[in] s wStream
|
|
|
|
* @param[in] toRead number of bytes to read
|
2014-07-28 15:12:01 +04:00
|
|
|
* @return < 0 on error; 0 if not enough data is available (non blocking mode); 1 toRead bytes read
|
2014-07-22 21:14:43 +04:00
|
|
|
*/
|
|
|
|
static int transport_read_layer_bytes(rdpTransport* transport, wStream* s, unsigned int toRead)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
status = transport_read_layer(transport, Stream_Pointer(s), toRead);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
if (status <= 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
Stream_Seek(s, status);
|
|
|
|
return status == toRead ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-28 15:12:01 +04:00
|
|
|
* @brief Try to read a complete PDU (NLA, fast-path or tpkt) from the underlying transport.
|
2014-07-22 21:14:43 +04:00
|
|
|
*
|
|
|
|
* If possible a complete PDU is read, in case of non blocking transport this might not succeed.
|
|
|
|
* Except in case of an error the passed stream will point to the last byte read (correct
|
|
|
|
* position). When the pdu read is completed the stream is sealed and the pointer set to 0
|
|
|
|
*
|
|
|
|
* @param[in] transport rdpTransport
|
|
|
|
* @param[in] s wStream
|
2014-07-28 15:12:01 +04:00
|
|
|
* @return < 0 on error; 0 if not enough data is available (non blocking mode); > 0 number of
|
2014-07-22 21:14:43 +04:00
|
|
|
* bytes of the *complete* pdu read
|
|
|
|
*/
|
2014-07-22 13:19:38 +04:00
|
|
|
int transport_read_pdu(rdpTransport* transport, wStream* s)
|
2012-12-08 07:18:50 +04:00
|
|
|
{
|
|
|
|
int status;
|
2013-11-08 02:37:58 +04:00
|
|
|
int position;
|
2013-05-15 20:14:26 +04:00
|
|
|
int pduLength;
|
2014-08-19 20:26:39 +04:00
|
|
|
BYTE* header;
|
2015-02-15 18:06:17 +03:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
position = 0;
|
2013-05-15 20:14:26 +04:00
|
|
|
pduLength = 0;
|
2012-12-08 07:18:50 +04:00
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return -1;
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
position = Stream_GetPosition(s);
|
2014-07-28 15:12:01 +04:00
|
|
|
/* Make sure there is enough space for the longest header within the stream */
|
2014-07-22 21:14:43 +04:00
|
|
|
Stream_EnsureCapacity(s, 4);
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2015-02-15 22:54:10 +03:00
|
|
|
/* Make sure at least two bytes are read for further processing */
|
2014-07-22 21:14:43 +04:00
|
|
|
if (position < 2 && (status = transport_read_layer_bytes(transport, s, 2 - position)) != 1)
|
|
|
|
{
|
|
|
|
/* No data available at the moment */
|
|
|
|
return status;
|
2011-08-01 18:21:06 +04:00
|
|
|
}
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2015-02-23 13:57:46 +03:00
|
|
|
/* update position value for further checks */
|
2015-02-22 00:07:25 +03:00
|
|
|
position = Stream_GetPosition(s);
|
2014-05-21 19:32:14 +04:00
|
|
|
header = Stream_Buffer(s);
|
2013-11-08 02:37:58 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
if (transport->NlaMode)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2014-07-22 21:14:43 +04:00
|
|
|
/*
|
2014-07-28 15:12:01 +04:00
|
|
|
* In case NlaMode is set TSRequest package(s) are expected
|
2014-07-22 21:14:43 +04:00
|
|
|
* 0x30 = DER encoded data with these bits set:
|
|
|
|
* bit 6 P/C constructed
|
|
|
|
* bit 5 tag number - sequence
|
|
|
|
*/
|
2014-03-27 22:24:15 +04:00
|
|
|
if (header[0] == 0x30)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2014-03-27 22:24:15 +04:00
|
|
|
/* TSRequest (NLA) */
|
|
|
|
if (header[1] & 0x80)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2014-03-27 22:24:15 +04:00
|
|
|
if ((header[1] & ~(0x80)) == 1)
|
|
|
|
{
|
2015-02-23 13:57:46 +03:00
|
|
|
/* check for header bytes already was readed in previous calls */
|
|
|
|
if (position < 3
|
|
|
|
&& (status = transport_read_layer_bytes(transport, s, 3 - position)) != 1)
|
2014-07-22 21:14:43 +04:00
|
|
|
return status;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = header[2];
|
|
|
|
pduLength += 3;
|
|
|
|
}
|
|
|
|
else if ((header[1] & ~(0x80)) == 2)
|
|
|
|
{
|
2015-02-23 13:57:46 +03:00
|
|
|
/* check for header bytes already was readed in previous calls */
|
|
|
|
if (position < 4
|
|
|
|
&& (status = transport_read_layer_bytes(transport, s, 4 - position)) != 1)
|
2014-07-22 21:14:43 +04:00
|
|
|
return status;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = (header[2] << 8) | header[3];
|
|
|
|
pduLength += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-02 19:50:56 +03:00
|
|
|
WLog_ERR(TAG, "Error reading TSRequest!");
|
2014-03-27 22:24:15 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = header[1];
|
|
|
|
pduLength += 2;
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
else
|
|
|
|
{
|
2014-03-27 22:24:15 +04:00
|
|
|
if (header[0] == 0x03)
|
|
|
|
{
|
|
|
|
/* TPKT header */
|
2015-02-23 13:57:46 +03:00
|
|
|
/* check for header bytes already was readed in previous calls */
|
|
|
|
if (position < 4
|
|
|
|
&& (status = transport_read_layer_bytes(transport, s, 4 - position)) != 1)
|
2014-07-22 21:14:43 +04:00
|
|
|
return status;
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = (header[2] << 8) | header[3];
|
2014-07-22 21:14:43 +04:00
|
|
|
|
|
|
|
/* min and max values according to ITU-T Rec. T.123 (01/2007) section 8 */
|
|
|
|
if (pduLength < 7 || pduLength > 0xFFFF)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "tpkt - invalid pduLength: %d", pduLength);
|
2014-07-22 21:14:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-27 22:24:15 +04:00
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
else
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
|
|
|
/* Fast-Path Header */
|
|
|
|
if (header[1] & 0x80)
|
2014-07-28 15:12:01 +04:00
|
|
|
{
|
2015-02-23 13:57:46 +03:00
|
|
|
/* check for header bytes already was readed in previous calls */
|
|
|
|
if (position < 3
|
|
|
|
&& (status = transport_read_layer_bytes(transport, s, 3 - position)) != 1)
|
2014-07-22 21:14:43 +04:00
|
|
|
return status;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = ((header[1] & 0x7F) << 8) | header[2];
|
2014-07-22 21:14:43 +04:00
|
|
|
}
|
2014-03-27 22:24:15 +04:00
|
|
|
else
|
|
|
|
pduLength = header[1];
|
2014-07-22 21:14:43 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fast-path has 7 bits for length so the maximum size, including headers is 0x8000
|
|
|
|
* The theoretical minimum fast-path PDU consists only of two header bytes plus one
|
|
|
|
* byte for data (e.g. fast-path input synchronize pdu)
|
|
|
|
*/
|
|
|
|
if (pduLength < 3 || pduLength > 0x8000)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "fast path - invalid pduLength: %d", pduLength);
|
2014-07-22 21:14:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-27 22:24:15 +04:00
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
}
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
Stream_EnsureCapacity(s, Stream_GetPosition(s) + pduLength);
|
|
|
|
status = transport_read_layer_bytes(transport, s, pduLength - Stream_GetPosition(s));
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
if (status != 1)
|
|
|
|
return status;
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
if (Stream_GetPosition(s) >= pduLength)
|
2015-01-28 23:37:20 +03:00
|
|
|
WLog_Packet(WLog_Get(TAG), WLOG_TRACE, Stream_Buffer(s), pduLength, WLOG_PACKET_INBOUND);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
Stream_SealLength(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
return Stream_Length(s);
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int transport_write(rdpTransport* transport, wStream* s)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
2011-08-01 11:04:07 +04:00
|
|
|
int length;
|
2013-05-15 20:14:26 +04:00
|
|
|
int status = -1;
|
2015-02-15 18:06:17 +03:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
EnterCriticalSection(&(transport->WriteLock));
|
2015-02-15 18:06:17 +03:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
length = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2011-08-03 05:34:23 +04:00
|
|
|
|
2013-10-09 07:43:57 +04:00
|
|
|
if (length > 0)
|
|
|
|
{
|
2015-01-28 23:37:20 +03:00
|
|
|
WLog_Packet(WLog_Get(TAG), WLOG_TRACE, Stream_Buffer(s), length, WLOG_PACKET_OUTBOUND);
|
2013-10-09 07:43:57 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
while (length > 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
status = BIO_write(transport->frontBio, Stream_Pointer(s), length);
|
2011-08-01 11:04:07 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status <= 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
/* the buffered BIO that is at the end of the chain always says OK for writing,
|
|
|
|
* so a retry means that for any reason we need to read. The most probable
|
|
|
|
* is a SSL or TSG BIO in the chain.
|
|
|
|
*/
|
|
|
|
if (!BIO_should_retry(transport->frontBio))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/* non-blocking can live with blocked IOs */
|
2011-08-01 18:21:06 +04:00
|
|
|
if (!transport->blocking)
|
2014-05-21 19:32:14 +04:00
|
|
|
return status;
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_wait_write(transport->frontBio, 100) < 0)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "error when selecting for write");
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
2011-08-20 16:30:18 +04:00
|
|
|
}
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-21 12:23:50 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->blocking || transport->settings->WaitForOutputBufferFlush)
|
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
while (BIO_write_blocked(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_wait_write(transport->frontBio, 100) < 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "error when selecting for write");
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_flush(transport->frontBio) < 1)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-13 22:26:02 +03:00
|
|
|
WLog_ERR(TAG, "error when flushing outputBuffer");
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-08-01 11:04:07 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
length -= status;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, status);
|
2011-08-01 11:04:07 +04:00
|
|
|
}
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2011-08-25 09:30:17 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
/* A write error indicates that the peer has dropped the connection */
|
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:05:40 +04:00
|
|
|
if (s->pool)
|
|
|
|
Stream_Release(s);
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
LeaveCriticalSection(&(transport->WriteLock));
|
2011-07-10 23:34:43 +04:00
|
|
|
return status;
|
2011-07-04 06:11:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
UINT32 transport_get_event_handles(rdpTransport* transport, HANDLE* events)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
UINT32 nCount = 0;
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (!transport->GatewayEnabled)
|
2012-11-27 05:15:48 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
if (events)
|
|
|
|
BIO_get_event(transport->frontBio, &events[nCount]);
|
|
|
|
nCount++;
|
2012-11-27 05:15:48 +04:00
|
|
|
}
|
2015-02-15 18:06:17 +03:00
|
|
|
else
|
2013-03-28 04:06:10 +04:00
|
|
|
{
|
2015-03-17 21:54:16 +03:00
|
|
|
if (transport->rdg)
|
|
|
|
{
|
|
|
|
nCount += rdg_get_event_handles(transport->rdg, events);
|
|
|
|
}
|
|
|
|
else if (transport->tsg)
|
|
|
|
{
|
|
|
|
nCount += tsg_get_event_handles(transport->tsg, events);
|
|
|
|
}
|
2013-03-28 04:06:10 +04:00
|
|
|
}
|
2015-02-15 18:06:17 +03:00
|
|
|
|
|
|
|
return nCount;
|
2011-08-20 16:30:18 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
void transport_get_fds(rdpTransport* transport, void** rfds, int* rcount)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
UINT32 index;
|
|
|
|
UINT32 nCount;
|
|
|
|
HANDLE events[64];
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
nCount = transport_get_event_handles(transport, events);
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
for (index = 0; index < nCount; index++)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
rfds[*rcount] = GetEventWaitObject(events[index]);
|
|
|
|
(*rcount)++;
|
2013-05-01 01:16:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:54:39 +03:00
|
|
|
BOOL transport_is_write_blocked(rdpTransport* transport)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
return BIO_write_blocked(transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 18:54:39 +03:00
|
|
|
int transport_drain_output_buffer(rdpTransport* transport)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-11 19:57:02 +03:00
|
|
|
BOOL status = FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_write_blocked(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_flush(transport->frontBio) < 1)
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
status |= BIO_write_blocked(transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 19:57:02 +03:00
|
|
|
return status;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 01:25:56 +04:00
|
|
|
int transport_check_fds(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-08-01 08:43:53 +04:00
|
|
|
int status;
|
2013-01-07 02:24:08 +04:00
|
|
|
int recv_status;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* received;
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2013-11-01 07:35:24 +04:00
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-10 23:35:14 +04:00
|
|
|
/**
|
|
|
|
* Loop through and read all available PDUs. Since multiple
|
|
|
|
* PDUs can exist, it's important to deliver them all before
|
|
|
|
* returning. Otherwise we run the risk of having a thread
|
2014-03-27 22:24:15 +04:00
|
|
|
* wait for a socket to get signaled that data is available
|
2014-03-10 23:35:14 +04:00
|
|
|
* (which may never happen).
|
|
|
|
*/
|
|
|
|
for (;;)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
2014-04-03 23:42:32 +04:00
|
|
|
/**
|
2014-07-22 21:14:43 +04:00
|
|
|
* Note: transport_read_pdu tries to read one PDU from
|
|
|
|
* the transport layer.
|
2014-07-28 15:12:01 +04:00
|
|
|
* The ReceiveBuffer might have a position > 0 in case of a non blocking
|
2014-07-22 21:14:43 +04:00
|
|
|
* transport. If transport_read_pdu returns 0 the pdu couldn't be read at
|
|
|
|
* this point.
|
2014-04-03 23:42:32 +04:00
|
|
|
* Note that transport->ReceiveBuffer is replaced after each iteration
|
|
|
|
* of this loop with a fresh stream instance from a pool.
|
|
|
|
*/
|
2014-07-22 21:14:43 +04:00
|
|
|
if ((status = transport_read_pdu(transport, transport->ReceiveBuffer)) <= 0)
|
2014-04-03 23:42:32 +04:00
|
|
|
{
|
2014-07-22 21:14:43 +04:00
|
|
|
return status;
|
2014-04-03 23:42:32 +04:00
|
|
|
}
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
received = transport->ReceiveBuffer;
|
|
|
|
transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0);
|
|
|
|
/**
|
|
|
|
* status:
|
|
|
|
* -1: error
|
|
|
|
* 0: success
|
|
|
|
* 1: redirection
|
|
|
|
*/
|
|
|
|
recv_status = transport->ReceiveCallback(transport, received, transport->ReceiveExtra);
|
2014-09-12 01:04:32 +04:00
|
|
|
Stream_Release(received);
|
2012-04-09 21:28:33 +04:00
|
|
|
|
2014-09-12 01:04:32 +04:00
|
|
|
/* session redirection or activation */
|
|
|
|
if (recv_status == 1 || recv_status == 2)
|
2014-04-29 00:44:52 +04:00
|
|
|
{
|
2014-09-12 01:04:32 +04:00
|
|
|
return recv_status;
|
2014-04-29 00:44:52 +04:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
if (recv_status < 0)
|
|
|
|
return -1;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:07:34 +04:00
|
|
|
return 0;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_set_blocking_mode(rdpTransport* transport, BOOL blocking)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
|
|
|
transport->blocking = blocking;
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (!BIO_set_nonblock(transport->frontBio, blocking ? FALSE : TRUE))
|
|
|
|
return FALSE;
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2015-02-13 17:27:54 +03:00
|
|
|
return TRUE;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 23:19:52 +04:00
|
|
|
void transport_set_gateway_enabled(rdpTransport* transport, BOOL GatewayEnabled)
|
2014-03-24 22:44:18 +04:00
|
|
|
{
|
|
|
|
transport->GatewayEnabled = GatewayEnabled;
|
|
|
|
}
|
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
void transport_set_nla_mode(rdpTransport* transport, BOOL NlaMode)
|
|
|
|
{
|
|
|
|
transport->NlaMode = NlaMode;
|
|
|
|
}
|
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
void transport_stop(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
if (transport->async)
|
|
|
|
{
|
|
|
|
if (transport->stopEvent)
|
|
|
|
{
|
|
|
|
SetEvent(transport->stopEvent);
|
|
|
|
WaitForSingleObject(transport->thread, INFINITE);
|
|
|
|
CloseHandle(transport->thread);
|
|
|
|
CloseHandle(transport->stopEvent);
|
|
|
|
transport->thread = NULL;
|
|
|
|
transport->stopEvent = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_disconnect(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
BOOL status = TRUE;
|
|
|
|
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
transport_stop(transport);
|
|
|
|
|
2015-02-18 23:36:57 +03:00
|
|
|
if (transport->tls)
|
2015-02-13 00:22:25 +03:00
|
|
|
{
|
2015-02-18 23:36:57 +03:00
|
|
|
tls_free(transport->tls);
|
|
|
|
transport->tls = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 23:36:57 +03:00
|
|
|
if (transport->frontBio)
|
|
|
|
BIO_free(transport->frontBio);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport->tsg)
|
|
|
|
{
|
|
|
|
tsg_free(transport->tsg);
|
|
|
|
transport->tsg = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
if (transport->rdg)
|
|
|
|
{
|
|
|
|
rdg_free(transport->rdg);
|
|
|
|
transport->rdg = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
transport->frontBio = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-03-27 21:03:41 +04:00
|
|
|
static void* transport_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
2015-01-30 17:47:02 +03:00
|
|
|
HANDLE handles[64];
|
2014-12-15 17:42:04 +03:00
|
|
|
rdpTransport* transport = (rdpTransport*) arg;
|
|
|
|
rdpContext* context = transport->context;
|
2015-02-11 22:27:29 +03:00
|
|
|
rdpRdp* rdp = context->rdp;
|
2015-01-28 23:37:20 +03:00
|
|
|
|
|
|
|
WLog_DBG(TAG, "Starting transport thread");
|
|
|
|
|
2013-11-04 21:40:29 +04:00
|
|
|
nCount = 0;
|
|
|
|
handles[nCount++] = transport->stopEvent;
|
|
|
|
handles[nCount++] = transport->connectedEvent;
|
2015-01-28 23:37:20 +03:00
|
|
|
|
2013-11-04 21:40:29 +04:00
|
|
|
status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2013-11-05 20:49:42 +04:00
|
|
|
if (WaitForSingleObject(transport->stopEvent, 0) == WAIT_OBJECT_0)
|
2013-11-04 21:40:29 +04:00
|
|
|
{
|
2015-01-28 23:37:20 +03:00
|
|
|
WLog_DBG(TAG, "Terminating transport thread");
|
2013-11-04 21:40:29 +04:00
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2015-01-28 23:37:20 +03:00
|
|
|
WLog_DBG(TAG, "Asynchronous transport activated");
|
2013-11-05 00:52:29 +04:00
|
|
|
|
2013-03-27 21:03:41 +04:00
|
|
|
while (1)
|
|
|
|
{
|
2013-05-01 01:16:38 +04:00
|
|
|
nCount = 0;
|
2013-11-04 21:40:29 +04:00
|
|
|
handles[nCount++] = transport->stopEvent;
|
2015-01-30 17:47:02 +03:00
|
|
|
nCount += freerdp_get_event_handles(context, &handles[nCount]);
|
2015-01-30 06:57:58 +03:00
|
|
|
|
2014-03-18 23:27:23 +04:00
|
|
|
status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
|
2014-06-01 18:46:43 +04:00
|
|
|
|
2013-12-21 03:22:29 +04:00
|
|
|
if (transport->layer == TRANSPORT_LAYER_CLOSED)
|
|
|
|
{
|
2013-12-21 03:26:07 +04:00
|
|
|
rdp_set_error_info(rdp, ERRINFO_PEER_DISCONNECTED);
|
2013-03-27 21:03:41 +04:00
|
|
|
break;
|
2013-12-21 03:22:29 +04:00
|
|
|
}
|
2015-01-30 06:57:58 +03:00
|
|
|
|
|
|
|
if (WaitForSingleObject(transport->stopEvent, 0) == WAIT_OBJECT_0)
|
2013-12-21 03:22:29 +04:00
|
|
|
break;
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2015-01-30 17:47:02 +03:00
|
|
|
if (WaitForMultipleObjects(nCount - 1, &handles[1], FALSE, 0) != WAIT_TIMEOUT)
|
2015-01-30 06:57:58 +03:00
|
|
|
{
|
2015-01-30 17:47:02 +03:00
|
|
|
if (!freerdp_check_event_handles(context))
|
|
|
|
{
|
|
|
|
rdp_set_error_info(rdp, ERRINFO_PEER_DISCONNECTED);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-21 03:22:29 +04:00
|
|
|
}
|
2013-03-27 21:03:41 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:37:20 +03:00
|
|
|
WLog_DBG(TAG, "Terminating transport thread");
|
2013-09-05 15:40:04 +04:00
|
|
|
ExitThread(0);
|
2013-03-27 21:03:41 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-15 18:23:06 +03:00
|
|
|
rdpTransport* transport_new(rdpContext* context)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2011-07-07 21:37:48 +04:00
|
|
|
rdpTransport* transport;
|
2015-01-28 23:37:20 +03:00
|
|
|
|
2014-12-15 17:42:04 +03:00
|
|
|
transport = (rdpTransport*) calloc(1, sizeof(rdpTransport));
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport)
|
|
|
|
return NULL;
|
2013-03-28 04:06:10 +04:00
|
|
|
|
2014-12-15 18:23:06 +03:00
|
|
|
transport->context = context;
|
|
|
|
transport->settings = context->settings;
|
2013-10-09 07:43:57 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->ReceivePool = StreamPool_New(TRUE, BUFFER_SIZE);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->ReceivePool)
|
2015-02-13 23:22:27 +03:00
|
|
|
goto out_free_transport;
|
2013-01-26 02:52:37 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/* receive buffer for non-blocking read. */
|
|
|
|
transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->ReceiveBuffer)
|
|
|
|
goto out_free_receivepool;
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->connectedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->connectedEvent || transport->connectedEvent == INVALID_HANDLE_VALUE)
|
2015-02-15 18:06:17 +03:00
|
|
|
goto out_free_receivebuffer;
|
2011-09-04 00:36:27 +04:00
|
|
|
|
2015-01-13 18:35:06 +03:00
|
|
|
transport->blocking = TRUE;
|
|
|
|
transport->GatewayEnabled = FALSE;
|
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(transport->ReadLock), 4000))
|
|
|
|
goto out_free_connectedEvent;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(transport->WriteLock), 4000))
|
|
|
|
goto out_free_readlock;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
return transport;
|
2014-05-21 19:32:14 +04:00
|
|
|
out_free_readlock:
|
|
|
|
DeleteCriticalSection(&(transport->ReadLock));
|
|
|
|
out_free_connectedEvent:
|
|
|
|
CloseHandle(transport->connectedEvent);
|
|
|
|
out_free_receivebuffer:
|
|
|
|
StreamPool_Return(transport->ReceivePool, transport->ReceiveBuffer);
|
|
|
|
out_free_receivepool:
|
|
|
|
StreamPool_Free(transport->ReceivePool);
|
2015-02-13 23:22:27 +03:00
|
|
|
out_free_transport:
|
2014-05-21 19:32:14 +04:00
|
|
|
free(transport);
|
|
|
|
return NULL;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void transport_free(rdpTransport* transport)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport)
|
|
|
|
return;
|
2013-11-12 18:04:26 +04:00
|
|
|
|
2015-01-12 15:32:59 +03:00
|
|
|
transport_disconnect(transport);
|
2013-04-13 01:05:42 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->ReceiveBuffer)
|
|
|
|
Stream_Release(transport->ReceiveBuffer);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
StreamPool_Free(transport->ReceivePool);
|
|
|
|
CloseHandle(transport->connectedEvent);
|
|
|
|
DeleteCriticalSection(&(transport->ReadLock));
|
|
|
|
DeleteCriticalSection(&(transport->WriteLock));
|
2015-02-11 18:57:14 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
free(transport);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|