1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.telnet;
19
20 /**
21 * The TelnetCommand class cannot be instantiated and only serves as a storehouse for Telnet command constants.
22 *
23 * @see org.apache.commons.net.telnet.Telnet
24 * @see org.apache.commons.net.telnet.TelnetClient
25 */
26
27 public final class TelnetCommand {
28
29 /** The maximum value a command code can have. This value is 255. */
30 public static final int MAX_COMMAND_VALUE = 255;
31
32 /** Interpret As Command code. Value is 255 according to RFC 854. */
33 public static final int IAC = 255;
34
35 /** Don't use option code. Value is 254 according to RFC 854. */
36 public static final int DONT = 254;
37
38 /** Request to use option code. Value is 253 according to RFC 854. */
39 public static final int DO = 253;
40
41 /** Refuse to use option code. Value is 252 according to RFC 854. */
42 public static final int WONT = 252;
43
44 /** Agree to use option code. Value is 251 according to RFC 854. */
45 public static final int WILL = 251;
46
47 /** Start subnegotiation code. Value is 250 according to RFC 854. */
48 public static final int SB = 250;
49
50 /** Go Ahead code. Value is 249 according to RFC 854. */
51 public static final int GA = 249;
52
53 /** Erase Line code. Value is 248 according to RFC 854. */
54 public static final int EL = 248;
55
56 /** Erase Character code. Value is 247 according to RFC 854. */
57 public static final int EC = 247;
58
59 /** Are You There code. Value is 246 according to RFC 854. */
60 public static final int AYT = 246;
61
62 /** Abort Output code. Value is 245 according to RFC 854. */
63 public static final int AO = 245;
64
65 /** Interrupt Process code. Value is 244 according to RFC 854. */
66 public static final int IP = 244;
67
68 /** Break code. Value is 243 according to RFC 854. */
69 public static final int BREAK = 243;
70
71 /** Data mark code. Value is 242 according to RFC 854. */
72 public static final int DM = 242;
73
74 /** No Operation code. Value is 241 according to RFC 854. */
75 public static final int NOP = 241;
76
77 /** End subnegotiation code. Value is 240 according to RFC 854. */
78 public static final int SE = 240;
79
80 /** End of record code. Value is 239. */
81 public static final int EOR = 239;
82
83 /** Abort code. Value is 238. */
84 public static final int ABORT = 238;
85
86 /** Suspend process code. Value is 237. */
87 public static final int SUSP = 237;
88
89 /** End of file code. Value is 236. */
90 public static final int EOF = 236;
91
92 /** Synchronize code. Value is 242. */
93 public static final int SYNCH = 242;
94
95 /** String representations of commands. */
96 private static final String[] commandString = { "IAC", "DONT", "DO", "WONT", "WILL", "SB", "GA", "EL", "EC", "AYT", "AO", "IP", "BRK", "DMARK", "NOP", "SE",
97 "EOR", "ABORT", "SUSP", "EOF" };
98
99 private static final int FIRST_COMMAND = IAC;
100 private static final int LAST_COMMAND = EOF;
101
102 /**
103 * Gets the string representation of the Telnet protocol command corresponding to the given command code.
104 *
105 * @param code The command code of the Telnet protocol command.
106 * @return The string representation of the Telnet protocol command.
107 */
108 public static String getCommand(final int code) {
109 return commandString[FIRST_COMMAND - code];
110 }
111
112 /**
113 * Tests if a given command code is valid. Returns true if valid, false if not.
114 *
115 * @param code The command code to test.
116 * @return True if the command code is valid, false if not.
117 **/
118 public static boolean isValidCommand(final int code) {
119 return code <= FIRST_COMMAND && code >= LAST_COMMAND;
120 }
121
122 /** Cannot be instantiated. */
123 private TelnetCommand() {
124 }
125 }