]> git.sesse.net Git - rdpsrv/blob - iso.c
Attempt to send an CC back (but fail?)
[rdpsrv] / iso.c
1 /* -*- c-basic-offset: 8 -*-
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 static void
46 iso_send_connection_request(char *username)
47 {
48         STREAM s;
49         int length = 30 + strlen(username);
50
51         s = tcp_init(length);
52
53         out_uint8(s, 3);        /* version */
54         out_uint8(s, 0);        /* reserved */
55         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         out_uint8(s, 0x0d);     /* Unknown */
67         out_uint8(s, 0x0a);     /* Unknown */
68
69         s_mark_end(s);
70         tcp_send(s);
71 }
72
73 /* Receive a message on the ISO layer, return code */
74 static STREAM
75 iso_recv_msg(uint8 * code)
76 {
77         STREAM s;
78         uint16 length;
79         uint8 version;
80
81       next_packet:
82         s = tcp_recv(NULL, 4);
83         if (s == NULL)
84                 return NULL;
85
86         in_uint8(s, version);
87         switch (version & 3)
88         {
89                 case 0:
90                         in_uint8(s, length);
91                         if (length & 0x80)
92                         {
93                                 length &= ~0x80;
94                                 next_be(s, length);
95                         }
96                         break;
97
98                 case 3:
99                         in_uint8s(s, 1);        /* pad */
100                         in_uint16_be(s, length);
101                         break;
102
103                 default:
104                         error("TPKT v%d\n", version);
105                         return NULL;
106         }
107
108         s = tcp_recv(s, length - 4);
109         if (s == NULL)
110                 return NULL;
111
112         if ((version & 3) == 0)
113         {
114                 // FIXME :-)
115                 //              rdp5_process(s, version & 0x80);
116                 printf("rdp5_process()\n");
117                 goto next_packet;
118         }
119
120         in_uint8s(s, 1);        /* hdrlen */
121         in_uint8(s, *code);
122
123         if (*code == ISO_PDU_DT)
124         {
125                 in_uint8s(s, 1);        /* eot */
126                 return s;
127         }
128
129         in_uint8s(s, 5);        /* dst_ref, src_ref, class */
130         return s;
131 }
132
133 /* Initialise ISO transport data packet */
134 STREAM
135 iso_init(int length)
136 {
137         STREAM s;
138
139         s = tcp_init(length + 7);
140         s_push_layer(s, iso_hdr, 7);
141
142         return s;
143 }
144
145 /* Send an ISO data PDU */
146 void
147 iso_send(STREAM s)
148 {
149         uint16 length;
150
151         s_pop_layer(s, iso_hdr);
152         length = s->end - s->p;
153
154         out_uint8(s, 3);        /* version */
155         out_uint8(s, 0);        /* reserved */
156         out_uint16_be(s, length);
157
158         out_uint8(s, 2);        /* hdrlen */
159         out_uint8(s, ISO_PDU_DT);       /* code */
160         out_uint8(s, 0x80);     /* eot */
161
162         tcp_send(s);
163 }
164
165 /* Receive ISO transport data packet */
166 STREAM
167 iso_recv(void)
168 {
169         STREAM s;
170         uint8 code;
171
172         s = iso_recv_msg(&code);
173         if (s == NULL)
174                 return NULL;
175
176         if (code != ISO_PDU_DT)
177         {
178                 error("expected DT, got 0x%x\n", code);
179                 return NULL;
180         }
181
182         return s;
183 }
184
185 BOOL
186 iso_recv_connect(int server_sock)
187 {
188         STREAM s;
189         uint8 code;
190
191         tcp_recv_connect(server_sock);
192         
193         s = iso_recv_msg(&code);
194         if (s == NULL)
195                 return 0;
196
197         if (code != ISO_PDU_CR)
198         {
199                 error("expected CR, got 0x%x\n", code);
200                 return 0;
201         }
202
203         DEBUG(("Got ISO connection request\n"));
204
205         iso_init(0);
206         
207         /* send an CC PDU back */
208         out_uint8(s, 3);        /* version */
209         out_uint8(s, 0);        /* reserved */
210         out_uint16_be(s, 0);
211
212         out_uint8(s, 2);        /* hdrlen */
213         out_uint8(s, ISO_PDU_CC);       /* code */
214         out_uint8(s, 0x80);     /* eot */
215
216         tcp_send(s);
217         
218         return 1;
219 }
220
221 /* Disconnect from the ISO layer */
222 void
223 iso_disconnect(void)
224 {
225         iso_send_msg(ISO_PDU_DR);
226         tcp_disconnect();
227 }