/src/FreeRDP/libfreerdp/core/tpdu.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * X.224 Transport Protocol Data Units (TPDUs) |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <freerdp/config.h> |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <winpr/print.h> |
24 | | |
25 | | #include <freerdp/log.h> |
26 | | |
27 | | #include "tpdu.h" |
28 | | |
29 | | #define TAG FREERDP_TAG("core") |
30 | | |
31 | | /** |
32 | | * TPDUs are defined in: |
33 | | * |
34 | | * http://www.itu.int/rec/T-REC-X.224-199511-I/ |
35 | | * X.224: Information technology - Open Systems Interconnection - Protocol for providing the |
36 | | * connection-mode transport service |
37 | | * |
38 | | * RDP uses only TPDUs of class 0, the "simple class" defined in section 8 of X.224 |
39 | | * |
40 | | * TPDU Header |
41 | | * ____________________ byte |
42 | | * | | |
43 | | * | LI | 1 |
44 | | * |____________________| |
45 | | * | | |
46 | | * | Code | 2 |
47 | | * |____________________| |
48 | | * | | |
49 | | * | | 3 |
50 | | * |_______DST-REF______| |
51 | | * | | |
52 | | * | | 4 |
53 | | * |____________________| |
54 | | * | | |
55 | | * | | 5 |
56 | | * |_______SRC-REF______| |
57 | | * | | |
58 | | * | | 6 |
59 | | * |____________________| |
60 | | * | | |
61 | | * | Class | 7 |
62 | | * |____________________| |
63 | | * | ... | |
64 | | */ |
65 | | |
66 | | static BOOL tpdu_write_header(wStream* s, UINT16 length, BYTE code); |
67 | | |
68 | | /** |
69 | | * Read TPDU header. |
70 | | * @param s stream |
71 | | * @param code variable pointer to receive TPDU code |
72 | | * @return TPDU length indicator (LI) |
73 | | */ |
74 | | |
75 | | BOOL tpdu_read_header(wStream* s, BYTE* code, BYTE* li, UINT16 tpktlength) |
76 | 56.8k | { |
77 | 56.8k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 3)) |
78 | 2.23k | return FALSE; |
79 | | |
80 | 54.6k | Stream_Read_UINT8(s, *li); /* LI */ |
81 | 54.6k | Stream_Read_UINT8(s, *code); /* Code */ |
82 | | |
83 | 54.6k | if (*li + 4 > tpktlength) |
84 | 47.1k | { |
85 | 47.1k | WLog_ERR(TAG, "tpdu length %" PRIu8 " > tpkt header length %" PRIu16, *li, tpktlength); |
86 | 47.1k | return FALSE; |
87 | 47.1k | } |
88 | | |
89 | 7.50k | if (*code == X224_TPDU_DATA) |
90 | 7.01k | { |
91 | | /* EOT (1 byte) */ |
92 | 7.01k | Stream_Seek(s, 1); |
93 | 7.01k | } |
94 | 495 | else |
95 | 495 | { |
96 | | /* DST-REF (2 bytes) */ |
97 | | /* SRC-REF (2 bytes) */ |
98 | | /* Class 0 (1 byte) */ |
99 | 495 | if (!Stream_SafeSeek(s, 5)) |
100 | 38 | { |
101 | 38 | WLog_WARN(TAG, "tpdu invalid data, got %" PRIuz ", require at least 5 more", |
102 | 38 | Stream_GetRemainingLength(s)); |
103 | 38 | return FALSE; |
104 | 38 | } |
105 | 495 | } |
106 | | |
107 | 7.46k | return TRUE; |
108 | 7.50k | } |
109 | | |
110 | | /** |
111 | | * Write TDPU header. |
112 | | * @param s stream |
113 | | * @param length length |
114 | | * @param code TPDU code |
115 | | */ |
116 | | |
117 | | BOOL tpdu_write_header(wStream* s, UINT16 length, BYTE code) |
118 | 2.70k | { |
119 | 2.70k | if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 3)) |
120 | 0 | return FALSE; |
121 | | |
122 | 2.70k | WINPR_ASSERT(length <= UINT8_MAX); |
123 | 2.70k | Stream_Write_UINT8(s, (UINT8)length); /* LI */ |
124 | 2.70k | Stream_Write_UINT8(s, code); /* code */ |
125 | | |
126 | 2.70k | if (code == X224_TPDU_DATA) |
127 | 2.70k | { |
128 | 2.70k | Stream_Write_UINT8(s, 0x80); /* EOT */ |
129 | 2.70k | } |
130 | 0 | else |
131 | 0 | { |
132 | 0 | if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 5)) |
133 | 0 | return FALSE; |
134 | 0 | Stream_Write_UINT16(s, 0); /* DST-REF */ |
135 | 0 | Stream_Write_UINT16(s, 0); /* SRC-REF */ |
136 | 0 | Stream_Write_UINT8(s, 0); /* Class 0 */ |
137 | 0 | } |
138 | 2.70k | return TRUE; |
139 | 2.70k | } |
140 | | |
141 | | /** |
142 | | * Read Connection Request TPDU |
143 | | * @param s stream |
144 | | * @return length indicator (LI) |
145 | | */ |
146 | | |
147 | | BOOL tpdu_read_connection_request(wStream* s, BYTE* li, UINT16 tpktlength) |
148 | 0 | { |
149 | 0 | BYTE code = 0; |
150 | |
|
151 | 0 | if (!tpdu_read_header(s, &code, li, tpktlength)) |
152 | 0 | return FALSE; |
153 | | |
154 | 0 | if (code != X224_TPDU_CONNECTION_REQUEST) |
155 | 0 | { |
156 | 0 | WLog_ERR(TAG, "Error: expected X224_TPDU_CONNECTION_REQUEST"); |
157 | 0 | return FALSE; |
158 | 0 | } |
159 | | |
160 | 0 | return TRUE; |
161 | 0 | } |
162 | | |
163 | | /** |
164 | | * Write Connection Request TPDU. |
165 | | * @param s stream |
166 | | * @param length TPDU length |
167 | | */ |
168 | | |
169 | | BOOL tpdu_write_connection_request(wStream* s, UINT16 length) |
170 | 0 | { |
171 | 0 | return tpdu_write_header(s, length, X224_TPDU_CONNECTION_REQUEST); |
172 | 0 | } |
173 | | |
174 | | /** |
175 | | * Read Connection Confirm TPDU. |
176 | | * @param s stream |
177 | | * @return length indicator (LI) |
178 | | */ |
179 | | |
180 | | BOOL tpdu_read_connection_confirm(wStream* s, BYTE* li, UINT16 tpktlength) |
181 | 0 | { |
182 | 0 | BYTE code = 0; |
183 | 0 | size_t position = 0; |
184 | 0 | size_t bytes_read = 0; |
185 | | |
186 | | /* save the position to determine the number of bytes read */ |
187 | 0 | position = Stream_GetPosition(s); |
188 | |
|
189 | 0 | if (!tpdu_read_header(s, &code, li, tpktlength)) |
190 | 0 | return FALSE; |
191 | | |
192 | 0 | if (code != X224_TPDU_CONNECTION_CONFIRM) |
193 | 0 | { |
194 | 0 | WLog_ERR(TAG, "Error: expected X224_TPDU_CONNECTION_CONFIRM"); |
195 | 0 | return FALSE; |
196 | 0 | } |
197 | | /* |
198 | | * To ensure that there are enough bytes remaining for processing |
199 | | * check against the length indicator (li). Already read bytes need |
200 | | * to be taken into account. |
201 | | * The -1 is because li was read but isn't included in the TPDU size. |
202 | | * For reference see ITU-T Rec. X.224 - 13.2.1 |
203 | | */ |
204 | 0 | bytes_read = (Stream_GetPosition(s) - position) - 1; |
205 | |
|
206 | 0 | return Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(*li - bytes_read)); |
207 | 0 | } |
208 | | |
209 | | /** |
210 | | * Write Connection Confirm TPDU. |
211 | | * @param s stream |
212 | | * @param length TPDU length |
213 | | */ |
214 | | |
215 | | BOOL tpdu_write_connection_confirm(wStream* s, UINT16 length) |
216 | 0 | { |
217 | 0 | return tpdu_write_header(s, length, X224_TPDU_CONNECTION_CONFIRM); |
218 | 0 | } |
219 | | |
220 | | /** |
221 | | * Write Disconnect Request TPDU. |
222 | | * @param s stream |
223 | | * @param length TPDU length |
224 | | */ |
225 | | |
226 | | BOOL tpdu_write_disconnect_request(wStream* s, UINT16 length) |
227 | 0 | { |
228 | 0 | return tpdu_write_header(s, length, X224_TPDU_DISCONNECT_REQUEST); |
229 | 0 | } |
230 | | |
231 | | /** |
232 | | * Write Data TPDU. |
233 | | * @param s stream |
234 | | */ |
235 | | |
236 | | BOOL tpdu_write_data(wStream* s) |
237 | 2.70k | { |
238 | 2.70k | return tpdu_write_header(s, 2, X224_TPDU_DATA); |
239 | 2.70k | } |
240 | | |
241 | | /** |
242 | | * Read Data TPDU. |
243 | | * @param s stream |
244 | | */ |
245 | | |
246 | | BOOL tpdu_read_data(wStream* s, UINT16* LI, UINT16 tpktlength) |
247 | 30.1k | { |
248 | 30.1k | BYTE code = 0; |
249 | 30.1k | BYTE li = 0; |
250 | | |
251 | 30.1k | if (!tpdu_read_header(s, &code, &li, tpktlength)) |
252 | 23.8k | return FALSE; |
253 | | |
254 | 6.29k | if (code != X224_TPDU_DATA) |
255 | 198 | { |
256 | 198 | const BYTE dCode = X224_TPDU_DATA; |
257 | 198 | WLog_ERR(TAG, "tpdu got code 0x%02" PRIx8 " expected X224_TPDU_DATA [0x%02" PRIx8 "]", code, |
258 | 198 | dCode); |
259 | 198 | return FALSE; |
260 | 198 | } |
261 | | |
262 | 6.10k | *LI = li; |
263 | | |
264 | 6.10k | return TRUE; |
265 | 6.29k | } |
266 | | |
267 | | const char* tpdu_type_to_string(int type) |
268 | 196 | { |
269 | 196 | switch (type) |
270 | 196 | { |
271 | 1 | case X224_TPDU_CONNECTION_REQUEST: |
272 | 1 | return "X224_TPDU_CONNECTION_REQUEST"; |
273 | 7 | case X224_TPDU_CONNECTION_CONFIRM: |
274 | 7 | return "X224_TPDU_CONNECTION_CONFIRM"; |
275 | 0 | case X224_TPDU_DISCONNECT_REQUEST: |
276 | 0 | return "X224_TPDU_DISCONNECT_REQUEST"; |
277 | 98 | case X224_TPDU_DATA: |
278 | 98 | return "X224_TPDU_DATA"; |
279 | 0 | case X224_TPDU_ERROR: |
280 | 0 | return "X224_TPDU_ERROR"; |
281 | 90 | default: |
282 | 90 | return "X224_TPDU_UNKNOWN"; |
283 | 196 | } |
284 | 196 | } |