00001 using System;
00002 using System.Net;
00003 using System.Net.Sockets;
00004 using Tamir.SharpSsh.java.lang;
00005
00006 namespace Tamir.SharpSsh.jsch
00007 {
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 internal class ChannelX11 : Channel
00037 {
00038
00039 private const int LOCAL_WINDOW_SIZE_MAX=0x20000;
00040 private const int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00041
00042 internal static String host="127.0.0.1";
00043 internal static int port=6000;
00044
00045 internal bool _init=true;
00046
00047 internal static byte[] cookie=null;
00048
00049 internal static byte[] cookie_hex=null;
00050
00051 private static System.Collections.Hashtable faked_cookie_pool=new System.Collections.Hashtable();
00052 private static System.Collections.Hashtable faked_cookie_hex_pool=new System.Collections.Hashtable();
00053
00054 internal static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
00055 0x61,0x62,0x63,0x64,0x65,0x66};
00056 internal static int revtable(byte foo)
00057 {
00058 for(int i=0; i<table.Length; i++)
00059 {
00060 if(table[i]==foo)return i;
00061 }
00062 return 0;
00063 }
00064 internal static void setCookie(String foo)
00065 {
00066 cookie_hex=Util.getBytes(foo);
00067 cookie=new byte[16];
00068 for(int i=0; i<16; i++)
00069 {
00070 cookie[i]=(byte)(((revtable(cookie_hex[i*2])<<4)&0xf0) |
00071 ((revtable(cookie_hex[i*2+1]))&0xf));
00072 }
00073 }
00074 internal static void setHost(String foo){ host=foo; }
00075 internal static void setPort(int foo){ port=foo; }
00076 internal static byte[] getFakedCookie(Session session)
00077 {
00078 lock(faked_cookie_hex_pool)
00079 {
00080 byte[] foo=(byte[])faked_cookie_hex_pool[session];
00081 if(foo==null)
00082 {
00083 Random random=Session.random;
00084 foo=new byte[16];
00085 lock(random)
00086 {
00087 random.fill(foo, 0, 16);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 faked_cookie_pool.Add(session, foo);
00097 byte[] bar=new byte[32];
00098 for(int i=0; i<16; i++)
00099 {
00100 bar[2*i]=table[(foo[i]>>4)&0xf];
00101 bar[2*i+1]=table[(foo[i])&0xf];
00102 }
00103 faked_cookie_hex_pool.Add(session, bar);
00104 foo=bar;
00105 }
00106 return foo;
00107 }
00108 }
00109
00110 Socket socket = null;
00111 internal ChannelX11():base()
00112 {
00113
00114 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00115 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00116 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00117
00118 type=Util.getBytes("x11");
00119 try
00120 {
00121 IPEndPoint ep = new IPEndPoint(Dns.GetHostByName(host).AddressList[0], port);
00122 socket=new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
00123 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
00124 socket.Connect(ep);
00125 io=new IO();
00126 NetworkStream ns = new NetworkStream( socket );
00127 io.setInputStream(ns);
00128 io.setOutputStream(ns);
00129 }
00130 catch(Exception e)
00131 {
00132 Console.WriteLine(e);
00133 }
00134 }
00135
00136 public override void run()
00137 {
00138 thread=Thread.currentThread();
00139 Buffer buf=new Buffer(rmpsize);
00140 Packet packet=new Packet(buf);
00141 int i=0;
00142 try
00143 {
00144 while(thread!=null)
00145 {
00146 i=io.ins.Read(buf.buffer,
00147 14,
00148 buf.buffer.Length-14
00149 -16 -20
00150 );
00151 if(i<=0)
00152 {
00153 eof();
00154 break;
00155 }
00156 if(_close)break;
00157 packet.reset();
00158 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00159 buf.putInt(recipient);
00160 buf.putInt(i);
00161 buf.skip(i);
00162 session.write(packet, this, i);
00163 }
00164 }
00165 catch
00166 {
00167
00168 }
00169 thread=null;
00170 }
00171
00172 internal override void write(byte[] foo, int s, int l)
00173 {
00174
00175
00176 if(_init)
00177 {
00178 int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
00179 int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
00180 if((foo[s]&0xff)==0x42)
00181 {
00182 }
00183 else if((foo[s]&0xff)==0x6c)
00184 {
00185 plen=(int)(((uint)plen>>8)&0xff)|((plen<<8)&0xff00);
00186 dlen=(int)(((uint)dlen>>8)&0xff)|((dlen<<8)&0xff00);
00187 }
00188 else
00189 {
00190
00191 }
00192 byte[] bar=new byte[dlen];
00193 Array.Copy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
00194 byte[] faked_cookie=(byte[])faked_cookie_pool[session];
00195
00196 if(equals(bar, faked_cookie))
00197 {
00198 if(cookie!=null)
00199 Array.Copy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
00200 }
00201 else
00202 {
00203 Console.WriteLine("wrong cookie");
00204 }
00205 _init=false;
00206 }
00207 io.put(foo, s, l);
00208 }
00209
00210 public override void disconnect()
00211 {
00212 close();
00213 thread=null;
00214 try
00215 {
00216 if(io!=null)
00217 {
00218 try
00219 {
00220 if(io.ins!=null)
00221 io.ins.Close();
00222 }
00223 catch{}
00224 try
00225 {
00226 if(io.outs!=null)
00227 io.outs.Close();
00228 }
00229 catch{}
00230 }
00231 try
00232 {
00233 if(socket!=null)
00234 socket.Close();
00235 }
00236 catch{}
00237 }
00238 catch(Exception e)
00239 {
00240 Console.WriteLine(e.StackTrace);
00241 }
00242 io=null;
00243 Channel.del(this);
00244 }
00245
00246 private static bool equals(byte[] foo, byte[] bar)
00247 {
00248 if(foo.Length!=bar.Length)return false;
00249 for(int i=0; i<foo.Length; i++)
00250 {
00251 if(foo[i]!=bar[i])return false;
00252 }
00253 return true;
00254 }
00255 }
00256
00257 }