Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.j2ssh.forwarding » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   package com.sshtools.j2ssh.forwarding;
   27   
   28   import com.sshtools.j2ssh.configuration.SshConnectionProperties;
   29   import com.sshtools.j2ssh.connection.Channel;
   30   import com.sshtools.j2ssh.connection.ChannelFactory;
   31   import com.sshtools.j2ssh.connection.ConnectionProtocol;
   32   import com.sshtools.j2ssh.connection.InvalidChannelException;
   33   import com.sshtools.j2ssh.io.ByteArrayReader;
   34   import com.sshtools.j2ssh.io.ByteArrayWriter;
   35   import com.sshtools.j2ssh.util.StartStopState;
   36   
   37   import org.apache.commons.logging.Log;
   38   import org.apache.commons.logging.LogFactory;
   39   
   40   import java.io.IOException;
   41   
   42   import java.net.Socket;
   43   import java.net.SocketPermission;
   44   
   45   import java.util.HashMap;
   46   import java.util.Iterator;
   47   import java.util.List;
   48   import java.util.Map;
   49   import java.util.Vector;
   50   
   51   
   52   /**
   53    *
   54    *
   55    * @author $author$
   56    * @version $Revision: 1.37 $
   57    */
   58   public class ForwardingClient implements ChannelFactory {
   59       private static Log log = LogFactory.getLog(ForwardingClient.class);
   60   
   61       /**  */
   62       public final static String REMOTE_FORWARD_REQUEST = "tcpip-forward";
   63   
   64       /**  */
   65       public final static String REMOTE_FORWARD_CANCEL_REQUEST = "cancel-tcpip-forward";
   66       private ConnectionProtocol connection;
   67       private List channelTypes = new Vector();
   68       private Map localForwardings = new HashMap();
   69       private Map remoteForwardings = new HashMap();
   70       private XDisplay xDisplay;
   71       private ForwardingConfiguration x11ForwardingConfiguration;
   72   
   73       /**
   74        * Creates a new ForwardingClient object.
   75        *
   76        * @param connection
   77        *
   78        * @throws IOException
   79        */
   80       public ForwardingClient(ConnectionProtocol connection)
   81           throws IOException {
   82           this.connection = connection;
   83   
   84           //channelTypes.add(ForwardingSocketChannel.REMOTE_FORWARDING_CHANNEL);
   85           connection.addChannelFactory(ForwardingSocketChannel.REMOTE_FORWARDING_CHANNEL,
   86               this);
   87           connection.addChannelFactory(ForwardingSocketChannel.X11_FORWARDING_CHANNEL,
   88               this);
   89       }
   90   
   91       /**
   92        *
   93        *
   94        * @return
   95        */
   96       public List getChannelType() {
   97           return channelTypes;
   98       }
   99   
  100       /**
  101        *
  102        *
  103        * @param localDisplay
  104        */
  105       public void enableX11Forwarding(XDisplay localDisplay) {
  106           xDisplay = localDisplay;
  107           x11ForwardingConfiguration = new ForwardingConfiguration("x11", "", 0,
  108                   xDisplay.getHost(), xDisplay.getPort());
  109       }
  110   
  111       /**
  112        *
  113        *
  114        * @return
  115        */
  116       public ForwardingConfiguration getX11ForwardingConfiguration() {
  117           return x11ForwardingConfiguration;
  118       }
  119   
  120       /**
  121        *
  122        *
  123        * @return
  124        */
  125       public boolean hasActiveConfigurations() {
  126           // First check the size
  127           if ((localForwardings.size() == 0) && (remoteForwardings.size() == 0)) {
  128               return false;
  129           }
  130   
  131           Iterator it = localForwardings.values().iterator();
  132   
  133           while (it.hasNext()) {
  134               if (((ForwardingConfiguration) it.next()).getState().getValue() == StartStopState.STARTED) {
  135                   return true;
  136               }
  137           }
  138   
  139           it = remoteForwardings.values().iterator();
  140   
  141           while (it.hasNext()) {
  142               if (((ForwardingConfiguration) it.next()).getState().getValue() == StartStopState.STARTED) {
  143                   return true;
  144               }
  145           }
  146   
  147           return false;
  148       }
  149   
  150       public void synchronizeConfiguration(SshConnectionProperties properties) {
  151           ForwardingConfiguration fwd = null;
  152   
  153           if (properties.getLocalForwardings().size() > 0) {
  154               for (Iterator it = properties.getLocalForwardings().values()
  155                                            .iterator(); it.hasNext();) {
  156                   try {
  157                       fwd = (ForwardingConfiguration) it.next();
  158                       fwd = addLocalForwarding(fwd);
  159   
  160                       if (properties.getForwardingAutoStartMode()) {
  161                           startLocalForwarding(fwd.getName());
  162                       }
  163                   } catch (Throwable ex) {
  164                       log.warn((("Failed to start local forwarding " + fwd) != null)
  165                           ? fwd.getName() : "", ex);
  166                   }
  167               }
  168           }
  169   
  170           if (properties.getRemoteForwardings().size() > 0) {
  171               for (Iterator it = properties.getRemoteForwardings().values()
  172                                            .iterator(); it.hasNext();) {
  173                   try {
  174                       fwd = (ForwardingConfiguration) it.next();
  175                       addRemoteForwarding(fwd);
  176   
  177                       if (properties.getForwardingAutoStartMode()) {
  178                           startRemoteForwarding(fwd.getName());
  179                       }
  180                   } catch (Throwable ex) {
  181                       log.warn((("Failed to start remote forwarding " + fwd) != null)
  182                           ? fwd.getName() : "", ex);
  183                   }
  184               }
  185           }
  186       }
  187   
  188       /**
  189        *
  190        *
  191        * @return
  192        */
  193       public boolean hasActiveForwardings() {
  194           // First check the size
  195           if ((localForwardings.size() == 0) && (remoteForwardings.size() == 0)) {
  196               return false;
  197           }
  198   
  199           Iterator it = localForwardings.values().iterator();
  200   
  201           while (it.hasNext()) {
  202               if (((ForwardingConfiguration) it.next()).getActiveForwardingSocketChannels()
  203                        .size() > 0) {
  204                   return true;
  205               }
  206           }
  207   
  208           it = remoteForwardings.values().iterator();
  209   
  210           while (it.hasNext()) {
  211               if (((ForwardingConfiguration) it.next()).getActiveForwardingSocketChannels()
  212                        .size() > 0) {
  213                   return true;
  214               }
  215           }
  216   
  217           return false;
  218       }
  219   
  220       /**
  221        *
  222        *
  223        * @param addressToBind
  224        * @param portToBind
  225        *
  226        * @return
  227        *
  228        * @throws ForwardingConfigurationException
  229        */
  230       public ForwardingConfiguration getLocalForwardingByAddress(
  231           String addressToBind, int portToBind)
  232           throws ForwardingConfigurationException {
  233           Iterator it = localForwardings.values().iterator();
  234           ForwardingConfiguration config;
  235   
  236           while (it.hasNext()) {
  237               config = (ForwardingConfiguration) it.next();
  238   
  239               if (config.getAddressToBind().equals(addressToBind) &&
  240                       (config.getPortToBind() == portToBind)) {
  241                   return config;
  242               }
  243           }
  244   
  245           throw new ForwardingConfigurationException(
  246               "The configuration does not exist");
  247       }
  248   
  249       /**
  250        *
  251        *
  252        * @param name
  253        *
  254        * @return
  255        *
  256        * @throws ForwardingConfigurationException
  257        */
  258       public ForwardingConfiguration getLocalForwardingByName(String name)
  259           throws ForwardingConfigurationException {
  260           if (!localForwardings.containsKey(name)) {
  261               throw new ForwardingConfigurationException(
  262                   "The configuraiton does not exist!");
  263           }
  264   
  265           return (ForwardingConfiguration) localForwardings.get(name);
  266       }
  267   
  268       /**
  269        *
  270        *
  271        * @param name
  272        *
  273        * @return
  274        *
  275        * @throws ForwardingConfigurationException
  276        */
  277       public ForwardingConfiguration getRemoteForwardingByName(String name)
  278           throws ForwardingConfigurationException {
  279           if (!remoteForwardings.containsKey(name)) {
  280               throw new ForwardingConfigurationException(
  281                   "The configuraiton does not exist!");
  282           }
  283   
  284           return (ForwardingConfiguration) remoteForwardings.get(name);
  285       }
  286   
  287       /**
  288        *
  289        *
  290        * @return
  291        */
  292       public Map getLocalForwardings() {
  293           return localForwardings;
  294       }
  295   
  296       /**
  297        *
  298        *
  299        * @return
  300        */
  301       public Map getRemoteForwardings() {
  302           return remoteForwardings;
  303       }
  304   
  305       /**
  306        *
  307        *
  308        * @param addressToBind
  309        * @param portToBind
  310        *
  311        * @return
  312        *
  313        * @throws ForwardingConfigurationException
  314        */
  315       public ForwardingConfiguration getRemoteForwardingByAddress(
  316           String addressToBind, int portToBind)
  317           throws ForwardingConfigurationException {
  318           Iterator it = remoteForwardings.values().iterator();
  319           ForwardingConfiguration config;
  320   
  321           while (it.hasNext()) {
  322               config = (ForwardingConfiguration) it.next();
  323   
  324               if (config.getAddressToBind().equals(addressToBind) &&
  325                       (config.getPortToBind() == portToBind)) {
  326                   return config;
  327               }
  328           }
  329   
  330           throw new ForwardingConfigurationException(
  331               "The configuration does not exist");
  332       }
  333   
  334       /**
  335        *
  336        *
  337        * @param name
  338        *
  339        * @throws ForwardingConfigurationException
  340        */
  341       public void removeLocalForwarding(String name)
  342           throws ForwardingConfigurationException {
  343           if (!localForwardings.containsKey(name)) {
  344               throw new ForwardingConfigurationException(
  345                   "The name is not a valid forwarding configuration");
  346           }
  347   
  348           ForwardingListener listener = (ForwardingListener) localForwardings.get(name);
  349   
  350           if (listener.isRunning()) {
  351               stopLocalForwarding(name);
  352           }
  353   
  354           localForwardings.remove(name);
  355       }
  356   
  357       /**
  358        *
  359        *
  360        * @param name
  361        *
  362        * @throws IOException
  363        * @throws ForwardingConfigurationException
  364        */
  365       public void removeRemoteForwarding(String name)
  366           throws IOException, ForwardingConfigurationException {
  367           if (!remoteForwardings.containsKey(name)) {
  368               throw new ForwardingConfigurationException(
  369                   "The name is not a valid forwarding configuration");
  370           }
  371   
  372           ForwardingListener listener = (ForwardingListener) remoteForwardings.get(name);
  373   
  374           if (listener.isRunning()) {
  375               stopRemoteForwarding(name);
  376           }
  377   
  378           remoteForwardings.remove(name);
  379       }
  380   
  381       /**
  382        *
  383        *
  384        * @param uniqueName
  385        * @param addressToBind
  386        * @param portToBind
  387        * @param hostToConnect
  388        * @param portToConnect
  389        *
  390        * @return
  391        *
  392        * @throws ForwardingConfigurationException
  393        */
  394       public ForwardingConfiguration addLocalForwarding(String uniqueName,
  395           String addressToBind, int portToBind, String hostToConnect,
  396           int portToConnect) throws ForwardingConfigurationException {
  397           // Check that the name does not exist
  398           if (localForwardings.containsKey(uniqueName)) {
  399               throw new ForwardingConfigurationException(
  400                   "The configuration name already exists!");
  401           }
  402   
  403           // Check that the address to bind and port are not already being used
  404           Iterator it = localForwardings.values().iterator();
  405           ForwardingConfiguration config;
  406   
  407           while (it.hasNext()) {
  408               config = (ForwardingConfiguration) it.next();
  409   
  410               if (config.getAddressToBind().equals(addressToBind) &&
  411                       (config.getPortToBind() == portToBind)) {
  412                   throw new ForwardingConfigurationException(
  413                       "The address and port are already in use");
  414               }
  415           }
  416   
  417           // Check the security mananger
  418           SecurityManager manager = System.getSecurityManager();
  419   
  420           if (manager != null) {
  421               try {
  422                   manager.checkPermission(new SocketPermission(addressToBind +
  423                           ":" + String.valueOf(portToBind), "accept,listen"));
  424               } catch (SecurityException e) {
  425                   throw new ForwardingConfigurationException(
  426                       "The security manager has denied listen permision on " +
  427                       addressToBind + ":" + String.valueOf(portToBind));
  428               }
  429           }
  430   
  431           // Create the configuration object
  432           ForwardingConfiguration cf = new ClientForwardingListener(uniqueName,
  433                   connection, addressToBind, portToBind, hostToConnect,
  434                   portToConnect);
  435           localForwardings.put(uniqueName, cf);
  436   
  437           return cf;
  438       }
  439   
  440       /**
  441        *
  442        *
  443        * @param fwd
  444        *
  445        * @return
  446        *
  447        * @throws ForwardingConfigurationException
  448        */
  449       public ForwardingConfiguration addLocalForwarding(
  450           ForwardingConfiguration fwd) throws ForwardingConfigurationException {
  451           return addLocalForwarding(fwd.getName(), fwd.getAddressToBind(),
  452               fwd.getPortToBind(), fwd.getHostToConnect(), fwd.getPortToConnect());
  453   
  454           /*     // Check that the name does not exist
  455                 if (localForwardings.containsKey(fwd.getName())) {
  456            throw new ForwardingConfigurationException(
  457                "The configuration name already exists!");
  458                 }
  459                 // Check that the address to bind and port are not already being used
  460                 Iterator it = localForwardings.values().iterator();
  461                 ForwardingConfiguration config;
  462                 while (it.hasNext()) {
  463            config = (ForwardingConfiguration) it.next();
  464            if (config.getAddressToBind().equals(fwd.getAddressToBind())
  465                    && (config.getPortToBind() == fwd.getPortToBind())) {
  466                throw new ForwardingConfigurationException(
  467                    "The address and port are already in use");
  468            }
  469                 }
  470                 // Check the security mananger
  471                 SecurityManager manager = System.getSecurityManager();
  472                 if (manager != null) {
  473            try {
  474                manager.checkPermission(new SocketPermission(fwd
  475                        .getAddressToBind() + ":"
  476                        + String.valueOf(fwd.getPortToBind()), "accept,listen"));
  477            } catch (SecurityException e) {
  478                throw new ForwardingConfigurationException(
  479                    "The security manager has denied listen permision on "
  480                    + fwd.getAddressToBind() + ":"
  481                    + String.valueOf(fwd.getPortToBind()));
  482            }
  483                 }
  484                 // Create the configuration object
  485                 localForwardings.put(fwd.getName(),
  486            new ClientForwardingListener(fwd.getName(), connection,
  487                fwd.getAddressToBind(), fwd.getPortToBind(),
  488                fwd.getHostToConnect(), fwd.getPortToConnect()));*/
  489       }
  490   
  491       /**
  492        *
  493        *
  494        * @param uniqueName
  495        * @param addressToBind
  496        * @param portToBind
  497        * @param hostToConnect
  498        * @param portToConnect
  499        *
  500        * @throws ForwardingConfigurationException
  501        */
  502       public void addRemoteForwarding(String uniqueName, String addressToBind,
  503           int portToBind, String hostToConnect, int portToConnect)
  504           throws ForwardingConfigurationException {
  505           // Check that the name does not exist
  506           if (remoteForwardings.containsKey(uniqueName)) {
  507               throw new ForwardingConfigurationException(
  508                   "The remote forwaring configuration name already exists!");
  509           }
  510   
  511           // Check that the address to bind and port are not already being used
  512           Iterator it = remoteForwardings.values().iterator();
  513           ForwardingConfiguration config;
  514   
  515           while (it.hasNext()) {
  516               config = (ForwardingConfiguration) it.next();
  517   
  518               if (config.getAddressToBind().equals(addressToBind) &&
  519                       (config.getPortToBind() == portToBind)) {
  520                   throw new ForwardingConfigurationException(
  521                       "The remote forwarding address and port are already in use");
  522               }
  523           }
  524   
  525           // Check the security mananger
  526           SecurityManager manager = System.getSecurityManager();
  527   
  528           if (manager != null) {
  529               try {
  530                   manager.checkPermission(new SocketPermission(hostToConnect +
  531                           ":" + String.valueOf(portToConnect), "connect"));
  532               } catch (SecurityException e) {
  533                   throw new ForwardingConfigurationException(
  534                       "The security manager has denied connect permision on " +
  535                       hostToConnect + ":" + String.valueOf(portToConnect));
  536               }
  537           }
  538   
  539           // Create the configuration object
  540           remoteForwardings.put(uniqueName,
  541               new ForwardingConfiguration(uniqueName, addressToBind, portToBind,
  542                   hostToConnect, portToConnect));
  543       }
  544   
  545       /**
  546        *
  547        *
  548        * @param fwd
  549        *
  550        * @throws ForwardingConfigurationException
  551        */
  552       public void addRemoteForwarding(ForwardingConfiguration fwd)
  553           throws ForwardingConfigurationException {
  554           // Check that the name does not exist
  555           if (remoteForwardings.containsKey(fwd.getName())) {
  556               throw new ForwardingConfigurationException(
  557                   "The remote forwaring configuration name already exists!");
  558           }
  559   
  560           // Check that the address to bind and port are not already being used
  561           Iterator it = remoteForwardings.values().iterator();
  562           ForwardingConfiguration config;
  563   
  564           while (it.hasNext()) {
  565               config = (ForwardingConfiguration) it.next();
  566   
  567               if (config.getAddressToBind().equals(fwd.getAddressToBind()) &&
  568                       (config.getPortToBind() == fwd.getPortToBind())) {
  569                   throw new ForwardingConfigurationException(
  570                       "The remote forwarding address and port are already in use");
  571               }
  572           }
  573   
  574           // Check the security mananger
  575           SecurityManager manager = System.getSecurityManager();
  576   
  577           if (manager != null) {
  578               try {
  579                   manager.checkPermission(new SocketPermission(fwd.getHostToConnect() +
  580                           ":" + String.valueOf(fwd.getPortToConnect()), "connect"));
  581               } catch (SecurityException e) {
  582                   throw new ForwardingConfigurationException(
  583                       "The security manager has denied connect permision on " +
  584                       fwd.getHostToConnect() + ":" +
  585                       String.valueOf(fwd.getPortToConnect()));
  586               }
  587           }
  588   
  589           // Create the configuration object
  590           remoteForwardings.put(fwd.getName(), fwd);
  591       }
  592   
  593       /**
  594        *
  595        *
  596        * @param channelType
  597        * @param requestData
  598        *
  599        * @return
  600        *
  601        * @throws InvalidChannelException
  602        */
  603       public Channel createChannel(String channelType, byte[] requestData)
  604           throws InvalidChannelException {
  605           if (channelType.equals(ForwardingSocketChannel.X11_FORWARDING_CHANNEL)) {
  606               if (xDisplay == null) {
  607                   throw new InvalidChannelException(
  608                       "Local display has not been set for X11 forwarding.");
  609               }
  610   
  611               try {
  612                   ByteArrayReader bar = new ByteArrayReader(requestData);
  613                   String originatingHost = bar.readString();
  614                   int originatingPort = (int) bar.readInt();
  615                   log.debug("Creating socket to " +
  616                       x11ForwardingConfiguration.getHostToConnect() + "/" +
  617                       x11ForwardingConfiguration.getPortToConnect());
  618   
  619                   Socket socket = new Socket(x11ForwardingConfiguration.getHostToConnect(),
  620                           x11ForwardingConfiguration.getPortToConnect());
  621   
  622                   // Create the channel adding it to the active channels
  623                   ForwardingSocketChannel channel = x11ForwardingConfiguration.createForwardingSocketChannel(channelType,
  624                           x11ForwardingConfiguration.getHostToConnect(),
  625                           x11ForwardingConfiguration.getPortToConnect(),
  626                           originatingHost, originatingPort);
  627                   channel.bindSocket(socket);
  628                   channel.addEventListener(x11ForwardingConfiguration.monitor);
  629   
  630                   return channel;
  631               } catch (IOException ioe) {
  632                   throw new InvalidChannelException(ioe.getMessage());
  633               }
  634           }
  635   
  636           if (channelType.equals(
  637                       ForwardingSocketChannel.REMOTE_FORWARDING_CHANNEL)) {
  638               try {
  639                   ByteArrayReader bar = new ByteArrayReader(requestData);
  640                   String addressBound = bar.readString();
  641                   int portBound = (int) bar.readInt();
  642                   String originatingHost = bar.readString();
  643                   int originatingPort = (int) bar.readInt();
  644                   ForwardingConfiguration config = getRemoteForwardingByAddress(addressBound,
  645                           portBound);
  646                   Socket socket = new Socket(config.getHostToConnect(),
  647                           config.getPortToConnect());
  648   
  649                   /*Socket socket = new Socket();
  650                    socket.connect(new InetSocketAddress(
  651                        config.getHostToConnect(), config.getPortToConnect()));*/
  652   
  653                   // Create the channel adding it to the active channels
  654                   ForwardingSocketChannel channel = config.createForwardingSocketChannel(channelType,
  655                           config.getHostToConnect(), config.getPortToConnect(),
  656                           originatingHost, originatingPort);
  657                   channel.bindSocket(socket);
  658                   channel.addEventListener(config.monitor);
  659   
  660                   return channel;
  661               } catch (ForwardingConfigurationException fce) {
  662                   throw new InvalidChannelException(
  663                       "No valid forwarding configuration was available for the request address");
  664               } catch (IOException ioe) {
  665                   throw new InvalidChannelException(ioe.getMessage());
  666               }
  667           }
  668   
  669           throw new InvalidChannelException(
  670               "The server can only request a remote forwarding channel or an" +
  671               "X11 forwarding channel");
  672       }
  673   
  674       /**
  675        *
  676        *
  677        * @param uniqueName
  678        *
  679        * @throws ForwardingConfigurationException
  680        */
  681       public void startLocalForwarding(String uniqueName)
  682           throws ForwardingConfigurationException {
  683           if (!localForwardings.containsKey(uniqueName)) {
  684               throw new ForwardingConfigurationException(
  685                   "The name is not a valid forwarding configuration");
  686           }
  687   
  688           try {
  689               ForwardingListener listener = (ForwardingListener) localForwardings.get(uniqueName);
  690               listener.start();
  691           } catch (IOException ex) {
  692               throw new ForwardingConfigurationException(ex.getMessage());
  693           }
  694       }
  695   
  696       /**
  697        *
  698        *
  699        * @throws IOException
  700        * @throws ForwardingConfigurationException
  701        */
  702       public void startX11Forwarding()
  703           throws IOException, ForwardingConfigurationException {
  704           if (x11ForwardingConfiguration == null) {
  705               throw new ForwardingConfigurationException(
  706                   "X11 forwarding hasn't been enabled.");
  707           }
  708   
  709           ByteArrayWriter baw = new ByteArrayWriter();
  710           baw.writeString(x11ForwardingConfiguration.getAddressToBind());
  711           baw.writeInt(x11ForwardingConfiguration.getPortToBind());
  712           x11ForwardingConfiguration.getState().setValue(StartStopState.STARTED);
  713   
  714           if (log.isDebugEnabled()) {
  715               log.info("X11 forwarding started");
  716               log.debug("Address to bind: " +
  717                   x11ForwardingConfiguration.getAddressToBind());
  718               log.debug("Port to bind: " +
  719                   String.valueOf(x11ForwardingConfiguration.getPortToBind()));
  720               log.debug("Host to connect: " +
  721                   x11ForwardingConfiguration.hostToConnect);
  722               log.debug("Port to connect: " +
  723                   x11ForwardingConfiguration.portToConnect);
  724           } else {
  725               log.info("Request for X11 rejected.");
  726           }
  727       }
  728   
  729       /**
  730        *
  731        *
  732        * @param name
  733        *
  734        * @throws IOException
  735        * @throws ForwardingConfigurationException
  736        */
  737       public void startRemoteForwarding(String name)
  738           throws IOException, ForwardingConfigurationException {
  739           if (!remoteForwardings.containsKey(name)) {
  740               throw new ForwardingConfigurationException(
  741                   "The name is not a valid forwarding configuration");
  742           }
  743   
  744           ForwardingConfiguration config = (ForwardingConfiguration) remoteForwardings.get(name);
  745           ByteArrayWriter baw = new ByteArrayWriter();
  746           baw.writeString(config.getAddressToBind());
  747           baw.writeInt(config.getPortToBind());
  748           connection.sendGlobalRequest(REMOTE_FORWARD_REQUEST, true,
  749               baw.toByteArray());
  750           remoteForwardings.put(name, config);
  751           config.getState().setValue(StartStopState.STARTED);
  752           log.info("Remote forwarding configuration '" + name + "' started");
  753   
  754           if (log.isDebugEnabled()) {
  755               log.debug("Address to bind: " + config.getAddressToBind());
  756               log.debug("Port to bind: " +
  757                   String.valueOf(config.getPortToBind()));
  758               log.debug("Host to connect: " + config.hostToConnect);
  759               log.debug("Port to connect: " + config.portToConnect);
  760           }
  761       }
  762   
  763       /**
  764        *
  765        *
  766        * @param uniqueName
  767        *
  768        * @throws ForwardingConfigurationException
  769        */
  770       public void stopLocalForwarding(String uniqueName)
  771           throws ForwardingConfigurationException {
  772           if (!localForwardings.containsKey(uniqueName)) {
  773               throw new ForwardingConfigurationException(
  774                   "The name is not a valid forwarding configuration");
  775           }
  776   
  777           ForwardingListener listener = (ForwardingListener) localForwardings.get(uniqueName);
  778           listener.stop();
  779           log.info("Local forwarding configuration " + uniqueName + "' stopped");
  780       }
  781   
  782       /**
  783        *
  784        *
  785        * @param name
  786        *
  787        * @throws IOException
  788        * @throws ForwardingConfigurationException
  789        */
  790       public void stopRemoteForwarding(String name)
  791           throws IOException, ForwardingConfigurationException {
  792           if (!remoteForwardings.containsKey(name)) {
  793               throw new ForwardingConfigurationException(
  794                   "The remote forwarding configuration does not exist");
  795           }
  796   
  797           ForwardingConfiguration config = (ForwardingConfiguration) remoteForwardings.get(name);
  798           ByteArrayWriter baw = new ByteArrayWriter();
  799           baw.writeString(config.getAddressToBind());
  800           baw.writeInt(config.getPortToBind());
  801           connection.sendGlobalRequest(REMOTE_FORWARD_CANCEL_REQUEST, true,
  802               baw.toByteArray());
  803           config.getState().setValue(StartStopState.STOPPED);
  804           log.info("Remote forwarding configuration '" + name + "' stopped");
  805       }
  806   
  807       public class ClientForwardingListener extends ForwardingListener {
  808           public ClientForwardingListener(String name,
  809               ConnectionProtocol connection, String addressToBind,
  810               int portToBind, String hostToConnect, int portToConnect) {
  811               super(name, connection, addressToBind, portToBind, hostToConnect,
  812                   portToConnect);
  813           }
  814   
  815           public ForwardingSocketChannel createChannel(String hostToConnect,
  816               int portToConnect, Socket socket)
  817               throws ForwardingConfigurationException {
  818               return createForwardingSocketChannel(ForwardingSocketChannel.LOCAL_FORWARDING_CHANNEL,
  819                   hostToConnect, portToConnect,
  820                   
  821               /*( (InetSocketAddress) socket.
  822               getRemoteSocketAddress()).getAddress()
  823               .getHostAddress()*/
  824               socket.getInetAddress().getHostAddress(), 
  825               /*( (InetSocketAddress) socket.
  826               getRemoteSocketAddress()).getPort()*/
  827               socket.getPort());
  828           }
  829       }
  830   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.j2ssh.forwarding » [javadoc | source]