Beispiel #1
0
 public void UnsetOption(TelnetOptions myOption)
 {
     if (_LocalOptions[(Int32)myOption] == _RemoteOptions[(Int32)myOption])
     {
         SendDontOption(myOption);
     }
 }
Beispiel #2
0
        public TelnetSymbol(TelnetCommands myCommand, TelnetOptions myOption)
        {
            _Command = myCommand;
            _Option  = myOption;

            _TelnetSymbolType = TelnetSymbolTypes.Command;
        }
Beispiel #3
0
 public void SetWillOption(TelnetOptions myOption)
 {
     if (_LocalOptions[(Int32)myOption] == _RemoteOptions[(Int32)myOption])
     {
         SendWillOption(myOption);
     }
 }
Beispiel #4
0
        public TelnetSymbol(TelnetCommands myCommand, TelnetOptions myOption)
        {
            _Command = myCommand;
            _Option = myOption;

            _TelnetSymbolType = TelnetSymbolTypes.Command;
        }
Beispiel #5
0
        public static TelnetOptions ToTelnetOption(this byte b)
        {
            foreach (FieldInfo fi in typeof(TelnetOptions).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                TelnetOptions option = (TelnetOptions)fi.GetValue(null);
                if ((byte)option == b)
                {
                    return(option);
                }
            }

            return(TelnetOptions.SupressGoAhead);
        }
Beispiel #6
0
        /// <summary> This method is if you want to send specific telenet options to the server.</summary>
        /// <param name="telnetCommand"> This is the TelnetCommand you want to send.</param>
        /// <param name="telnetOption"> This is the TelnetOption you want to send.</param>
        /// <param name="data"> This is data you want to send along.  If you do not want to send any data set as null.</param>
        public void TransmitTelnetCommand(TelnetCommands telnetCommand, TelnetOptions telnetOption, byte[] data)
        {
            if (!_socket.Connected)
            {
                throw new Exception("Socket is no longer connected." + Environment.NewLine);
            }

            var wholeRequestList = new List <byte> {
                (byte)TelnetCommands.IAC, (byte)telnetCommand, (byte)telnetOption
            };

            if (data != null)
            {
                if (data.Length > 0)
                {
                    wholeRequestList.AddRange(data);
                }
            }
            TransmitByteArray(wholeRequestList.ToArray());
        }
Beispiel #7
0
 public void UnsetOption(TelnetOptions myOption)
 {
     if (_LocalOptions[(Int32)myOption] == _RemoteOptions[(Int32)myOption])
     {
         SendDontOption(myOption);
     }
 }
Beispiel #8
0
 public void SetWillOption(TelnetOptions myOption)
 {
     if (_LocalOptions[(Int32)myOption] == _RemoteOptions[(Int32)myOption])
     {
         SendWillOption(myOption);
     }
 }
Beispiel #9
0
 public static byte Translate(this TelnetOptions option)
 {
     return((byte)option);
 }
Beispiel #10
0
        public static TelnetOptionFlags ToFlag(this TelnetOptions option)
        {
            string name = Enum.GetName(typeof(TelnetOptions), option);

            return((TelnetOptionFlags)Enum.Parse(typeof(TelnetOptionFlags), name, true));
        }
Beispiel #11
0
 /// <summary>
 /// If we are willing to accept the requested option
 /// </summary>
 /// <param name="option"></param>
 private void SendWillOption(TelnetOptions myOption)
 {
     SendOption(TelnetCommands.Will, myOption);
 }
Beispiel #12
0
 private void SendOption(TelnetCommands myCommand, TelnetOptions myOption)
 {
     Byte[] data = new Byte[] { (Byte)TelnetCommands.Iac, (Byte)myCommand, (Byte)myOption };
     HandleDataToSend(data);
 }
Beispiel #13
0
 /// <summary>
 /// Send a dont option request
 /// </summary>
 /// <param name="myOption"></param>
 private void SendDontOption(TelnetOptions myOption)
 {
     SendOption(TelnetCommands.Dont, myOption);
 }
Beispiel #14
0
 /// <summary>
 /// If we are willing to accept the requested option
 /// </summary>
 /// <param name="option"></param>
 private void SendWillOption(TelnetOptions myOption)
 {
     SendOption(TelnetCommands.Will, myOption);
 }
Beispiel #15
0
 /// <summary>
 /// Send a do option request
 /// </summary>
 /// <param name="option"></param>
 private void SendDoOption(TelnetOptions myOption)
 {
     SendOption(TelnetCommands.Do, myOption);
 }
Beispiel #16
0
 private void SendOption(TelnetCommands myCommand, TelnetOptions myOption)
 {
     Byte[] data = new Byte[] { (Byte)TelnetCommands.Iac, (Byte)myCommand, (Byte)myOption };
     HandleDataToSend(data);
 }
Beispiel #17
0
        public Boolean Parse(Byte[] myBytes)
        {
            //List<TelnetSymbol> resolvedSymbols = new List<TelnetSymbol>();
            TelnetSymbolTypes lastByteType        = TelnetSymbolTypes.Unknown;
            List <Byte>       dataBytes           = new List <byte>();
            List <Byte>       subnegotiationBytes = new List <byte>();

            for (Int32 i = 0; i < myBytes.Length; i++)
            {
                //As required by the Telnet protocol, any occurrence of 255 in the subnegotiation must be doubled to distinguish it from the IAC character (which has a value of 255)
                if (myBytes[i] == (Byte)TelnetCommands.Iac || lastByteType == TelnetSymbolTypes.Subnegotiation)
                {
                    if (lastByteType == TelnetSymbolTypes.Data)
                    {
                        HandleData(dataBytes.ToArray());
                        HandleKeys(dataBytes.ToArray());
                        dataBytes.Clear();
                    }

                    #region Read the next command or subnegotiation data

                    TelnetCommands command;
                    if (lastByteType == TelnetSymbolTypes.Subnegotiation)
                    {
                        command = (TelnetCommands)myBytes[i];
                    }
                    else
                    {
                        command = (TelnetCommands)myBytes[++i];
                    }

                    if (lastByteType == TelnetSymbolTypes.Subnegotiation)
                    {
                        if (command != TelnetCommands.Iac || myBytes[i + 1] != (Byte)TelnetCommands.SubnegotiationEnd)
                        {
                            Console.Write(myBytes[i] + ", ");
                            subnegotiationBytes.Add(myBytes[i]);

                            //As required by the Telnet protocol, any occurrence of 255 in the subnegotiation must be doubled to distinguish it from the IAC character (which has a value of 255)
                            if (command == TelnetCommands.Iac && i + 1 < myBytes.Length && myBytes[i + 1] == (Byte)TelnetCommands.Iac)
                            {
                                i++;
                            }

                            continue;
                        }
                        else
                        {
                            command = (TelnetCommands)myBytes[++i];
                        }
                    }

                    #endregion

                    switch (command)
                    {
                        #region Subnegotiation

                    case TelnetCommands.Subnegotiation:
                        //throw new NotImplementedException("TelnetCommands.Subnegotiation");
                        Console.WriteLine("TelnetCommands.Subnegotiation");
                        subnegotiationBytes.Clear();
                        lastByteType = TelnetSymbolTypes.Subnegotiation;
                        break;

                    case TelnetCommands.SubnegotiationEnd:
                        //throw new NotImplementedException("TelnetCommands.SubnegotiationEnd");
                        Console.WriteLine("TelnetCommands.SubnegotiationEnd");
                        HandleSubnegotiation(subnegotiationBytes.ToArray());
                        lastByteType = TelnetSymbolTypes.Unknown;
                        break;

                        #endregion

                        #region Simple commands without option

                    case TelnetCommands.NoOperation:
                    case TelnetCommands.DataMark:
                    case TelnetCommands.Break:
                    case TelnetCommands.InterruptProcess:
                    case TelnetCommands.AbortOutput:
                    case TelnetCommands.AreYouThere:
                    case TelnetCommands.EraseCharacter:
                    case TelnetCommands.EraseLine:
                    case TelnetCommands.GoAhead:
                        //resolvedSymbols.Add(new TelnetSymbol(command));
                        lastByteType = TelnetSymbolTypes.Command;
                        HandleTelnetCommand(new TelnetSymbol(command));
                        break;

                        #endregion

                        #region Option commands

                    case TelnetCommands.Will:
                    case TelnetCommands.Wont:
                    case TelnetCommands.Do:
                    case TelnetCommands.Dont:
                        TelnetOptions option = (TelnetOptions)myBytes[++i];
                        //resolvedSymbols.Add(new TelnetSymbol(command, option));
                        lastByteType = TelnetSymbolTypes.Command;
                        HandleTelnetOption(new TelnetSymbol(command, option));
                        break;

                        #endregion

                    default:
                        throw new NotSupportedException("Not supported command: " + command);
                    }
                }
                else
                {
                    lastByteType = TelnetSymbolTypes.Data;
                    //if (myBytes[i] != (Byte)ASCIIControlCodes.CR && myBytes[i] != (Byte)ASCIIControlCodes.LF)
                    dataBytes.Add(myBytes[i]);
                }
            }

            if (lastByteType == TelnetSymbolTypes.Data && dataBytes.Count > 0)
            {
                HandleData(dataBytes.ToArray());
                HandleKeys(dataBytes.ToArray());
            }

            return(true);
        }
Beispiel #18
0
 /// <summary>
 /// If we won't accept  the requested option
 /// </summary>
 /// <param name="option"></param>
 private void SendWontOption(TelnetOptions myOption)
 {
     SendOption(TelnetCommands.Wont, myOption);
 }
Beispiel #19
0
        void ParseResponse()
        {
            // track the current color that should be applied to the text
            Color currentColor = Colors.Cyan;//Color.FromArgb(0xFF, 0x0, 0x0, 0x0);

            do
            {
                // wait for input
                _writeLock.WaitOne();

                // reset the lock
                _writeLock.Reset();

                // create an array to hold the bytes
                byte[] data = new byte[_overflow.Length];

                // copy any overflow bytes into the array
                Array.Copy(_overflow, data, _overflow.Length);

                // clear the overflow
                _overflow = new byte[0];

                // lock the buffer to keep additional data entry
                lock (_buffer)
                {
                    // append each block in the buffer to the data array
                    while (_buffer.Count > 0)
                    {
                        byte[] next = _buffer.Dequeue();
                        Array.Resize <byte>(ref data, data.Length + next.Length);
                        Array.Copy(next, 0, data, data.Length - next.Length, next.Length);
                    }
                }

                // begin processing the bytes

                // check if consumer supports ansi
                bool ansi = _flags.Contains(TelnetOptionFlags.ANSI);

                // create a list to hold the segments
                List <IFormattedTextSegment> segments = new List <IFormattedTextSegment>();

                // create a string builder to construct segments
                StringBuilder segment = new StringBuilder();

                // create a byte array to store any response to server requests
                byte[] response = new byte[0];

                // process the entire byte stream
                for (int i = 0; i < data.Length; ++i)
                {
                    if (data[i] == ESC) // ansi escape
                    {
                        Color background = Colors.Black;
                        int   row = 0, col = 0;

                        if (segment.Length > 0)
                        {
                            segments.Add(new DisplayText()
                            {
                                Text = segment.ToString(), TextColor = currentColor
                            });
                            segment.Clear();
                        }

                        int processed = AnsiProcessor.ReadCommand(data, i, ref row, ref col, ref background, ref currentColor);

                        if (processed < 0) // fragment
                        {
                            _overflow = new byte[data.Length - i];
                            Array.Copy(data, i, _overflow, 0, _overflow.Length);
                            break;
                        }


                        i += processed; // advance past the command

                        continue;
                    }
                    else if (data[i] == TelnetCommands.IAC.Translate()) // process the telnet command
                    {
                        TelnetCommands command = data[++i].ToTelnetCommand();

                        // subnegotiation so there will be more data
                        if (command == TelnetCommands.Subnegotation)
                        {
                        }
                        // delete a character
                        else if (command == TelnetCommands.EraseCharacter)
                        {
                        }
                        // delete the line
                        else if (command == TelnetCommands.EraseLine)
                        {
                        }
                        else
                        {
                            // regular negotiation
                            TelnetOptions option = data[++i].ToTelnetOption();

                            Array.Resize <byte>(ref response, response.Length + 3);
                            response[response.Length - 3] = TelnetCommands.IAC.Translate();

                            // check to see if that option is supported
                            if (_flags.Contains(option.ToFlag()))
                            {
                                if (command == TelnetCommands.Will || command == TelnetCommands.Do)
                                {
                                    response[response.Length - 2] = TelnetCommands.Will.Translate();
                                }
                                else
                                {
                                    response[response.Length - 2] = TelnetCommands.Wont.Translate();
                                }

                                if (option == TelnetOptions.NAWS && (command == TelnetCommands.Will))
                                {
                                    _nawsEnabled = true;
                                }
                            }
                            else // tell the server not to support this
                            {
                                if (command == TelnetCommands.Will)
                                {
                                    response[response.Length - 2] = TelnetCommands.Dont.Translate();
                                }
                                else
                                {
                                    response[response.Length - 2] = TelnetCommands.Wont.Translate();
                                }
                            }

                            response[response.Length - 1] = option.Translate();

                            _negotiated |= option.ToFlag();
                        }
                        continue;
                    }

                    // append the data to the segment
                    char c = (char)data[i];
                    if (c == '\b')
                    {
                        if (segment.Length > 0)
                        {
                            segment.Remove(segment.Length - 1, 1);
                        }
                    }
                    else
                    {
                        segment.Append(c);
                    }
                }

                // check for server negotiation
                if (response.Length > 0)
                {
                    // add any additional flags that haven't been negotiated
                    foreach (TelnetOptionFlags option in _flags.ParseOptions())
                    {
                        // this has already been negotiated
                        if (_negotiated.Contains(option))
                        {
                            continue;
                        }

                        // request the ones that we are comfortable with so far
                        switch (option)
                        {
                        case TelnetOptionFlags.SupressGoAhead: goto case TelnetOptionFlags.NAWS;

                        case TelnetOptionFlags.Echo: goto case TelnetOptionFlags.NAWS;

                        case TelnetOptionFlags.NAWS:
                            Array.Resize <byte>(ref response, response.Length + 3);

                            response[response.Length - 3] = TelnetCommands.IAC.Translate();
                            response[response.Length - 2] = TelnetCommands.Do.Translate();
                            response[response.Length - 1] = option.Translate().Translate();

                            break;
                        }

                        _negotiated |= option;
                    }

                    // create the socket event args
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.UserToken      = _socket;
                    args.RemoteEndPoint = _address;
                    args.Completed     += new EventHandler <SocketAsyncEventArgs>(AsyncCompletedHandler);

                    // put the response in the buffer
                    args.SetBuffer(response, 0, response.Length);

                    // send the response to the server
                    _socket.SendAsync(args);
                }

                // notify any listeners
                if (MessageRecieved != null)
                {
                    segments.Add(new DisplayText()
                    {
                        Text = segment.ToString(), TextColor = currentColor
                    });
                    MessageRecieved(this, new MessageRecievedEventArgs()
                    {
                        Segments = segments.ToArray()
                    });
                }

                // end byte processing
            } while (_socket.Connected);
        }