telnet.java

来自「apache推出的net包」· Java 代码 · 共 1,334 行 · 第 1/3 页

JAVA
1,334
字号
/* * Copyright 2003-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.net.telnet;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.OutputStream;import java.io.IOException;import org.apache.commons.net.SocketClient;/** * @author Daniel F. Savarese * @author Bruno D'Avanzo */class Telnet extends SocketClient{    static final boolean debug =  /*true;*/ false;    static final boolean debugoptions =  /*true;*/ false;    static final byte[] _COMMAND_DO = {                                          (byte)TelnetCommand.IAC, (byte)TelnetCommand.DO                                      };    static final byte[] _COMMAND_DONT = {                                            (byte)TelnetCommand.IAC, (byte)TelnetCommand.DONT                                        };    static final byte[] _COMMAND_WILL = {                                            (byte)TelnetCommand.IAC, (byte)TelnetCommand.WILL                                        };    static final byte[] _COMMAND_WONT = {                                            (byte)TelnetCommand.IAC, (byte)TelnetCommand.WONT                                        };    static final byte[] _COMMAND_SB = {                                          (byte)TelnetCommand.IAC, (byte)TelnetCommand.SB                                      };    static final byte[] _COMMAND_SE = {                                          (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE                                      };    static final int _WILL_MASK = 0x01, _DO_MASK = 0x02,                                  _REQUESTED_WILL_MASK = 0x04, _REQUESTED_DO_MASK = 0x08;    /* public */    static final int DEFAULT_PORT =  23;    int[] _doResponse, _willResponse, _options;    /* TERMINAL-TYPE option (start)*/    /***     * Terminal type option     ***/    protected static final int TERMINAL_TYPE = 24;    /***     * Send (for subnegotiation)     ***/    protected static final int TERMINAL_TYPE_SEND =  1;    /***     * Is (for subnegotiation)     ***/    protected static final int TERMINAL_TYPE_IS =  0;    /***     * Is sequence (for subnegotiation)     ***/    static final byte[] _COMMAND_IS = {                                          (byte) TERMINAL_TYPE, (byte) TERMINAL_TYPE_IS                                      };    /***     * Terminal type     ***/    private String terminalType = null;    /* TERMINAL-TYPE option (end)*/    /* open TelnetOptionHandler functionality (start)*/    /***     * Array of option handlers     ***/    private TelnetOptionHandler optionHandlers[];    /* open TelnetOptionHandler functionality (end)*/    /* Code Section added for supporting AYT (start)*/    /***     * AYT sequence     ***/    static final byte[] _COMMAND_AYT = {                                          (byte) TelnetCommand.IAC, (byte) TelnetCommand.AYT                                       };    /***     * monitor to wait for AYT     ***/    private Object aytMonitor = new Object();    /***     * flag for AYT     ***/    private boolean aytFlag = true;    /* Code Section added for supporting AYT (end)*/    /***     * The stream on which to spy     ***/    private OutputStream spyStream = null;    /***     * The notification handler     ***/    private TelnetNotificationHandler __notifhand = null;    /***     * Empty Constructor     ***/    Telnet()    {        setDefaultPort(DEFAULT_PORT);        _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];        _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];        _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];        optionHandlers =            new TelnetOptionHandler[TelnetOption.MAX_OPTION_VALUE + 1];    }    /* TERMINAL-TYPE option (start)*/    /***     * This constructor lets you specify the terminal type.     * <p>     * @param termtype - terminal type to be negotiated (ej. VT100)     ***/    Telnet(String termtype)    {        setDefaultPort(DEFAULT_PORT);        _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];        _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];        _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];        terminalType = termtype;        optionHandlers =            new TelnetOptionHandler[TelnetOption.MAX_OPTION_VALUE + 1];    }    /* TERMINAL-TYPE option (end)*/    /***     * Looks for the state of the option.     * <p>     * @return returns true if a will has been acknowledged     * <p>     * @param option - option code to be looked up.     ***/    boolean _stateIsWill(int option)    {        return ((_options[option] & _WILL_MASK) != 0);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a wont has been acknowledged     * <p>     * @param option - option code to be looked up.     ***/    boolean _stateIsWont(int option)    {        return !_stateIsWill(option);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a do has been acknowledged     * <p>     * @param option - option code to be looked up.     ***/    boolean _stateIsDo(int option)    {        return ((_options[option] & _DO_MASK) != 0);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a dont has been acknowledged     * <p>     * @param option - option code to be looked up.     ***/    boolean _stateIsDont(int option)    {        return !_stateIsDo(option);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a will has been reuqested     * <p>     * @param option - option code to be looked up.     ***/    boolean _requestedWill(int option)    {        return ((_options[option] & _REQUESTED_WILL_MASK) != 0);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a wont has been reuqested     * <p>     * @param option - option code to be looked up.     ***/    boolean _requestedWont(int option)    {        return !_requestedWill(option);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a do has been reuqested     * <p>     * @param option - option code to be looked up.     ***/    boolean _requestedDo(int option)    {        return ((_options[option] & _REQUESTED_DO_MASK) != 0);    }    /***     * Looks for the state of the option.     * <p>     * @return returns true if a dont has been reuqested     * <p>     * @param option - option code to be looked up.     ***/    boolean _requestedDont(int option)    {        return !_requestedDo(option);    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWill(int option)    {        _options[option] |= _WILL_MASK;        /* open TelnetOptionHandler functionality (start)*/        if (_requestedWill(option))        {            if (optionHandlers[option] != null)            {                optionHandlers[option].setWill(true);                int subneg[] =                    optionHandlers[option].startSubnegotiationLocal();                if (subneg != null)                {                    try                    {                        _sendSubnegotiation(subneg);                    }                    catch (Exception e)                    {                        System.err.println(                            "Exception in option subnegotiation"                            + e.getMessage());                    }                }            }        }        /* open TelnetOptionHandler functionality (end)*/    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setDo(int option)    {        _options[option] |= _DO_MASK;        /* open TelnetOptionHandler functionality (start)*/        if (_requestedDo(option))        {            if (optionHandlers[option] != null)            {                optionHandlers[option].setDo(true);                int subneg[] =                    optionHandlers[option].startSubnegotiationRemote();                if (subneg != null)                {                    try                    {                        _sendSubnegotiation(subneg);                    }                    catch (Exception e)                    {                        System.err.println("Exception in option subnegotiation"                            + e.getMessage());                    }                }            }        }        /* open TelnetOptionHandler functionality (end)*/    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWantWill(int option)    {        _options[option] |= _REQUESTED_WILL_MASK;    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWantDo(int option)    {        _options[option] |= _REQUESTED_DO_MASK;    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWont(int option)    {        _options[option] &= ~_WILL_MASK;        /* open TelnetOptionHandler functionality (start)*/        if (optionHandlers[option] != null)        {            optionHandlers[option].setWill(false);        }        /* open TelnetOptionHandler functionality (end)*/    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setDont(int option)    {        _options[option] &= ~_DO_MASK;        /* open TelnetOptionHandler functionality (start)*/        if (optionHandlers[option] != null)        {            optionHandlers[option].setDo(false);        }        /* open TelnetOptionHandler functionality (end)*/    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWantWont(int option)    {        _options[option] &= ~_REQUESTED_WILL_MASK;    }    /***     * Sets the state of the option.     * <p>     * @param option - option code to be set.     ***/    void _setWantDont(int option)    {        _options[option] &= ~_REQUESTED_DO_MASK;    }    /***     * Processes a DO request.     * <p>     * @throws IOException - Exception in I/O.     * <p>     * @param option - option code to be set.     ***/    void _processDo(int option) throws IOException    {        if (debugoptions)        {            System.err.println("RECEIVED DO: "                + TelnetOption.getOption(option));        }        if (__notifhand != null)        {            __notifhand.receivedNegotiation(                TelnetNotificationHandler.RECEIVED_DO,                option);        }        boolean acceptNewState = false;        /* open TelnetOptionHandler functionality (start)*/        if (optionHandlers[option] != null)        {            acceptNewState = optionHandlers[option].getAcceptLocal();        }        else        {        /* open TelnetOptionHandler functionality (end)*/            /* TERMINAL-TYPE option (start)*/            if (option == TERMINAL_TYPE)            {                if ((terminalType != null) && (terminalType.length() > 0))                {                    acceptNewState = true;                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?