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>
|
2011-07-03 05:50:11 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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>
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2012-12-14 09:25:48 +04:00
|
|
|
#include <freerdp/error.h>
|
2012-09-04 01:08:46 +04:00
|
|
|
#include <freerdp/utils/tcp.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-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);
|
2013-05-16 02:05:40 +04:00
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
void transport_attach(rdpTransport* transport, int sockfd)
|
|
|
|
{
|
2013-07-21 06:34:05 +04:00
|
|
|
tcp_attach(transport->TcpIn, sockfd);
|
2012-12-13 00:55:42 +04:00
|
|
|
transport->SplitInputOutput = FALSE;
|
|
|
|
transport->TcpOut = transport->TcpIn;
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = transport->TcpIn->bufferedBio;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 23:27:23 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_disconnect(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2012-12-13 05:02:56 +04:00
|
|
|
BOOL status = TRUE;
|
|
|
|
|
2013-11-14 13:09:21 +04:00
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-03-18 23:27:23 +04:00
|
|
|
transport_stop(transport);
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
BIO_free_all(transport->frontBio);
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = 0;
|
2012-12-13 05:02:56 +04:00
|
|
|
return status;
|
2011-07-03 05:50:11 +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
|
|
|
}
|
|
|
|
|
2013-10-11 23:27:22 +04:00
|
|
|
long transport_bio_tsg_callback(BIO* bio, int mode, const char* argp, int argi, long argl, long ret)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-11 14:12:50 +04:00
|
|
|
static int transport_bio_tsg_write(BIO* bio, const char* buf, int num)
|
|
|
|
{
|
2013-10-11 23:27:22 +04:00
|
|
|
int status;
|
|
|
|
rdpTsg* tsg;
|
|
|
|
|
|
|
|
tsg = (rdpTsg*) bio->ptr;
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE);
|
2014-05-30 20:31:26 +04:00
|
|
|
|
2014-05-30 22:53:10 +04:00
|
|
|
status = tsg_write(tsg, (BYTE*) buf, num);
|
2013-10-11 14:12:50 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_WRITE);
|
|
|
|
}
|
2014-05-30 22:53:10 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return status >= 0 ? status : -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_tsg_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
2013-10-11 23:27:22 +04:00
|
|
|
int status;
|
|
|
|
rdpTsg* tsg;
|
|
|
|
|
|
|
|
tsg = (rdpTsg*) bio->ptr;
|
2014-05-30 22:53:10 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ);
|
2013-10-11 23:27:22 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
status = tsg_read(bio->ptr, (BYTE*) buf, size);
|
2013-10-11 14:12:50 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status < 0)
|
2013-10-11 23:27:22 +04:00
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2013-12-21 02:56:59 +04:00
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
else
|
2013-12-21 02:56:59 +04:00
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_READ);
|
2013-10-11 23:27:22 +04:00
|
|
|
}
|
|
|
|
|
2013-12-21 02:56:59 +04:00
|
|
|
return status >= 0 ? status : -1;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_tsg_puts(BIO* bio, const char* str)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_tsg_gets(BIO* bio, char* str, int size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long transport_bio_tsg_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
|
|
|
|
{
|
2013-10-29 04:20:18 +04:00
|
|
|
if (cmd == BIO_CTRL_FLUSH)
|
|
|
|
{
|
2013-10-24 22:13:41 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-10-29 04:20:18 +04:00
|
|
|
|
2013-10-24 22:13:41 +04:00
|
|
|
return 0;
|
2013-10-11 14:12:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_tsg_new(BIO* bio)
|
|
|
|
{
|
|
|
|
bio->init = 1;
|
|
|
|
bio->num = 0;
|
|
|
|
bio->ptr = NULL;
|
2014-06-02 05:37:20 +04:00
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
2013-10-11 14:12:50 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_tsg_free(BIO* bio)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIO_TYPE_TSG 65
|
|
|
|
|
|
|
|
static BIO_METHOD transport_bio_tsg_methods =
|
|
|
|
{
|
|
|
|
BIO_TYPE_TSG,
|
|
|
|
"TSGateway",
|
|
|
|
transport_bio_tsg_write,
|
|
|
|
transport_bio_tsg_read,
|
|
|
|
transport_bio_tsg_puts,
|
|
|
|
transport_bio_tsg_gets,
|
|
|
|
transport_bio_tsg_ctrl,
|
|
|
|
transport_bio_tsg_new,
|
|
|
|
transport_bio_tsg_free,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_tsg(void)
|
|
|
|
{
|
|
|
|
return &transport_bio_tsg_methods;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_connect_tls(rdpTransport* transport)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
rdpSettings *settings = transport->settings;
|
|
|
|
rdpTls *targetTls;
|
|
|
|
BIO *targetBio;
|
2014-04-02 00:23:27 +04:00
|
|
|
int tls_status;
|
2014-03-25 23:19:52 +04:00
|
|
|
freerdp* instance;
|
|
|
|
rdpContext* context;
|
|
|
|
|
|
|
|
instance = (freerdp*) transport->settings->instance;
|
|
|
|
context = instance->context;
|
|
|
|
|
2012-11-15 08:21:00 +04:00
|
|
|
if (transport->layer == TRANSPORT_LAYER_TSG)
|
2013-10-11 14:12:50 +04:00
|
|
|
{
|
2013-10-24 22:13:41 +04:00
|
|
|
transport->TsgTls = tls_new(transport->settings);
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG_TLS;
|
2013-10-11 14:12:50 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
targetTls = transport->TsgTls;
|
|
|
|
targetBio = transport->frontBio;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!transport->TlsIn)
|
|
|
|
transport->TlsIn = tls_new(settings);
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->TlsOut)
|
|
|
|
transport->TlsOut = transport->TlsIn;
|
2013-10-11 14:12:50 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
targetTls = transport->TlsIn;
|
|
|
|
targetBio = transport->TcpIn->bufferedBio;
|
2013-10-11 14:12:50 +04:00
|
|
|
|
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
|
|
|
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
targetTls->hostname = settings->ServerHostname;
|
|
|
|
targetTls->port = settings->ServerPort;
|
2012-11-15 08:21:00 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (targetTls->port == 0)
|
|
|
|
targetTls->port = 3389;
|
2013-12-07 07:15:45 +04:00
|
|
|
|
2014-05-30 22:36:18 +04:00
|
|
|
targetTls->isGatewayTransport = FALSE;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls_status = tls_connect(targetTls, targetBio);
|
2014-04-02 00:23:27 +04:00
|
|
|
|
|
|
|
if (tls_status < 1)
|
2012-11-15 08:21:00 +04:00
|
|
|
{
|
2014-04-02 00:23:27 +04:00
|
|
|
if (tls_status < 0)
|
|
|
|
{
|
|
|
|
if (!connectErrorCode)
|
|
|
|
connectErrorCode = TLSCONNECTERROR;
|
2012-11-15 05:46:51 +04:00
|
|
|
|
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
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = targetTls->bio;
|
|
|
|
if (!transport->frontBio)
|
|
|
|
{
|
2014-05-21 21:13:40 +04:00
|
|
|
fprintf(stderr, "%s: unable to prepend a filtering TLS bio", __FUNCTION__);
|
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
|
|
|
{
|
2012-02-18 02:12:21 +04:00
|
|
|
freerdp* instance;
|
|
|
|
rdpSettings* settings;
|
2014-05-21 19:32:14 +04:00
|
|
|
rdpCredssp *credSsp;
|
2012-02-18 02:12:21 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
settings = transport->settings;
|
|
|
|
instance = (freerdp*) settings->instance;
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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-18 21:07:52 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->credssp)
|
2014-02-12 09:43:02 +04:00
|
|
|
{
|
2012-11-15 06:30:21 +04:00
|
|
|
transport->credssp = credssp_new(instance, transport, settings);
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->credssp)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, TRUE);
|
2011-07-07 21:37:48 +04:00
|
|
|
|
2014-02-12 09:43:02 +04:00
|
|
|
if (settings->AuthenticationServiceClass)
|
|
|
|
{
|
|
|
|
transport->credssp->ServicePrincipalName =
|
|
|
|
credssp_make_spn(settings->AuthenticationServiceClass, settings->ServerHostname);
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->credssp->ServicePrincipalName)
|
|
|
|
return FALSE;
|
2014-02-12 09:43:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
credSsp = transport->credssp;
|
|
|
|
if (credssp_authenticate(credSsp) < 0)
|
2011-07-07 21:37:48 +04:00
|
|
|
{
|
2013-01-10 20:19:57 +04:00
|
|
|
if (!connectErrorCode)
|
|
|
|
connectErrorCode = AUTHENTICATIONERROR;
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2014-03-21 02:19:54 +04:00
|
|
|
if (!freerdp_get_last_error(instance->context))
|
|
|
|
{
|
|
|
|
freerdp_set_last_error(instance->context, FREERDP_ERROR_AUTHENTICATION_FAILED);
|
|
|
|
}
|
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Authentication failure, check credentials.\n"
|
2011-07-07 21:37:48 +04:00
|
|
|
"If credentials are valid, the NTLMSSP implementation may be to blame.\n");
|
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2014-05-21 19:32:14 +04:00
|
|
|
credssp_free(credSsp);
|
2013-07-01 21:24:19 +04:00
|
|
|
transport->credssp = NULL;
|
2014-03-27 22:24:15 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-07 21:37:48 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2014-05-21 19:32:14 +04:00
|
|
|
credssp_free(credSsp);
|
2013-11-08 02:37:58 +04:00
|
|
|
transport->credssp = NULL;
|
2011-07-07 21:37:48 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
BOOL transport_tsg_connect(rdpTransport* transport, const char* hostname, UINT16 port)
|
2012-03-26 20:20:38 +04:00
|
|
|
{
|
2014-04-02 04:56:34 +04:00
|
|
|
rdpTsg* tsg;
|
2014-04-02 00:23:27 +04:00
|
|
|
int tls_status;
|
|
|
|
freerdp* instance;
|
|
|
|
rdpContext* context;
|
2014-05-21 19:32:14 +04:00
|
|
|
rdpSettings *settings = transport->settings;
|
2014-04-02 00:23:27 +04:00
|
|
|
|
|
|
|
instance = (freerdp*) transport->settings->instance;
|
|
|
|
context = instance->context;
|
|
|
|
|
2014-04-02 04:56:34 +04:00
|
|
|
tsg = tsg_new(transport);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tsg)
|
|
|
|
return FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
|
|
|
tsg->transport = transport;
|
|
|
|
transport->tsg = tsg;
|
2012-11-15 05:46:51 +04:00
|
|
|
transport->SplitInputOutput = TRUE;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsIn)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
transport->TlsIn = tls_new(settings);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->TlsIn)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsOut)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
transport->TlsOut = tls_new(settings);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->TlsOut)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/* put a decent default value for gateway port */
|
|
|
|
if (!settings->GatewayPort)
|
|
|
|
settings->GatewayPort = 443;
|
2013-12-07 07:15:45 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->TlsIn->hostname = transport->TlsOut->hostname = settings->GatewayHostname;
|
|
|
|
transport->TlsIn->port = transport->TlsOut->port = settings->GatewayPort;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-30 22:36:18 +04:00
|
|
|
transport->TlsIn->isGatewayTransport = TRUE;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls_status = tls_connect(transport->TlsIn, transport->TcpIn->bufferedBio);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
if (tls_status < 1)
|
|
|
|
{
|
|
|
|
if (tls_status < 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-04-02 00:23:27 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 22:36:18 +04:00
|
|
|
transport->TlsOut->isGatewayTransport = TRUE;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tls_status = tls_connect(transport->TlsOut, transport->TcpOut->bufferedBio);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-04-02 00:23:27 +04:00
|
|
|
if (tls_status < 1)
|
|
|
|
{
|
|
|
|
if (tls_status < 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-04-02 00:23:27 +04:00
|
|
|
}
|
2012-03-26 20:20:38 +04:00
|
|
|
|
|
|
|
if (!tsg_connect(tsg, hostname, port))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = BIO_new(BIO_s_tsg());
|
|
|
|
transport->frontBio->ptr = tsg;
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-03-26 20:20:38 +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
|
|
|
{
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL status = FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
rdpSettings* settings = transport->settings;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->SplitInputOutput = TRUE;
|
2012-11-15 05:46:51 +04:00
|
|
|
transport->TcpOut = tcp_new(settings);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
if (!tcp_connect(transport->TcpIn, settings->GatewayHostname, settings->GatewayPort, timeout) ||
|
2014-05-21 19:32:14 +04:00
|
|
|
!tcp_set_blocking_mode(transport->TcpIn, FALSE))
|
|
|
|
return FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
if (!tcp_connect(transport->TcpOut, settings->GatewayHostname, settings->GatewayPort, timeout) ||
|
2014-05-21 19:32:14 +04:00
|
|
|
!tcp_set_blocking_mode(transport->TcpOut, FALSE))
|
|
|
|
return FALSE;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport_tsg_connect(transport, hostname, port))
|
|
|
|
return FALSE;
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
status = TRUE;
|
2012-03-26 20:20:38 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-30 22:03:20 +04:00
|
|
|
status = tcp_connect(transport->TcpIn, hostname, port, timeout);
|
2012-11-15 05:46:51 +04:00
|
|
|
|
|
|
|
transport->SplitInputOutput = FALSE;
|
|
|
|
transport->TcpOut = transport->TcpIn;
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = transport->TcpIn->bufferedBio;
|
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,
|
|
|
|
(LPTHREAD_START_ROUTINE) transport_client_thread, transport, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsIn)
|
2012-11-15 05:46:51 +04:00
|
|
|
transport->TlsIn = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsOut)
|
2012-12-13 00:55:42 +04:00
|
|
|
transport->TlsOut = transport->TlsIn;
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls_accept(transport->TlsIn, transport->TcpIn->bufferedBio, transport->settings->CertificateFile, transport->settings->PrivateKeyFile))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = transport->TlsIn->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
|
|
|
{
|
2012-02-18 02:12:21 +04:00
|
|
|
freerdp* instance;
|
|
|
|
rdpSettings* settings;
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
settings = transport->settings;
|
|
|
|
instance = (freerdp*) settings->instance;
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsIn)
|
2012-11-15 05:46:51 +04:00
|
|
|
transport->TlsIn = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (!transport->TlsOut)
|
2013-01-16 15:58:37 +04:00
|
|
|
transport->TlsOut = transport->TlsIn;
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tls_accept(transport->TlsIn, transport->TcpIn->bufferedBio, settings->CertificateFile, settings->PrivateKeyFile))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->frontBio = transport->TlsIn->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
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (!transport->credssp)
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
2012-11-15 06:30:21 +04:00
|
|
|
transport->credssp = credssp_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
|
|
|
|
|
|
|
if (credssp_authenticate(transport->credssp) < 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "client authentication failure\n");
|
2014-03-27 22:24:15 +04:00
|
|
|
|
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2012-02-14 07:27:59 +04:00
|
|
|
credssp_free(transport->credssp);
|
2013-07-01 21:24:19 +04:00
|
|
|
transport->credssp = NULL;
|
2013-12-19 04:44:18 +04:00
|
|
|
|
|
|
|
tls_set_alert_code(transport->TlsIn, 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
|
|
|
}
|
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
/* don't free credssp 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);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL nla_verify_header(wStream* s)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
if ((Stream_Pointer(s)[0] == 0x30) && (Stream_Pointer(s)[1] & 0x80))
|
2012-12-21 23:13:40 +04:00
|
|
|
return TRUE;
|
2011-07-04 06:11:14 +04:00
|
|
|
|
2012-12-21 23:13:40 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-04 06:11:14 +04:00
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
UINT32 nla_read_header(wStream* s)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
|
|
|
UINT32 length = 0;
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (Stream_Pointer(s)[1] & 0x80)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
if ((Stream_Pointer(s)[1] & ~(0x80)) == 1)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
length = Stream_Pointer(s)[2];
|
2012-12-21 23:13:40 +04:00
|
|
|
length += 3;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 3);
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
2013-11-08 00:14:59 +04:00
|
|
|
else if ((Stream_Pointer(s)[1] & ~(0x80)) == 2)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
length = (Stream_Pointer(s)[2] << 8) | Stream_Pointer(s)[3];
|
2012-12-21 23:13:40 +04:00
|
|
|
length += 4;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 4);
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Error reading TSRequest!\n");
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
length = Stream_Pointer(s)[1];
|
2012-12-21 23:13:40 +04:00
|
|
|
length += 2;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 2);
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
UINT32 nla_header_length(wStream* s)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
|
|
|
UINT32 length = 0;
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
if (Stream_Pointer(s)[1] & 0x80)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2013-11-08 00:14:59 +04:00
|
|
|
if ((Stream_Pointer(s)[1] & ~(0x80)) == 1)
|
2012-12-21 23:13:40 +04:00
|
|
|
length = 3;
|
2013-11-08 00:14:59 +04:00
|
|
|
else if ((Stream_Pointer(s)[1] & ~(0x80)) == 2)
|
2012-12-21 23:13:40 +04:00
|
|
|
length = 4;
|
|
|
|
else
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Error reading TSRequest!\n");
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
static int transport_wait_for_read(rdpTransport* transport)
|
|
|
|
{
|
2014-07-02 17:15:46 +04:00
|
|
|
rdpTcp *tcpIn = transport->TcpIn;
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (tcpIn->readBlocked)
|
|
|
|
{
|
2014-07-07 12:44:57 +04:00
|
|
|
return tcp_wait_read(tcpIn, 10);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
else if (tcpIn->writeBlocked)
|
|
|
|
{
|
2014-07-07 12:44:57 +04:00
|
|
|
return tcp_wait_write(tcpIn, 10);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
USleep(1000);
|
|
|
|
return 0;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_wait_for_write(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
rdpTcp *tcpOut;
|
|
|
|
|
|
|
|
tcpOut = transport->SplitInputOutput ? transport->TcpOut : transport->TcpIn;
|
|
|
|
if (tcpOut->writeBlocked)
|
|
|
|
{
|
2014-07-07 12:44:57 +04:00
|
|
|
return tcp_wait_write(tcpOut, 10);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
else if (tcpOut->readBlocked)
|
|
|
|
{
|
2014-07-07 12:44:57 +04:00
|
|
|
return tcp_wait_read(tcpOut, 10);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
USleep(1000);
|
|
|
|
return 0;
|
2014-05-21 19:32:14 +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;
|
|
|
|
|
|
|
|
/* blocking means that we can't continue until we have read the number of
|
|
|
|
* requested bytes */
|
|
|
|
if (transport_wait_for_read(transport) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: error when selecting for read\n", __FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int transport_read(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-05-21 19:32:14 +04:00
|
|
|
BYTE *header;
|
2012-12-08 07:43:53 +04:00
|
|
|
int transport_status;
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
position = 0;
|
2013-05-15 20:14:26 +04:00
|
|
|
pduLength = 0;
|
2012-12-08 07:43:53 +04:00
|
|
|
transport_status = 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;
|
|
|
|
|
2012-12-08 07:18:50 +04:00
|
|
|
/* first check if we have header */
|
2013-11-08 02:37:58 +04:00
|
|
|
position = Stream_GetPosition(s);
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (position < 4)
|
2011-08-01 18:21:06 +04:00
|
|
|
{
|
2014-07-10 14:09:48 +04:00
|
|
|
Stream_EnsureCapacity(s, 4);
|
2013-11-08 02:37:58 +04:00
|
|
|
status = transport_read_layer(transport, Stream_Buffer(s) + position, 4 - position);
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2012-12-08 07:18:50 +04:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2012-12-08 07:43:53 +04:00
|
|
|
|
|
|
|
transport_status += status;
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if ((status + position) < 4)
|
2012-12-08 07:43:53 +04:00
|
|
|
return transport_status;
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
position += status;
|
2011-08-01 18:21:06 +04:00
|
|
|
}
|
2012-12-08 07:43:53 +04:00
|
|
|
|
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 header is present, read exactly one PDU */
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
if (transport->NlaMode)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
pduLength = header[2];
|
|
|
|
pduLength += 3;
|
|
|
|
}
|
|
|
|
else if ((header[1] & ~(0x80)) == 2)
|
|
|
|
{
|
|
|
|
pduLength = (header[2] << 8) | header[3];
|
|
|
|
pduLength += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error reading TSRequest!\n");
|
|
|
|
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 */
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
pduLength = (header[2] << 8) | header[3];
|
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
else
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
|
|
|
/* Fast-Path Header */
|
|
|
|
|
|
|
|
if (header[1] & 0x80)
|
|
|
|
pduLength = ((header[1] & 0x7F) << 8) | header[2];
|
|
|
|
else
|
|
|
|
pduLength = header[1];
|
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
}
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2014-07-10 14:09:48 +04:00
|
|
|
if (pduLength < 0 || pduLength > 0xFFFF)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: invalid pduLength: %d\n", __FUNCTION__, pduLength);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_EnsureCapacity(s, pduLength);
|
2013-11-08 02:37:58 +04:00
|
|
|
status = transport_read_layer(transport, Stream_Buffer(s) + position, pduLength - position);
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2012-12-08 07:18:50 +04:00
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2012-12-08 07:43:53 +04:00
|
|
|
|
|
|
|
transport_status += status;
|
|
|
|
|
2012-12-09 03:27:08 +04:00
|
|
|
#ifdef WITH_DEBUG_TRANSPORT
|
|
|
|
/* dump when whole PDU is read */
|
2013-11-08 02:37:58 +04:00
|
|
|
if (position + status >= pduLength)
|
2012-12-09 03:27:08 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Local < Remote\n");
|
2013-05-15 20:14:26 +04:00
|
|
|
winpr_HexDump(Stream_Buffer(s), pduLength);
|
2012-12-09 03:27:08 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (position + status >= pduLength)
|
2013-10-09 07:43:57 +04:00
|
|
|
{
|
2013-10-09 23:11:56 +04:00
|
|
|
WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), pduLength, WLOG_PACKET_INBOUND);
|
2013-10-09 07:43:57 +04:00
|
|
|
}
|
|
|
|
|
2012-12-08 07:43:53 +04:00
|
|
|
return transport_status;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
static int transport_read_nonblocking(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2012-12-22 00:49:02 +04:00
|
|
|
status = transport_read(transport, transport->ReceiveBuffer);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
return status;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(transport->ReceiveBuffer, status);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
BOOL transport_bio_buffered_drain(BIO *bio);
|
|
|
|
|
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;
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
EnterCriticalSection(&(transport->WriteLock));
|
2013-05-16 02:05:40 +04: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
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_TRANSPORT
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Local > Remote\n");
|
2013-05-15 20:14:26 +04:00
|
|
|
winpr_HexDump(Stream_Buffer(s), length);
|
2011-08-03 05:34:23 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-09 07:43:57 +04:00
|
|
|
if (length > 0)
|
|
|
|
{
|
2013-10-09 23:11:56 +04:00
|
|
|
WLog_Packet(transport->log, 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;
|
|
|
|
|
|
|
|
if (transport_wait_for_write(transport) < 0)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
fprintf(stderr, "%s: error when selecting for write\n", __FUNCTION__);
|
|
|
|
return -1;
|
2011-08-20 16:30:18 +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)
|
|
|
|
{
|
|
|
|
/* blocking transport, we must ensure the write buffer is really empty */
|
|
|
|
rdpTcp *out = transport->TcpOut;
|
|
|
|
|
|
|
|
while (out->writeBlocked)
|
|
|
|
{
|
|
|
|
if (transport_wait_for_write(transport) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: error when selecting for write\n", __FUNCTION__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!transport_bio_buffered_drain(out->bufferedBio))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: error when draining outputBuffer\n", __FUNCTION__);
|
|
|
|
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));
|
2013-05-16 02:05:40 +04:00
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
return status;
|
2011-07-04 06:11:14 +04:00
|
|
|
}
|
|
|
|
|
2011-08-20 16:30:18 +04:00
|
|
|
void transport_get_fds(rdpTransport* transport, void** rfds, int* rcount)
|
|
|
|
{
|
2012-12-19 21:16:39 +04:00
|
|
|
void* pfd;
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2012-09-18 01:44:06 +04:00
|
|
|
#ifdef _WIN32
|
2012-11-22 18:06:45 +04:00
|
|
|
rfds[*rcount] = transport->TcpIn->wsa_event;
|
2012-11-17 13:47:13 +04:00
|
|
|
(*rcount)++;
|
2012-11-22 18:06:45 +04:00
|
|
|
|
|
|
|
if (transport->SplitInputOutput)
|
|
|
|
{
|
|
|
|
rfds[*rcount] = transport->TcpOut->wsa_event;
|
|
|
|
(*rcount)++;
|
|
|
|
}
|
2012-09-18 01:44:06 +04:00
|
|
|
#else
|
2012-11-15 05:46:51 +04:00
|
|
|
rfds[*rcount] = (void*)(long)(transport->TcpIn->sockfd);
|
2011-08-20 16:30:18 +04:00
|
|
|
(*rcount)++;
|
2012-11-17 13:47:13 +04:00
|
|
|
|
|
|
|
if (transport->SplitInputOutput)
|
|
|
|
{
|
|
|
|
rfds[*rcount] = (void*)(long)(transport->TcpOut->sockfd);
|
|
|
|
(*rcount)++;
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2012-12-22 00:49:02 +04:00
|
|
|
pfd = GetEventWaitObject(transport->ReceiveEvent);
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2012-12-19 21:16:39 +04:00
|
|
|
if (pfd)
|
2012-11-27 05:15:48 +04:00
|
|
|
{
|
2012-12-19 21:16:39 +04:00
|
|
|
rfds[*rcount] = pfd;
|
2012-11-27 05:15:48 +04:00
|
|
|
(*rcount)++;
|
|
|
|
}
|
2013-03-28 04:06:10 +04:00
|
|
|
|
|
|
|
if (transport->GatewayEvent)
|
|
|
|
{
|
|
|
|
pfd = GetEventWaitObject(transport->GatewayEvent);
|
|
|
|
|
|
|
|
if (pfd)
|
|
|
|
{
|
|
|
|
rfds[*rcount] = pfd;
|
|
|
|
(*rcount)++;
|
|
|
|
}
|
|
|
|
}
|
2011-08-20 16:30:18 +04:00
|
|
|
}
|
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
void transport_get_read_handles(rdpTransport* transport, HANDLE* events, DWORD* count)
|
|
|
|
{
|
|
|
|
events[*count] = tcp_get_event_handle(transport->TcpIn);
|
|
|
|
(*count)++;
|
|
|
|
|
|
|
|
if (transport->SplitInputOutput)
|
|
|
|
{
|
|
|
|
events[*count] = tcp_get_event_handle(transport->TcpOut);
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport->ReceiveEvent)
|
|
|
|
{
|
|
|
|
events[*count] = transport->ReceiveEvent;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport->GatewayEvent)
|
|
|
|
{
|
|
|
|
events[*count] = transport->GatewayEvent;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
BOOL tranport_is_write_blocked(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
if (transport->TcpIn->writeBlocked)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return transport->SplitInputOutput &&
|
|
|
|
transport->TcpOut &&
|
|
|
|
transport->TcpOut->writeBlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tranport_drain_output_buffer(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
/* First try to send some accumulated bytes in the send buffer */
|
|
|
|
if (transport->TcpIn->writeBlocked)
|
|
|
|
{
|
|
|
|
if (!transport_bio_buffered_drain(transport->TcpIn->bufferedBio))
|
|
|
|
return -1;
|
|
|
|
ret |= transport->TcpIn->writeBlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transport->SplitInputOutput && transport->TcpOut && transport->TcpOut->writeBlocked)
|
|
|
|
{
|
|
|
|
if (!transport_bio_buffered_drain(transport->TcpOut->bufferedBio))
|
|
|
|
return -1;
|
|
|
|
ret |= transport->TcpOut->writeBlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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 pos;
|
|
|
|
int status;
|
2013-11-08 02:37:58 +04:00
|
|
|
int length;
|
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;
|
|
|
|
|
2012-10-01 22:08:13 +04:00
|
|
|
#ifdef _WIN32
|
2012-11-22 18:06:45 +04:00
|
|
|
WSAResetEvent(transport->TcpIn->wsa_event);
|
2012-10-01 22:08:13 +04:00
|
|
|
#endif
|
2012-12-22 00:49:02 +04:00
|
|
|
ResetEvent(transport->ReceiveEvent);
|
2011-08-20 16:30:18 +04:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Note: transport_read_nonblocking() reads max 1 additional PDU from
|
|
|
|
* the layer. Also note that transport_read_nonblocking() is also called
|
|
|
|
* outside of this function in transport_write()! This means that when
|
|
|
|
* entering transport_check_fds it is possible that the stream position
|
|
|
|
* of transport->ReceiveBuffer position is > 0. We must process this data
|
|
|
|
* even if transport_read_nonblocking() returns 0.
|
|
|
|
* Note that transport->ReceiveBuffer is replaced after each iteration
|
|
|
|
* of this loop with a fresh stream instance from a pool.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((status = transport_read_nonblocking(transport)) < 0)
|
|
|
|
return status;
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
if ((pos = Stream_GetPosition(transport->ReceiveBuffer)) < 2)
|
2014-06-04 11:57:27 +04:00
|
|
|
return 0;
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, 0);
|
2014-04-17 14:04:26 +04:00
|
|
|
length = 0;
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
if (transport->NlaMode)
|
|
|
|
{
|
|
|
|
if (nla_verify_header(transport->ReceiveBuffer))
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2014-04-03 23:42:32 +04:00
|
|
|
/* TSRequest */
|
2014-03-10 23:35:14 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
/* Ensure the TSRequest header is available. */
|
|
|
|
if (pos <= 4)
|
|
|
|
{
|
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
/* TSRequest header can be 2, 3 or 4 bytes long */
|
|
|
|
length = nla_header_length(transport->ReceiveBuffer);
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
if (pos < length)
|
|
|
|
{
|
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-10 23:35:14 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
length = nla_read_header(transport->ReceiveBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tpkt_verify_header(transport->ReceiveBuffer)) /* TPKT */
|
|
|
|
{
|
|
|
|
/* Ensure the TPKT header is available. */
|
|
|
|
if (pos <= 4)
|
|
|
|
{
|
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0;
|
2014-03-10 23:35:14 +04:00
|
|
|
}
|
2014-04-03 23:42:32 +04:00
|
|
|
|
|
|
|
length = tpkt_read_header(transport->ReceiveBuffer);
|
2014-03-10 23:35:14 +04:00
|
|
|
}
|
2014-04-03 23:42:32 +04:00
|
|
|
else /* Fast Path */
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2014-04-03 23:42:32 +04:00
|
|
|
/* Ensure the Fast Path header is available. */
|
|
|
|
if (pos <= 2)
|
2014-03-10 23:35:14 +04:00
|
|
|
{
|
2014-04-03 23:42:32 +04:00
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0;
|
2014-03-10 23:35:14 +04:00
|
|
|
}
|
2014-04-03 23:42:32 +04:00
|
|
|
|
|
|
|
/* Fastpath header can be two or three bytes long. */
|
|
|
|
length = fastpath_header_length(transport->ReceiveBuffer);
|
|
|
|
|
|
|
|
if (pos < length)
|
2014-03-10 23:35:14 +04:00
|
|
|
{
|
2014-04-03 23:42:32 +04:00
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0;
|
2014-03-10 23:35:14 +04:00
|
|
|
}
|
2012-12-21 23:13:40 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
length = fastpath_read_header(NULL, transport->ReceiveBuffer);
|
2011-08-23 11:36:03 +04:00
|
|
|
}
|
2014-04-03 23:42:32 +04:00
|
|
|
}
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
if (length == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "transport_check_fds: protocol error, not a TPKT or Fast Path header.\n");
|
|
|
|
winpr_HexDump(Stream_Buffer(transport->ReceiveBuffer), pos);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
if (pos < length)
|
|
|
|
{
|
|
|
|
Stream_SetPosition(transport->ReceiveBuffer, pos);
|
|
|
|
return 0; /* Packet is not yet completely received. */
|
|
|
|
}
|
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);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
Stream_SetPosition(received, length);
|
|
|
|
Stream_SealLength(received);
|
|
|
|
Stream_SetPosition(received, 0);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
/**
|
|
|
|
* status:
|
|
|
|
* -1: error
|
|
|
|
* 0: success
|
|
|
|
* 1: redirection
|
|
|
|
*/
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-04-03 23:42:32 +04:00
|
|
|
recv_status = transport->ReceiveCallback(transport, received, transport->ReceiveExtra);
|
2012-04-09 21:28:33 +04:00
|
|
|
|
2014-04-29 00:44:52 +04:00
|
|
|
if (recv_status == 1)
|
|
|
|
{
|
|
|
|
return 1; /* session redirection */
|
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
Stream_Release(received);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-11-28 03:34:00 +04:00
|
|
|
BOOL status;
|
|
|
|
|
|
|
|
status = TRUE;
|
2011-08-01 08:43:53 +04:00
|
|
|
transport->blocking = blocking;
|
2012-11-28 03:34:00 +04:00
|
|
|
|
|
|
|
if (transport->SplitInputOutput)
|
|
|
|
{
|
|
|
|
status &= tcp_set_blocking_mode(transport->TcpIn, blocking);
|
|
|
|
status &= tcp_set_blocking_mode(transport->TcpOut, blocking);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status &= tcp_set_blocking_mode(transport->TcpIn, blocking);
|
|
|
|
}
|
|
|
|
|
2013-10-26 03:17:36 +04:00
|
|
|
if (transport->layer == TRANSPORT_LAYER_TSG || transport->layer == TRANSPORT_LAYER_TSG_TLS)
|
2012-12-07 01:49:50 +04:00
|
|
|
{
|
|
|
|
tsg_set_blocking_mode(transport->tsg, blocking);
|
|
|
|
}
|
|
|
|
|
2012-11-28 03:34:00 +04:00
|
|
|
return status;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-27 21:03:41 +04:00
|
|
|
static void* transport_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
2013-11-04 21:40:29 +04:00
|
|
|
HANDLE handles[8];
|
2013-03-27 21:03:41 +04:00
|
|
|
freerdp* instance;
|
2013-06-19 00:55:23 +04:00
|
|
|
rdpContext* context;
|
2013-03-27 21:03:41 +04:00
|
|
|
rdpTransport* transport;
|
|
|
|
|
|
|
|
transport = (rdpTransport*) arg;
|
2013-09-05 15:40:04 +04:00
|
|
|
assert(NULL != transport);
|
|
|
|
assert(NULL != transport->settings);
|
|
|
|
|
2013-03-27 21:03:41 +04:00
|
|
|
instance = (freerdp*) transport->settings->instance;
|
2013-09-05 15:40:04 +04:00
|
|
|
assert(NULL != instance);
|
|
|
|
|
2013-06-19 00:55:23 +04:00
|
|
|
context = instance->context;
|
2013-09-05 15:40:04 +04:00
|
|
|
assert(NULL != instance->context);
|
2013-11-04 21:40:29 +04:00
|
|
|
|
2013-11-05 00:52:29 +04:00
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "Starting transport thread");
|
|
|
|
|
2013-11-04 21:40:29 +04:00
|
|
|
nCount = 0;
|
|
|
|
handles[nCount++] = transport->stopEvent;
|
|
|
|
handles[nCount++] = transport->connectedEvent;
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
|
|
|
|
|
2013-11-05 20:49:42 +04:00
|
|
|
if (WaitForSingleObject(transport->stopEvent, 0) == WAIT_OBJECT_0)
|
2013-11-04 21:40:29 +04:00
|
|
|
{
|
2013-11-05 00:52:29 +04:00
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "Terminating transport thread");
|
2013-11-04 21:40:29 +04:00
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2013-11-05 00:52:29 +04:00
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "Asynchronous transport activated");
|
|
|
|
|
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;
|
2013-05-01 02:25:18 +04:00
|
|
|
|
2013-11-04 21:40:29 +04:00
|
|
|
transport_get_read_handles(transport, (HANDLE*) &handles, &nCount);
|
2013-10-16 06:42:07 +04: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
|
|
|
rdpRdp* rdp = (rdpRdp*) transport->rdp;
|
|
|
|
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
|
|
|
}
|
|
|
|
else if (status != WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (WaitForSingleObject(transport->stopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
break;
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2013-12-21 03:22:29 +04:00
|
|
|
if (!freerdp_check_fds(instance))
|
2014-06-01 18:46:43 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-12-21 03:22:29 +04:00
|
|
|
}
|
2013-03-27 21:03:41 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:52:29 +04:00
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "Terminating transport thread");
|
|
|
|
|
2013-09-05 15:40:04 +04:00
|
|
|
ExitThread(0);
|
2013-03-27 21:03:41 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
rdpTransport* transport_new(rdpSettings* settings)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2011-07-07 21:37:48 +04:00
|
|
|
rdpTransport* transport;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport = (rdpTransport *)calloc(1, sizeof(rdpTransport));
|
|
|
|
if (!transport)
|
|
|
|
return NULL;
|
2013-03-28 04:06:10 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
WLog_Init();
|
|
|
|
transport->log = WLog_Get("com.freerdp.core.transport");
|
|
|
|
if (!transport->log)
|
|
|
|
goto out_free;
|
2013-10-09 07:43:57 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->TcpIn = tcp_new(settings);
|
|
|
|
if (!transport->TcpIn)
|
|
|
|
goto out_free;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->settings = settings;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/* a small 0.1ms delay when transport is blocking. */
|
|
|
|
transport->SleepInterval = 100;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->ReceivePool = StreamPool_New(TRUE, BUFFER_SIZE);
|
|
|
|
if (!transport->ReceivePool)
|
|
|
|
goto out_free_tcpin;
|
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);
|
|
|
|
if (!transport->ReceiveBuffer)
|
|
|
|
goto out_free_receivepool;
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->ReceiveEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!transport->ReceiveEvent || transport->ReceiveEvent == INVALID_HANDLE_VALUE)
|
|
|
|
goto out_free_receivebuffer;
|
2013-05-01 02:25:18 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->connectedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!transport->connectedEvent || transport->connectedEvent == INVALID_HANDLE_VALUE)
|
|
|
|
goto out_free_receiveEvent;
|
2011-09-04 00:36:27 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->blocking = TRUE;
|
|
|
|
transport->GatewayEnabled = FALSE;
|
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
2013-05-16 02:05:40 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(transport->ReadLock), 4000))
|
|
|
|
goto out_free_connectedEvent;
|
|
|
|
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_receiveEvent:
|
|
|
|
CloseHandle(transport->ReceiveEvent);
|
|
|
|
out_free_receivebuffer:
|
|
|
|
StreamPool_Return(transport->ReceivePool, transport->ReceiveBuffer);
|
|
|
|
out_free_receivepool:
|
|
|
|
StreamPool_Free(transport->ReceivePool);
|
|
|
|
out_free_tcpin:
|
|
|
|
tcp_free(transport->TcpIn);
|
|
|
|
out_free:
|
|
|
|
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
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport_stop(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);
|
2013-05-01 02:25:18 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
CloseHandle(transport->ReceiveEvent);
|
|
|
|
CloseHandle(transport->connectedEvent);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->TlsIn)
|
|
|
|
tls_free(transport->TlsIn);
|
2012-12-13 07:03:40 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->TlsOut != transport->TlsIn)
|
|
|
|
tls_free(transport->TlsOut);
|
2013-11-01 07:35:24 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->TlsIn = NULL;
|
|
|
|
transport->TlsOut = NULL;
|
2012-12-13 07:03:40 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->TcpIn)
|
|
|
|
tcp_free(transport->TcpIn);
|
2012-12-13 07:03:40 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->TcpOut != transport->TcpIn)
|
|
|
|
tcp_free(transport->TcpOut);
|
2013-11-01 07:35:24 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->TcpIn = NULL;
|
|
|
|
transport->TcpOut = NULL;
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tsg_free(transport->tsg);
|
|
|
|
transport->tsg = NULL;
|
2013-05-16 02:05:40 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
DeleteCriticalSection(&(transport->ReadLock));
|
|
|
|
DeleteCriticalSection(&(transport->WriteLock));
|
|
|
|
|
|
|
|
free(transport);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|