1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - ISO layer |
Protocol services - ISO layer |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
Copyright (C) Matthew Chapman 1999-2005 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include "includes.h" |
#include "rdesktop.h" |
22 |
|
|
23 |
/* Establish a connection up to the ISO layer */ |
/* Send a self-contained ISO PDU */ |
24 |
HCONN iso_connect(char *server) |
static void |
25 |
|
iso_send_msg(uint8 code) |
26 |
{ |
{ |
27 |
HCONN conn; |
STREAM s; |
|
uint8 code; |
|
28 |
|
|
29 |
if ((conn = tcp_connect(server)) == NULL) |
s = tcp_init(11); |
|
return NULL; |
|
30 |
|
|
31 |
iso_send_msg(conn, ISO_PDU_CR); |
out_uint8(s, 3); /* version */ |
32 |
|
out_uint8(s, 0); /* reserved */ |
33 |
|
out_uint16_be(s, 11); /* length */ |
34 |
|
|
35 |
if (!iso_recv_msg(conn, &code) || (code != ISO_PDU_CC)) |
out_uint8(s, 6); /* hdrlen */ |
36 |
{ |
out_uint8(s, code); |
37 |
fprintf(stderr, "ISO error, expected CC\n"); |
out_uint16(s, 0); /* dst_ref */ |
38 |
tcp_disconnect(conn); |
out_uint16(s, 0); /* src_ref */ |
39 |
return NULL; |
out_uint8(s, 0); /* class */ |
|
} |
|
40 |
|
|
41 |
return conn; |
s_mark_end(s); |
42 |
|
tcp_send(s); |
43 |
} |
} |
44 |
|
|
45 |
/* Disconnect from the ISO layer */ |
static void |
46 |
void iso_disconnect(HCONN conn) |
iso_send_connection_request(char *username) |
47 |
{ |
{ |
48 |
iso_send_msg(conn, ISO_PDU_DR); |
STREAM s; |
49 |
tcp_disconnect(conn); |
int length = 30 + strlen(username); |
|
} |
|
50 |
|
|
51 |
/* Send self-contained ISO message identified by code */ |
s = tcp_init(length); |
52 |
BOOL iso_send_msg(HCONN conn, uint8 code) |
|
53 |
{ |
out_uint8(s, 3); /* version */ |
54 |
TPKT tpkt; |
out_uint8(s, 0); /* reserved */ |
55 |
TPDU tpdu; |
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 |
iso_make_tpkt(&tpkt, 11); |
out_uint8(s, 0x0d); /* Unknown */ |
67 |
iso_io_tpkt(&conn->out, &tpkt); |
out_uint8(s, 0x0a); /* Unknown */ |
68 |
iso_make_tpdu(&tpdu, code); |
|
69 |
iso_io_tpdu(&conn->out, &tpdu); |
s_mark_end(s); |
70 |
MARK_END(conn->out); |
tcp_send(s); |
|
return tcp_send(conn); |
|
71 |
} |
} |
72 |
|
|
73 |
/* Receive a message on the ISO layer, return code */ |
/* Receive a message on the ISO layer, return code */ |
74 |
BOOL iso_recv_msg(HCONN conn, uint8 *code) |
static STREAM |
75 |
|
iso_recv_msg(uint8 * code, uint8 * rdpver) |
76 |
{ |
{ |
77 |
TPDU tpdu; |
STREAM s; |
78 |
TPKT tpkt; |
uint16 length; |
79 |
BOOL res; |
uint8 version; |
|
|
|
|
res = tcp_recv(conn, 4); |
|
|
res = res ? iso_io_tpkt(&conn->in, &tpkt) : False; |
|
|
res = res ? tcp_recv(conn, tpkt.length - 4) : False; |
|
|
res = res ? iso_io_tpdu(&conn->in, &tpdu) : False; |
|
80 |
|
|
81 |
*code = tpdu.code; |
s = tcp_recv(NULL, 4); |
82 |
return res; |
if (s == NULL) |
83 |
|
return NULL; |
84 |
|
in_uint8(s, version); |
85 |
|
if (rdpver != NULL) |
86 |
|
*rdpver = version; |
87 |
|
if (version == 3) |
88 |
|
{ |
89 |
|
in_uint8s(s, 1); /* pad */ |
90 |
|
in_uint16_be(s, length); |
91 |
|
} |
92 |
|
else |
93 |
|
{ |
94 |
|
in_uint8(s, length); |
95 |
|
if (length & 0x80) |
96 |
|
{ |
97 |
|
length &= ~0x80; |
98 |
|
next_be(s, length); |
99 |
|
} |
100 |
|
} |
101 |
|
s = tcp_recv(s, length - 4); |
102 |
|
if (s == NULL) |
103 |
|
return NULL; |
104 |
|
if (version != 3) |
105 |
|
return s; |
106 |
|
in_uint8s(s, 1); /* hdrlen */ |
107 |
|
in_uint8(s, *code); |
108 |
|
if (*code == ISO_PDU_DT) |
109 |
|
{ |
110 |
|
in_uint8s(s, 1); /* eot */ |
111 |
|
return s; |
112 |
|
} |
113 |
|
in_uint8s(s, 5); /* dst_ref, src_ref, class */ |
114 |
|
return s; |
115 |
} |
} |
116 |
|
|
117 |
/* Initialise ISO transport data packet */ |
/* Initialise ISO transport data packet */ |
118 |
void iso_init(struct connection *conn) |
STREAM |
119 |
|
iso_init(int length) |
120 |
{ |
{ |
121 |
PUSH_LAYER(conn->out, iso_offset, 7); |
STREAM s; |
122 |
|
|
123 |
|
s = tcp_init(length + 7); |
124 |
|
s_push_layer(s, iso_hdr, 7); |
125 |
|
|
126 |
|
return s; |
127 |
} |
} |
128 |
|
|
129 |
/* Receive ISO transport data packet */ |
/* Send an ISO data PDU */ |
130 |
BOOL iso_recv(HCONN conn) |
void |
131 |
|
iso_send(STREAM s) |
132 |
{ |
{ |
133 |
uint8 code; |
uint16 length; |
134 |
|
|
135 |
if (!iso_recv_msg(conn, &code) || (code != ISO_PDU_DT)) |
s_pop_layer(s, iso_hdr); |
136 |
{ |
length = s->end - s->p; |
|
fprintf(stderr, "ISO error, expected DT\n"); |
|
|
return False; |
|
|
} |
|
137 |
|
|
138 |
return True; |
out_uint8(s, 3); /* version */ |
139 |
|
out_uint8(s, 0); /* reserved */ |
140 |
|
out_uint16_be(s, length); |
141 |
|
|
142 |
|
out_uint8(s, 2); /* hdrlen */ |
143 |
|
out_uint8(s, ISO_PDU_DT); /* code */ |
144 |
|
out_uint8(s, 0x80); /* eot */ |
145 |
|
|
146 |
|
tcp_send(s); |
147 |
} |
} |
148 |
|
|
149 |
/* Receive ISO transport data packet */ |
/* Receive ISO transport data packet */ |
150 |
BOOL iso_send(HCONN conn) |
STREAM |
151 |
|
iso_recv(uint8 * rdpver) |
152 |
{ |
{ |
153 |
TPKT tpkt; |
STREAM s; |
154 |
TPDU tpdu; |
uint8 code = 0; |
155 |
|
|
156 |
POP_LAYER(conn->out, iso_offset); |
s = iso_recv_msg(&code, rdpver); |
157 |
iso_make_tpkt(&tpkt, conn->out.end); |
if (s == NULL) |
158 |
iso_io_tpkt(&conn->out, &tpkt); |
return NULL; |
159 |
iso_make_tpdu(&tpdu, ISO_PDU_DT); |
if (rdpver != NULL) |
160 |
iso_io_tpdu(&conn->out, &tpdu); |
if (*rdpver != 3) |
161 |
return tcp_send(conn); |
return s; |
162 |
|
if (code != ISO_PDU_DT) |
163 |
|
{ |
164 |
|
error("expected DT, got 0x%x\n", code); |
165 |
|
return NULL; |
166 |
|
} |
167 |
|
return s; |
168 |
} |
} |
169 |
|
|
170 |
/* Initialise a TPKT structure */ |
/* Establish a connection up to the ISO layer */ |
171 |
void iso_make_tpkt(TPKT *tpkt, int length) |
BOOL |
172 |
|
iso_connect(char *server, char *username) |
173 |
{ |
{ |
174 |
tpkt->version = 3; |
uint8 code = 0; |
|
tpkt->reserved = 0; |
|
|
tpkt->length = length; |
|
|
} |
|
175 |
|
|
176 |
/* Marshall/demarshall a TPKT structure */ |
if (!tcp_connect(server)) |
|
BOOL iso_io_tpkt(STREAM s, TPKT *tpkt) |
|
|
{ |
|
|
if (!prs_io_uint8(s, &tpkt->version)) |
|
177 |
return False; |
return False; |
178 |
|
|
179 |
if (tpkt->version != 3) |
iso_send_connection_request(username); |
|
{ |
|
|
fprintf(stderr, "Wrong TPKT version %d\n", tpkt->version); |
|
|
return False; |
|
|
} |
|
180 |
|
|
181 |
if (!prs_io_uint8 (s, &tpkt->reserved)) |
if (iso_recv_msg(&code, NULL) == NULL) |
182 |
return False; |
return False; |
183 |
|
|
184 |
if (!msb_io_uint16(s, &tpkt->length)) |
if (code != ISO_PDU_CC) |
185 |
|
{ |
186 |
|
error("expected CC, got 0x%x\n", code); |
187 |
|
tcp_disconnect(); |
188 |
return False; |
return False; |
189 |
|
} |
190 |
|
|
191 |
return True; |
return True; |
192 |
} |
} |
193 |
|
|
194 |
/* Initialise a TPDU structure */ |
/* Disconnect from the ISO layer */ |
195 |
void iso_make_tpdu(TPDU *tpdu, uint8 code) |
void |
196 |
{ |
iso_disconnect(void) |
|
tpdu->hlen = (code == ISO_PDU_DT) ? 2 : 6; |
|
|
tpdu->code = code; |
|
|
tpdu->dst_ref = tpdu->src_ref = 0; |
|
|
tpdu->class = 0; |
|
|
tpdu->eot = 0x80; |
|
|
} |
|
|
|
|
|
/* Marshall/demarshall a TPDU structure */ |
|
|
BOOL iso_io_tpdu(STREAM s, TPDU *tpdu) |
|
197 |
{ |
{ |
198 |
BOOL res = True; |
iso_send_msg(ISO_PDU_DR); |
199 |
|
tcp_disconnect(); |
|
res = res ? prs_io_uint8 (s, &tpdu->hlen) : False; |
|
|
res = res ? prs_io_uint8 (s, &tpdu->code) : False; |
|
|
|
|
|
if (tpdu->code == ISO_PDU_DT) |
|
|
{ |
|
|
res = res ? prs_io_uint8(s, &tpdu->eot) : False; |
|
|
} |
|
|
else |
|
|
{ |
|
|
res = res ? msb_io_uint16(s, &tpdu->dst_ref) : False; |
|
|
res = res ? msb_io_uint16(s, &tpdu->src_ref) : False; |
|
|
res = res ? prs_io_uint8 (s, &tpdu->class ) : False; |
|
|
} |
|
|
|
|
|
return res; |
|
200 |
} |
} |