View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.cmd;
26  
27  import java.io.IOException;
28  
29  import net.sf.hermesftp.exception.FtpCmdException;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /***
35   * Abstract super class of commands setting up the data channel in passive mode (PASV, EPSV).
36   * 
37   * @author Behnke
38   */
39  public abstract class AbstractFtpCmdPort extends AbstractFtpCmd {
40  
41      private static Log log = LogFactory.getLog(AbstractFtpCmdPort.class);
42  
43      /***
44       * {@inheritDoc}
45       */
46      public void execute() throws FtpCmdException {
47          try {
48              String args = getArguments();
49              if (args.length() == 0) {
50                  msgOut(MSG501);
51                  return;
52              }
53  
54              int protocolIdx = doReadProtocolIdx(args);
55              String addr = doReadIPAddr(args);
56              int port = doReadPort(args);
57              log.debug("Data Channel Protocol: " + protocolIdx + ", IPAddr: " + addr + ", port: " + port);
58  
59              setupDataChannel(protocolIdx, addr, port);
60  
61              msgOut(MSG200);
62          } catch (IOException e) {
63              log.error(e.toString());
64              msgOut(MSG500);
65          } catch (IllegalArgumentException e) {
66              log.error(e.toString());
67              msgOut(MSG501);
68          }
69      }
70  
71      /***
72       * Sets up the data channel in active transfer mode. IPv4 and IPv6 are supported.
73       * 
74       * @param protocolIdx Protocol index (IPv4 or IPv6)
75       * @param ipAddr IPv4 or IPv6 compliant address.
76       * @param port The port.
77       * @throws IOException Setting up data channel failed.
78       */
79      protected void setupDataChannel(int protocolIdx, String ipAddr, int port) throws IOException {
80          getCtx().closeSockets();
81          DataChannelInfo info = new DataChannelInfo(ipAddr, port);
82          SocketProvider provider = new ActiveModeSocketProvider(getCtx(), info);
83          provider.init();
84          getCtx().setDataSocketProvider(provider);
85      }
86  
87      /***
88       * {@inheritDoc}
89       */
90      public boolean isAuthenticationRequired() {
91          return true;
92      }
93  
94      /***
95       * Reads port from passed arguments.
96       * 
97       * @param args The arguments.
98       * @return The port.
99       */
100     protected abstract int doReadPort(String args);
101 
102     /***
103      * Reads the IPv4 or IPv6 compliant address from the passed arguments.
104      * 
105      * @param args The arguments.
106      * @return The IP address.
107      */
108     protected abstract String doReadIPAddr(String args);
109 
110     /***
111      * Reads the protocol index (1=IPv4, 2=IPv6) from the passed arguments.
112      * 
113      * @param args The arguments.
114      * @return The protocol index.
115      */
116     protected abstract int doReadProtocolIdx(String args);
117 
118 }