View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * PORT command
35   */
36  public class PORT extends AbstractCommand {
37    /**
38     * Internal Logger
39     */
40    private static final WaarpLogger logger =
41        WaarpLoggerFactory.getLogger(PORT.class);
42  
43    @Override
44    public final void exec() throws Reply501Exception {
45      // Check if Active mode is OK
46      if (((FtpConfiguration) (FtpConfiguration.ftpConfiguration)).getActivePassiveMode() <
47          0) {
48        // Passive only
49        throw new Reply501Exception("Active mode not allowed");
50      }
51      // First Check if any argument
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      // Check if Inet Address is OK
62      final InetSocketAddress inetSocketAddress =
63          FtpChannelUtils.getInetSocketAddress(getArg());
64      if (inetSocketAddress == null) {
65        // ERROR
66        throw new Reply501Exception("Need correct Inet Address as argument");
67      }
68      // Check if the Client address is the same as given
69      final InetAddress remoteAddress = inetSocketAddress.getAddress();
70      final InetAddress trueRemoteAddress =
71          getSession().getDataConn().getRemoteAddress().getAddress();
72      if (!remoteAddress.equals(trueRemoteAddress)) {
73        // ERROR
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      // OK now try to initialize connection (not open)
80      getSession().getDataConn().setActive(inetSocketAddress);
81      getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
82                                "PORT command successful on (" +
83                                inetSocketAddress + ')');
84    }
85  }