KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > ChannelX11


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jsch;
31
32 import java.net.*;
33
34 class ChannelX11 extends Channel{
35
36   static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
37   static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
38
39   static private final int TIMEOUT=10*1000;
40
41   private static String JavaDoc host="127.0.0.1";
42   private static int port=6000;
43
44   private boolean init=true;
45
46   static byte[] cookie=null;
47   private static byte[] cookie_hex=null;
48
49   private static java.util.Hashtable JavaDoc faked_cookie_pool=new java.util.Hashtable JavaDoc();
50   private static java.util.Hashtable JavaDoc faked_cookie_hex_pool=new java.util.Hashtable JavaDoc();
51
52   private static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
53                                0x61,0x62,0x63,0x64,0x65,0x66};
54
55   private Socket socket = null;
56
57   static int revtable(byte foo){
58     for(int i=0; i<table.length; i++){
59       if(table[i]==foo)return i;
60     }
61     return 0;
62   }
63   static void setCookie(String JavaDoc foo){
64     cookie_hex=foo.getBytes();
65     cookie=new byte[16];
66     for(int i=0; i<16; i++){
67     cookie[i]=(byte)(((revtable(cookie_hex[i*2])<<4)&0xf0) |
68              ((revtable(cookie_hex[i*2+1]))&0xf));
69     }
70   }
71   static void setHost(String JavaDoc foo){ host=foo; }
72   static void setPort(int foo){ port=foo; }
73   static byte[] getFakedCookie(Session session){
74     synchronized(faked_cookie_hex_pool){
75       byte[] foo=(byte[])faked_cookie_hex_pool.get(session);
76       if(foo==null){
77     Random random=Session.random;
78     foo=new byte[16];
79     synchronized(random){
80       random.fill(foo, 0, 16);
81     }
82 /*
83 System.err.print("faked_cookie: ");
84 for(int i=0; i<foo.length; i++){
85     System.err.print(Integer.toHexString(foo[i]&0xff)+":");
86 }
87 System.err.println("");
88 */

89     faked_cookie_pool.put(session, foo);
90     byte[] bar=new byte[32];
91     for(int i=0; i<16; i++){
92       bar[2*i]=table[(foo[i]>>>4)&0xf];
93       bar[2*i+1]=table[(foo[i])&0xf];
94     }
95     faked_cookie_hex_pool.put(session, bar);
96     foo=bar;
97       }
98       return foo;
99     }
100   }
101
102   ChannelX11(){
103     super();
104
105     setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
106     setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
107     setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
108
109     type="x11".getBytes();
110
111     connected=true;
112     /*
113     try{
114       socket=Util.createSocket(host, port, TIMEOUT);
115       socket.setTcpNoDelay(true);
116       io=new IO();
117       io.setInputStream(socket.getInputStream());
118       io.setOutputStream(socket.getOutputStream());
119     }
120     catch(Exception e){
121       //System.err.println(e);
122     }
123     */

124   }
125
126   public void run(){
127
128     try{
129       socket=Util.createSocket(host, port, TIMEOUT);
130       socket.setTcpNoDelay(true);
131       io=new IO();
132       io.setInputStream(socket.getInputStream());
133       io.setOutputStream(socket.getOutputStream());
134       sendOpenConfirmation();
135     }
136     catch(Exception JavaDoc e){
137       sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
138       close=true;
139       disconnect();
140       return;
141     }
142
143     thread=Thread.currentThread();
144     Buffer buf=new Buffer(rmpsize);
145     Packet packet=new Packet(buf);
146     int i=0;
147     try{
148       while(thread!=null &&
149             io!=null &&
150             io.in!=null){
151         i=io.in.read(buf.buffer,
152              14,
153              buf.buffer.length-14
154              -32 -20 // padding and mac
155
);
156     if(i<=0){
157       eof();
158           break;
159     }
160     if(close)break;
161         packet.reset();
162         buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
163         buf.putInt(recipient);
164         buf.putInt(i);
165         buf.skip(i);
166     session.write(packet, this, i);
167       }
168     }
169     catch(Exception JavaDoc e){
170       //System.err.println(e);
171
}
172     disconnect();
173   }
174
175   private byte[] cache=new byte[0];
176   private byte[] addCache(byte[] foo, int s, int l){
177     byte[] bar=new byte[cache.length+l];
178     System.arraycopy(foo, s, bar, cache.length, l);
179     if(cache.length>0)
180       System.arraycopy(cache, 0, bar, 0, cache.length);
181     cache=bar;
182     return cache;
183   }
184
185   void write(byte[] foo, int s, int l) throws java.io.IOException JavaDoc {
186     //if(eof_local)return;
187

188     if(init){
189
190       foo=addCache(foo, s, l);
191       s=0;
192       l=foo.length;
193
194       if(l<9)
195         return;
196
197       int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
198       int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
199
200       if((foo[s]&0xff)==0x42){
201       }
202       else if((foo[s]&0xff)==0x6c){
203          plen=((plen>>>8)&0xff)|((plen<<8)&0xff00);
204          dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00);
205       }
206       else{
207       // ??
208
}
209
210       if(l<12+plen+((-plen)&3)+dlen)
211         return;
212
213       byte[] bar=new byte[dlen];
214       System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
215       byte[] faked_cookie=null;
216
217       synchronized(faked_cookie_pool){
218     faked_cookie=(byte[])faked_cookie_pool.get(session);
219       }
220
221       /*
222 System.err.print("faked_cookie: ");
223 for(int i=0; i<faked_cookie.length; i++){
224     System.err.print(Integer.toHexString(faked_cookie[i]&0xff)+":");
225 }
226 System.err.println("");
227 System.err.print("bar: ");
228 for(int i=0; i<bar.length; i++){
229     System.err.print(Integer.toHexString(bar[i]&0xff)+":");
230 }
231 System.err.println("");
232       */

233
234       if(equals(bar, faked_cookie)){
235         if(cookie!=null)
236           System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
237       }
238       else{
239       //System.err.println("wrong cookie");
240
thread=null;
241           eof();
242           io.close();
243           disconnect();
244       }
245       init=false;
246       io.put(foo, s, l);
247       cache=null;
248       return;
249     }
250     io.put(foo, s, l);
251   }
252
253   private static boolean equals(byte[] foo, byte[] bar){
254     if(foo.length!=bar.length)return false;
255     for(int i=0; i<foo.length; i++){
256       if(foo[i]!=bar[i])return false;
257     }
258     return true;
259   }
260 }
261
Popular Tags