20 #include <freerdp/config.h>
23 #include <winpr/print.h>
25 #include <freerdp/log.h>
29 #define TAG FREERDP_TAG("core")
66 static BOOL tpdu_write_header(
wStream* s, UINT16 length, BYTE code);
75 BOOL tpdu_read_header(
wStream* s, BYTE* code, BYTE* li, UINT16 tpktlength)
77 if (!Stream_CheckAndLogRequiredLength(TAG, s, 3))
80 Stream_Read_UINT8(s, *li);
81 Stream_Read_UINT8(s, *code);
83 if (*li + 4 > tpktlength)
85 WLog_ERR(TAG,
"tpdu length %" PRIu8
" > tpkt header length %" PRIu16, *li, tpktlength);
89 if (*code == X224_TPDU_DATA)
99 if (!Stream_SafeSeek(s, 5))
101 WLog_WARN(TAG,
"tpdu invalid data, got %" PRIuz
", require at least 5 more",
102 Stream_GetRemainingLength(s));
117 BOOL tpdu_write_header(
wStream* s, UINT16 length, BYTE code)
119 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 3))
122 WINPR_ASSERT(length <= UINT8_MAX);
123 Stream_Write_UINT8(s, (UINT8)length);
124 Stream_Write_UINT8(s, code);
126 if (code == X224_TPDU_DATA)
128 Stream_Write_UINT8(s, 0x80);
132 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 5))
134 Stream_Write_UINT16(s, 0);
135 Stream_Write_UINT16(s, 0);
136 Stream_Write_UINT8(s, 0);
147 BOOL tpdu_read_connection_request(
wStream* s, BYTE* li, UINT16 tpktlength)
151 if (!tpdu_read_header(s, &code, li, tpktlength))
154 if (code != X224_TPDU_CONNECTION_REQUEST)
156 WLog_ERR(TAG,
"Error: expected X224_TPDU_CONNECTION_REQUEST");
169 BOOL tpdu_write_connection_request(
wStream* s, UINT16 length)
171 return tpdu_write_header(s, length, X224_TPDU_CONNECTION_REQUEST);
180 BOOL tpdu_read_connection_confirm(
wStream* s, BYTE* li, UINT16 tpktlength)
184 size_t bytes_read = 0;
187 position = Stream_GetPosition(s);
189 if (!tpdu_read_header(s, &code, li, tpktlength))
192 if (code != X224_TPDU_CONNECTION_CONFIRM)
194 WLog_ERR(TAG,
"Error: expected X224_TPDU_CONNECTION_CONFIRM");
204 bytes_read = (Stream_GetPosition(s) - position) - 1;
206 if (!Stream_CheckAndLogRequiredLength(TAG, s, (
size_t)(*li - bytes_read)))
217 BOOL tpdu_write_connection_confirm(
wStream* s, UINT16 length)
219 return tpdu_write_header(s, length, X224_TPDU_CONNECTION_CONFIRM);
228 BOOL tpdu_write_disconnect_request(
wStream* s, UINT16 length)
230 return tpdu_write_header(s, length, X224_TPDU_DISCONNECT_REQUEST);
238 BOOL tpdu_write_data(
wStream* s)
240 return tpdu_write_header(s, 2, X224_TPDU_DATA);
248 BOOL tpdu_read_data(
wStream* s, UINT16* LI, UINT16 tpktlength)
253 if (!tpdu_read_header(s, &code, &li, tpktlength))
256 if (code != X224_TPDU_DATA)
258 WLog_ERR(TAG,
"tpdu got code 0x%02" PRIx8
" expected X224_TPDU_DATA [0x%02x]", code,
268 const char* tpdu_type_to_string(
int type)
272 case X224_TPDU_CONNECTION_REQUEST:
273 return "X224_TPDU_CONNECTION_REQUEST";
274 case X224_TPDU_CONNECTION_CONFIRM:
275 return "X224_TPDU_CONNECTION_CONFIRM";
276 case X224_TPDU_DISCONNECT_REQUEST:
277 return "X224_TPDU_DISCONNECT_REQUEST";
279 return "X224_TPDU_DATA";
280 case X224_TPDU_ERROR:
281 return "X224_TPDU_ERROR";
283 return "X224_TPDU_UNKNOWN";