Package org.apache.ftpserver.command

Source Code of org.apache.ftpserver.command.PORT

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/ 

package org.apache.ftpserver.command;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.interfaces.DataConnectionConfiguration;
import org.apache.ftpserver.interfaces.FtpIoSession;
import org.apache.ftpserver.interfaces.FtpServerContext;
import org.apache.ftpserver.util.FtpReplyUtil;
import org.apache.ftpserver.util.IllegalInetAddressException;
import org.apache.ftpserver.util.IllegalPortException;
import org.apache.ftpserver.util.SocketAddressEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <code>PORT &lt;SP&gt; <host-port> &lt;CRLF&gt;</code><br>
*
* The argument is a HOST-PORT specification for the data port
* to be used in data connection.  There are defaults for both
* the user and server data ports, and under normal
* circumstances this command and its reply are not needed.  If
* this command is used, the argument is the concatenation of a
* 32-bit internet host address and a 16-bit TCP port address.
* This address information is broken into 8-bit fields and the
* value of each field is transmitted as a decimal number (in
* character string representation).  The fields are separated
* by commas.  A port command would be:
*
*   PORT h1,h2,h3,h4,p1,p2
*
* where h1 is the high order 8 bits of the internet host address.
*/
public
class PORT extends AbstractCommand {

    private final Logger LOG = LoggerFactory.getLogger(PORT.class);
   
    /**
     * Execute command.
     */
    public void execute(final FtpIoSession session,
            final FtpServerContext context,
            final FtpRequest request) throws IOException {
       
        // reset state variables
        session.resetState();
       
        // argument check
        if(!request.hasArgument()) {
            session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, "PORT", null));
            return
        }

        // is port enabled
        DataConnectionConfiguration dataCfg = session.getListener().getDataConnectionConfiguration();
        if(!dataCfg.isActiveEnabled()) {
            session.write(FtpReplyUtil.translate(session, request, context, 510, "PORT.disabled", null));
            return;
        }
       
        InetSocketAddress address;
        try {
            address = SocketAddressEncoder.decode(request.getArgument());
        } catch(IllegalInetAddressException e) {
            session.write(FtpReplyUtil.translate(session, request, context, 510, "PORT", null));
            return;
        } catch(IllegalPortException e) {
            LOG.debug("Invalid data port: " + request.getArgument(), e);
            session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_552_REQUESTED_FILE_ACTION_ABORTED_EXCEEDED_STORAGE, "PORT.invalid", null));
            return;
        } catch(UnknownHostException e) {
            LOG.debug("Unknown host", e);
            session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_553_REQUESTED_ACTION_NOT_TAKEN_FILE_NAME_NOT_ALLOWED, "PORT.host", null));
            return;
        }
       
        // check IP
        if(dataCfg.isActiveIpCheck()) {
          if(session.getRemoteAddress() instanceof InetSocketAddress) {
            InetAddress clientAddr = ((InetSocketAddress)session.getRemoteAddress()).getAddress();
            if(!address.getAddress().equals(clientAddr)) {
              session.write(FtpReplyUtil.translate(session, request, context, 510, "PORT.mismatch", null));
              return;
            }
          }
        }
       
        session.getDataConnection().initActiveDataConnection(address);
        session.write(FtpReplyUtil.translate(session, request, context, FtpReply.REPLY_200_COMMAND_OKAY, "PORT", null));
    }
   
}
TOP

Related Classes of org.apache.ftpserver.command.PORT

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.