com.jcraft.jsch.ChannelForwardedTCPIP Maven / Gradle / Ivy
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/*
Copyright (c) 2002-2009 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcraft.jsch;
import java.net.*;
import java.io.*;
public class ChannelForwardedTCPIP extends Channel{
static java.util.Vector pool=new java.util.Vector();
static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
//static private final int LOCAL_WINDOW_SIZE_MAX=0x100000;
static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
static private final int TIMEOUT=10*1000;
SocketFactory factory=null;
private Socket socket=null;
private ForwardedTCPIPDaemon daemon=null;
String target;
int lport;
int rport;
ChannelForwardedTCPIP(){
super();
setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
io=new IO();
connected=true;
}
public void run(){
try{
if(lport==-1){
Class c=Class.forName(target);
daemon=(ForwardedTCPIPDaemon)c.newInstance();
PipedOutputStream out=new PipedOutputStream();
io.setInputStream(new PassiveInputStream(out
, 32*1024
), false);
daemon.setChannel(this, getInputStream(), out);
Object[] foo=getPort(getSession(), rport);
daemon.setArg((Object[])foo[3]);
new Thread(daemon).start();
}
else{
socket=(factory==null) ?
Util.createSocket(target, lport, TIMEOUT) :
factory.createSocket(target, lport);
socket.setTcpNoDelay(true);
io.setInputStream(socket.getInputStream());
io.setOutputStream(socket.getOutputStream());
}
sendOpenConfirmation();
}
catch(Exception e){
sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
close=true;
disconnect();
return;
}
thread=Thread.currentThread();
Buffer buf=new Buffer(rmpsize);
Packet packet=new Packet(buf);
int i=0;
try{
while(thread!=null &&
io!=null &&
io.in!=null){
i=io.in.read(buf.buffer,
14,
buf.buffer.length-14
-32 -20 // padding and mac
);
if(i<=0){
eof();
break;
}
packet.reset();
if(close)break;
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
buf.putInt(recipient);
buf.putInt(i);
buf.skip(i);
getSession().write(packet, this, i);
}
}
catch(Exception e){
//System.err.println(e);
}
//thread=null;
//eof();
disconnect();
}
void getData(Buffer buf){
setRecipient(buf.getInt());
setRemoteWindowSize(buf.getInt());
setRemotePacketSize(buf.getInt());
byte[] addr=buf.getString();
int port=buf.getInt();
byte[] orgaddr=buf.getString();
int orgport=buf.getInt();
/*
System.err.println("addr: "+new String(addr));
System.err.println("port: "+port);
System.err.println("orgaddr: "+new String(orgaddr));
System.err.println("orgport: "+orgport);
*/
Session _session=null;
try{
_session=getSession();
}
catch(JSchException e){
// session has been already down.
}
synchronized(pool){
for(int i=0; i=6){
this.factory=((SocketFactory)foo[5]);
}
break;
}
if(target==null){
//System.err.println("??");
}
}
}
static Object[] getPort(Session session, int rport){
synchronized(pool){
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy