1dfb59d50631968ab1a13002ea5421ece93169851chrismair/*
2dfb59d50631968ab1a13002ea5421ece93169851chrismair * Copyright 2008 the original author or authors.
3dfb59d50631968ab1a13002ea5421ece93169851chrismair *
4dfb59d50631968ab1a13002ea5421ece93169851chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5dfb59d50631968ab1a13002ea5421ece93169851chrismair * you may not use this file except in compliance with the License.
6dfb59d50631968ab1a13002ea5421ece93169851chrismair * You may obtain a copy of the License at
7dfb59d50631968ab1a13002ea5421ece93169851chrismair *
8dfb59d50631968ab1a13002ea5421ece93169851chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9dfb59d50631968ab1a13002ea5421ece93169851chrismair *
10dfb59d50631968ab1a13002ea5421ece93169851chrismair * Unless required by applicable law or agreed to in writing, software
11dfb59d50631968ab1a13002ea5421ece93169851chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12dfb59d50631968ab1a13002ea5421ece93169851chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dfb59d50631968ab1a13002ea5421ece93169851chrismair * See the License for the specific language governing permissions and
14dfb59d50631968ab1a13002ea5421ece93169851chrismair * limitations under the License.
15dfb59d50631968ab1a13002ea5421ece93169851chrismair */
16dfb59d50631968ab1a13002ea5421ece93169851chrismairpackage org.mockftpserver.core.util;
17dfb59d50631968ab1a13002ea5421ece93169851chrismair
18dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.CommandSyntaxException;
19dfb59d50631968ab1a13002ea5421ece93169851chrismairimport org.mockftpserver.core.MockFtpServerException;
20dfb59d50631968ab1a13002ea5421ece93169851chrismair
21dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.net.InetAddress;
22dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.net.UnknownHostException;
23dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.util.Arrays;
24dfb59d50631968ab1a13002ea5421ece93169851chrismairimport java.util.List;
25dfb59d50631968ab1a13002ea5421ece93169851chrismair
26dfb59d50631968ab1a13002ea5421ece93169851chrismair/**
27dfb59d50631968ab1a13002ea5421ece93169851chrismair * Utility class for parsing host and port values from command arguments.
28dfb59d50631968ab1a13002ea5421ece93169851chrismair *
29dfb59d50631968ab1a13002ea5421ece93169851chrismair * @author Chris Mair
30dfb59d50631968ab1a13002ea5421ece93169851chrismair * @version $Revision$ - $Date$
31dfb59d50631968ab1a13002ea5421ece93169851chrismair */
32dfb59d50631968ab1a13002ea5421ece93169851chrismairpublic final class PortParser {
33dfb59d50631968ab1a13002ea5421ece93169851chrismair
34dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
35dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Parse the host address and port number of an extended address. This encoded format is used by
36dfb59d50631968ab1a13002ea5421ece93169851chrismair     * the EPRT FTP command, and supports IPv6.
37dfb59d50631968ab1a13002ea5421ece93169851chrismair     * <p/>
38dfb59d50631968ab1a13002ea5421ece93169851chrismair     * The client network address can be in IPv4 format (e.g., "132.235.1.2") or
39dfb59d50631968ab1a13002ea5421ece93169851chrismair     * IPv6 format (e.g., "1080::8:800:200C:417A"). See RFC2428 for more information.
40dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
41dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param parameter - the single parameter String containing the encoded host and port number
42dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the populated HostAndPort object
43dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
44dfb59d50631968ab1a13002ea5421ece93169851chrismair    public static HostAndPort parseExtendedAddressHostAndPort(String parameter) {
45dfb59d50631968ab1a13002ea5421ece93169851chrismair        if (parameter == null || parameter.length() == 0) {
46dfb59d50631968ab1a13002ea5421ece93169851chrismair            throw new CommandSyntaxException("The parameter string must not be empty or null");
47dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
48dfb59d50631968ab1a13002ea5421ece93169851chrismair
49dfb59d50631968ab1a13002ea5421ece93169851chrismair        String delimiter = parameter.substring(0,1);
50dfb59d50631968ab1a13002ea5421ece93169851chrismair        String[] tokens = parameter.split("\\" + delimiter);
51dfb59d50631968ab1a13002ea5421ece93169851chrismair
52dfb59d50631968ab1a13002ea5421ece93169851chrismair        if (tokens.length < 4) {
53dfb59d50631968ab1a13002ea5421ece93169851chrismair            throw new CommandSyntaxException("Error parsing host and port number [" + parameter + "]");
54dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
55dfb59d50631968ab1a13002ea5421ece93169851chrismair
56dfb59d50631968ab1a13002ea5421ece93169851chrismair        int port = Integer.parseInt(tokens[3]);
57dfb59d50631968ab1a13002ea5421ece93169851chrismair
58dfb59d50631968ab1a13002ea5421ece93169851chrismair        InetAddress host;
59dfb59d50631968ab1a13002ea5421ece93169851chrismair        try {
60dfb59d50631968ab1a13002ea5421ece93169851chrismair            host = InetAddress.getByName(tokens[2]);
61dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
62dfb59d50631968ab1a13002ea5421ece93169851chrismair        catch (UnknownHostException e) {
63dfb59d50631968ab1a13002ea5421ece93169851chrismair            throw new CommandSyntaxException("Error parsing host [" + tokens[2] + "]", e);
64dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
65dfb59d50631968ab1a13002ea5421ece93169851chrismair
66dfb59d50631968ab1a13002ea5421ece93169851chrismair        return new HostAndPort(host, port);
67dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
68dfb59d50631968ab1a13002ea5421ece93169851chrismair
69dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
70dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Parse a 32-bit IP address and 16-bit port number from the String[] of FTP command parameters.
71dfb59d50631968ab1a13002ea5421ece93169851chrismair     * This is used by the FTP "PORT" command.
72dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
73dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param parameters - the String[] of command parameters. It is the concatenation
74dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   of a 32-bit internet host address and a 16-bit TCP port address. This address
75dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   information is broken into 8-bit fields and the value of each field is encoded
76dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   as a separate parameter whose value is a decimal number (in character string
77dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   representation).  Thus, the six parameters for the port command would be:
78dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   h1,h2,h3,h4,p1,p2
79dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   where h1 is the high order 8 bits of the internet host address, and p1 is the
80dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                   high order 8 bits of the port number.
81dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the HostAndPort object with the host InetAddres and int port parsed from the parameters
82dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
83dfb59d50631968ab1a13002ea5421ece93169851chrismair     *                               - if parameters is null or contains an insufficient number of elements
84dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @throws NumberFormatException - if one of the parameters does not contain a parsable integer
85dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
86dfb59d50631968ab1a13002ea5421ece93169851chrismair    public static HostAndPort parseHostAndPort(String[] parameters) {
87dfb59d50631968ab1a13002ea5421ece93169851chrismair        verifySufficientParameters(parameters);
88dfb59d50631968ab1a13002ea5421ece93169851chrismair
89dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte host1 = parseByte(parameters[0]);
90dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte host2 = parseByte(parameters[1]);
91dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte host3 = parseByte(parameters[2]);
92dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte host4 = parseByte(parameters[3]);
93dfb59d50631968ab1a13002ea5421ece93169851chrismair
94dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte[] address = {host1, host2, host3, host4};
95dfb59d50631968ab1a13002ea5421ece93169851chrismair        InetAddress inetAddress = null;
96dfb59d50631968ab1a13002ea5421ece93169851chrismair        try {
97dfb59d50631968ab1a13002ea5421ece93169851chrismair            inetAddress = InetAddress.getByAddress(address);
98dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
99dfb59d50631968ab1a13002ea5421ece93169851chrismair        catch (UnknownHostException e) {
100dfb59d50631968ab1a13002ea5421ece93169851chrismair            throw new MockFtpServerException("Error parsing host", e);
101dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
102dfb59d50631968ab1a13002ea5421ece93169851chrismair
103dfb59d50631968ab1a13002ea5421ece93169851chrismair        int port1 = Integer.parseInt(parameters[4]);
104dfb59d50631968ab1a13002ea5421ece93169851chrismair        int port2 = Integer.parseInt(parameters[5]);
105dfb59d50631968ab1a13002ea5421ece93169851chrismair        int port = (port1 << 8) + port2;
106dfb59d50631968ab1a13002ea5421ece93169851chrismair
107dfb59d50631968ab1a13002ea5421ece93169851chrismair        return new HostAndPort(inetAddress, port);
108dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
109dfb59d50631968ab1a13002ea5421ece93169851chrismair
110dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
111dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Convert the InetAddess and port number to a comma-delimited list of byte values,
112dfb59d50631968ab1a13002ea5421ece93169851chrismair     * suitable for the response String from the PASV command.
113dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
114dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param host - the InetAddress
115dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param port - the port number
116dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the comma-delimited list of byte values, e.g., "196,168,44,55,23,77"
117dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
118dfb59d50631968ab1a13002ea5421ece93169851chrismair    public static String convertHostAndPortToCommaDelimitedBytes(InetAddress host, int port) {
119dfb59d50631968ab1a13002ea5421ece93169851chrismair        StringBuffer buffer = new StringBuffer();
120dfb59d50631968ab1a13002ea5421ece93169851chrismair        byte[] address = host.getAddress();
121dfb59d50631968ab1a13002ea5421ece93169851chrismair        for (int i = 0; i < address.length; i++) {
122dfb59d50631968ab1a13002ea5421ece93169851chrismair            int positiveValue = (address[i] >= 0) ? address[i] : 256 + address[i];
123dfb59d50631968ab1a13002ea5421ece93169851chrismair            buffer.append(positiveValue);
124dfb59d50631968ab1a13002ea5421ece93169851chrismair            buffer.append(",");
125dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
126dfb59d50631968ab1a13002ea5421ece93169851chrismair        int p1 = port >> 8;
127dfb59d50631968ab1a13002ea5421ece93169851chrismair        int p2 = port % 256;
128dfb59d50631968ab1a13002ea5421ece93169851chrismair        buffer.append(String.valueOf(p1));
129dfb59d50631968ab1a13002ea5421ece93169851chrismair        buffer.append(",");
130dfb59d50631968ab1a13002ea5421ece93169851chrismair        buffer.append(String.valueOf(p2));
131dfb59d50631968ab1a13002ea5421ece93169851chrismair        return buffer.toString();
132dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
133dfb59d50631968ab1a13002ea5421ece93169851chrismair
134dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
135dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Verify that the parameters is not null and contains the required number of elements
136dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
137dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param parameters - the String[] of command parameters
138dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @throws CommandSyntaxException - if parameters is null or contains an insufficient number of elements
139dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
140dfb59d50631968ab1a13002ea5421ece93169851chrismair    private static void verifySufficientParameters(String[] parameters) {
141dfb59d50631968ab1a13002ea5421ece93169851chrismair        if (parameters == null || parameters.length < 6) {
142dfb59d50631968ab1a13002ea5421ece93169851chrismair            List parms = parameters == null ? null : Arrays.asList(parameters);
143dfb59d50631968ab1a13002ea5421ece93169851chrismair            throw new CommandSyntaxException("The PORT command must contain least be 6 parameters: " + parms);
144dfb59d50631968ab1a13002ea5421ece93169851chrismair        }
145dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
146dfb59d50631968ab1a13002ea5421ece93169851chrismair
147dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
148dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Parse the specified String as an unsigned decimal byte value (i.e., 0..255). We can't just use
149dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Byte.parseByte(string) because that parses the string as a signed byte.
150dfb59d50631968ab1a13002ea5421ece93169851chrismair     *
151dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @param string - the String containing the decimal byte representation to be parsed
152dfb59d50631968ab1a13002ea5421ece93169851chrismair     * @return the byte value
153dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
154dfb59d50631968ab1a13002ea5421ece93169851chrismair    private static byte parseByte(String string) {
155dfb59d50631968ab1a13002ea5421ece93169851chrismair        return (byte) (0xFF & Short.parseShort(string));
156dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
157dfb59d50631968ab1a13002ea5421ece93169851chrismair
158dfb59d50631968ab1a13002ea5421ece93169851chrismair    /**
159dfb59d50631968ab1a13002ea5421ece93169851chrismair     * Private constructor. All methods are static.
160dfb59d50631968ab1a13002ea5421ece93169851chrismair     */
161dfb59d50631968ab1a13002ea5421ece93169851chrismair    private PortParser() {
162dfb59d50631968ab1a13002ea5421ece93169851chrismair    }
163dfb59d50631968ab1a13002ea5421ece93169851chrismair
164dfb59d50631968ab1a13002ea5421ece93169851chrismair}