Пример #1
0
        private long readBytes(long lastMaxOffset)
        {
            _reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);


            var buffer  = new byte[_hexEncoder.BlockLength];
            var buffPos = 0;
            int i;

            i = _reader.BaseStream.ReadByte();
            while (i != -1)
            {
                buffer[buffPos] = (byte)i;
                buffPos++;
                if (buffPos == _hexEncoder.BlockLength)
                {
                    var l = _hexEncoder.GetHex(_address, buffer);
                    Session.AddEvent(this, l);
                    _address += (ulong)buffPos;
                    buffPos   = 0;
                }
                i = _reader.BaseStream.ReadByte();
            }

            lastMaxOffset = _reader.BaseStream.Position;
            return(lastMaxOffset);
        }
Пример #2
0
        /// <summary>
        /// This is where all the actual 'listening' is done.
        /// </summary>
        protected void Listen()
        {
            if (Session == null)
            {
                throw new NullReferenceException("This IListener has no Session yet!");
            }

            IPEndPoint remoteEndPoint;

            if (_endpoint == "ANY")
            {
                remoteEndPoint = new IPEndPoint(IPAddress.Any, _port);
            }
            else
            {
                remoteEndPoint = new IPEndPoint(IPAddress.Parse(_endpoint), _port);
            }

            var    buffer = new Byte[256];
            string loggingEvent;

            try
            {
                _tcpListener = new System.Net.Sockets.TcpListener(remoteEndPoint);
                _tcpListener.Start();
                _log.Info(GetHashCode(), "Started listening for TCP on port " + _port.ToString() + " using prefix: " + MessagePrefix);

                while (true)
                {
                    var client = _tcpListener.AcceptTcpClient();

                    var sender = "";
                    if (Session.Viewer.ShowListenerPrefix)
                    {
                        sender = client.Client.RemoteEndPoint.ToString();
                    }

                    var data   = "";
                    var stream = client.GetStream();

                    int i;

                    if (_encName == HexEncoder.EncName)
                    {
                        data = _hexEncoder.GetHex(_address, buffer);
                    }
                    else
                    {
                        // Loop to receive all the data sent by the client.
                        while ((i = stream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            data += Encoding.GetEncoding(_encName).GetString(buffer, 0, i);
                        }
                    }
                    // Shutdown and end connection
                    client.Close();
                    loggingEvent = sender + " " + data;
                    Session.AddEvent(this, loggingEvent);
                }
            }
            catch (SocketException ex)
            {
                var foo = "(TCP ERROR) The TCP listener could not be started at port " + _port.ToString() + " " + Environment.NewLine + ex.Message;
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            catch (ThreadAbortException)
            {
                var foo = "(TCP ERROR) TcpListener aborted.";
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            catch (Exception e)
            {
                var foo = "(TCP ERROR) Internal error in TcpListener: " + e.Message + Environment.NewLine + e.StackTrace;
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            finally
            {
                if (_tcpListener != null)
                {
                    _tcpListener.Stop();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// This is where all the actual 'listening' is done.
        /// </summary>
        protected void Listen()
        {
            if (Session == null)
            {
                throw new NullReferenceException("This IListener has no Session yet!");
            }

            IPEndPoint remoteEndPoint;

            if (_endpoint == "ANY")
            {
                remoteEndPoint = new IPEndPoint(IPAddress.Any, _port);
            }
            else
            {
                remoteEndPoint = new IPEndPoint(IPAddress.Parse(_endpoint), _port);
            }

            byte[] buffer;
            string loggingEvent;

            _hexEncoder = new HexEncoder();
            try
            {
                _udpClient = new UdpClient(_port);

                _log.Info(GetHashCode(), "Started listening for UDP on port " + _port.ToString() + " using prefix: " + MessagePrefix);

                while (true)
                {
                    buffer = _udpClient.Receive(ref remoteEndPoint);

                    var sender = "";
                    if (Session.Viewer.ShowListenerPrefix)
                    {
                        sender = remoteEndPoint.Address.ToString() + " ";
                    }

                    if (_encName == HexEncoder.EncName)
                    {
                        loggingEvent = sender + _hexEncoder.GetHex(_address, buffer);
                        _address    += (ulong)buffer.Length;
                    }
                    else
                    {
                        loggingEvent = sender + Encoding.GetEncoding(_encName).GetString(buffer);
                    }
                    Session.AddEvent(this, loggingEvent);
                }
            }
            catch (SocketException ex)
            {
                var foo = "(UDP ERROR) The UDP listener could not be started at port " + _port.ToString() + " " + Environment.NewLine + ex.Message;
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            catch (ThreadAbortException)
            {
                var foo = "(UDP) UdpListener thread aborted.";
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            catch (Exception e)
            {
                var foo = "(UDP ERROR) Internal error in UdpListener: " + e.Message + Environment.NewLine + e.StackTrace;
                _log.Error(GetHashCode(), foo);
                Session.AddEvent(this, foo);
            }
            finally
            {
                if (_udpClient != null)
                {
                    _udpClient.Close();
                }
            }
        }