001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.telnet;
019
020/**
021 * The TelnetOption class cannot be instantiated and only serves as a storehouse for Telnet option constants.
022 * <p>
023 * Details regarding Telnet option specification can be found in RFC 855.
024 * </p>
025 *
026 * @see org.apache.commons.net.telnet.Telnet
027 * @see org.apache.commons.net.telnet.TelnetClient
028 */
029public class TelnetOption {
030
031    /** The maximum value an option code can have. This value is 255. */
032    public static final int MAX_OPTION_VALUE = 255;
033
034    /**
035     * {@value}
036     */
037    public static final int BINARY = 0;
038
039    /**
040     * {@value}
041     */
042    public static final int ECHO = 1;
043
044    /**
045     * {@value}
046     */
047    public static final int PREPARE_TO_RECONNECT = 2;
048
049    /**
050     * {@value}
051     */
052    public static final int SUPPRESS_GO_AHEAD = 3;
053
054    /**
055     * {@value}
056     */
057    public static final int APPROXIMATE_MESSAGE_SIZE = 4;
058
059    /**
060     * {@value}
061     */
062    public static final int STATUS = 5;
063
064    /**
065     * {@value}
066     */
067    public static final int TIMING_MARK = 6;
068
069    /**
070     * {@value}
071     */
072    public static final int REMOTE_CONTROLLED_TRANSMISSION = 7;
073
074    /**
075     * {@value}
076     */
077    public static final int NEGOTIATE_OUTPUT_LINE_WIDTH = 8;
078
079    /**
080     * {@value}
081     */
082    public static final int NEGOTIATE_OUTPUT_PAGE_SIZE = 9;
083
084    /**
085     * {@value}
086     */
087    public static final int NEGOTIATE_CARRIAGE_RETURN = 10;
088
089    /**
090     * {@value}
091     */
092    public static final int NEGOTIATE_HORIZONTAL_TAB_STOP = 11;
093
094    /**
095     * {@value}
096     */
097    public static final int NEGOTIATE_HORIZONTAL_TAB = 12;
098
099    /**
100     * {@value}
101     */
102    public static final int NEGOTIATE_FORMFEED = 13;
103
104    /**
105     * {@value}
106     */
107    public static final int NEGOTIATE_VERTICAL_TAB_STOP = 14;
108
109    /**
110     * {@value}
111     */
112    public static final int NEGOTIATE_VERTICAL_TAB = 15;
113
114    /**
115     * {@value}
116     */
117    public static final int NEGOTIATE_LINEFEED = 16;
118
119    /**
120     * {@value}
121     */
122    public static final int EXTENDED_ASCII = 17;
123
124    /**
125     * {@value}
126     */
127    public static final int FORCE_LOGOUT = 18;
128
129    /**
130     * {@value}
131     */
132    public static final int BYTE_MACRO = 19;
133
134    /**
135     * {@value}
136     */
137    public static final int DATA_ENTRY_TERMINAL = 20;
138
139    /**
140     * {@value}
141     */
142    public static final int SUPDUP = 21;
143
144    /**
145     * {@value}
146     */
147    public static final int SUPDUP_OUTPUT = 22;
148
149    /**
150     * {@value}
151     */
152    public static final int SEND_LOCATION = 23;
153
154    /**
155     * {@value}
156     */
157    public static final int TERMINAL_TYPE = 24;
158
159    /**
160     * {@value}
161     */
162    public static final int END_OF_RECORD = 25;
163
164    /**
165     * {@value}
166     */
167    public static final int TACACS_USER_IDENTIFICATION = 26;
168
169    /**
170     * {@value}
171     */
172    public static final int OUTPUT_MARKING = 27;
173
174    /**
175     * {@value}
176     */
177    public static final int TERMINAL_LOCATION_NUMBER = 28;
178
179    /**
180     * {@value}
181     */
182    public static final int REGIME_3270 = 29;
183
184    /**
185     * {@value}
186     */
187    public static final int X3_PAD = 30;
188
189    /**
190     * {@value}
191     */
192    public static final int WINDOW_SIZE = 31;
193
194    /**
195     * {@value}
196     */
197    public static final int TERMINAL_SPEED = 32;
198
199    /**
200     * {@value}
201     */
202    public static final int REMOTE_FLOW_CONTROL = 33;
203
204    /**
205     * {@value}
206     */
207    public static final int LINEMODE = 34;
208
209    /**
210     * {@value}
211     */
212    public static final int X_DISPLAY_LOCATION = 35;
213
214    /**
215     * {@value}
216     */
217    public static final int OLD_ENVIRONMENT_VARIABLES = 36;
218
219    /**
220     * {@value}
221     */
222    public static final int AUTHENTICATION = 37;
223
224    /**
225     * {@value}
226     */
227    public static final int ENCRYPTION = 38;
228
229    /**
230     * {@value}
231     */
232    public static final int NEW_ENVIRONMENT_VARIABLES = 39;
233
234    /**
235     * {@value}
236     */
237    public static final int EXTENDED_OPTIONS_LIST = 255;
238
239    /**
240     * {@value}
241     */
242    @SuppressWarnings("unused")
243    private static final int FIRST_OPTION = BINARY;
244
245    /**
246     * {@value}
247     */
248    private static final int LAST_OPTION = EXTENDED_OPTIONS_LIST;
249
250    /**
251     * {@value}
252     */
253    private static final String[] optionString = { "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME", "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
254            "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS", "NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO", "DATA ENTRY TERMINAL", "SUPDUP",
255            "SUPDUP OUTPUT", "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD", "TACACS UID", "OUTPUT MARKING", "TTYLOC", "3270 REGIME", "X.3 PAD", "NAWS",
256            "TSPEED", "LFLOW", "LINEMODE", "XDISPLOC", "OLD-ENVIRON", "AUTHENTICATION", "ENCRYPT", "NEW-ENVIRON", "TN3270E", "XAUTH", "CHARSET", "RSP",
257            "Com Port Control", "Suppress Local Echo", "Start TLS", "KERMIT", "SEND-URL", "FORWARD_X", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
258            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
259            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
260            "TELOPT PRAGMA LOGON", "TELOPT SSPI LOGON", "TELOPT PRAGMA HEARTBEAT", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
261            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
262            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
263            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Extended-Options-List" };
264
265    /**
266     * Returns the string representation of the Telnet protocol option corresponding to the given option code.
267     *
268     * @param code The option code of the Telnet protocol option
269     * @return The string representation of the Telnet protocol option.
270     */
271    public static final String getOption(final int code) {
272        if (optionString[code].isEmpty()) {
273            return "UNASSIGNED";
274        }
275        return optionString[code];
276    }
277
278    /**
279     * Determines if a given option code is valid. Returns true if valid, false if not.
280     *
281     * @param code The option code to test.
282     * @return True if the option code is valid, false if not.
283     **/
284    public static final boolean isValidOption(final int code) {
285        return code <= LAST_OPTION;
286    }
287
288    /** Cannot be instantiated. */
289    private TelnetOption() {
290    }
291}