    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    /** The maximum value an option code can have. This value is 255. */
    031    public static final int MAX_OPTION_VALUE = 255;
    032
    033    /**
    034     * {@value}
    035     */
    036    public static final int BINARY = 0;
    037
    038    /**
    039     * {@value}
    040     */
    041    public static final int ECHO = 1;
    042
    043    /**
    044     * {@value}
    045     */
    046    public static final int PREPARE_TO_RECONNECT = 2;
    047
    048    /**
    049     * {@value}
    050     */
    051    public static final int SUPPRESS_GO_AHEAD = 3;
    052
    053    /**
    054     * {@value}
    055     */
    056    public static final int APPROXIMATE_MESSAGE_SIZE = 4;
    057
    058    /**
    059     * {@value}
    060     */
    061    public static final int STATUS = 5;
    062
    063    /**
    064     * {@value}
    065     */
    066    public static final int TIMING_MARK = 6;
    067
    068    /**
    069     * {@value}
    070     */
    071    public static final int REMOTE_CONTROLLED_TRANSMISSION = 7;
    072
    073    /**
    074     * {@value}
    075     */
    076    public static final int NEGOTIATE_OUTPUT_LINE_WIDTH = 8;
    077
    078    /**
    079     * {@value}
    080     */
    081    public static final int NEGOTIATE_OUTPUT_PAGE_SIZE = 9;
    082
    083    /**
    084     * {@value}
    085     */
    086    public static final int NEGOTIATE_CARRIAGE_RETURN = 10;
    087
    088    /**
    089     * {@value}
    090     */
    091    public static final int NEGOTIATE_HORIZONTAL_TAB_STOP = 11;
    092
    093    /**
    094     * {@value}
    095     */
    096    public static final int NEGOTIATE_HORIZONTAL_TAB = 12;
    097
    098    /**
    099     * {@value}
    100     */
    101    public static final int NEGOTIATE_FORMFEED = 13;
    102
    103    /**
    104     * {@value}
    105     */
    106    public static final int NEGOTIATE_VERTICAL_TAB_STOP = 14;
    107
    108    /**
    109     * {@value}
    110     */
    111    public static final int NEGOTIATE_VERTICAL_TAB = 15;
    112
    113    /**
    114     * {@value}
    115     */
    116    public static final int NEGOTIATE_LINEFEED = 16;
    117
    118    /**
    119     * {@value}
    120     */
    121    public static final int EXTENDED_ASCII = 17;
    122
    123    /**
    124     * {@value}
    125     */
    126    public static final int FORCE_LOGOUT = 18;
    127
    128    /**
    129     * {@value}
    130     */
    131    public static final int BYTE_MACRO = 19;
    132
    133    /**
    134     * {@value}
    135     */
    136    public static final int DATA_ENTRY_TERMINAL = 20;
    137
    138    /**
    139     * {@value}
    140     */
    141    public static final int SUPDUP = 21;
    142
    143    /**
    144     * {@value}
    145     */
    146    public static final int SUPDUP_OUTPUT = 22;
    147
    148    /**
    149     * {@value}
    150     */
    151    public static final int SEND_LOCATION = 23;
    152
    153    /**
    154     * {@value}
    155     */
    156    public static final int TERMINAL_TYPE = 24;
    157
    158    /**
    159     * {@value}
    160     */
    161    public static final int END_OF_RECORD = 25;
    162
    163    /**
    164     * {@value}
    165     */
    166    public static final int TACACS_USER_IDENTIFICATION = 26;
    167
    168    /**
    169     * {@value}
    170     */
    171    public static final int OUTPUT_MARKING = 27;
    172
    173    /**
    174     * {@value}
    175     */
    176    public static final int TERMINAL_LOCATION_NUMBER = 28;
    177
    178    /**
    179     * {@value}
    180     */
    181    public static final int REGIME_3270 = 29;
    182
    183    /**
    184     * {@value}
    185     */
    186    public static final int X3_PAD = 30;
    187
    188    /**
    189     * {@value}
    190     */
    191    public static final int WINDOW_SIZE = 31;
    192
    193    /**
    194     * {@value}
    195     */
    196    public static final int TERMINAL_SPEED = 32;
    197
    198    /**
    199     * {@value}
    200     */
    201    public static final int REMOTE_FLOW_CONTROL = 33;
    202
    203    /**
    204     * {@value}
    205     */
    206    public static final int LINEMODE = 34;
    207
    208    /**
    209     * {@value}
    210     */
    211    public static final int X_DISPLAY_LOCATION = 35;
    212
    213    /**
    214     * {@value}
    215     */
    216    public static final int OLD_ENVIRONMENT_VARIABLES = 36;
    217
    218    /**
    219     * {@value}
    220     */
    221    public static final int AUTHENTICATION = 37;
    222
    223    /**
    224     * {@value}
    225     */
    226    public static final int ENCRYPTION = 38;
    227
    228    /**
    229     * {@value}
    230     */
    231    public static final int NEW_ENVIRONMENT_VARIABLES = 39;
    232
    233    /**
    234     * {@value}
    235     */
    236    public static final int EXTENDED_OPTIONS_LIST = 255;
    237
    238    /**
    239     * {@value}
    240     */
    241    @SuppressWarnings("unused")
    242    private static final int FIRST_OPTION = BINARY;
    243
    244    /**
    245     * {@value}
    246     */
    247    private static final int LAST_OPTION = EXTENDED_OPTIONS_LIST;
    248
    249    /**
    250     * {@value}
    251     */
    252    private static final String[] optionString = { "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME", "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
    253            "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS", "NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO", "DATA ENTRY TERMINAL", "SUPDUP",
    254            "SUPDUP OUTPUT", "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD", "TACACS UID", "OUTPUT MARKING", "TTYLOC", "3270 REGIME", "X.3 PAD", "NAWS",
    255            "TSPEED", "LFLOW", "LINEMODE", "XDISPLOC", "OLD-ENVIRON", "AUTHENTICATION", "ENCRYPT", "NEW-ENVIRON", "TN3270E", "XAUTH", "CHARSET", "RSP",
    256            "Com Port Control", "Suppress Local Echo", "Start TLS", "KERMIT", "SEND-URL", "FORWARD_X", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    257            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    258            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    259            "TELOPT PRAGMA LOGON", "TELOPT SSPI LOGON", "TELOPT PRAGMA HEARTBEAT", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    260            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    261            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    262            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Extended-Options-List" };
    263
    264    /**
    265     * Returns the string representation of the Telnet protocol option corresponding to the given option code.
    266     *
    267     * @param code The option code of the Telnet protocol option
    268     * @return The string representation of the Telnet protocol option.
    269     */
    270    public static final String getOption(final int code) {
    271        if (optionString[code].isEmpty()) {
    272            return "UNASSIGNED";
    273        }
    274        return optionString[code];
    275    }
    276
    277    /**
    278     * Determines if a given option code is valid. Returns true if valid, false if not.
    279     *
    280     * @param code The option code to test.
    281     * @return True if the option code is valid, false if not.
    282     **/
    283    public static final boolean isValidOption(final int code) {
    284        return code <= LAST_OPTION;
    285    }
    286
    287    /** Cannot be instantiated. */
    288    private TelnetOption() {
    289    }
    290}