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.Reply501Exception;
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.FtpConfiguration;
28 import org.waarp.ftp.core.utils.FtpChannelUtils;
29
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32
33
34
35
36 public class PORT extends AbstractCommand {
37
38
39
40 private static final WaarpLogger logger =
41 WaarpLoggerFactory.getLogger(PORT.class);
42
43 @Override
44 public final void exec() throws Reply501Exception {
45
46 if (((FtpConfiguration) (FtpConfiguration.ftpConfiguration)).getActivePassiveMode() <
47 0) {
48
49 throw new Reply501Exception("Active mode not allowed");
50 }
51
52 if (!hasArg()) {
53 final InetSocketAddress inetSocketAddress =
54 getSession().getDataConn().getRemoteAddress();
55 getSession().getDataConn().setActive(inetSocketAddress);
56 getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
57 "PORT command successful on (" +
58 inetSocketAddress + ')');
59 return;
60 }
61
62 final InetSocketAddress inetSocketAddress =
63 FtpChannelUtils.getInetSocketAddress(getArg());
64 if (inetSocketAddress == null) {
65
66 throw new Reply501Exception("Need correct Inet Address as argument");
67 }
68
69 final InetAddress remoteAddress = inetSocketAddress.getAddress();
70 final InetAddress trueRemoteAddress =
71 getSession().getDataConn().getRemoteAddress().getAddress();
72 if (!remoteAddress.equals(trueRemoteAddress)) {
73
74 logger.warn("Given Inet Address {} mismatchs actual client Address {}",
75 remoteAddress, trueRemoteAddress);
76 throw new Reply501Exception(
77 "Given Inet Address mismatchs actual client Address");
78 }
79
80 getSession().getDataConn().setActive(inetSocketAddress);
81 getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
82 "PORT command successful on (" +
83 inetSocketAddress + ')');
84 }
85 }