2011-07-03 05:50:11 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* 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
|
|
|
|
|
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-09-04 01:08:46 +04:00
|
|
|
#include <freerdp/utils/tcp.h>
|
2011-08-17 00:04:28 +04:00
|
|
|
#include <freerdp/utils/sleep.h>
|
2011-07-03 05:50:11 +04:00
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
2011-08-03 05:34:23 +04:00
|
|
|
#include <freerdp/utils/hexdump.h>
|
2012-04-13 15:16:08 +04:00
|
|
|
#include <freerdp/errorcodes.h>
|
2011-07-03 05:50:11 +04:00
|
|
|
|
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>
|
|
|
|
#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"
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2012-05-25 09:50:46 +04:00
|
|
|
#include <freerdp/crypto/nla.h>
|
2012-02-18 22:00:34 +04:00
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#define BUFFER_SIZE 16384
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
STREAM* transport_recv_stream_init(rdpTransport* transport, int size)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
STREAM* s = transport->recv_stream;
|
|
|
|
stream_check_size(s, size);
|
|
|
|
stream_set_pos(s, 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM* transport_send_stream_init(rdpTransport* transport, int size)
|
|
|
|
{
|
|
|
|
STREAM* s = transport->send_stream;
|
|
|
|
stream_check_size(s, size);
|
|
|
|
stream_set_pos(s, 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
void transport_attach(rdpTransport* transport, int sockfd)
|
|
|
|
{
|
|
|
|
transport->tcp->sockfd = sockfd;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean transport_disconnect(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-09-04 00:36:27 +04:00
|
|
|
if (transport->layer == TRANSPORT_LAYER_TLS)
|
2011-10-20 14:50:12 +04:00
|
|
|
tls_disconnect(transport->tls);
|
2012-02-12 21:46:53 +04:00
|
|
|
|
2011-10-20 14:50:12 +04:00
|
|
|
return tcp_disconnect(transport->tcp);
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean transport_connect_rdp(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-04 03:27:02 +04:00
|
|
|
/* RDP encryption */
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean transport_connect_tls(rdpTransport* transport)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
|
|
|
if (transport->tls == NULL)
|
2012-02-03 03:20:02 +04:00
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
2011-07-04 03:27:02 +04:00
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
2012-05-22 00:01:24 +04:00
|
|
|
if (tls_connect(transport->tls) != true)
|
|
|
|
{
|
|
|
|
if (!connectErrorCode)
|
2012-04-13 15:16:08 +04:00
|
|
|
connectErrorCode = TLSCONNECTERROR;
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2012-04-10 23:15:36 +04:00
|
|
|
tls_free(transport->tls);
|
|
|
|
transport->tls = NULL;
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2012-04-10 23:15:36 +04:00
|
|
|
}
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean 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;
|
|
|
|
|
2011-07-04 03:27:02 +04:00
|
|
|
if (transport->tls == NULL)
|
2012-02-03 03:20:02 +04:00
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
2011-07-04 03:27:02 +04:00
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
2012-05-22 00:01:24 +04:00
|
|
|
if (tls_connect(transport->tls) != true)
|
|
|
|
{
|
|
|
|
if (!connectErrorCode)
|
2012-04-13 15:16:08 +04:00
|
|
|
connectErrorCode = TLSCONNECTERROR;
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2012-04-10 23:15:36 +04:00
|
|
|
tls_free(transport->tls);
|
|
|
|
transport->tls = NULL;
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2012-04-10 23:15:36 +04:00
|
|
|
}
|
2011-07-04 03:27:02 +04:00
|
|
|
|
|
|
|
/* Network Level Authentication */
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
if (transport->settings->authentication != true)
|
|
|
|
return true;
|
2011-08-18 21:07:52 +04:00
|
|
|
|
2012-02-18 02:12:21 +04:00
|
|
|
settings = transport->settings;
|
|
|
|
instance = (freerdp*) settings->instance;
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
if (transport->credssp == NULL)
|
2012-02-18 02:12:21 +04:00
|
|
|
transport->credssp = credssp_new(instance, transport->tls, settings);
|
2011-07-07 21:37:48 +04:00
|
|
|
|
|
|
|
if (credssp_authenticate(transport->credssp) < 0)
|
|
|
|
{
|
2012-05-22 00:01:24 +04:00
|
|
|
if (!connectErrorCode)
|
2012-04-13 15:16:08 +04:00
|
|
|
connectErrorCode = AUTHENTICATIONERROR;
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
printf("Authentication failure, check credentials.\n"
|
|
|
|
"If credentials are valid, the NTLMSSP implementation may be to blame.\n");
|
|
|
|
|
|
|
|
credssp_free(transport->credssp);
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2011-07-07 21:37:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
credssp_free(transport->credssp);
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 20:20:38 +04:00
|
|
|
boolean transport_tsg_connect(rdpTransport* transport, const char* hostname, uint16 port)
|
|
|
|
{
|
2012-04-24 00:33:18 +04:00
|
|
|
rdpTsg* tsg = tsg_new(transport);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
|
|
|
tsg->transport = transport;
|
|
|
|
transport->tsg = tsg;
|
|
|
|
|
|
|
|
if (transport->tls_in == NULL)
|
|
|
|
transport->tls_in = tls_new(transport->settings);
|
|
|
|
|
|
|
|
transport->tls_in->sockfd = transport->tcp_in->sockfd;
|
|
|
|
|
|
|
|
if (transport->tls_out == NULL)
|
|
|
|
transport->tls_out = tls_new(transport->settings);
|
|
|
|
|
|
|
|
transport->tls_out->sockfd = transport->tcp_out->sockfd;
|
|
|
|
|
|
|
|
if (tls_connect(transport->tls_in) != true)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (tls_connect(transport->tls_out) != true)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!tsg_connect(tsg, hostname, port))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean transport_connect(rdpTransport* transport, const char* hostname, uint16 port)
|
|
|
|
{
|
|
|
|
boolean status = false;
|
|
|
|
rdpSettings* settings = transport->settings;
|
|
|
|
|
|
|
|
if (transport->settings->ts_gateway)
|
|
|
|
{
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
transport->tcp_out = tcp_new(settings);
|
|
|
|
|
|
|
|
status = tcp_connect(transport->tcp_in, settings->tsg_hostname, 443);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
status = tcp_connect(transport->tcp_out, settings->tsg_hostname, 443);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
status = transport_tsg_connect(transport, hostname, port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = tcp_connect(transport->tcp, hostname, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
boolean transport_accept_rdp(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
/* RDP encryption */
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean transport_accept_tls(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
if (transport->tls == NULL)
|
2012-02-03 03:20:02 +04:00
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
if (tls_accept(transport->tls, transport->settings->cert_file, transport->settings->privatekey_file) != true)
|
|
|
|
return false;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean transport_accept_nla(rdpTransport* transport)
|
|
|
|
{
|
2012-02-18 02:12:21 +04:00
|
|
|
freerdp* instance;
|
|
|
|
rdpSettings* settings;
|
|
|
|
|
2011-08-19 09:35:29 +04:00
|
|
|
if (transport->tls == NULL)
|
2012-02-03 03:20:02 +04:00
|
|
|
transport->tls = tls_new(transport->settings);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
if (tls_accept(transport->tls, transport->settings->cert_file, transport->settings->privatekey_file) != true)
|
|
|
|
return false;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
/* Network Level Authentication */
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
if (transport->settings->authentication != true)
|
|
|
|
return true;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2012-02-18 02:12:21 +04:00
|
|
|
settings = transport->settings;
|
|
|
|
instance = (freerdp*) settings->instance;
|
|
|
|
|
2012-02-14 07:27:59 +04:00
|
|
|
if (transport->credssp == NULL)
|
2012-02-18 02:12:21 +04:00
|
|
|
transport->credssp = credssp_new(instance, transport->tls, settings);
|
2012-02-14 07:27:59 +04:00
|
|
|
|
|
|
|
if (credssp_authenticate(transport->credssp) < 0)
|
|
|
|
{
|
|
|
|
printf("client authentication failure\n");
|
|
|
|
credssp_free(transport->credssp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
/* don't free credssp module yet, we need to copy the credentials from it first */
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
int transport_read(rdpTransport* transport, STREAM* s)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
int status = -1;
|
2011-07-04 06:11:14 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
while (true)
|
2011-08-01 18:21:06 +04:00
|
|
|
{
|
|
|
|
if (transport->layer == TRANSPORT_LAYER_TLS)
|
2011-08-08 10:54:46 +04:00
|
|
|
status = tls_read(transport->tls, stream_get_tail(s), stream_get_left(s));
|
2011-08-01 18:21:06 +04:00
|
|
|
else if (transport->layer == TRANSPORT_LAYER_TCP)
|
2011-08-08 10:54:46 +04:00
|
|
|
status = tcp_read(transport->tcp, stream_get_tail(s), stream_get_left(s));
|
2012-03-26 20:20:38 +04:00
|
|
|
else if (transport->layer == TRANSPORT_LAYER_TSG)
|
|
|
|
status = tsg_read(transport->tsg, stream_get_tail(s), stream_get_left(s));
|
2011-08-01 18:21:06 +04:00
|
|
|
|
|
|
|
if (status == 0 && transport->blocking)
|
|
|
|
{
|
2011-08-17 00:04:28 +04:00
|
|
|
freerdp_usleep(transport->usleep_interval);
|
2011-08-01 18:21:06 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-07-04 06:11:14 +04:00
|
|
|
|
2011-08-03 05:34:23 +04:00
|
|
|
#ifdef WITH_DEBUG_TRANSPORT
|
|
|
|
if (status > 0)
|
|
|
|
{
|
2011-08-20 16:30:18 +04:00
|
|
|
printf("Local < Remote\n");
|
2011-08-03 05:34:23 +04:00
|
|
|
freerdp_hexdump(s->data, status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
return 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;
|
|
|
|
|
|
|
|
stream_check_size(transport->recv_buffer, 4096);
|
|
|
|
status = transport_read(transport, transport->recv_buffer);
|
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
stream_seek(transport->recv_buffer, status);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
int transport_write(rdpTransport* transport, STREAM* s)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
2011-07-10 23:34:43 +04:00
|
|
|
int status = -1;
|
2011-08-01 11:04:07 +04:00
|
|
|
int length;
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
length = stream_get_length(s);
|
|
|
|
stream_set_pos(s, 0);
|
2011-08-03 05:34:23 +04:00
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_TRANSPORT
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2011-08-20 16:30:18 +04:00
|
|
|
printf("Local > Remote\n");
|
2011-08-03 05:34:23 +04:00
|
|
|
freerdp_hexdump(s->data, length);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
while (length > 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
|
|
|
if (transport->layer == TRANSPORT_LAYER_TLS)
|
|
|
|
status = tls_write(transport->tls, stream_get_tail(s), length);
|
|
|
|
else if (transport->layer == TRANSPORT_LAYER_TCP)
|
|
|
|
status = tcp_write(transport->tcp, stream_get_tail(s), length);
|
2012-03-26 20:20:38 +04:00
|
|
|
else if (transport->layer == TRANSPORT_LAYER_TSG)
|
|
|
|
status = tsg_write(transport->tsg, stream_get_tail(s), length);
|
2011-08-01 11:04:07 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
break; /* error occurred */
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
/* blocking while sending */
|
2011-08-17 00:04:28 +04:00
|
|
|
freerdp_usleep(transport->usleep_interval);
|
2011-08-01 18:21:06 +04:00
|
|
|
|
|
|
|
/* when sending is blocked in nonblocking mode, the receiving buffer should be checked */
|
|
|
|
if (!transport->blocking)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
|
|
|
/* and in case we do have buffered some data, we set the event so next loop will get it */
|
|
|
|
if (transport_read_nonblocking(transport) > 0)
|
|
|
|
wait_obj_set(transport->recv_event);
|
|
|
|
}
|
2011-08-01 11:04:07 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
length -= status;
|
2011-08-01 11:04:07 +04:00
|
|
|
stream_seek(s, status);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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-09-18 01:44:06 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
rfds[*rcount] = transport->tcp->wsa_event;
|
|
|
|
#else
|
2011-08-20 16:30:18 +04:00
|
|
|
rfds[*rcount] = (void*)(long)(transport->tcp->sockfd);
|
2012-09-18 01:44:06 +04:00
|
|
|
#endif
|
2011-08-20 16:30:18 +04:00
|
|
|
(*rcount)++;
|
|
|
|
wait_obj_get_fds(transport->recv_event, rfds, rcount);
|
|
|
|
}
|
|
|
|
|
2012-03-16 21:12:49 +04:00
|
|
|
int transport_check_fds(rdpTransport** ptransport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-08-01 08:43:53 +04:00
|
|
|
int pos;
|
|
|
|
int status;
|
|
|
|
uint16 length;
|
|
|
|
STREAM* received;
|
2012-03-16 21:12:49 +04:00
|
|
|
rdpTransport* transport = *ptransport;
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2012-10-01 22:08:13 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSAResetEvent(transport->tcp->wsa_event);
|
|
|
|
#endif
|
2011-08-20 16:30:18 +04:00
|
|
|
wait_obj_clear(transport->recv_event);
|
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
status = transport_read_nonblocking(transport);
|
2011-09-04 01:02:26 +04:00
|
|
|
|
2011-08-12 19:09:58 +04:00
|
|
|
if (status < 0)
|
2011-08-01 08:43:53 +04:00
|
|
|
return status;
|
|
|
|
|
|
|
|
while ((pos = stream_get_pos(transport->recv_buffer)) > 0)
|
|
|
|
{
|
|
|
|
stream_set_pos(transport->recv_buffer, 0);
|
2011-08-09 19:28:02 +04:00
|
|
|
if (tpkt_verify_header(transport->recv_buffer)) /* TPKT */
|
2011-08-23 11:36:03 +04:00
|
|
|
{
|
|
|
|
/* Ensure the TPKT header is available. */
|
|
|
|
if (pos <= 4)
|
|
|
|
{
|
|
|
|
stream_set_pos(transport->recv_buffer, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-01 08:43:53 +04:00
|
|
|
length = tpkt_read_header(transport->recv_buffer);
|
2011-08-23 11:36:03 +04:00
|
|
|
}
|
2011-08-01 20:19:39 +04:00
|
|
|
else /* Fast Path */
|
2011-08-23 11:36:03 +04:00
|
|
|
{
|
|
|
|
/* Ensure the Fast Path header is available. */
|
|
|
|
if (pos <= 2)
|
|
|
|
{
|
|
|
|
stream_set_pos(transport->recv_buffer, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-08 14:50:29 +04:00
|
|
|
/* Fastpath header can be two or three bytes long. */
|
|
|
|
length = fastpath_header_length(transport->recv_buffer);
|
|
|
|
if (pos < length)
|
|
|
|
{
|
|
|
|
stream_set_pos(transport->recv_buffer, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-09 19:28:02 +04:00
|
|
|
length = fastpath_read_header(NULL, transport->recv_buffer);
|
2011-08-23 11:36:03 +04:00
|
|
|
}
|
2011-08-01 08:43:53 +04:00
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
{
|
2011-08-08 10:54:46 +04:00
|
|
|
printf("transport_check_fds: protocol error, not a TPKT or Fast Path header.\n");
|
|
|
|
freerdp_hexdump(stream_get_head(transport->recv_buffer), pos);
|
2011-08-01 08:43:53 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos < length)
|
|
|
|
{
|
|
|
|
stream_set_pos(transport->recv_buffer, pos);
|
|
|
|
return 0; /* Packet is not yet completely received. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A complete packet has been received. In case there are trailing data
|
|
|
|
* for the next packet, we copy it to the new receive buffer.
|
|
|
|
*/
|
|
|
|
received = transport->recv_buffer;
|
|
|
|
transport->recv_buffer = stream_new(BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (pos > length)
|
|
|
|
{
|
|
|
|
stream_set_pos(received, length);
|
|
|
|
stream_check_size(transport->recv_buffer, pos - length);
|
|
|
|
stream_copy(transport->recv_buffer, received, pos - length);
|
|
|
|
}
|
|
|
|
|
2011-08-03 05:46:04 +04:00
|
|
|
stream_set_pos(received, length);
|
|
|
|
stream_seal(received);
|
2011-08-01 08:43:53 +04:00
|
|
|
stream_set_pos(received, 0);
|
2012-04-09 21:28:33 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
if (transport->recv_callback(transport, received, transport->recv_extra) == false)
|
2011-10-19 01:47:44 +04:00
|
|
|
status = -1;
|
2012-04-09 21:28:33 +04:00
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
stream_free(received);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2012-03-16 21:12:49 +04:00
|
|
|
|
|
|
|
/* transport might now have been freed by rdp_client_redirect and a new rdp->transport created */
|
|
|
|
transport = *ptransport;
|
2012-04-09 21:28:33 +04:00
|
|
|
|
|
|
|
if (transport->process_single_pdu)
|
|
|
|
{
|
|
|
|
/* one at a time but set event if data buffered
|
|
|
|
* so the main loop will call freerdp_check_fds asap */
|
|
|
|
if (stream_get_pos(transport->recv_buffer) > 0)
|
|
|
|
wait_obj_set(transport->recv_event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
boolean transport_set_blocking_mode(rdpTransport* transport, boolean blocking)
|
|
|
|
{
|
|
|
|
transport->blocking = blocking;
|
2011-09-04 00:36:27 +04:00
|
|
|
return tcp_set_blocking_mode(transport->tcp, blocking);
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
transport = (rdpTransport*) xzalloc(sizeof(rdpTransport));
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
if (transport != NULL)
|
|
|
|
{
|
2011-07-10 20:10:24 +04:00
|
|
|
transport->tcp = tcp_new(settings);
|
2012-03-26 20:20:38 +04:00
|
|
|
transport->tcp_in = tcp_new(settings);
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
transport->settings = settings;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
/* a small 0.1ms delay when transport is blocking. */
|
2011-08-17 00:04:28 +04:00
|
|
|
transport->usleep_interval = 100;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
/* receive buffer for non-blocking read. */
|
|
|
|
transport->recv_buffer = stream_new(BUFFER_SIZE);
|
2011-08-20 16:30:18 +04:00
|
|
|
transport->recv_event = wait_obj_new();
|
2011-07-10 23:34:43 +04:00
|
|
|
|
|
|
|
/* buffers for blocking read/write */
|
|
|
|
transport->recv_stream = stream_new(BUFFER_SIZE);
|
|
|
|
transport->send_stream = stream_new(BUFFER_SIZE);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
transport->blocking = true;
|
2011-09-04 00:36:27 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return transport;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void transport_free(rdpTransport* transport)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
|
|
|
if (transport != NULL)
|
|
|
|
{
|
|
|
|
stream_free(transport->recv_buffer);
|
2011-07-10 23:34:43 +04:00
|
|
|
stream_free(transport->recv_stream);
|
|
|
|
stream_free(transport->send_stream);
|
2011-08-20 16:30:18 +04:00
|
|
|
wait_obj_free(transport->recv_event);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2011-08-23 12:14:32 +04:00
|
|
|
if (transport->tls)
|
|
|
|
tls_free(transport->tls);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
tcp_free(transport->tcp);
|
2012-06-28 11:26:19 +04:00
|
|
|
tcp_free(transport->tcp_in);
|
2012-04-24 00:33:18 +04:00
|
|
|
tsg_free(transport->tsg);
|
2012-03-26 20:20:38 +04:00
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
xfree(transport);
|
|
|
|
}
|
|
|
|
}
|