~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to libfreerdp/core/tpdu.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.2.5)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20141111122050-7z628f4ab38qxad5
Tags: upstream-1.1.0~git20140921.1.440916e+dfsg1
ImportĀ upstreamĀ versionĀ 1.1.0~git20140921.1.440916e+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <stdio.h>
 
25
 
 
26
#include "tpdu.h"
 
27
 
 
28
/**
 
29
 * TPDUs are defined in:
 
30
 *
 
31
 * http://www.itu.int/rec/T-REC-X.224-199511-I/
 
32
 * X.224: Information technology - Open Systems Interconnection - Protocol for providing the connection-mode transport service
 
33
 *
 
34
 * RDP uses only TPDUs of class 0, the "simple class" defined in section 8 of X.224
 
35
 *
 
36
 *       TPDU Header
 
37
 *  ____________________   byte
 
38
 * |                    |
 
39
 * |         LI         |   1
 
40
 * |____________________|
 
41
 * |                    |
 
42
 * |        Code        |   2
 
43
 * |____________________|
 
44
 * |                    |
 
45
 * |                    |   3
 
46
 * |_______DST-REF______|
 
47
 * |                    |
 
48
 * |                    |   4
 
49
 * |____________________|
 
50
 * |                    |
 
51
 * |                    |   5
 
52
 * |_______SRC-REF______|
 
53
 * |                    |
 
54
 * |                    |   6
 
55
 * |____________________|
 
56
 * |                    |
 
57
 * |        Class       |   7
 
58
 * |____________________|
 
59
 * |         ...        |
 
60
 */
 
61
 
 
62
/**
 
63
 * Read TPDU header.
 
64
 * @param s stream
 
65
 * @param code variable pointer to receive TPDU code
 
66
 * @return TPDU length indicator (LI)
 
67
 */
 
68
 
 
69
BOOL tpdu_read_header(wStream* s, BYTE* code, BYTE *li)
 
70
{
 
71
        if(Stream_GetRemainingLength(s) < 3)
 
72
                return FALSE;
 
73
 
 
74
        Stream_Read_UINT8(s, *li); /* LI */
 
75
        Stream_Read_UINT8(s, *code); /* Code */
 
76
 
 
77
        if (*code == X224_TPDU_DATA)
 
78
        {
 
79
                /* EOT (1 byte) */
 
80
                Stream_Seek(s, 1);
 
81
        }
 
82
        else
 
83
        {
 
84
                /* DST-REF (2 bytes) */
 
85
                /* SRC-REF (2 bytes) */
 
86
                /* Class 0 (1 byte) */
 
87
                return Stream_SafeSeek(s, 5);
 
88
        }
 
89
        return TRUE;
 
90
}
 
91
 
 
92
/**
 
93
 * Write TDPU header.
 
94
 * @param s stream
 
95
 * @param length length
 
96
 * @param code TPDU code
 
97
 */
 
98
 
 
99
void tpdu_write_header(wStream* s, UINT16 length, BYTE code)
 
100
{
 
101
        Stream_Write_UINT8(s, length); /* LI */
 
102
        Stream_Write_UINT8(s, code); /* code */
 
103
 
 
104
        if (code == X224_TPDU_DATA)
 
105
        {
 
106
                Stream_Write_UINT8(s, 0x80); /* EOT */
 
107
        }
 
108
        else
 
109
        {
 
110
                Stream_Write_UINT16(s, 0); /* DST-REF */
 
111
                Stream_Write_UINT16(s, 0); /* SRC-REF */
 
112
                Stream_Write_UINT8(s, 0); /* Class 0 */
 
113
        }
 
114
}
 
115
 
 
116
/**
 
117
 * Read Connection Request TPDU
 
118
 * @param s stream
 
119
 * @return length indicator (LI)
 
120
 */
 
121
 
 
122
BOOL tpdu_read_connection_request(wStream* s, BYTE *li)
 
123
{
 
124
        BYTE code;
 
125
 
 
126
        if(!tpdu_read_header(s, &code, li))
 
127
                return FALSE;
 
128
 
 
129
        if (code != X224_TPDU_CONNECTION_REQUEST)
 
130
        {
 
131
                fprintf(stderr, "Error: expected X224_TPDU_CONNECTION_REQUEST\n");
 
132
                return FALSE;
 
133
        }
 
134
 
 
135
        return TRUE;
 
136
}
 
137
 
 
138
/**
 
139
 * Write Connection Request TPDU.
 
140
 * @param s stream
 
141
 * @param length TPDU length
 
142
 */
 
143
 
 
144
void tpdu_write_connection_request(wStream* s, UINT16 length)
 
145
{
 
146
        tpdu_write_header(s, length, X224_TPDU_CONNECTION_REQUEST);
 
147
}
 
148
 
 
149
/**
 
150
 * Read Connection Confirm TPDU.
 
151
 * @param s stream
 
152
 * @return length indicator (LI)
 
153
 */
 
154
 
 
155
BOOL tpdu_read_connection_confirm(wStream* s, BYTE *li)
 
156
{
 
157
        BYTE code;
 
158
 
 
159
        if(!tpdu_read_header(s, &code, li))
 
160
                return FALSE;
 
161
 
 
162
        if (code != X224_TPDU_CONNECTION_CONFIRM)
 
163
        {
 
164
                fprintf(stderr, "Error: expected X224_TPDU_CONNECTION_CONFIRM\n");
 
165
                return FALSE;
 
166
        }
 
167
 
 
168
        return (Stream_GetRemainingLength(s) >= *li);
 
169
}
 
170
 
 
171
/**
 
172
 * Write Connection Confirm TPDU.
 
173
 * @param s stream
 
174
 * @param length TPDU length
 
175
 */
 
176
 
 
177
void tpdu_write_connection_confirm(wStream* s, UINT16 length)
 
178
{
 
179
        tpdu_write_header(s, length, X224_TPDU_CONNECTION_CONFIRM);
 
180
}
 
181
 
 
182
/**
 
183
 * Write Disconnect Request TPDU.
 
184
 * @param s stream
 
185
 * @param length TPDU length
 
186
 */
 
187
 
 
188
void tpdu_write_disconnect_request(wStream* s, UINT16 length)
 
189
{
 
190
        tpdu_write_header(s, length, X224_TPDU_DISCONNECT_REQUEST);
 
191
}
 
192
 
 
193
/**
 
194
 * Write Data TPDU.
 
195
 * @param s stream
 
196
 */
 
197
 
 
198
void tpdu_write_data(wStream* s)
 
199
{
 
200
        tpdu_write_header(s, 2, X224_TPDU_DATA);
 
201
}
 
202
 
 
203
/**
 
204
 * Read Data TPDU.
 
205
 * @param s stream
 
206
 */
 
207
 
 
208
BOOL tpdu_read_data(wStream* s, UINT16 *LI)
 
209
{
 
210
        BYTE code;
 
211
        BYTE li;
 
212
 
 
213
        if(!tpdu_read_header(s, &code, &li))
 
214
                return FALSE;
 
215
 
 
216
        if (code != X224_TPDU_DATA)
 
217
                return FALSE;
 
218
        *LI = li;
 
219
        return TRUE;
 
220
}