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