1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }