2011-08-18 12:06:32 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol client.
|
|
|
|
* RDP Server Peer
|
|
|
|
*
|
|
|
|
* 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 "peer.h"
|
|
|
|
|
|
|
|
static boolean freerdp_peer_initialize(freerdp_peer* client)
|
|
|
|
{
|
2011-08-18 19:15:28 +04:00
|
|
|
rdpPeer* peer = (rdpPeer*)client->peer;
|
|
|
|
|
2011-08-21 05:58:05 +04:00
|
|
|
peer->rdp->settings->server_mode = True;
|
2011-08-19 05:54:43 +04:00
|
|
|
peer->rdp->state = CONNECTION_STATE_INITIAL;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2011-08-18 12:06:32 +04:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
|
|
|
|
{
|
|
|
|
rdpPeer* peer = (rdpPeer*)client->peer;
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
rfds[*rcount] = (void*)(long)(peer->rdp->transport->tcp->sockfd);
|
2011-08-18 12:06:32 +04:00
|
|
|
(*rcount)++;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean freerdp_peer_check_fds(freerdp_peer* client)
|
|
|
|
{
|
2011-08-18 19:15:28 +04:00
|
|
|
rdpPeer* peer = (rdpPeer*)client->peer;
|
|
|
|
rdpRdp* rdp;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
rdp = (rdpRdp*) peer->rdp;
|
|
|
|
|
|
|
|
status = rdp_check_fds(rdp);
|
|
|
|
if (status < 0)
|
|
|
|
return False;
|
|
|
|
|
2011-08-18 12:06:32 +04:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-08-23 08:58:10 +04:00
|
|
|
static boolean peer_recv_data_pdu(rdpPeer* peer, STREAM* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
|
|
|
uint8 type;
|
|
|
|
uint16 length;
|
|
|
|
uint32 share_id;
|
2011-09-08 09:17:58 +04:00
|
|
|
uint8 compressed_type;
|
|
|
|
uint16 compressed_len;
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2011-09-08 09:17:58 +04:00
|
|
|
|
|
|
|
if (!rdp_read_share_data_header(s, &length, &type, &share_id, &compressed_type, &compressed_len))
|
2011-08-21 18:52:37 +04:00
|
|
|
return False;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case DATA_PDU_TYPE_SYNCHRONIZE:
|
2011-08-23 08:58:10 +04:00
|
|
|
if (!rdp_recv_client_synchronize_pdu(s))
|
2011-08-21 18:52:37 +04:00
|
|
|
return False;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_CONTROL:
|
2011-08-21 19:20:37 +04:00
|
|
|
if (!rdp_server_accept_client_control_pdu(peer->rdp, s))
|
|
|
|
return False;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST:
|
|
|
|
/* TODO: notify server bitmap cache data */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_FONT_LIST:
|
|
|
|
if (!rdp_server_accept_client_font_list_pdu(peer->rdp, s))
|
2011-08-21 18:52:37 +04:00
|
|
|
return False;
|
2011-08-22 19:02:56 +04:00
|
|
|
if (peer->client->PostConnect)
|
|
|
|
{
|
|
|
|
if (!peer->client->PostConnect(peer->client))
|
|
|
|
return False;
|
2011-09-06 14:28:03 +04:00
|
|
|
/**
|
|
|
|
* PostConnect should only be called once and should not be called
|
|
|
|
* after a reactivation sequence.
|
|
|
|
*/
|
|
|
|
peer->client->PostConnect = NULL;
|
2011-08-22 19:02:56 +04:00
|
|
|
}
|
2011-09-06 14:48:17 +04:00
|
|
|
if (peer->client->Activate)
|
|
|
|
{
|
|
|
|
/* Activate will be called everytime after the client is activated/reactivated. */
|
|
|
|
if (!peer->client->Activate(peer->client))
|
|
|
|
return False;
|
|
|
|
}
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-26 14:14:34 +04:00
|
|
|
case DATA_PDU_TYPE_SHUTDOWN_REQUEST:
|
|
|
|
mcs_send_disconnect_provider_ultimatum(peer->rdp->mcs);
|
|
|
|
return False;
|
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
default:
|
|
|
|
printf("Data PDU type %d\n", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-08-23 08:58:10 +04:00
|
|
|
static boolean peer_recv_tpkt_pdu(rdpPeer* peer, STREAM* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
|
|
|
uint16 length;
|
|
|
|
uint16 pduType;
|
|
|
|
uint16 pduLength;
|
|
|
|
uint16 channelId;
|
|
|
|
|
|
|
|
if (!rdp_read_header(peer->rdp, s, &length, &channelId))
|
|
|
|
{
|
|
|
|
printf("Incorrect RDP header.\n");
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelId != MCS_GLOBAL_CHANNEL_ID)
|
|
|
|
{
|
|
|
|
/* TODO: process channel data from client */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!rdp_read_share_control_header(s, &pduLength, &pduType, &peer->rdp->settings->pdu_source))
|
|
|
|
return False;
|
|
|
|
|
|
|
|
switch (pduType)
|
|
|
|
{
|
|
|
|
case PDU_TYPE_DATA:
|
2011-08-23 08:58:10 +04:00
|
|
|
if (!peer_recv_data_pdu(peer, s))
|
2011-08-21 18:52:37 +04:00
|
|
|
return False;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Client sent pduType %d\n", pduType);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2011-08-23 08:58:10 +04:00
|
|
|
static boolean peer_recv_fastpath_pdu(rdpPeer* peer, STREAM* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2011-08-23 11:51:51 +04:00
|
|
|
uint16 length;
|
2011-09-27 09:30:58 +04:00
|
|
|
rdpRdp* rdp;
|
|
|
|
rdpFastPath* fastpath;
|
|
|
|
|
|
|
|
rdp = peer->rdp;
|
|
|
|
fastpath = rdp->fastpath;
|
|
|
|
length = fastpath_read_header_rdp(fastpath, s);
|
2011-08-23 11:51:51 +04:00
|
|
|
|
2011-09-15 01:14:50 +04:00
|
|
|
if (length == 0 || length > stream_get_left(s))
|
2011-08-23 11:51:51 +04:00
|
|
|
{
|
|
|
|
printf("incorrect FastPath PDU header length %d\n", length);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
if (fastpath->encryptionFlags & FASTPATH_OUTPUT_ENCRYPTED)
|
2011-09-15 01:14:50 +04:00
|
|
|
{
|
2011-09-27 09:30:58 +04:00
|
|
|
rdp_decrypt(rdp, s, length);
|
2011-09-15 01:14:50 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
return fastpath_recv_inputs(fastpath, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2011-08-23 08:58:10 +04:00
|
|
|
static boolean peer_recv_pdu(rdpPeer* peer, STREAM* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
|
|
|
if (tpkt_verify_header(s))
|
2011-08-23 08:58:10 +04:00
|
|
|
return peer_recv_tpkt_pdu(peer, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
else
|
2011-08-23 08:58:10 +04:00
|
|
|
return peer_recv_fastpath_pdu(peer, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2011-09-25 22:54:07 +04:00
|
|
|
static boolean peer_recv_callback(rdpTransport* transport, STREAM* s, void* extra)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
2011-09-25 22:54:07 +04:00
|
|
|
rdpPeer* peer = (rdpPeer*) extra;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2011-08-19 05:54:43 +04:00
|
|
|
switch (peer->rdp->state)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
|
|
|
case CONNECTION_STATE_INITIAL:
|
2011-08-19 05:54:43 +04:00
|
|
|
if (!rdp_server_accept_nego(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-19 13:39:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_NEGO:
|
|
|
|
if (!rdp_server_accept_mcs_connect_initial(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-19 22:03:48 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_CONNECT:
|
|
|
|
if (!rdp_server_accept_mcs_erect_domain_request(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-19 22:03:48 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_ERECT_DOMAIN:
|
|
|
|
if (!rdp_server_accept_mcs_attach_user_request(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-20 10:05:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_ATTACH_USER:
|
|
|
|
if (!rdp_server_accept_mcs_channel_join_request(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-20 14:22:14 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
case CONNECTION_STATE_MCS_CHANNEL_JOIN:
|
2011-08-20 14:22:14 +04:00
|
|
|
if (!rdp_server_accept_client_info(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-21 11:52:44 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_LICENSE:
|
|
|
|
if (!rdp_server_accept_confirm_active(peer->rdp, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_ACTIVE:
|
2011-08-23 08:58:10 +04:00
|
|
|
if (!peer_recv_pdu(peer, s))
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-19 05:54:43 +04:00
|
|
|
break;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
|
|
|
default:
|
2011-08-19 05:54:43 +04:00
|
|
|
printf("Invalid state %d\n", peer->rdp->state);
|
2011-09-25 22:54:07 +04:00
|
|
|
return False;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2011-09-25 22:54:07 +04:00
|
|
|
return True;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 12:06:32 +04:00
|
|
|
static void freerdp_peer_disconnect(freerdp_peer* client)
|
|
|
|
{
|
2011-09-25 22:54:07 +04:00
|
|
|
rdpPeer* peer = (rdpPeer*) client->peer;
|
2011-08-23 12:14:32 +04:00
|
|
|
|
|
|
|
transport_disconnect(peer->rdp->transport);
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_peer* freerdp_peer_new(int sockfd)
|
|
|
|
{
|
|
|
|
freerdp_peer* client;
|
|
|
|
rdpPeer* peer;
|
|
|
|
|
|
|
|
client = xnew(freerdp_peer);
|
|
|
|
|
|
|
|
client->Initialize = freerdp_peer_initialize;
|
|
|
|
client->GetFileDescriptor = freerdp_peer_get_fds;
|
|
|
|
client->CheckFileDescriptor = freerdp_peer_check_fds;
|
|
|
|
client->Disconnect = freerdp_peer_disconnect;
|
|
|
|
|
|
|
|
peer = xnew(rdpPeer);
|
|
|
|
peer->client = client;
|
2011-08-18 19:15:28 +04:00
|
|
|
peer->rdp = rdp_new(NULL);
|
2011-08-18 12:06:32 +04:00
|
|
|
|
2011-09-25 22:54:07 +04:00
|
|
|
client->peer = (void*) peer;
|
2011-08-18 19:15:28 +04:00
|
|
|
client->settings = peer->rdp->settings;
|
2011-08-23 07:50:41 +04:00
|
|
|
client->input = peer->rdp->input;
|
|
|
|
client->update = peer->rdp->update;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2011-08-23 16:37:08 +04:00
|
|
|
update_register_server_callbacks(client->update);
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
transport_attach(peer->rdp->transport, sockfd);
|
|
|
|
|
|
|
|
peer->rdp->transport->recv_callback = peer_recv_callback;
|
|
|
|
peer->rdp->transport->recv_extra = peer;
|
|
|
|
transport_set_blocking_mode(peer->rdp->transport, False);
|
2011-08-18 12:06:32 +04:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_peer_free(freerdp_peer* client)
|
|
|
|
{
|
|
|
|
rdpPeer* peer = (rdpPeer*)client->peer;
|
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
rdp_free(peer->rdp);
|
2011-08-18 12:06:32 +04:00
|
|
|
xfree(peer);
|
|
|
|
xfree(client);
|
|
|
|
}
|
|
|
|
|