ChannelX11.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.net.*;
00033 
00034 class ChannelX11 extends Channel{
00035 
00036   static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
00037   static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00038 
00039   static private final int TIMEOUT=10*1000;
00040 
00041   private static String host="127.0.0.1";
00042   private static int port=6000;
00043 
00044   private boolean init=true;
00045 
00046   static byte[] cookie=null;
00047   private static byte[] cookie_hex=null;
00048 
00049   private static java.util.Hashtable faked_cookie_pool=new java.util.Hashtable();
00050   private static java.util.Hashtable faked_cookie_hex_pool=new java.util.Hashtable();
00051 
00052   private static byte[] table={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
00053                                0x61,0x62,0x63,0x64,0x65,0x66};
00054 
00055   private Socket socket = null;
00056 
00057   static int revtable(byte foo){
00058     for(int i=0; i<table.length; i++){
00059       if(table[i]==foo)return i;
00060     }
00061     return 0;
00062   }
00063   static void setCookie(String foo){
00064     cookie_hex=Util.str2byte(foo); 
00065     cookie=new byte[16];
00066     for(int i=0; i<16; i++){
00067     cookie[i]=(byte)(((revtable(cookie_hex[i*2])<<4)&0xf0) |
00068              ((revtable(cookie_hex[i*2+1]))&0xf));
00069     }
00070   }
00071   static void setHost(String foo){ host=foo; }
00072   static void setPort(int foo){ port=foo; }
00073   static byte[] getFakedCookie(Session session){
00074     synchronized(faked_cookie_hex_pool){
00075       byte[] foo=(byte[])faked_cookie_hex_pool.get(session);
00076       if(foo==null){
00077     Random random=Session.random;
00078     foo=new byte[16];
00079     synchronized(random){
00080       random.fill(foo, 0, 16);
00081     }
00082 /*
00083 System.err.print("faked_cookie: ");
00084 for(int i=0; i<foo.length; i++){
00085     System.err.print(Integer.toHexString(foo[i]&0xff)+":");
00086 }
00087 System.err.println("");
00088 */
00089     faked_cookie_pool.put(session, foo);
00090     byte[] bar=new byte[32];
00091     for(int i=0; i<16; i++){
00092       bar[2*i]=table[(foo[i]>>>4)&0xf];
00093       bar[2*i+1]=table[(foo[i])&0xf];
00094     }
00095     faked_cookie_hex_pool.put(session, bar);
00096     foo=bar;
00097       }
00098       return foo;
00099     }
00100   }
00101 
00102   static void removeFakedCookie(Session session){
00103     synchronized(faked_cookie_hex_pool){
00104       faked_cookie_hex_pool.remove(session);
00105       faked_cookie_pool.remove(session);
00106     }
00107   }
00108 
00109   ChannelX11(){
00110     super();
00111 
00112     setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00113     setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00114     setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00115 
00116     type=Util.str2byte("x11");
00117 
00118     connected=true;
00119     /*
00120     try{ 
00121       socket=Util.createSocket(host, port, TIMEOUT);
00122       socket.setTcpNoDelay(true);
00123       io=new IO();
00124       io.setInputStream(socket.getInputStream());
00125       io.setOutputStream(socket.getOutputStream());
00126     }
00127     catch(Exception e){
00128       //System.err.println(e);
00129     }
00130     */
00131   }
00132 
00133   public void run(){
00134 
00135     try{ 
00136       socket=Util.createSocket(host, port, TIMEOUT);
00137       socket.setTcpNoDelay(true);
00138       io=new IO();
00139       io.setInputStream(socket.getInputStream());
00140       io.setOutputStream(socket.getOutputStream());
00141       sendOpenConfirmation();
00142     }
00143     catch(Exception e){
00144       sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
00145       close=true;
00146       disconnect();
00147       return;
00148     }
00149 
00150     thread=Thread.currentThread();
00151     Buffer buf=new Buffer(rmpsize);
00152     Packet packet=new Packet(buf);
00153     int i=0;
00154     try{
00155       while(thread!=null &&
00156             io!=null &&
00157             io.in!=null){
00158         i=io.in.read(buf.buffer, 
00159              14, 
00160              buf.buffer.length-14-Session.buffer_margin);
00161     if(i<=0){
00162       eof();
00163           break;
00164     }
00165     if(close)break;
00166         packet.reset();
00167         buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00168         buf.putInt(recipient);
00169         buf.putInt(i);
00170         buf.skip(i);
00171     getSession().write(packet, this, i);
00172       }
00173     }
00174     catch(Exception e){
00175       //System.err.println(e);
00176     }
00177     disconnect();
00178   }
00179 
00180   private byte[] cache=new byte[0];
00181   private byte[] addCache(byte[] foo, int s, int l){
00182     byte[] bar=new byte[cache.length+l];
00183     System.arraycopy(foo, s, bar, cache.length, l);
00184     if(cache.length>0)
00185       System.arraycopy(cache, 0, bar, 0, cache.length);
00186     cache=bar;
00187     return cache;
00188   }
00189 
00190   void write(byte[] foo, int s, int l) throws java.io.IOException {
00191     //if(eof_local)return;
00192 
00193     if(init){
00194 
00195       Session _session=null;
00196       try{
00197         _session=getSession();
00198       }
00199       catch(JSchException e){
00200         throw new java.io.IOException(e.toString());
00201       }
00202 
00203       foo=addCache(foo, s, l);
00204       s=0; 
00205       l=foo.length;
00206 
00207       if(l<9)
00208         return;
00209 
00210       int plen=(foo[s+6]&0xff)*256+(foo[s+7]&0xff);
00211       int dlen=(foo[s+8]&0xff)*256+(foo[s+9]&0xff);
00212 
00213       if((foo[s]&0xff)==0x42){
00214       }
00215       else if((foo[s]&0xff)==0x6c){
00216          plen=((plen>>>8)&0xff)|((plen<<8)&0xff00);
00217          dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00);
00218       }
00219       else{
00220       // ??
00221       }
00222 
00223       if(l<12+plen+((-plen)&3)+dlen)
00224         return;
00225 
00226       byte[] bar=new byte[dlen];
00227       System.arraycopy(foo, s+12+plen+((-plen)&3), bar, 0, dlen);
00228       byte[] faked_cookie=null;
00229 
00230       synchronized(faked_cookie_pool){
00231     faked_cookie=(byte[])faked_cookie_pool.get(_session);
00232       }
00233 
00234       /*
00235 System.err.print("faked_cookie: ");
00236 for(int i=0; i<faked_cookie.length; i++){
00237     System.err.print(Integer.toHexString(faked_cookie[i]&0xff)+":");
00238 }
00239 System.err.println("");
00240 System.err.print("bar: ");
00241 for(int i=0; i<bar.length; i++){
00242     System.err.print(Integer.toHexString(bar[i]&0xff)+":");
00243 }
00244 System.err.println("");
00245       */
00246 
00247       if(equals(bar, faked_cookie)){
00248         if(cookie!=null)
00249           System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen);
00250       }
00251       else{
00252       //System.err.println("wrong cookie");
00253           thread=null;
00254           eof();
00255           io.close();
00256           disconnect();
00257       }
00258       init=false;
00259       io.put(foo, s, l);
00260       cache=null;
00261       return;
00262     }
00263     io.put(foo, s, l);
00264   }
00265 
00266   private static boolean equals(byte[] foo, byte[] bar){
00267     if(foo.length!=bar.length)return false;
00268     for(int i=0; i<foo.length; i++){
00269       if(foo[i]!=bar[i])return false;
00270     }
00271     return true;
00272   }
00273 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1