summaryrefslogtreecommitdiffstats
path: root/libfreerdp/core/tpdu.c
blob: 9718cb11fa3fc9fca792a9d0e0e7b4aece41044e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * 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.
 */

#include <freerdp/config.h>

#include <stdio.h>
#include <winpr/print.h>

#include <freerdp/log.h>

#include "tpdu.h"

#define TAG FREERDP_TAG("core")

/**
 * 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
 * |____________________|
 * |         ...        |
 */

static BOOL tpdu_write_header(wStream* s, UINT16 length, BYTE code);

/**
 * Read TPDU header.
 * @param s stream
 * @param code variable pointer to receive TPDU code
 * @return TPDU length indicator (LI)
 */

BOOL tpdu_read_header(wStream* s, BYTE* code, BYTE* li, UINT16 tpktlength)
{
	if (!Stream_CheckAndLogRequiredLength(TAG, s, 3))
		return FALSE;

	Stream_Read_UINT8(s, *li);   /* LI */
	Stream_Read_UINT8(s, *code); /* Code */

	if (*li + 4 > tpktlength)
	{
		WLog_ERR(TAG, "tpdu length %" PRIu8 " > tpkt header length %" PRIu16, *li, tpktlength);
		return FALSE;
	}

	if (*code == X224_TPDU_DATA)
	{
		/* EOT (1 byte) */
		Stream_Seek(s, 1);
	}
	else
	{
		/* DST-REF (2 bytes) */
		/* SRC-REF (2 bytes) */
		/* Class 0 (1 byte) */
		if (!Stream_SafeSeek(s, 5))
		{
			WLog_WARN(TAG, "tpdu invalid data, got %" PRIuz ", require at least 5 more",
			          Stream_GetRemainingLength(s));
			return FALSE;
		}
	}

	return TRUE;
}

/**
 * Write TDPU header.
 * @param s stream
 * @param length length
 * @param code TPDU code
 */

BOOL tpdu_write_header(wStream* s, UINT16 length, BYTE code)
{
	if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 3))
		return FALSE;

	Stream_Write_UINT8(s, length); /* LI */
	Stream_Write_UINT8(s, code);   /* code */

	if (code == X224_TPDU_DATA)
	{
		Stream_Write_UINT8(s, 0x80); /* EOT */
	}
	else
	{
		if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 5))
			return FALSE;
		Stream_Write_UINT16(s, 0); /* DST-REF */
		Stream_Write_UINT16(s, 0); /* SRC-REF */
		Stream_Write_UINT8(s, 0);  /* Class 0 */
	}
	return TRUE;
}

/**
 * Read Connection Request TPDU
 * @param s stream
 * @return length indicator (LI)
 */

BOOL tpdu_read_connection_request(wStream* s, BYTE* li, UINT16 tpktlength)
{
	BYTE code = 0;

	if (!tpdu_read_header(s, &code, li, tpktlength))
		return FALSE;

	if (code != X224_TPDU_CONNECTION_REQUEST)
	{
		WLog_ERR(TAG, "Error: expected X224_TPDU_CONNECTION_REQUEST");
		return FALSE;
	}

	return TRUE;
}

/**
 * Write Connection Request TPDU.
 * @param s stream
 * @param length TPDU length
 */

BOOL tpdu_write_connection_request(wStream* s, UINT16 length)
{
	return tpdu_write_header(s, length, X224_TPDU_CONNECTION_REQUEST);
}

/**
 * Read Connection Confirm TPDU.
 * @param s stream
 * @return length indicator (LI)
 */

BOOL tpdu_read_connection_confirm(wStream* s, BYTE* li, UINT16 tpktlength)
{
	BYTE code = 0;
	size_t position = 0;
	size_t bytes_read = 0;

	/* save the position to determine the number of bytes read */
	position = Stream_GetPosition(s);

	if (!tpdu_read_header(s, &code, li, tpktlength))
		return FALSE;

	if (code != X224_TPDU_CONNECTION_CONFIRM)
	{
		WLog_ERR(TAG, "Error: expected X224_TPDU_CONNECTION_CONFIRM");
		return FALSE;
	}
	/*
	 * To ensure that there are enough bytes remaining for processing
	 * check against the length indicator (li). Already read bytes need
	 * to be taken into account.
	 * The -1 is because li was read but isn't included in the TPDU size.
	 * For reference see ITU-T Rec. X.224 - 13.2.1
	 */
	bytes_read = (Stream_GetPosition(s) - position) - 1;

	if (!Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(*li - bytes_read)))
		return FALSE;
	return TRUE;
}

/**
 * Write Connection Confirm TPDU.
 * @param s stream
 * @param length TPDU length
 */

BOOL tpdu_write_connection_confirm(wStream* s, UINT16 length)
{
	return tpdu_write_header(s, length, X224_TPDU_CONNECTION_CONFIRM);
}

/**
 * Write Disconnect Request TPDU.
 * @param s stream
 * @param length TPDU length
 */

BOOL tpdu_write_disconnect_request(wStream* s, UINT16 length)
{
	return tpdu_write_header(s, length, X224_TPDU_DISCONNECT_REQUEST);
}

/**
 * Write Data TPDU.
 * @param s stream
 */

BOOL tpdu_write_data(wStream* s)
{
	return tpdu_write_header(s, 2, X224_TPDU_DATA);
}

/**
 * Read Data TPDU.
 * @param s stream
 */

BOOL tpdu_read_data(wStream* s, UINT16* LI, UINT16 tpktlength)
{
	BYTE code = 0;
	BYTE li = 0;

	if (!tpdu_read_header(s, &code, &li, tpktlength))
		return FALSE;

	if (code != X224_TPDU_DATA)
	{
		WLog_ERR(TAG, "tpdu got code 0x%02" PRIx8 " expected X224_TPDU_DATA [0x%02x]", code,
		         X224_TPDU_DATA);
		return FALSE;
	}

	*LI = li;

	return TRUE;
}

const char* tpdu_type_to_string(int type)
{
	switch (type)
	{
		case X224_TPDU_CONNECTION_REQUEST:
			return "X224_TPDU_CONNECTION_REQUEST";
		case X224_TPDU_CONNECTION_CONFIRM:
			return "X224_TPDU_CONNECTION_CONFIRM";
		case X224_TPDU_DISCONNECT_REQUEST:
			return "X224_TPDU_DISCONNECT_REQUEST";
		case X224_TPDU_DATA:
			return "X224_TPDU_DATA";
		case X224_TPDU_ERROR:
			return "X224_TPDU_ERROR";
		default:
			return "X224_TPDU_UNKNOWN";
	}
}