1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.ftp.core.command.parameter;
21
22 import org.waarp.common.command.ReplyCode;
23 import org.waarp.common.command.exception.Reply425Exception;
24 import org.waarp.common.command.exception.Reply501Exception;
25 import org.waarp.common.logging.WaarpLogger;
26 import org.waarp.common.logging.WaarpLoggerFactory;
27 import org.waarp.ftp.core.command.AbstractCommand;
28 import org.waarp.ftp.core.config.FtpConfiguration;
29 import org.waarp.ftp.core.config.FtpInternalConfiguration;
30 import org.waarp.ftp.core.data.FtpDataAsyncConn;
31 import org.waarp.ftp.core.utils.FtpChannelUtils;
32
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35
36
37
38
39 public class PASV extends AbstractCommand {
40
41
42
43 private static final WaarpLogger logger =
44 WaarpLoggerFactory.getLogger(PASV.class);
45
46 @Override
47 public final void exec() throws Reply425Exception, Reply501Exception {
48
49 if (((FtpConfiguration) (FtpConfiguration.ftpConfiguration)).getActivePassiveMode() >
50 0) {
51
52 throw new Reply501Exception("Passive mode not allowed");
53 }
54
55 if (hasArg()) {
56 throw new Reply501Exception("No argument allowed");
57 }
58
59 boolean isInit = false;
60 if (getSession().getDataConn().isPassiveMode()) {
61
62 final InetSocketAddress local =
63 getSession().getDataConn().getLocalAddress();
64 final InetAddress remote =
65 getSession().getDataConn().getRemoteAddress().getAddress();
66 getConfiguration().delFtpSession(remote, local);
67 }
68 for (int i = 1; i <= FtpInternalConfiguration.RETRYNB; i++) {
69 final int newport =
70 FtpDataAsyncConn.getNewPassivePort(getConfiguration());
71 if (newport == -1) {
72 throw new Reply425Exception("No port available");
73 }
74 logger.info("PASV: set Passive Port {}", newport);
75 getSession().getDataConn().setLocalPort(newport);
76 getSession().getDataConn().setPassive();
77
78 try {
79 if (getSession().getDataConn().initPassiveConnection()) {
80 isInit = true;
81 break;
82 }
83 } catch (final Reply425Exception e) {
84 logger.warn(
85 "Pasv refused at try: " + i + " with port: since {}" + newport,
86 e.getMessage());
87 }
88 }
89 if (!isInit) {
90 throw new Reply425Exception("Passive mode not started");
91 }
92
93 final InetSocketAddress local =
94 getSession().getDataConn().getLocalAddress();
95 final int servPort = local.getPort();
96 String address = getSession().getConfiguration().getServerAddress();
97 if (address == null) {
98 address = local.getAddress().getHostAddress();
99 }
100 final String slocal = "Entering Passive Mode (" +
101 FtpChannelUtils.getAddress(address, servPort) + ')';
102 final InetAddress remote =
103 getSession().getDataConn().getRemoteAddress().getAddress();
104
105
106 getConfiguration().setNewFtpSession(remote, local, getSession());
107
108 getSession().getDataConn().getFtpTransferControl()
109 .resetWaitForOpenedDataChannel();
110 getSession().setReplyCode(ReplyCode.REPLY_227_ENTERING_PASSIVE_MODE,
111 slocal);
112 logger.info("PASV: answer ready on {}", slocal);
113 }
114
115 }