1 |
matty |
3 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Protocol services - ISO layer |
4 |
|
|
Copyright (C) Matthew Chapman 1999-2000 |
5 |
|
|
|
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 |
|
|
static void iso_send_msg(uint8 code) |
25 |
matty |
3 |
{ |
26 |
matty |
10 |
STREAM s; |
27 |
matty |
3 |
|
28 |
matty |
10 |
s = tcp_init(11); |
29 |
matty |
3 |
|
30 |
matty |
24 |
out_uint8(s, 3); /* version */ |
31 |
|
|
out_uint8(s, 0); /* reserved */ |
32 |
|
|
out_uint16_be(s, 11); /* length */ |
33 |
matty |
3 |
|
34 |
matty |
24 |
out_uint8(s, 6); /* hdrlen */ |
35 |
matty |
10 |
out_uint8(s, code); |
36 |
matty |
24 |
out_uint16(s, 0); /* dst_ref */ |
37 |
|
|
out_uint16(s, 0); /* src_ref */ |
38 |
|
|
out_uint8(s, 0); /* class */ |
39 |
matty |
10 |
|
40 |
|
|
s_mark_end(s); |
41 |
|
|
tcp_send(s); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/* Receive a message on the ISO layer, return code */ |
45 |
|
|
static STREAM iso_recv_msg(uint8 *code) |
46 |
|
|
{ |
47 |
|
|
STREAM s; |
48 |
|
|
uint16 length; |
49 |
|
|
uint8 version; |
50 |
|
|
|
51 |
|
|
s = tcp_recv(4); |
52 |
|
|
if (s == NULL) |
53 |
|
|
return False; |
54 |
|
|
|
55 |
|
|
in_uint8(s, version); |
56 |
|
|
if (version != 3) |
57 |
matty |
3 |
{ |
58 |
matty |
10 |
ERROR("TPKT v%d\n", version); |
59 |
|
|
return False; |
60 |
matty |
3 |
} |
61 |
|
|
|
62 |
matty |
24 |
in_uint8s(s, 1); /* pad */ |
63 |
matty |
10 |
in_uint16_be(s, length); |
64 |
matty |
3 |
|
65 |
matty |
10 |
s = tcp_recv(length - 4); |
66 |
|
|
if (s == NULL) |
67 |
|
|
return False; |
68 |
|
|
|
69 |
matty |
24 |
in_uint8s(s, 1); /* hdrlen */ |
70 |
matty |
10 |
in_uint8(s, *code); |
71 |
|
|
|
72 |
|
|
if (*code == ISO_PDU_DT) |
73 |
|
|
{ |
74 |
matty |
24 |
in_uint8s(s, 1); /* eot */ |
75 |
|
|
return s; |
76 |
matty |
10 |
} |
77 |
|
|
|
78 |
matty |
24 |
in_uint8s(s, 5); /* dst_ref, src_ref, class */ |
79 |
matty |
10 |
return s; |
80 |
matty |
3 |
} |
81 |
|
|
|
82 |
matty |
10 |
/* Initialise ISO transport data packet */ |
83 |
|
|
STREAM iso_init(int length) |
84 |
matty |
3 |
{ |
85 |
matty |
10 |
STREAM s; |
86 |
matty |
3 |
|
87 |
matty |
10 |
s = tcp_init(length + 7); |
88 |
|
|
s_push_layer(s, iso_hdr, 7); |
89 |
|
|
|
90 |
|
|
return s; |
91 |
matty |
3 |
} |
92 |
|
|
|
93 |
matty |
10 |
/* Send an ISO data PDU */ |
94 |
|
|
void iso_send(STREAM s) |
95 |
matty |
3 |
{ |
96 |
matty |
10 |
uint16 length; |
97 |
matty |
3 |
|
98 |
matty |
10 |
s_pop_layer(s, iso_hdr); |
99 |
|
|
length = s->end - s->p; |
100 |
matty |
3 |
|
101 |
matty |
24 |
out_uint8(s, 3); /* version */ |
102 |
|
|
out_uint8(s, 0); /* reserved */ |
103 |
matty |
10 |
out_uint16_be(s, length); |
104 |
matty |
3 |
|
105 |
matty |
24 |
out_uint8(s, 2); /* hdrlen */ |
106 |
|
|
out_uint8(s, ISO_PDU_DT); /* code */ |
107 |
|
|
out_uint8(s, 0x80); /* eot */ |
108 |
matty |
10 |
|
109 |
|
|
tcp_send(s); |
110 |
matty |
3 |
} |
111 |
|
|
|
112 |
|
|
/* Receive ISO transport data packet */ |
113 |
matty |
10 |
STREAM iso_recv() |
114 |
matty |
3 |
{ |
115 |
matty |
10 |
STREAM s; |
116 |
matty |
3 |
uint8 code; |
117 |
|
|
|
118 |
matty |
10 |
s = iso_recv_msg(&code); |
119 |
|
|
if ((s == NULL) || (code != ISO_PDU_DT)) |
120 |
matty |
3 |
{ |
121 |
matty |
10 |
ERROR("expected DT, got %d\n", code); |
122 |
matty |
3 |
return False; |
123 |
|
|
} |
124 |
|
|
|
125 |
matty |
10 |
return s; |
126 |
matty |
3 |
} |
127 |
|
|
|
128 |
matty |
10 |
/* Establish a connection up to the ISO layer */ |
129 |
|
|
BOOL iso_connect(char *server) |
130 |
matty |
3 |
{ |
131 |
matty |
10 |
uint8 code; |
132 |
matty |
3 |
|
133 |
matty |
10 |
if (!tcp_connect(server)) |
134 |
|
|
return False; |
135 |
matty |
3 |
|
136 |
matty |
10 |
iso_send_msg(ISO_PDU_CR); |
137 |
matty |
3 |
|
138 |
matty |
10 |
if ((iso_recv_msg(&code) == NULL) || (code != ISO_PDU_CC)) |
139 |
matty |
3 |
{ |
140 |
matty |
10 |
ERROR("expected CC, got %d\n", code); |
141 |
|
|
tcp_disconnect(); |
142 |
matty |
3 |
return False; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
return True; |
146 |
|
|
} |
147 |
|
|
|
148 |
matty |
10 |
/* Disconnect from the ISO layer */ |
149 |
|
|
void iso_disconnect() |
150 |
matty |
3 |
{ |
151 |
matty |
10 |
iso_send_msg(ISO_PDU_DR); |
152 |
|
|
tcp_disconnect(); |
153 |
matty |
3 |
} |