How to get IPv6 from LPRT ftp command

312 Views Asked by At

I am creating a FTP sever . According to FTP specifications they have added new command called LPRT .

Its format is LPRT 6,16,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,2,193,160

Now i am able to get FTP port from it using logic below.But I need IPV6 address as well from this string .

public static void main(String[] args) {
    StringTokenizer st = new StringTokenizer(
            "6,16,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,2,193,160");

      final String portStr = st.nextToken(); 
      final int lastDelimIdx =portStr.lastIndexOf(',', portStr.lastIndexOf(',') - 1);
      final StringTokenizer portst = new StringTokenizer(portStr.substring(lastDelimIdx + 1, portStr.length()), ",");
      final int p1 = Integer.parseInt(portst.nextToken());
      final int p2 = Integer.parseInt(portst.nextToken());
      final int dataPort = (p1 << 8) | p2;
      System.out.println(dataPort);
}

Can some one help me to find the IPv6 address from this string.

2

There are 2 best solutions below

3
On BEST ANSWER

LPRT and LPSV are considered obsolete, see https://www.iana.org/assignments/ftp-commands-extensions/ftp-commands-extensions.txt. To use IPv6 use EPSV and EPRT which are specified in RFC2428.

If you still need to know how do deal with LPRT look into the obsolete RFC1639. For example:

LPRT 6,16,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,2,193,160

means that the 16 numbers after the host length argument are the bytes of the address and then follows the port address length with 2 bytes, i.e.

[0000:0000:0000:0000:0300:0000:0000:0000]:49568
0
On

I created a program which does this extraction

    public static String longToIP(long[] ip) {
    String ipString = "";

    int flag = 0;
    for (long crtLong : ip) {

        if (flag == 2) {
            ipString = ipString + ":";
            flag = 0;
        }
        String s = Long.toHexString(crtLong & 0xFFFFFFFFL);
        if (s.length() == 1) {
            s = "0" + s;
        }
        ipString = ipString + "" + s;
        flag++;

    }
    return ipString;

}

Input = { 0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0 }

OutPut = {0000:0000:0000:0000:0300:0000:0000:0000}