1 |
forsberg |
347 |
/* -*- c-basic-offset: 8 -*- |
2 |
matty |
3 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Protocol services - ISO layer |
4 |
matthewc |
207 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
matty |
3 |
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
matty |
10 |
#include "rdesktop.h" |
22 |
matty |
3 |
|
23 |
matty |
10 |
/* Send a self-contained ISO PDU */ |
24 |
matty |
25 |
static void |
25 |
|
|
iso_send_msg(uint8 code) |
26 |
matty |
3 |
{ |
27 |
matty |
10 |
STREAM s; |
28 |
matty |
3 |
|
29 |
matty |
10 |
s = tcp_init(11); |
30 |
matty |
3 |
|
31 |
matty |
24 |
out_uint8(s, 3); /* version */ |
32 |
|
|
out_uint8(s, 0); /* reserved */ |
33 |
|
|
out_uint16_be(s, 11); /* length */ |
34 |
matty |
3 |
|
35 |
matty |
24 |
out_uint8(s, 6); /* hdrlen */ |
36 |
matty |
10 |
out_uint8(s, code); |
37 |
matty |
24 |
out_uint16(s, 0); /* dst_ref */ |
38 |
|
|
out_uint16(s, 0); /* src_ref */ |
39 |
|
|
out_uint8(s, 0); /* class */ |
40 |
matty |
10 |
|
41 |
|
|
s_mark_end(s); |
42 |
|
|
tcp_send(s); |
43 |
|
|
} |
44 |
|
|
|
45 |
forsberg |
347 |
static void |
46 |
|
|
iso_send_connection_request(char *username) |
47 |
|
|
{ |
48 |
|
|
STREAM s; |
49 |
|
|
int length = 30 + strlen(username); |
50 |
|
|
|
51 |
|
|
s = tcp_init(length); |
52 |
|
|
|
53 |
|
|
out_uint8(s, 3); /* version */ |
54 |
|
|
out_uint8(s, 0); /* reserved */ |
55 |
|
|
out_uint16_be(s, length); /* length */ |
56 |
|
|
|
57 |
|
|
out_uint8(s, length - 5); /* hdrlen */ |
58 |
|
|
out_uint8(s, ISO_PDU_CR); |
59 |
|
|
out_uint16(s, 0); /* dst_ref */ |
60 |
|
|
out_uint16(s, 0); /* src_ref */ |
61 |
|
|
out_uint8(s, 0); /* class */ |
62 |
|
|
|
63 |
|
|
out_uint8p(s, "Cookie: mstshash=", strlen("Cookie: mstshash=")); |
64 |
|
|
out_uint8p(s, username, strlen(username)); |
65 |
|
|
|
66 |
|
|
out_uint8(s, 0x0d); /* Unknown */ |
67 |
|
|
out_uint8(s, 0x0a); /* Unknown */ |
68 |
|
|
|
69 |
|
|
s_mark_end(s); |
70 |
|
|
tcp_send(s); |
71 |
|
|
} |
72 |
|
|
|
73 |
matty |
10 |
/* Receive a message on the ISO layer, return code */ |
74 |
matty |
25 |
static STREAM |
75 |
jsorg71 |
733 |
iso_recv_msg(uint8 * code, uint8 * rdpver) |
76 |
matty |
10 |
{ |
77 |
|
|
STREAM s; |
78 |
|
|
uint16 length; |
79 |
|
|
uint8 version; |
80 |
|
|
|
81 |
matthewc |
423 |
s = tcp_recv(NULL, 4); |
82 |
matty |
10 |
if (s == NULL) |
83 |
matty |
33 |
return NULL; |
84 |
matty |
10 |
in_uint8(s, version); |
85 |
jsorg71 |
733 |
if (rdpver != NULL) |
86 |
|
|
*rdpver = version; |
87 |
|
|
if (version == 3) |
88 |
matty |
3 |
{ |
89 |
jsorg71 |
733 |
in_uint8s(s, 1); /* pad */ |
90 |
|
|
in_uint16_be(s, length); |
91 |
matty |
3 |
} |
92 |
jsorg71 |
733 |
else |
93 |
|
|
{ |
94 |
|
|
in_uint8(s, length); |
95 |
|
|
if (length & 0x80) |
96 |
|
|
{ |
97 |
|
|
length &= ~0x80; |
98 |
|
|
next_be(s, length); |
99 |
|
|
} |
100 |
|
|
} |
101 |
matthewc |
423 |
s = tcp_recv(s, length - 4); |
102 |
matty |
10 |
if (s == NULL) |
103 |
matty |
33 |
return NULL; |
104 |
jsorg71 |
733 |
if (version != 3) |
105 |
|
|
return s; |
106 |
matty |
24 |
in_uint8s(s, 1); /* hdrlen */ |
107 |
matty |
10 |
in_uint8(s, *code); |
108 |
|
|
if (*code == ISO_PDU_DT) |
109 |
|
|
{ |
110 |
matty |
24 |
in_uint8s(s, 1); /* eot */ |
111 |
|
|
return s; |
112 |
matty |
10 |
} |
113 |
matty |
24 |
in_uint8s(s, 5); /* dst_ref, src_ref, class */ |
114 |
matty |
10 |
return s; |
115 |
matty |
3 |
} |
116 |
|
|
|
117 |
matty |
10 |
/* Initialise ISO transport data packet */ |
118 |
matty |
25 |
STREAM |
119 |
|
|
iso_init(int length) |
120 |
matty |
3 |
{ |
121 |
matty |
10 |
STREAM s; |
122 |
matty |
3 |
|
123 |
matty |
10 |
s = tcp_init(length + 7); |
124 |
|
|
s_push_layer(s, iso_hdr, 7); |
125 |
|
|
|
126 |
|
|
return s; |
127 |
matty |
3 |
} |
128 |
|
|
|
129 |
matty |
10 |
/* Send an ISO data PDU */ |
130 |
matty |
25 |
void |
131 |
|
|
iso_send(STREAM s) |
132 |
matty |
3 |
{ |
133 |
matty |
10 |
uint16 length; |
134 |
matty |
3 |
|
135 |
matty |
10 |
s_pop_layer(s, iso_hdr); |
136 |
|
|
length = s->end - s->p; |
137 |
matty |
3 |
|
138 |
matty |
24 |
out_uint8(s, 3); /* version */ |
139 |
|
|
out_uint8(s, 0); /* reserved */ |
140 |
matty |
10 |
out_uint16_be(s, length); |
141 |
matty |
3 |
|
142 |
matty |
24 |
out_uint8(s, 2); /* hdrlen */ |
143 |
|
|
out_uint8(s, ISO_PDU_DT); /* code */ |
144 |
|
|
out_uint8(s, 0x80); /* eot */ |
145 |
matty |
10 |
|
146 |
|
|
tcp_send(s); |
147 |
matty |
3 |
} |
148 |
|
|
|
149 |
|
|
/* Receive ISO transport data packet */ |
150 |
matty |
25 |
STREAM |
151 |
jsorg71 |
733 |
iso_recv(uint8 * rdpver) |
152 |
matty |
3 |
{ |
153 |
matty |
10 |
STREAM s; |
154 |
jsorg71 |
733 |
uint8 code = 0; |
155 |
matty |
3 |
|
156 |
jsorg71 |
733 |
s = iso_recv_msg(&code, rdpver); |
157 |
matty |
33 |
if (s == NULL) |
158 |
|
|
return NULL; |
159 |
jsorg71 |
733 |
if (rdpver != NULL) |
160 |
|
|
if (*rdpver != 3) |
161 |
|
|
return s; |
162 |
matty |
33 |
if (code != ISO_PDU_DT) |
163 |
matty |
3 |
{ |
164 |
matty |
33 |
error("expected DT, got 0x%x\n", code); |
165 |
|
|
return NULL; |
166 |
matty |
3 |
} |
167 |
matty |
10 |
return s; |
168 |
matty |
3 |
} |
169 |
|
|
|
170 |
matty |
10 |
/* Establish a connection up to the ISO layer */ |
171 |
matty |
25 |
BOOL |
172 |
forsberg |
347 |
iso_connect(char *server, char *username) |
173 |
matty |
3 |
{ |
174 |
jsorg71 |
733 |
uint8 code = 0; |
175 |
matty |
3 |
|
176 |
matty |
10 |
if (!tcp_connect(server)) |
177 |
|
|
return False; |
178 |
matty |
3 |
|
179 |
forsberg |
347 |
iso_send_connection_request(username); |
180 |
matty |
3 |
|
181 |
jsorg71 |
733 |
if (iso_recv_msg(&code, NULL) == NULL) |
182 |
matty |
33 |
return False; |
183 |
|
|
|
184 |
|
|
if (code != ISO_PDU_CC) |
185 |
matty |
3 |
{ |
186 |
matty |
33 |
error("expected CC, got 0x%x\n", code); |
187 |
matty |
10 |
tcp_disconnect(); |
188 |
matty |
3 |
return False; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
return True; |
192 |
|
|
} |
193 |
|
|
|
194 |
matty |
10 |
/* Disconnect from the ISO layer */ |
195 |
matty |
25 |
void |
196 |
matthewc |
192 |
iso_disconnect(void) |
197 |
matty |
3 |
{ |
198 |
matty |
10 |
iso_send_msg(ISO_PDU_DR); |
199 |
|
|
tcp_disconnect(); |
200 |
matty |
3 |
} |