2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
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.
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.
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.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
33
using System.Collections;
35
using System.Net.Sockets;
41
internal class ChannelX11 : Channel
43
private const int LOCAL_WINDOW_SIZE_MAX = unchecked((int)(0x20000));
45
private const int LOCAL_MAXIMUM_PACKET_SIZE = unchecked((int)(0x4000));
47
private const int TIMEOUT = 10 * 1000;
49
private static string host = "127.0.0.1";
51
private static int port = 6000;
53
private bool init = true;
55
internal static byte[] cookie = null;
57
private static byte[] cookie_hex = null;
59
private static Hashtable faked_cookie_pool = new Hashtable();
61
private static Hashtable faked_cookie_hex_pool = new Hashtable();
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(
70
private Socket socket = null;
72
internal static int Revtable(byte foo)
74
for (int i = 0; i < table.Length; i++)
84
internal static void SetCookie(string foo)
86
cookie_hex = Util.Str2byte(foo);
87
cookie = new byte[16];
88
for (int i = 0; i < 16; i++)
90
cookie[i] = unchecked((byte)(((Revtable(cookie_hex[i * 2]) << 4) & unchecked((int
91
)(0xf0))) | ((Revtable(cookie_hex[i * 2 + 1])) & unchecked((int)(0xf)))));
95
internal static void SetHost(string foo)
100
internal static void SetPort(int foo)
105
internal static byte[] GetFakedCookie(Session session)
107
lock (faked_cookie_hex_pool)
109
byte[] foo = (byte[])faked_cookie_hex_pool[session];
112
Random random = Session.random;
116
random.Fill(foo, 0, 16);
118
faked_cookie_pool.Put(session, foo);
119
byte[] bar = new byte[32];
120
for (int i = 0; i < 16; i++)
122
bar[2 * i] = table[(foo[i] >> 4) & unchecked((int)(0xf))];
123
bar[2 * i + 1] = table[(foo[i]) & unchecked((int)(0xf))];
125
faked_cookie_hex_pool.Put(session, bar);
132
public ChannelX11() : base()
134
SetLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
135
SetLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
136
SetLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
137
type = Util.Str2byte("x11");
141
public override void Run()
145
socket = Util.CreateSocket(host, port, TIMEOUT);
146
socket.NoDelay = true;
148
io.SetInputStream(socket.GetInputStream());
149
io.SetOutputStream(socket.GetOutputStream());
150
SendOpenConfirmation();
154
SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
159
thread = Sharpen.Thread.CurrentThread();
160
Buffer buf = new Buffer(rmpsize);
161
Packet packet = new Packet(buf);
165
while (thread != null && io != null && io.@in != null)
167
i = io.@in.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20);
179
buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
180
buf.PutInt(recipient);
183
GetSession().Write(packet, this, i);
189
//System.err.println(e);
193
private byte[] cache = new byte[0];
195
private byte[] AddCache(byte[] foo, int s, int l)
197
byte[] bar = new byte[cache.Length + l];
198
System.Array.Copy(foo, s, bar, cache.Length, l);
199
if (cache.Length > 0)
201
System.Array.Copy(cache, 0, bar, 0, cache.Length);
207
/// <exception cref="System.IO.IOException"></exception>
208
internal override void Write(byte[] foo, int s, int l)
210
//if(eof_local)return;
213
Session _session = null;
216
_session = GetSession();
218
catch (JSchException e)
220
throw new IOException(e.ToString());
222
foo = AddCache(foo, s, l);
229
int plen = (foo[s + 6] & unchecked((int)(0xff))) * 256 + (foo[s + 7] & unchecked(
231
int dlen = (foo[s + 8] & unchecked((int)(0xff))) * 256 + (foo[s + 9] & unchecked(
233
if ((foo[s] & unchecked((int)(0xff))) == unchecked((int)(0x42)))
238
if ((foo[s] & unchecked((int)(0xff))) == unchecked((int)(0x6c)))
240
plen = (((int)(((uint)plen) >> 8)) & unchecked((int)(0xff))) | ((plen << 8) & unchecked(
242
dlen = (((int)(((uint)dlen) >> 8)) & unchecked((int)(0xff))) | ((dlen << 8) & unchecked(
247
if (l < 12 + plen + ((-plen) & 3) + dlen)
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)
256
faked_cookie = (byte[])faked_cookie_pool[_session];
258
if (Equals(bar, faked_cookie))
262
System.Array.Copy(cookie, 0, foo, s + 12 + plen + ((-plen) & 3), dlen);
267
//System.err.println("wrong cookie");
281
private static bool Equals(byte[] foo, byte[] bar)
283
if (foo.Length != bar.Length)
287
for (int i = 0; i < foo.Length; i++)
289
if (foo[i] != bar[i])