Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / tpkt.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Transport Packets (TPKTs)
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 "tpdu.h"
21
22 #include "tpkt.h"
23
24 /**
25  * TPKTs are defined in:
26  *
27  * http://tools.ietf.org/html/rfc1006/
28  * RFC 1006 - ISO Transport Service on top of the TCP
29  *
30  * http://www.itu.int/rec/T-REC-T.123/
31  * ITU-T T.123 (01/2007) - Network-specific data protocol stacks for multimedia conferencing
32  *
33  *       TPKT Header
34  *  ____________________   byte
35  * |                    |
36  * |     3 (version)    |   1
37  * |____________________|
38  * |                    |
39  * |      Reserved      |   2
40  * |____________________|
41  * |                    |
42  * |    Length (MSB)    |   3
43  * |____________________|
44  * |                    |
45  * |    Length (LSB)    |   4
46  * |____________________|
47  * |                    |
48  * |     X.224 TPDU     |   5 - ?
49  *          ....
50  *
51  * A TPKT header is of fixed length 4, and the following X.224 TPDU is at least three bytes long.
52  * Therefore, the minimum TPKT length is 7, and the maximum TPKT length is 65535. Because the TPKT
53  * length includes the TPKT header (4 bytes), the maximum X.224 TPDU length is 65531.
54  */
55
56 /**
57  * Verify if a packet has valid TPKT header.\n
58  * @param s
59  * @return boolean
60  */
61
62 boolean tpkt_verify_header(STREAM* s)
63 {
64         uint8 version;
65
66         stream_peek_uint8(s, version);
67         if (version == 3)
68                 return true;
69         else
70                 return false;
71 }
72
73 /**
74  * Read a TPKT header.\n
75  * @param s
76  * @return length
77  */
78
79 uint16 tpkt_read_header(STREAM* s)
80 {
81         uint8 version;
82         uint16 length;
83
84         stream_peek_uint8(s, version);
85
86         if (version == 3)
87         {
88                 stream_seek(s, 2);
89                 stream_read_uint16_be(s, length);
90         }
91         else
92         {
93                 /* not a TPKT header */
94                 length = 0;
95         }
96
97         return length;
98 }
99
100 /**
101  * Write a TPKT header.\n
102  * @param s
103  * @param length
104  */
105
106 void tpkt_write_header(STREAM* s, uint16 length)
107 {
108         stream_write_uint8(s, 3); /* version */
109         stream_write_uint8(s, 0); /* reserved */
110         stream_write_uint16_be(s, length); /* length */
111 }