File: tpkt.c

package info (click to toggle)
freerdp2 2.0.0~git20190204.1.2693389a%2Bdfsg1-1%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,348 kB
  • sloc: ansic: 308,081; xml: 1,676; sh: 770; perl: 231; makefile: 158; python: 65
file content (127 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download
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
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Transport Packets (TPKTs)
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "tpdu.h"

#include "tpkt.h"

/**
 * TPKTs are defined in:
 *
 * http://tools.ietf.org/html/rfc1006/
 * RFC 1006 - ISO Transport Service on top of the TCP
 *
 * http://www.itu.int/rec/T-REC-T.123/
 * ITU-T T.123 (01/2007) - Network-specific data protocol stacks for multimedia conferencing
 *
 *       TPKT Header
 *  ____________________   byte
 * |                    |
 * |     3 (version)    |   1
 * |____________________|
 * |                    |
 * |      Reserved      |   2
 * |____________________|
 * |                    |
 * |    Length (MSB)    |   3
 * |____________________|
 * |                    |
 * |    Length (LSB)    |   4
 * |____________________|
 * |                    |
 * |     X.224 TPDU     |   5 - ?
 *          ....
 *
 * A TPKT header is of fixed length 4, and the following X.224 TPDU is at least three bytes long.
 * Therefore, the minimum TPKT length is 7, and the maximum TPKT length is 65535. Because the TPKT
 * length includes the TPKT header (4 bytes), the maximum X.224 TPDU length is 65531.
 */

/**
 * Verify if a packet has valid TPKT header.\n
 * @param s
 * @return BOOL
 */

BOOL tpkt_verify_header(wStream* s)
{
	BYTE version;

	Stream_Peek_UINT8(s, version);

	if (version == 3)
		return TRUE;
	else
		return FALSE;
}

/**
 * Read a TPKT header.\n
 * @param s
 * @param length
 * @return success
 */

BOOL tpkt_read_header(wStream* s, UINT16* length)
{
	BYTE version;

	if (Stream_GetRemainingLength(s) < 1)
		return FALSE;

	Stream_Peek_UINT8(s, version);

	if (version == 3)
	{
		UINT16 len;
		if (Stream_GetRemainingLength(s) < 4)
			return FALSE;

		Stream_Seek(s, 2);
		Stream_Read_UINT16_BE(s, len);
		if (len < 4)
			return FALSE;

		*length = len;
	}
	else
	{
		/* not a TPKT header */
		*length = 0;
	}

	return TRUE;
}

/**
 * Write a TPKT header.\n
 * @param s
 * @param length
 */

void tpkt_write_header(wStream* s, UINT16 length)
{
	Stream_Write_UINT8(s, 3); /* version */
	Stream_Write_UINT8(s, 0); /* reserved */
	Stream_Write_UINT16_BE(s, length); /* length */
}