00001 using System;
00002 using System.IO;
00003 using Tamir.SharpSsh.java.util;
00004 using Tamir.SharpSsh.java.net;
00005 using Tamir.SharpSsh.java.lang;
00006 using Str = Tamir.SharpSsh.java.String;
00007
00008 namespace Tamir.SharpSsh.jsch
00009 {
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 public class ChannelForwardedTCPIP : Channel
00040 {
00041
00042 internal static java.util.Vector pool=new java.util.Vector();
00043
00044
00045 static private int LOCAL_WINDOW_SIZE_MAX=0x100000;
00046 static private int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00047
00048 internal SocketFactory factory=null;
00049 internal String target;
00050 internal int lport;
00051 internal int rport;
00052
00053 internal ChannelForwardedTCPIP() : base()
00054 {
00055 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00056 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00057 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00058 }
00059
00060 public override void init ()
00061 {
00062 try
00063 {
00064 io=new IO();
00065 if(lport==-1)
00066 {
00067 Class c=Class.forName(target);
00068 ForwardedTCPIPDaemon daemon=(ForwardedTCPIPDaemon)c.newInstance();
00069 daemon.setChannel(this);
00070 Object[] foo=getPort(session, rport);
00071 daemon.setArg((Object[])foo[3]);
00072 new Thread(daemon).start();
00073 connected=true;
00074 return;
00075 }
00076 else
00077 {
00078 Socket socket=(factory==null) ?
00079 new Socket(target, lport) :
00080 factory.createSocket(target, lport);
00081 socket.setTcpNoDelay(true);
00082 io.setInputStream(socket.getInputStream());
00083 io.setOutputStream(socket.getOutputStream());
00084 connected=true;
00085 }
00086 }
00087 catch(Exception e)
00088 {
00089 Console.WriteLine("target={0},port={1}",target,lport);
00090 Console.WriteLine(e);
00091 }
00092 }
00093
00094 public override void run()
00095 {
00096 thread=Thread.currentThread();
00097 Buffer buf=new Buffer(rmpsize);
00098 Packet packet=new Packet(buf);
00099 int i=0;
00100 try
00101 {
00102 while(thread!=null && io!=null && io.ins!=null)
00103 {
00104 i=io.ins.Read(buf.buffer,
00105 14,
00106 buf.buffer.Length-14
00107 -32 -20
00108 );
00109 if(i<=0)
00110 {
00111 eof();
00112 break;
00113 }
00114 packet.reset();
00115 if(_close)break;
00116 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00117 buf.putInt(recipient);
00118 buf.putInt(i);
00119 buf.skip(i);
00120 session.write(packet, this, i);
00121 }
00122 }
00123 catch(Exception e)
00124 {
00125
00126 }
00127
00128
00129
00130 disconnect();
00131 }
00132 internal override void getData(Buffer buf)
00133 {
00134 setRecipient(buf.getInt());
00135 setRemoteWindowSize(buf.getInt());
00136 setRemotePacketSize(buf.getInt());
00137 byte[] addr=buf.getString();
00138 int port=buf.getInt();
00139 byte[] orgaddr=buf.getString();
00140 int orgport=buf.getInt();
00141
00142
00143
00144
00145
00146
00147
00148
00149 lock(pool)
00150 {
00151 for(int i=0; i<pool.size(); i++)
00152 {
00153 Object[] foo=(Object[])(pool.elementAt(i));
00154 if(foo[0]!=session) continue;
00155 if(((Integer)foo[1]).intValue()!=port) continue;
00156 this.rport=port;
00157 this.target=(String)foo[2];
00158 if(foo[3]==null || (foo[3] is Object[])){ this.lport=-1; }
00159 else{ this.lport=((Integer)foo[3]).intValue(); }
00160 if(foo.Length>=5)
00161 {
00162 this.factory=((SocketFactory)foo[4]);
00163 }
00164 break;
00165 }
00166 if(target==null)
00167 {
00168 Console.WriteLine("??");
00169 }
00170 }
00171 }
00172
00173 internal static Object[] getPort(Session session, int rport)
00174 {
00175 lock(pool)
00176 {
00177 for(int i=0; i<pool.size(); i++)
00178 {
00179 Object[] bar=(Object[])(pool.elementAt(i));
00180 if(bar[0]!=session) continue;
00181 if(((Integer)bar[1]).intValue()!=rport) continue;
00182 return bar;
00183 }
00184 return null;
00185 }
00186 }
00187
00188 internal static String[] getPortForwarding(Session session)
00189 {
00190 java.util.Vector foo=new java.util.Vector();
00191 lock(pool)
00192 {
00193 for(int i=0; i<pool.size(); i++)
00194 {
00195 Object[] bar=(Object[])(pool.elementAt(i));
00196 if(bar[0]!=session) continue;
00197 if(bar[3]==null){ foo.addElement(bar[1]+":"+bar[2]+":"); }
00198 else{ foo.addElement(bar[1]+":"+bar[2]+":"+bar[3]); }
00199 }
00200 }
00201 String[] bar2=new String[foo.size()];
00202 for(int i=0; i<foo.size(); i++)
00203 {
00204 bar2[i]=(String)(foo.elementAt(i));
00205 }
00206 return bar2;
00207 }
00208
00209 internal static void addPort(Session session, int port, String target, int lport, SocketFactory factory)
00210 {
00211 lock(pool)
00212 {
00213 if(getPort(session, port)!=null)
00214 {
00215 throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
00216 }
00217 Object[] foo=new Object[5];
00218 foo[0]=session; foo[1]=new Integer(port);
00219 foo[2]=target; foo[3]=new Integer(lport);
00220 foo[4]=factory;
00221 pool.addElement(foo);
00222 }
00223 }
00224 internal static void addPort(Session session, int port, String daemon, Object[] arg)
00225 {
00226 lock(pool)
00227 {
00228 if(getPort(session, port)!=null)
00229 {
00230 throw new JSchException("PortForwardingR: remote port "+port+" is already registered.");
00231 }
00232 Object[] foo=new Object[4];
00233 foo[0]=session; foo[1]=new Integer(port);
00234 foo[2]=daemon; foo[3]=arg;
00235 pool.addElement(foo);
00236 }
00237 }
00238 internal static void delPort(ChannelForwardedTCPIP c)
00239 {
00240 delPort(c.session, c.rport);
00241 }
00242 internal static void delPort(Session session, int rport)
00243 {
00244 lock(pool)
00245 {
00246 Object[] foo=null;
00247 for(int i=0; i<pool.size(); i++)
00248 {
00249 Object[] bar=(Object[])(pool.elementAt(i));
00250 if(bar[0]!=session) continue;
00251 if(((Integer)bar[1]).intValue()!=rport) continue;
00252 foo=bar;
00253 break;
00254 }
00255 if(foo==null)return;
00256 pool.removeElement(foo);
00257 }
00258
00259 Buffer buf=new Buffer(100);
00260 Packet packet=new Packet(buf);
00261
00262 try
00263 {
00264
00265
00266
00267
00268
00269 packet.reset();
00270 buf.putByte((byte) 80);
00271 buf.putString(new Str("cancel-tcpip-forward").getBytes());
00272 buf.putByte((byte)0);
00273 buf.putString(new Str("0.0.0.0").getBytes());
00274 buf.putInt(rport);
00275 session.write(packet);
00276 }
00277 catch(Exception e)
00278 {
00279
00280 }
00281 }
00282 internal static void delPort(Session session)
00283 {
00284 int[] rport=null;
00285 int count=0;
00286 lock(pool)
00287 {
00288 rport=new int[pool.size()];
00289 for(int i=0; i<pool.size(); i++)
00290 {
00291 Object[] bar=(Object[])(pool.elementAt(i));
00292 if(bar[0]==session)
00293 {
00294 rport[count++]=((Integer)bar[1]).intValue();
00295 }
00296 }
00297 }
00298 for(int i=0; i<count; i++)
00299 {
00300 delPort(session, rport[i]);
00301 }
00302 }
00303 public int getRemotePort(){return rport;}
00304 void setSocketFactory(SocketFactory factory)
00305 {
00306 this.factory=factory;
00307 }
00308 }
00309
00310 }