PortWatcher.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 import java.io.*;
00034 
00035 class PortWatcher implements Runnable{
00036   private static java.util.Vector pool=new java.util.Vector();
00037   private static InetAddress anyLocalAddress=null;
00038   static{
00039     // 0.0.0.0
00040 /*
00041     try{ anyLocalAddress=InetAddress.getByAddress(new byte[4]); }
00042     catch(UnknownHostException e){
00043     }
00044 */
00045     try{ anyLocalAddress=InetAddress.getByName("0.0.0.0"); }
00046     catch(UnknownHostException e){
00047     }
00048   }
00049 
00050   Session session;
00051   int lport;
00052   int rport;
00053   String host;
00054   InetAddress boundaddress;
00055   Runnable thread;
00056   ServerSocket ss;
00057 
00058   static String[] getPortForwarding(Session session){
00059     java.util.Vector foo=new java.util.Vector();
00060     synchronized(pool){
00061       for(int i=0; i<pool.size(); i++){
00062     PortWatcher p=(PortWatcher)(pool.elementAt(i));
00063     if(p.session==session){
00064       foo.addElement(p.lport+":"+p.host+":"+p.rport);
00065     }
00066       }
00067     }
00068     String[] bar=new String[foo.size()];
00069     for(int i=0; i<foo.size(); i++){
00070       bar[i]=(String)(foo.elementAt(i));
00071     }
00072     return bar;
00073   }
00074   static PortWatcher getPort(Session session, String address, int lport) throws JSchException{
00075     InetAddress addr;
00076     try{
00077       addr=InetAddress.getByName(address);
00078     }
00079     catch(UnknownHostException uhe){
00080       throw new JSchException("PortForwardingL: invalid address "+address+" specified.", uhe);
00081     }
00082     synchronized(pool){
00083       for(int i=0; i<pool.size(); i++){
00084     PortWatcher p=(PortWatcher)(pool.elementAt(i));
00085     if(p.session==session && p.lport==lport){
00086       if(/*p.boundaddress.isAnyLocalAddress() ||*/
00087              (anyLocalAddress!=null &&  p.boundaddress.equals(anyLocalAddress)) ||
00088          p.boundaddress.equals(addr))
00089       return p;
00090     }
00091       }
00092       return null;
00093     }
00094   }
00095   static PortWatcher addPort(Session session, String address, int lport, String host, int rport, ServerSocketFactory ssf) throws JSchException{
00096     if(getPort(session, address, lport)!=null){
00097       throw new JSchException("PortForwardingL: local port "+ address+":"+lport+" is already registered.");
00098     }
00099     PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
00100     pool.addElement(pw);
00101     return pw;
00102   }
00103   static void delPort(Session session, String address, int lport) throws JSchException{
00104     PortWatcher pw=getPort(session, address, lport);
00105     if(pw==null){
00106       throw new JSchException("PortForwardingL: local port "+address+":"+lport+" is not registered.");
00107     }
00108     pw.delete();
00109     pool.removeElement(pw);
00110   }
00111   static void delPort(Session session){
00112     synchronized(pool){
00113       PortWatcher[] foo=new PortWatcher[pool.size()];
00114       int count=0;
00115       for(int i=0; i<pool.size(); i++){
00116     PortWatcher p=(PortWatcher)(pool.elementAt(i));
00117     if(p.session==session) {
00118       p.delete();
00119       foo[count++]=p;
00120     }
00121       }
00122       for(int i=0; i<count; i++){
00123     PortWatcher p=foo[i];
00124     pool.removeElement(p);
00125       }
00126     }
00127   }
00128   PortWatcher(Session session, 
00129           String address, int lport, 
00130           String host, int rport,
00131               ServerSocketFactory factory) throws JSchException{
00132     this.session=session;
00133     this.lport=lport;
00134     this.host=host;
00135     this.rport=rport;
00136     try{
00137       boundaddress=InetAddress.getByName(address);
00138       ss=(factory==null) ? 
00139         new ServerSocket(lport, 0, boundaddress) :
00140         factory.createServerSocket(lport, 0, boundaddress);
00141     }
00142     catch(Exception e){ 
00143       //System.err.println(e);
00144       String message="PortForwardingL: local port "+address+":"+lport+" cannot be bound.";
00145       if(e instanceof Throwable)
00146         throw new JSchException(message, (Throwable)e);
00147       throw new JSchException(message);
00148     }
00149     if(lport==0){
00150       int assigned=ss.getLocalPort();
00151       if(assigned!=-1)
00152         this.lport=assigned;
00153     }
00154   }
00155 
00156   public void run(){
00157     thread=this;
00158     try{
00159       while(thread!=null){
00160         Socket socket=ss.accept();
00161     socket.setTcpNoDelay(true);
00162         InputStream in=socket.getInputStream();
00163         OutputStream out=socket.getOutputStream();
00164         ChannelDirectTCPIP channel=new ChannelDirectTCPIP();
00165         channel.init();
00166         channel.setInputStream(in);
00167         channel.setOutputStream(out);
00168     session.addChannel(channel);
00169     ((ChannelDirectTCPIP)channel).setHost(host);
00170     ((ChannelDirectTCPIP)channel).setPort(rport);
00171     ((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress());
00172     ((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort());
00173         channel.connect();
00174     if(channel.exitstatus!=-1){
00175     }
00176       }
00177     }
00178     catch(Exception e){
00179       if(session.jsch.getLogger().isEnabled(Logger.WARN)) {
00180         session.jsch.getLogger().log(Logger.WARN, e.toString());
00181       }
00182       //System.err.println("! "+e);
00183     }
00184 
00185     delete();
00186   }
00187 
00188   void delete(){
00189     thread=null;
00190     try{ 
00191       if(ss!=null)ss.close();
00192       ss=null;
00193     }
00194     catch(Exception e){
00195     }
00196   }
00197 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1