2011-07-03 01:10:10 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* X.224 Transport Protocol Data Units (TPDUs)
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
#include "tpdu.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TPDUs are defined in:
|
|
|
|
*
|
|
|
|
* http://www.itu.int/rec/T-REC-X.224-199511-I/
|
|
|
|
* X.224: Information technology - Open Systems Interconnection - Protocol for providing the connection-mode transport service
|
|
|
|
*
|
|
|
|
* RDP uses only TPDUs of class 0, the "simple class" defined in section 8 of X.224
|
|
|
|
*
|
|
|
|
* TPDU Header
|
|
|
|
* ____________________ byte
|
|
|
|
* | |
|
|
|
|
* | LI | 1
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | Code | 2
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | | 3
|
|
|
|
* |_______DST-REF______|
|
|
|
|
* | |
|
|
|
|
* | | 4
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | | 5
|
|
|
|
* |_______SRC-REF______|
|
|
|
|
* | |
|
|
|
|
* | | 6
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | Class | 7
|
|
|
|
* |____________________|
|
|
|
|
* | ... |
|
|
|
|
*/
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Read TPDU header.
|
|
|
|
* @param s stream
|
|
|
|
* @param code variable pointer to receive TPDU code
|
|
|
|
* @return TPDU length indicator (LI)
|
|
|
|
*/
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
uint8
|
2011-07-03 21:49:06 +04:00
|
|
|
tpdu_read_header(STREAM* s, uint8* code)
|
2011-07-03 01:10:10 +04:00
|
|
|
{
|
|
|
|
uint8 li;
|
|
|
|
|
|
|
|
stream_read_uint8(s, li); /* LI */
|
2011-07-03 21:49:06 +04:00
|
|
|
stream_read_uint8(s, *code); /* Code */
|
|
|
|
|
2011-07-04 03:27:02 +04:00
|
|
|
if (*code == X224_TPDU_DATA)
|
2011-07-03 01:10:10 +04:00
|
|
|
{
|
|
|
|
/* EOT (1 byte) */
|
|
|
|
stream_seek(s, 1);
|
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* DST-REF (2 bytes) */
|
|
|
|
/* SRC-REF (2 bytes) */
|
|
|
|
/* Class 0 (1 byte) */
|
|
|
|
stream_seek(s, 5);
|
|
|
|
}
|
2011-07-03 01:10:10 +04:00
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
return li;
|
2011-07-03 01:10:10 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write TDPU header.
|
|
|
|
* @param s stream
|
|
|
|
* @param length length
|
|
|
|
* @param code TPDU code
|
|
|
|
*/
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
void
|
|
|
|
tpdu_write_header(STREAM* s, uint16 length, uint8 code)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
stream_write_uint8(s, length); /* LI */
|
2011-07-03 01:10:10 +04:00
|
|
|
stream_write_uint8(s, code); /* code */
|
|
|
|
|
|
|
|
if (code == X224_TPDU_DATA)
|
|
|
|
{
|
|
|
|
stream_write_uint8(s, 0x80); /* EOT */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream_write_uint16(s, 0); /* DST-REF */
|
|
|
|
stream_write_uint16(s, 0); /* SRC-REF */
|
|
|
|
stream_write_uint8(s, 0); /* Class 0 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write Connection Request TPDU.
|
|
|
|
* @param s stream
|
|
|
|
* @param length TPDU length
|
|
|
|
*/
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
void
|
|
|
|
tpdu_write_connection_request(STREAM* s, uint16 length)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
tpdu_write_header(s, length, X224_TPDU_CONNECTION_REQUEST);
|
2011-07-03 01:10:10 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Read Connection Confirm TPDU.
|
|
|
|
* @param s stream
|
|
|
|
* @return length indicator (LI)
|
|
|
|
*/
|
|
|
|
|
2011-07-03 21:49:06 +04:00
|
|
|
uint8
|
|
|
|
tpdu_read_connection_confirm(STREAM* s)
|
|
|
|
{
|
|
|
|
uint8 li;
|
|
|
|
uint8 code;
|
|
|
|
|
|
|
|
li = tpdu_read_header(s, &code);
|
|
|
|
|
|
|
|
if (code != X224_TPDU_CONNECTION_CONFIRM)
|
|
|
|
{
|
|
|
|
printf("Error: expected X224_TPDU_CONNECTION_CONFIRM\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return li;
|
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write Disconnect Request TPDU.
|
|
|
|
* @param s stream
|
|
|
|
* @param length TPDU length
|
|
|
|
*/
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
void
|
|
|
|
tpdu_write_disconnect_request(STREAM* s, uint16 length)
|
|
|
|
{
|
2011-07-03 20:42:35 +04:00
|
|
|
tpdu_write_header(s, length, X224_TPDU_DISCONNECT_REQUEST);
|
2011-07-03 01:10:10 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write Data TPDU.
|
|
|
|
* @param s stream
|
|
|
|
*/
|
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
void
|
2011-07-08 08:37:25 +04:00
|
|
|
tpdu_write_data(STREAM* s)
|
2011-07-03 01:10:10 +04:00
|
|
|
{
|
2011-07-08 08:37:25 +04:00
|
|
|
tpdu_write_header(s, 2, X224_TPDU_DATA);
|
2011-07-03 01:10:10 +04:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read Data TPDU.
|
|
|
|
* @param s stream
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint16
|
|
|
|
tpdu_read_data(STREAM* s)
|
|
|
|
{
|
|
|
|
uint8 code;
|
|
|
|
uint16 li;
|
|
|
|
|
|
|
|
li = tpdu_read_header(s, &code);
|
|
|
|
|
|
|
|
if (code != X224_TPDU_DATA)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return li;
|
|
|
|
}
|