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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
|
2011-07-03 12:37:36 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2011-07-03 11:53:55 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#include "tpkt.h"
|
2011-07-03 06:59:07 +04:00
|
|
|
#include "transport.h"
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#define BUFFER_SIZE 16384
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
FRDP_BOOL
|
2011-07-03 06:59:07 +04:00
|
|
|
transport_connect(rdpTransport * transport, const char * server, int port)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-03 23:34:15 +04:00
|
|
|
return transport->tcp->connect(transport->tcp, server, port);
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
FRDP_BOOL
|
2011-07-03 06:59:07 +04:00
|
|
|
transport_disconnect(rdpTransport * transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-03 23:34:15 +04:00
|
|
|
return transport->tcp->disconnect(transport->tcp);
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-04 03:27:02 +04:00
|
|
|
FRDP_BOOL
|
|
|
|
transport_connect_rdp(rdpTransport * transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-04 03:27:02 +04:00
|
|
|
transport->state = TRANSPORT_STATE_RDP;
|
|
|
|
|
|
|
|
/* RDP encryption */
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
FRDP_BOOL
|
|
|
|
transport_connect_tls(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
if (transport->tls == NULL)
|
|
|
|
transport->tls = tls_new();
|
|
|
|
|
|
|
|
transport->state = TRANSPORT_STATE_TLS;
|
|
|
|
|
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
|
|
|
if (tls_connect(transport->tls) != True)
|
|
|
|
return False;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
FRDP_BOOL
|
|
|
|
transport_connect_nla(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
if (transport->tls == NULL)
|
|
|
|
transport->tls = tls_new();
|
|
|
|
|
|
|
|
transport->state = TRANSPORT_STATE_NLA;
|
|
|
|
|
|
|
|
transport->tls->sockfd = transport->tcp->sockfd;
|
|
|
|
|
|
|
|
if (tls_connect(transport->tls) != True)
|
|
|
|
return False;
|
|
|
|
|
|
|
|
/* Network Level Authentication */
|
|
|
|
|
|
|
|
return True;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 12:37:36 +04:00
|
|
|
static int
|
|
|
|
transport_delay(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
nanosleep(&transport->ts, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
transport_send_tls(rdpTransport * transport, STREAM * stream)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
transport_send_tcp(rdpTransport * transport, STREAM * stream)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
int bytes;
|
2011-07-03 12:37:36 +04:00
|
|
|
uint8 * head;
|
|
|
|
uint8 * tail;
|
|
|
|
|
|
|
|
head = stream_get_head(stream);
|
|
|
|
tail = stream_get_tail(stream);
|
2011-07-03 20:42:35 +04:00
|
|
|
|
2011-07-03 12:37:36 +04:00
|
|
|
while (head < tail)
|
|
|
|
{
|
2011-07-03 23:34:15 +04:00
|
|
|
bytes = send(transport->tcp->sockfd, head, tail - head, MSG_NOSIGNAL);
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
if (bytes < 0)
|
2011-07-03 12:37:36 +04:00
|
|
|
{
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
if (transport_delay(transport) != 0)
|
|
|
|
return -1;
|
2011-07-03 20:42:35 +04:00
|
|
|
|
2011-07-03 12:37:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
perror("send");
|
2011-07-03 12:37:36 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-07-03 20:42:35 +04:00
|
|
|
head += bytes;
|
2011-07-03 12:37:36 +04:00
|
|
|
}
|
2011-07-03 20:42:35 +04:00
|
|
|
|
2011-07-03 12:37:36 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-03 05:50:11 +04:00
|
|
|
int
|
2011-07-03 06:59:07 +04:00
|
|
|
transport_send(rdpTransport * transport, STREAM * stream)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-04 03:27:02 +04:00
|
|
|
if (transport->state == TRANSPORT_STATE_TLS)
|
2011-07-03 12:37:36 +04:00
|
|
|
return transport_send_tls(transport, stream);
|
|
|
|
else
|
|
|
|
return transport_send_tcp(transport, stream);
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
static int
|
|
|
|
transport_recv_tls(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
transport_recv_tcp(rdpTransport * transport)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
int bytes;
|
2011-07-03 14:30:43 +04:00
|
|
|
|
|
|
|
stream_check_capacity(transport->recv_buffer, BUFFER_SIZE);
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
bytes = recv(transport->tcp->sockfd, transport->recv_buffer->ptr, BUFFER_SIZE, 0);
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
if (bytes == -1)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
perror("recv");
|
2011-07-03 14:30:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
stream_seek(transport->recv_buffer, bytes);
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
return bytes;
|
2011-07-03 14:30:43 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 05:50:11 +04:00
|
|
|
int
|
2011-07-03 06:59:07 +04:00
|
|
|
transport_check_fds(rdpTransport * transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2011-07-03 14:30:43 +04:00
|
|
|
int pos;
|
2011-07-03 20:42:35 +04:00
|
|
|
int bytes;
|
|
|
|
uint16 length;
|
2011-07-03 14:30:43 +04:00
|
|
|
STREAM * received;
|
|
|
|
|
2011-07-04 03:27:02 +04:00
|
|
|
if (transport->state == TRANSPORT_STATE_TLS)
|
2011-07-03 20:42:35 +04:00
|
|
|
bytes = transport_recv_tls(transport);
|
2011-07-03 14:30:43 +04:00
|
|
|
else
|
2011-07-03 20:42:35 +04:00
|
|
|
bytes = transport_recv_tcp(transport);
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
if (bytes <= 0)
|
|
|
|
return bytes;
|
2011-07-03 14:30:43 +04:00
|
|
|
|
|
|
|
pos = stream_get_pos(transport->recv_buffer);
|
2011-07-03 20:42:35 +04:00
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
/* Ensure the TPKT header is available. */
|
|
|
|
if (pos <= 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream_set_pos(transport->recv_buffer, 0);
|
2011-07-03 20:42:35 +04:00
|
|
|
length = tpkt_read_header(transport->recv_buffer);
|
|
|
|
|
|
|
|
if (length == 0)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
|
|
|
printf("transport_check_fds: protocol error, not a TPKT header.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
if (pos < length)
|
2011-07-03 14:30:43 +04:00
|
|
|
return 0; /* Packet is not yet completely received. */
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
/*
|
|
|
|
* A complete packet has been received. In case there are trailing data
|
2011-07-03 14:30:43 +04:00
|
|
|
* for the next packet, we copy it to the new receive buffer.
|
|
|
|
*/
|
|
|
|
received = transport->recv_buffer;
|
|
|
|
transport->recv_buffer = stream_new(BUFFER_SIZE);
|
2011-07-03 20:42:35 +04:00
|
|
|
|
|
|
|
if (pos > length)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_set_pos(received, length);
|
|
|
|
stream_check_capacity(transport->recv_buffer, pos - length);
|
|
|
|
stream_copy(transport->recv_buffer, received, pos - length);
|
2011-07-03 14:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream_set_pos(received, 0);
|
2011-07-03 21:49:06 +04:00
|
|
|
bytes = transport->recv_callback(transport, received, transport->recv_extra);
|
2011-07-03 14:30:43 +04:00
|
|
|
stream_free(received);
|
|
|
|
|
2011-07-03 20:42:35 +04:00
|
|
|
return bytes;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
transport_init(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
transport->state = TRANSPORT_STATE_NEGO;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpTransport *
|
|
|
|
transport_new(void)
|
|
|
|
{
|
|
|
|
rdpTransport * transport;
|
|
|
|
|
|
|
|
transport = (rdpTransport *) xzalloc(sizeof(rdpTransport));
|
|
|
|
|
|
|
|
if (transport != NULL)
|
|
|
|
{
|
|
|
|
transport->tcp = tcp_new();
|
|
|
|
|
|
|
|
/* a small 0.1ms delay when transport is blocking. */
|
|
|
|
transport->ts.tv_sec = 0;
|
|
|
|
transport->ts.tv_nsec = 100000;
|
|
|
|
|
|
|
|
/* receive buffer for non-blocking read. */
|
|
|
|
transport->recv_buffer = stream_new(BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
transport_free(rdpTransport * transport)
|
|
|
|
{
|
|
|
|
if (transport != NULL)
|
|
|
|
{
|
|
|
|
stream_free(transport->recv_buffer);
|
|
|
|
tcp_free(transport->tcp);
|
|
|
|
xfree(transport);
|
|
|
|
}
|
|
|
|
}
|