1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.hermesftp.cmd.impl;
26
27 import net.sf.hermesftp.cmd.AbstractFtpCmdPort;
28
29 /***
30 * <b>DATA PORT (PORT)</b>
31 * <p>
32 * The argument is a HOST-PORT specification for the data port to be used in data connection. There
33 * are defaults for both the user and server data ports, and under normal circumstances this command
34 * and its reply are not needed. If this command is used, the argument is the concatenation of a
35 * 32-bit internet host address and a 16-bit TCP port address. This address information is broken
36 * into 8-bit fields and the value of each field is transmitted as a decimal number (in character
37 * string representation). The fields are separated by commas. A port command would be: PORT
38 * h1,h2,h3,h4,p1,p2 where h1 is the high order 8 bits of the internet host address.
39 * <p>
40 * <i>[Excerpt from RFC-959, Postel and Reynolds]</i>
41 * </p>
42 *
43 * @author Lars Behnke
44 */
45 public class FtpCmdPort extends AbstractFtpCmdPort {
46
47 private static final String DOT = ".";
48
49 private int port;
50
51 private String addr;
52
53 private String lastArgs;
54
55 /***
56 * {@inheritDoc}
57 */
58 public String getHelp() {
59 return "Sets port for active transfer.";
60 }
61
62 /***
63 * {@inheritDoc}
64 */
65 protected String doReadIPAddr(String args) {
66 if (!paramsParsed(args)) {
67 parseParams(args);
68 }
69 return addr;
70 }
71
72 /***
73 * {@inheritDoc}
74 */
75 protected int doReadPort(String args) {
76 if (!paramsParsed(args)) {
77 parseParams(args);
78 }
79 return port;
80 }
81
82 /***
83 * {@inheritDoc}
84 */
85 protected int doReadProtocolIdx(String args) {
86 return 1;
87 }
88
89 private boolean paramsParsed(String args) {
90 return lastArgs != null && lastArgs.equals(args);
91 }
92
93 private void parseParams(String args) {
94 try {
95 lastArgs = args;
96 String[] argParts = getArguments().split(",");
97 int idx = 0;
98 addr = argParts[idx++].trim() + DOT + argParts[idx++].trim() + DOT + argParts[idx++].trim() + DOT
99 + argParts[idx++].trim();
100 int p1 = Integer.parseInt(argParts[idx++].trim()) & BYTE_MASK;
101 int p2 = Integer.parseInt(argParts[idx++].trim()) & BYTE_MASK;
102 port = (p1 << BYTE_LENGTH) + p2;
103 } catch (Exception e) {
104 throw new IllegalArgumentException("Invalid arguments: " + args);
105 }
106 }
107
108 }