1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
34 class ChannelX11 extends Channel{
36 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
37 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
39 static private final int TIMEOUT=10*1000;
41 private static String host="127.0.0.1";
42 private static int port=6000;
44 private boolean init=true;
46 static byte[] cookie=null;
47 private static byte[] cookie_hex=null;
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();
52 private static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
53 0x61,0x62,0x63,0x64,0x65,0x66};
55 private Socket socket = null;
57 static int revtable(byte foo){
58 for(int i=0; i<table.length; i++){
59 if(table[i]==foo)return i;
63 static void setCookie(String foo){
64 cookie_hex=Util.str2byte(foo);
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));
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);
77 Random random=Session.random;
80 random.fill(foo, 0, 16);
83 System.err.print("faked_cookie: ");
84 for(int i=0; i<foo.length; i++){
85 System.err.print(Integer.toHexString(foo[i]&0xff)+":");
87 System.err.println("");
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];
95 faked_cookie_hex_pool.put(session, bar);
105 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
106 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
107 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
109 type=Util.str2byte("x11");
114 socket=Util.createSocket(host, port, TIMEOUT);
115 socket.setTcpNoDelay(true);
117 io.setInputStream(socket.getInputStream());
118 io.setOutputStream(socket.getOutputStream());
121 //System.err.println(e);
129 socket=Util.createSocket(host, port, TIMEOUT);
130 socket.setTcpNoDelay(true);
132 io.setInputStream(socket.getInputStream());
133 io.setOutputStream(socket.getOutputStream());
134 sendOpenConfirmation();
137 sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
143 thread=Thread.currentThread();
144 Buffer buf=new Buffer(rmpsize);
145 Packet packet=new Packet(buf);
148 while(thread!=null &&
151 i=io.in.read(buf.buffer,
154 -32 -20 // padding and mac
162 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
163 buf.putInt(recipient);
166 getSession().write(packet, this, i);
170 //System.err.println(e);
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);
180 System.arraycopy(cache, 0, bar, 0, cache.length);
185 void write(byte[] foo, int s, int l) throws java.io.IOException {
186 //if(eof_local)return;
190 Session _session=null;
192 _session=getSession();
194 catch(JSchException e){
195 throw new java.io.IOException(e.toString());
198 foo=addCache(foo, s, l);
205 int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
206 int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
208 if((foo[s]&0xff)==0x42){
210 else if((foo[s]&0xff)==0x6c){
211 plen=((plen>>>8)&0xff)|((plen<<8)&0xff00);
212 dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00);
218 if(l<12+plen+((-plen)&3)+dlen)
221 byte[] bar=new byte[dlen];
222 System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
223 byte[] faked_cookie=null;
225 synchronized(faked_cookie_pool){
226 faked_cookie=(byte[])faked_cookie_pool.get(_session);
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)+":");
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)+":");
239 System.err.println("");
242 if(equals(bar, faked_cookie)){
244 System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
247 //System.err.println("wrong cookie");
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;