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 |
|
|
#include "includes.h" |
22 |
|
|
|
23 |
|
|
/* Establish a connection up to the ISO layer */ |
24 |
|
|
HCONN iso_connect(char *server) |
25 |
|
|
{ |
26 |
|
|
HCONN conn; |
27 |
|
|
uint8 code; |
28 |
|
|
|
29 |
|
|
if ((conn = tcp_connect(server)) == NULL) |
30 |
|
|
return NULL; |
31 |
|
|
|
32 |
|
|
iso_send_msg(conn, ISO_PDU_CR); |
33 |
|
|
|
34 |
|
|
if (!iso_recv_msg(conn, &code) || (code != ISO_PDU_CC)) |
35 |
|
|
{ |
36 |
matty |
9 |
ERROR("ISO error, expected CC\n"); |
37 |
matty |
3 |
tcp_disconnect(conn); |
38 |
|
|
return NULL; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
return conn; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/* Disconnect from the ISO layer */ |
45 |
|
|
void iso_disconnect(HCONN conn) |
46 |
|
|
{ |
47 |
|
|
iso_send_msg(conn, ISO_PDU_DR); |
48 |
|
|
tcp_disconnect(conn); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/* Send self-contained ISO message identified by code */ |
52 |
|
|
BOOL iso_send_msg(HCONN conn, uint8 code) |
53 |
|
|
{ |
54 |
|
|
TPKT tpkt; |
55 |
|
|
TPDU tpdu; |
56 |
|
|
|
57 |
|
|
iso_make_tpkt(&tpkt, 11); |
58 |
|
|
iso_io_tpkt(&conn->out, &tpkt); |
59 |
|
|
iso_make_tpdu(&tpdu, code); |
60 |
|
|
iso_io_tpdu(&conn->out, &tpdu); |
61 |
|
|
MARK_END(conn->out); |
62 |
|
|
return tcp_send(conn); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/* Receive a message on the ISO layer, return code */ |
66 |
|
|
BOOL iso_recv_msg(HCONN conn, uint8 *code) |
67 |
|
|
{ |
68 |
|
|
TPDU tpdu; |
69 |
|
|
TPKT tpkt; |
70 |
|
|
BOOL res; |
71 |
|
|
|
72 |
|
|
res = tcp_recv(conn, 4); |
73 |
|
|
res = res ? iso_io_tpkt(&conn->in, &tpkt) : False; |
74 |
|
|
res = res ? tcp_recv(conn, tpkt.length - 4) : False; |
75 |
|
|
res = res ? iso_io_tpdu(&conn->in, &tpdu) : False; |
76 |
|
|
|
77 |
|
|
*code = tpdu.code; |
78 |
|
|
return res; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/* Initialise ISO transport data packet */ |
82 |
|
|
void iso_init(struct connection *conn) |
83 |
|
|
{ |
84 |
|
|
PUSH_LAYER(conn->out, iso_offset, 7); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/* Receive ISO transport data packet */ |
88 |
|
|
BOOL iso_recv(HCONN conn) |
89 |
|
|
{ |
90 |
|
|
uint8 code; |
91 |
|
|
|
92 |
|
|
if (!iso_recv_msg(conn, &code) || (code != ISO_PDU_DT)) |
93 |
|
|
{ |
94 |
matty |
9 |
ERROR("ISO error, expected DT\n"); |
95 |
matty |
3 |
return False; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
return True; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/* Receive ISO transport data packet */ |
102 |
|
|
BOOL iso_send(HCONN conn) |
103 |
|
|
{ |
104 |
|
|
TPKT tpkt; |
105 |
|
|
TPDU tpdu; |
106 |
|
|
|
107 |
|
|
POP_LAYER(conn->out, iso_offset); |
108 |
|
|
iso_make_tpkt(&tpkt, conn->out.end); |
109 |
|
|
iso_io_tpkt(&conn->out, &tpkt); |
110 |
|
|
iso_make_tpdu(&tpdu, ISO_PDU_DT); |
111 |
|
|
iso_io_tpdu(&conn->out, &tpdu); |
112 |
|
|
return tcp_send(conn); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/* Initialise a TPKT structure */ |
116 |
|
|
void iso_make_tpkt(TPKT *tpkt, int length) |
117 |
|
|
{ |
118 |
|
|
tpkt->version = 3; |
119 |
|
|
tpkt->reserved = 0; |
120 |
|
|
tpkt->length = length; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/* Marshall/demarshall a TPKT structure */ |
124 |
|
|
BOOL iso_io_tpkt(STREAM s, TPKT *tpkt) |
125 |
|
|
{ |
126 |
|
|
if (!prs_io_uint8(s, &tpkt->version)) |
127 |
|
|
return False; |
128 |
|
|
|
129 |
|
|
if (tpkt->version != 3) |
130 |
|
|
{ |
131 |
matty |
9 |
ERROR("Wrong TPKT version %d\n", tpkt->version); |
132 |
matty |
3 |
return False; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (!prs_io_uint8 (s, &tpkt->reserved)) |
136 |
|
|
return False; |
137 |
|
|
|
138 |
|
|
if (!msb_io_uint16(s, &tpkt->length)) |
139 |
|
|
return False; |
140 |
|
|
|
141 |
|
|
return True; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/* Initialise a TPDU structure */ |
145 |
|
|
void iso_make_tpdu(TPDU *tpdu, uint8 code) |
146 |
|
|
{ |
147 |
|
|
tpdu->hlen = (code == ISO_PDU_DT) ? 2 : 6; |
148 |
|
|
tpdu->code = code; |
149 |
|
|
tpdu->dst_ref = tpdu->src_ref = 0; |
150 |
|
|
tpdu->class = 0; |
151 |
|
|
tpdu->eot = 0x80; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
/* Marshall/demarshall a TPDU structure */ |
155 |
|
|
BOOL iso_io_tpdu(STREAM s, TPDU *tpdu) |
156 |
|
|
{ |
157 |
|
|
BOOL res = True; |
158 |
|
|
|
159 |
|
|
res = res ? prs_io_uint8 (s, &tpdu->hlen) : False; |
160 |
|
|
res = res ? prs_io_uint8 (s, &tpdu->code) : False; |
161 |
|
|
|
162 |
|
|
if (tpdu->code == ISO_PDU_DT) |
163 |
|
|
{ |
164 |
|
|
res = res ? prs_io_uint8(s, &tpdu->eot) : False; |
165 |
|
|
} |
166 |
|
|
else |
167 |
|
|
{ |
168 |
|
|
res = res ? msb_io_uint16(s, &tpdu->dst_ref) : False; |
169 |
|
|
res = res ? msb_io_uint16(s, &tpdu->src_ref) : False; |
170 |
|
|
res = res ? prs_io_uint8 (s, &tpdu->class ) : False; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
return res; |
174 |
|
|
} |