~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/ChannelX11.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using System.Collections;
 
34
using System.IO;
 
35
using System.Net.Sockets;
 
36
using NSch;
 
37
using Sharpen;
 
38
 
 
39
namespace NSch
 
40
{
 
41
        internal class ChannelX11 : Channel
 
42
        {
 
43
                private const int LOCAL_WINDOW_SIZE_MAX = unchecked((int)(0x20000));
 
44
 
 
45
                private const int LOCAL_MAXIMUM_PACKET_SIZE = unchecked((int)(0x4000));
 
46
 
 
47
                private const int TIMEOUT = 10 * 1000;
 
48
 
 
49
                private static string host = "127.0.0.1";
 
50
 
 
51
                private static int port = 6000;
 
52
 
 
53
                private bool init = true;
 
54
 
 
55
                internal static byte[] cookie = null;
 
56
 
 
57
                private static byte[] cookie_hex = null;
 
58
 
 
59
                private static Hashtable faked_cookie_pool = new Hashtable();
 
60
 
 
61
                private static Hashtable faked_cookie_hex_pool = new Hashtable();
 
62
 
 
63
                private static byte[] table = new byte[] { unchecked((int)(0x30)), unchecked((int
 
64
                        )(0x31)), unchecked((int)(0x32)), unchecked((int)(0x33)), unchecked((int)(0x34))
 
65
                        , unchecked((int)(0x35)), unchecked((int)(0x36)), unchecked((int)(0x37)), unchecked(
 
66
                        (int)(0x38)), unchecked((int)(0x39)), unchecked((int)(0x61)), unchecked((int)(0x62
 
67
                        )), unchecked((int)(0x63)), unchecked((int)(0x64)), unchecked((int)(0x65)), unchecked(
 
68
                        (int)(0x66)) };
 
69
 
 
70
                private Socket socket = null;
 
71
 
 
72
                internal static int Revtable(byte foo)
 
73
                {
 
74
                        for (int i = 0; i < table.Length; i++)
 
75
                        {
 
76
                                if (table[i] == foo)
 
77
                                {
 
78
                                        return i;
 
79
                                }
 
80
                        }
 
81
                        return 0;
 
82
                }
 
83
 
 
84
                internal static void SetCookie(string foo)
 
85
                {
 
86
                        cookie_hex = Util.Str2byte(foo);
 
87
                        cookie = new byte[16];
 
88
                        for (int i = 0; i < 16; i++)
 
89
                        {
 
90
                                cookie[i] = unchecked((byte)(((Revtable(cookie_hex[i * 2]) << 4) & unchecked((int
 
91
                                        )(0xf0))) | ((Revtable(cookie_hex[i * 2 + 1])) & unchecked((int)(0xf)))));
 
92
                        }
 
93
                }
 
94
 
 
95
                internal static void SetHost(string foo)
 
96
                {
 
97
                        host = foo;
 
98
                }
 
99
 
 
100
                internal static void SetPort(int foo)
 
101
                {
 
102
                        port = foo;
 
103
                }
 
104
 
 
105
                internal static byte[] GetFakedCookie(Session session)
 
106
                {
 
107
                        lock (faked_cookie_hex_pool)
 
108
                        {
 
109
                                byte[] foo = (byte[])faked_cookie_hex_pool[session];
 
110
                                if (foo == null)
 
111
                                {
 
112
                                        Random random = Session.random;
 
113
                                        foo = new byte[16];
 
114
                                        lock (random)
 
115
                                        {
 
116
                                                random.Fill(foo, 0, 16);
 
117
                                        }
 
118
                                        faked_cookie_pool.Put(session, foo);
 
119
                                        byte[] bar = new byte[32];
 
120
                                        for (int i = 0; i < 16; i++)
 
121
                                        {
 
122
                                                bar[2 * i] = table[(foo[i] >> 4) & unchecked((int)(0xf))];
 
123
                                                bar[2 * i + 1] = table[(foo[i]) & unchecked((int)(0xf))];
 
124
                                        }
 
125
                                        faked_cookie_hex_pool.Put(session, bar);
 
126
                                        foo = bar;
 
127
                                }
 
128
                                return foo;
 
129
                        }
 
130
                }
 
131
 
 
132
                public ChannelX11() : base()
 
133
                {
 
134
                        SetLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
 
135
                        SetLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
 
136
                        SetLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
 
137
                        type = Util.Str2byte("x11");
 
138
                        connected = true;
 
139
                }
 
140
 
 
141
                public override void Run()
 
142
                {
 
143
                        try
 
144
                        {
 
145
                                socket = Util.CreateSocket(host, port, TIMEOUT);
 
146
                                socket.NoDelay = true;
 
147
                                io = new IO();
 
148
                                io.SetInputStream(socket.GetInputStream());
 
149
                                io.SetOutputStream(socket.GetOutputStream());
 
150
                                SendOpenConfirmation();
 
151
                        }
 
152
                        catch (Exception)
 
153
                        {
 
154
                                SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
 
155
                                close = true;
 
156
                                Disconnect();
 
157
                                return;
 
158
                        }
 
159
                        thread = Sharpen.Thread.CurrentThread();
 
160
                        Buffer buf = new Buffer(rmpsize);
 
161
                        Packet packet = new Packet(buf);
 
162
                        int i = 0;
 
163
                        try
 
164
                        {
 
165
                                while (thread != null && io != null && io.@in != null)
 
166
                                {
 
167
                                        i = io.@in.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
 
168
                                        // padding and mac
 
169
                                        if (i <= 0)
 
170
                                        {
 
171
                                                Eof();
 
172
                                                break;
 
173
                                        }
 
174
                                        if (close)
 
175
                                        {
 
176
                                                break;
 
177
                                        }
 
178
                                        packet.Reset();
 
179
                                        buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
 
180
                                        buf.PutInt(recipient);
 
181
                                        buf.PutInt(i);
 
182
                                        buf.Skip(i);
 
183
                                        GetSession().Write(packet, this, i);
 
184
                                }
 
185
                        }
 
186
                        catch (Exception)
 
187
                        {
 
188
                        }
 
189
                        //System.err.println(e);
 
190
                        Disconnect();
 
191
                }
 
192
 
 
193
                private byte[] cache = new byte[0];
 
194
 
 
195
                private byte[] AddCache(byte[] foo, int s, int l)
 
196
                {
 
197
                        byte[] bar = new byte[cache.Length + l];
 
198
                        System.Array.Copy(foo, s, bar, cache.Length, l);
 
199
                        if (cache.Length > 0)
 
200
                        {
 
201
                                System.Array.Copy(cache, 0, bar, 0, cache.Length);
 
202
                        }
 
203
                        cache = bar;
 
204
                        return cache;
 
205
                }
 
206
 
 
207
                /// <exception cref="System.IO.IOException"></exception>
 
208
                internal override void Write(byte[] foo, int s, int l)
 
209
                {
 
210
                        //if(eof_local)return;
 
211
                        if (init)
 
212
                        {
 
213
                                Session _session = null;
 
214
                                try
 
215
                                {
 
216
                                        _session = GetSession();
 
217
                                }
 
218
                                catch (JSchException e)
 
219
                                {
 
220
                                        throw new IOException(e.ToString());
 
221
                                }
 
222
                                foo = AddCache(foo, s, l);
 
223
                                s = 0;
 
224
                                l = foo.Length;
 
225
                                if (l < 9)
 
226
                                {
 
227
                                        return;
 
228
                                }
 
229
                                int plen = (foo[s + 6] & unchecked((int)(0xff))) * 256 + (foo[s + 7] & unchecked(
 
230
                                        (int)(0xff)));
 
231
                                int dlen = (foo[s + 8] & unchecked((int)(0xff))) * 256 + (foo[s + 9] & unchecked(
 
232
                                        (int)(0xff)));
 
233
                                if ((foo[s] & unchecked((int)(0xff))) == unchecked((int)(0x42)))
 
234
                                {
 
235
                                }
 
236
                                else
 
237
                                {
 
238
                                        if ((foo[s] & unchecked((int)(0xff))) == unchecked((int)(0x6c)))
 
239
                                        {
 
240
                                                plen = (((int)(((uint)plen) >> 8)) & unchecked((int)(0xff))) | ((plen << 8) & unchecked(
 
241
                                                        (int)(0xff00)));
 
242
                                                dlen = (((int)(((uint)dlen) >> 8)) & unchecked((int)(0xff))) | ((dlen << 8) & unchecked(
 
243
                                                        (int)(0xff00)));
 
244
                                        }
 
245
                                }
 
246
                                // ??
 
247
                                if (l < 12 + plen + ((-plen) & 3) + dlen)
 
248
                                {
 
249
                                        return;
 
250
                                }
 
251
                                byte[] bar = new byte[dlen];
 
252
                                System.Array.Copy(foo, s + 12 + plen + ((-plen) & 3), bar, 0, dlen);
 
253
                                byte[] faked_cookie = null;
 
254
                                lock (faked_cookie_pool)
 
255
                                {
 
256
                                        faked_cookie = (byte[])faked_cookie_pool[_session];
 
257
                                }
 
258
                                if (Equals(bar, faked_cookie))
 
259
                                {
 
260
                                        if (cookie != null)
 
261
                                        {
 
262
                                                System.Array.Copy(cookie, 0, foo, s + 12 + plen + ((-plen) & 3), dlen);
 
263
                                        }
 
264
                                }
 
265
                                else
 
266
                                {
 
267
                                        //System.err.println("wrong cookie");
 
268
                                        thread = null;
 
269
                                        Eof();
 
270
                                        io.Close();
 
271
                                        Disconnect();
 
272
                                }
 
273
                                init = false;
 
274
                                io.Put(foo, s, l);
 
275
                                cache = null;
 
276
                                return;
 
277
                        }
 
278
                        io.Put(foo, s, l);
 
279
                }
 
280
 
 
281
                private static bool Equals(byte[] foo, byte[] bar)
 
282
                {
 
283
                        if (foo.Length != bar.Length)
 
284
                        {
 
285
                                return false;
 
286
                        }
 
287
                        for (int i = 0; i < foo.Length; i++)
 
288
                        {
 
289
                                if (foo[i] != bar[i])
 
290
                                {
 
291
                                        return false;
 
292
                                }
 
293
                        }
 
294
                        return true;
 
295
                }
 
296
        }
 
297
}