00001 using System;
00002 using System.IO;
00003 using System.Threading;
00004 using Tamir.SharpSsh.java.net;
00005 using Tamir.SharpSsh.java.lang;
00006 using InetAddress = Tamir.SharpSsh.java.net.InetAddress;
00007 using String = Tamir.SharpSsh.java.String;
00008
00009 namespace Tamir.SharpSsh.jsch
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
00040 class PortWatcher : Tamir.SharpSsh.java.lang.Runnable
00041 {
00042 private static Tamir.SharpSsh.java.util.Vector pool=new Tamir.SharpSsh.java.util.Vector();
00043
00044
00045 internal Session session;
00046 internal int lport;
00047 internal int rport;
00048 internal String host;
00049 internal InetAddress boundaddress;
00050 internal Runnable thread;
00051 internal ServerSocket ss;
00052
00053 internal static String[] getPortForwarding(Session session)
00054 {
00055 java.util.Vector foo=new java.util.Vector();
00056 lock(pool)
00057 {
00058 for(int i=0; i<pool.size(); i++)
00059 {
00060 PortWatcher p=(PortWatcher)(pool.elementAt(i));
00061 if(p.session==session)
00062 {
00063 foo.addElement(p.lport+":"+p.host+":"+p.rport);
00064 }
00065 }
00066 }
00067 String[] bar=new String[foo.size()];
00068 for(int i=0; i<foo.size(); i++)
00069 {
00070 bar[i]=(String)(foo.elementAt(i));
00071 }
00072 return bar;
00073 }
00074 internal static PortWatcher getPort(Session session, String address, int lport)
00075 {
00076 InetAddress addr;
00077 try
00078 {
00079 addr=InetAddress.getByName(address);
00080 }
00081 catch(Exception uhe)
00082 {
00083 throw new JSchException("PortForwardingL: invalid address "+address+" specified.");
00084 }
00085 lock(pool)
00086 {
00087 for(int i=0; i<pool.size(); i++)
00088 {
00089 PortWatcher p=(PortWatcher)(pool.elementAt(i));
00090 if(p.session==session && p.lport==lport)
00091 {
00092 if(p.boundaddress.isAnyLocalAddress() ||
00093 p.boundaddress.equals(addr))
00094 return p;
00095 }
00096 }
00097 return null;
00098 }
00099 }
00100 internal static PortWatcher addPort(Session session, String address, int lport, String host, int rport, ServerSocketFactory ssf)
00101 {
00102 if(getPort(session, address, lport)!=null)
00103 {
00104 throw new JSchException("PortForwardingL: local port "+ address+":"+lport+" is already registered.");
00105 }
00106 PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
00107 pool.addElement(pw);
00108 return pw;
00109 }
00110 internal static void delPort(Session session, String address, int lport)
00111 {
00112 PortWatcher pw=getPort(session, address, lport);
00113 if(pw==null)
00114 {
00115 throw new JSchException("PortForwardingL: local port "+address+":"+lport+" is not registered.");
00116 }
00117 pw.delete();
00118 pool.removeElement(pw);
00119 }
00120 internal static void delPort(Session session)
00121 {
00122 lock(pool)
00123 {
00124 PortWatcher[] foo=new PortWatcher[pool.size()];
00125 int count=0;
00126 for(int i=0; i<pool.size(); i++)
00127 {
00128 PortWatcher p=(PortWatcher)(pool.elementAt(i));
00129 if(p.session==session)
00130 {
00131 p.delete();
00132 foo[count++]=p;
00133 }
00134 }
00135 for(int i=0; i<count; i++)
00136 {
00137 PortWatcher p=foo[i];
00138 pool.removeElement(p);
00139 }
00140 }
00141 }
00142 internal PortWatcher(Session session,
00143 String address, int lport,
00144 String host, int rport,
00145 ServerSocketFactory factory)
00146 {
00147 this.session=session;
00148 this.lport=lport;
00149 this.host=host;
00150 this.rport=rport;
00151 try
00152 {
00153 boundaddress=InetAddress.getByName(address);
00154 ss=(factory==null) ?
00155 new ServerSocket(lport, 0, boundaddress) :
00156 factory.createServerSocket(lport, 0, boundaddress);
00157 }
00158 catch(Exception e)
00159 {
00160 Console.WriteLine(e);
00161 throw new JSchException("PortForwardingL: local port "+address+":"+lport+" cannot be bound.");
00162 }
00163 }
00164
00165 public void run()
00166 {
00167 Buffer buf=new Buffer(300);
00168 Packet packet=new Packet(buf);
00169 thread=this;
00170 try
00171 {
00172 while(thread!=null)
00173 {
00174 Socket socket=ss.accept();
00175 socket.setTcpNoDelay(true);
00176 Stream In=socket.getInputStream();
00177 Stream Out=socket.getOutputStream();
00178 ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
00179 channel.init();
00180 channel.setInputStream(In);
00181 channel.setOutputStream(Out);
00182 session.addChannel(channel);
00183 ((ChannelDirectTCPIP)channel).setHost(host);
00184 ((ChannelDirectTCPIP)channel).setPort(rport);
00185 ((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress());
00186 ((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort());
00187 channel.connect();
00188 if(channel.exitstatus!=-1)
00189 {
00190 }
00191 }
00192 }
00193 catch(Exception e)
00194 {
00195
00196 }
00197
00198 delete();
00199 }
00200
00201 internal void delete()
00202 {
00203 thread=null;
00204 try
00205 {
00206 if(ss!=null)ss.close();
00207 ss=null;
00208 }
00209 catch(Exception e)
00210 {
00211 }
00212 }
00213 }
00214 }