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