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.rfc2428;
21
22 import org.waarp.common.command.ReplyCode;
23 import org.waarp.common.command.exception.Reply425Exception;
24 import org.waarp.common.logging.WaarpLogger;
25 import org.waarp.common.logging.WaarpLoggerFactory;
26 import org.waarp.ftp.core.command.AbstractCommand;
27 import org.waarp.ftp.core.config.FtpInternalConfiguration;
28 import org.waarp.ftp.core.data.FtpDataAsyncConn;
29 import org.waarp.ftp.core.utils.FtpChannelUtils;
30
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33
34
35
36
37 public class EPSV extends AbstractCommand {
38
39
40
41 private static final WaarpLogger logger =
42 WaarpLoggerFactory.getLogger(EPSV.class);
43
44 @Override
45 public final void exec() throws Reply425Exception {
46
47
48 boolean isInit = false;
49 int newport = -1;
50 if (getSession().getDataConn().isPassiveMode()) {
51
52 final InetSocketAddress local =
53 getSession().getDataConn().getLocalAddress();
54 final InetAddress remote =
55 getSession().getDataConn().getRemoteAddress().getAddress();
56 getConfiguration().delFtpSession(remote, local);
57 }
58 for (int i = 1; i <= FtpInternalConfiguration.RETRYNB; i++) {
59 newport = FtpDataAsyncConn.getNewPassivePort(getConfiguration());
60 if (newport == -1) {
61 throw new Reply425Exception("No port available");
62 }
63 logger.info("EPSV: set Passive Port {}", newport);
64 getSession().getDataConn().setLocalPort(newport);
65 getSession().getDataConn().setPassive();
66
67 try {
68 if (getSession().getDataConn().initPassiveConnection()) {
69 isInit = true;
70 break;
71 }
72 } catch (final Reply425Exception e) {
73 logger.warn(
74 "EPSV refused at try: " + i + " with port: " + newport + " : {}",
75 e.getMessage());
76 }
77 }
78 if (!isInit) {
79 throw new Reply425Exception("Extended Passive mode not started");
80 }
81
82 final InetSocketAddress local =
83 getSession().getDataConn().getLocalAddress();
84 final String slocal = "Entering Extended Passive Mode (" +
85 FtpChannelUtils.get2428Address(local) + ')';
86 final InetAddress remote =
87 getSession().getDataConn().getRemoteAddress().getAddress();
88
89
90 getConfiguration().setNewFtpSession(remote, local, getSession());
91 getSession().setReplyCode(ReplyCode.REPLY_229_ENTERING_PASSIVE_MODE,
92 "Entering Extended Passive Mode (|||" + newport +
93 "|)");
94 logger.info("EPSV: answer ready on {}", slocal);
95
96
97
98 }
99
100 }