1 2 29 30 package com.jcraft.jsch; 31 32 import java.io.*; 33 34 public class ChannelDirectTCPIP 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 String host; 40 int port; 41 42 String originator_IP_address="127.0.0.1"; 43 int originator_port=0; 44 45 ChannelDirectTCPIP(){ 46 super(); 47 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX); 48 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX); 49 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE); 50 } 51 52 void init (){ 53 try{ 54 io=new IO(); 55 } 56 catch(Exception e){ 57 System.err.println(e); 58 } 59 } 60 61 public void connect() throws JSchException{ 62 try{ 63 if(!session.isConnected()){ 64 throw new JSchException("session is down"); 65 } 66 Buffer buf=new Buffer(150); 67 Packet packet=new Packet(buf); 68 75 packet.reset(); 76 buf.putByte((byte)90); 77 buf.putString("direct-tcpip".getBytes()); 78 buf.putInt(id); 79 buf.putInt(lwsize); 80 buf.putInt(lmpsize); 81 buf.putString(host.getBytes()); 82 buf.putInt(port); 83 buf.putString(originator_IP_address.getBytes()); 84 buf.putInt(originator_port); 85 session.write(packet); 86 87 int retry=1000; 88 try{ 89 while(this.getRecipient()==-1 && 90 session.isConnected() && 91 retry>0 && 92 !eof_remote){ 93 Thread.sleep(50); 95 retry--; 96 } 97 } 98 catch(Exception ee){ 99 } 100 if(!session.isConnected()){ 101 throw new JSchException("session is down"); 102 } 103 if(retry==0 || this.eof_remote){ 104 throw new JSchException("channel is not opened."); 105 } 106 112 113 connected=true; 114 115 thread=new Thread (this); 116 thread.start(); 117 } 118 catch(Exception e){ 119 io.close(); 120 io=null; 121 Channel.del(this); 122 if (e instanceof JSchException) { 123 throw (JSchException) e; 124 } 125 } 126 } 127 128 public void run(){ 129 Buffer buf=new Buffer(rmpsize); 130 Packet packet=new Packet(buf); 131 int i=0; 132 try{ 133 while(isConnected() && 134 thread!=null && 135 io!=null && 136 io.in!=null){ 137 i=io.in.read(buf.buffer, 138 14, 139 buf.buffer.length-14 140 -32 -20 ); 142 143 if(i<=0){ 144 eof(); 145 break; 146 } 147 if(close)break; 148 packet.reset(); 149 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); 150 buf.putInt(recipient); 151 buf.putInt(i); 152 buf.skip(i); 153 session.write(packet, this, i); 154 } 155 } 156 catch(Exception e){ 157 } 158 disconnect(); 159 } 161 162 public void setInputStream(InputStream in){ 163 io.setInputStream(in); 164 } 165 public void setOutputStream(OutputStream out){ 166 io.setOutputStream(out); 167 } 168 169 public void setHost(String host){this.host=host;} 170 public void setPort(int port){this.port=port;} 171 public void setOrgIPAddress(String foo){this.originator_IP_address=foo;} 172 public void setOrgPort(int foo){this.originator_port=foo;} 173 } 174 | Popular Tags |