]> Joshua Wise's Git repositories - dumload.git/blob - src/com/jcraft/jsch/ChannelX11.java
A few icon tweaks
[dumload.git] / src / com / jcraft / jsch / ChannelX11.java
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2010 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 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 faked_cookie_pool=new java.util.Hashtable();
50   private static java.util.Hashtable faked_cookie_hex_pool=new java.util.Hashtable();
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 foo){
64     cookie_hex=Util.str2byte(foo); 
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 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=Util.str2byte("x11");
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 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         getSession().write(packet, this, i);
167       }
168     }
169     catch(Exception 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 {
186     //if(eof_local)return;
187
188     if(init){
189
190       Session _session=null;
191       try{
192         _session=getSession();
193       }
194       catch(JSchException e){
195         throw new java.io.IOException(e.toString());
196       }
197
198       foo=addCache(foo, s, l);
199       s=0; 
200       l=foo.length;
201
202       if(l<9)
203         return;
204
205       int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
206       int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
207
208       if((foo[s]&0xff)==0x42){
209       }
210       else if((foo[s]&0xff)==0x6c){
211          plen=((plen>>>8)&0xff)|((plen<<8)&0xff00);
212          dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00);
213       }
214       else{
215           // ??
216       }
217
218       if(l<12+plen+((-plen)&3)+dlen)
219         return;
220
221       byte[] bar=new byte[dlen];
222       System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
223       byte[] faked_cookie=null;
224
225       synchronized(faked_cookie_pool){
226         faked_cookie=(byte[])faked_cookie_pool.get(_session);
227       }
228
229       /*
230 System.err.print("faked_cookie: ");
231 for(int i=0; i<faked_cookie.length; i++){
232     System.err.print(Integer.toHexString(faked_cookie[i]&0xff)+":");
233 }
234 System.err.println("");
235 System.err.print("bar: ");
236 for(int i=0; i<bar.length; i++){
237     System.err.print(Integer.toHexString(bar[i]&0xff)+":");
238 }
239 System.err.println("");
240       */
241
242       if(equals(bar, faked_cookie)){
243         if(cookie!=null)
244           System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
245       }
246       else{
247           //System.err.println("wrong cookie");
248           thread=null;
249           eof();
250           io.close();
251           disconnect();
252       }
253       init=false;
254       io.put(foo, s, l);
255       cache=null;
256       return;
257     }
258     io.put(foo, s, l);
259   }
260
261   private static boolean equals(byte[] foo, byte[] bar){
262     if(foo.length!=bar.length)return false;
263     for(int i=0; i<foo.length; i++){
264       if(foo[i]!=bar[i])return false;
265     }
266     return true;
267   }
268 }
This page took 0.03543 seconds and 4 git commands to generate.