Beispiel #1
0
        /// <summary>
        /// Creates a socket that waits for traffic and sets up the handler modules
        /// </summary>
        /// <returns>Returns true if the listener starts successfully.  Otherwise, false.</returns>
        public bool Start()
        {
            HandlerSection handlers;

            // Get the handlers collection
            Trace.WriteLine("Loading handlers");
            try
            {
                handlers = (HandlerSection)System.Configuration.ConfigurationManager.GetSection("handlerSection");
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not load configuration because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            // Ensure that a socket needs to be initialized.
            if (_socket != null)
            {
                return(true);
            }

            // Ensure that a socket needs to be initialized and bound.
            if (_socket != null && _socket.IsBound)
            {
                return(true);
            }

            try
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not create socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            Trace.WriteLine("Binding socket");
            // Bind the socket to the specificed IP and port
            try
            {
                _socket.Bind(new IPEndPoint(_listenAddress, _listenPort));
            }
            catch (Exception ex)
            {
                _socket.Close();
                _socket = null;

                EventLogger.LogEvent("Could not bind socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            if (_socket == null)
            {
                return(false);
            }

            // Create a new LogBuffer that will be used to store messages until they are discarded or flushed to a persistent store.
            _buffer = new LogBuffer(_logBufferFlushFrequency);

            if (handlers != null)
            {
                Trace.WriteLine("Initialising handlers");
                // Load each module (handler) from the configuration file
                foreach (HandlerConfiguration handler in handlers.Handlers)
                {
                    IDictionary <string, string> properties = new Dictionary <string, string>();
                    handler.HandlerProperties.AllKeys.All(key =>
                    {
                        properties[key] = handler.HandlerProperties[key].Value;
                        return(true);
                    });
                    var msgHandler = new MessageHandler(handler.AssemblyName, handler.ParserClassName, handler.StorageClassName, handler.ConnectionString, properties);

                    // If the handler has a storage class then setup the buffer to temporarily store the messages
                    if (handler.StorageClassName != null)
                    {
                        Trace.WriteLine("Intialising buffer");
                        _buffer.InitHandler(msgHandler);
                    }

                    // If the handler is configured for specific IP addresses, setup the IP handler lookup list
                    if (!string.IsNullOrEmpty(handler.FilterIPAddresses))
                    {
                        var filters = handler.FilterIPAddresses.Split(',', ';');

                        foreach (var filter in filters)
                        {
                            _ipFilters.Add(filter, msgHandler);

                            // If the handler also has IP forwards set, add them to the IP Forwards lookup list
                            if (!string.IsNullOrEmpty(handler.IPForwards))
                            {
                                _ipForwards.Add(filter, handler.IPForwards.Split(',', ';'));
                            }
                        }
                    }
                }
            }

            // If any handler has an IP forward setup, create a send socket that will be used forward messages
            if (_ipForwards.Count > 0)
            {
                try
                {
                    _sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    _sendSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
                }
                catch (Exception ex)
                {
                }
            }

            Trace.WriteLine("Registering receive event");
            try
            {
                // Start the listen operation on the socket
                RegisterReceiveOperation();
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not register socket on data received event because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a socket that waits for traffic and sets up the handler modules
        /// </summary>
        /// <returns>Returns true if the listener starts successfully.  Otherwise, false.</returns>
		public bool Start()
		{
			HandlerSection handlers;

            // Get the handlers collection
			try
			{
				handlers = (HandlerSection)System.Configuration.ConfigurationManager.GetSection("handlerSection");
			}
			catch (Exception ex)
			{
				EventLogger.LogEvent("Could not load configuration because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

				return false;
			}

            // Ensure that a socket needs to be initialized.
            if (this.socket != null) { return true; }

            // Ensure that a socket needs to be initialized and bound.
            if (this.socket != null && this.socket.IsBound) { return true; }

			try
			{
				this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			}
			catch (Exception ex)
			{
				EventLogger.LogEvent("Could not create socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

				return false;
			}

            // Bind the socket to the specificed IP and port
			try
			{
				this.socket.Bind(new IPEndPoint(this.listenAddress, SYSLOG_PORT));
			}
			catch (Exception ex)
			{
				this.socket.Close();
				this.socket = null;

				EventLogger.LogEvent("Could not bind socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

				return false;
			}

            if (this.socket == null) { return false; }

            // Create a new LogBuffer that will be used to store messages until they are discarded or flushed to a persistent store.
			buffer = new LogBuffer(this.logBufferFlushFrequency);

			if (handlers != null)
			{
                // Load each module (handler) from the configuration file
				foreach (HandlerConfiguration handler in handlers.Handlers)
				{
					MessageHandler msgHandler = new MessageHandler(handler.AssemblyName, handler.ParserClassName,
								handler.StorageClassName, handler.ConnectionString);

                    // If the handler has a storage class then setup the buffer to temporarily store the messages
					if (handler.StorageClassName != null)
					{
						buffer.InitializeBuffer(msgHandler);
					}

                    // If the handler is configured for specific IP addresses, setup the IP handler lookup list
					if (handler.FilterIPAddresses != null)
					{
						string[] filters = handler.FilterIPAddresses.Split(',', ';');

						for (int i = 0; i < filters.Length; i++)
						{
							ipFilters.Add(filters[i], msgHandler);

                            // If the handler also has IP forwards set, add them to the IP Forwards lookup list
							if (handler.IPForwards != null && handler.IPForwards.Length > 0)
							{
								ipForwards.Add(filters[i], handler.IPForwards.Split(',', ';'));
							}
						}
					}
				}
			}

            // If any handler has an IP forward setup, create a send socket that will be used forward messages
			if (ipForwards.Count > 0)
			{
				try
				{
					sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
					sendSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
				}
				catch (Exception ex)
				{

				}
			}

			try
			{
                // Start the listen operation on the socket
				RegisterReceiveOperation();
			}
			catch (Exception ex)
			{
				EventLogger.LogEvent("Could not register socket on data received event because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
				return false;
			}

			return true;
		}
Beispiel #3
0
        /// <summary>
        /// Creates a socket that waits for traffic and sets up the handler modules
        /// </summary>
        /// <returns>Returns true if the listener starts successfully.  Otherwise, false.</returns>
        public bool Start()
        {
            HandlerSection handlers;

            // Get the handlers collection
            try
            {
                handlers = (HandlerSection)System.Configuration.ConfigurationManager.GetSection("handlerSection");
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not load configuration because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            // Ensure that a socket needs to be initialized.
            if (this.socket != null)
            {
                return(true);
            }

            // Ensure that a socket needs to be initialized and bound.
            if (this.socket != null && this.socket.IsBound)
            {
                return(true);
            }

            try
            {
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not create socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            // Bind the socket to the specificed IP and port
            try
            {
                this.socket.Bind(new IPEndPoint(this.listenAddress, SYSLOG_PORT));
            }
            catch (Exception ex)
            {
                this.socket.Close();
                this.socket = null;

                EventLogger.LogEvent("Could not bind socket because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                return(false);
            }

            if (this.socket == null)
            {
                return(false);
            }

            // Create a new LogBuffer that will be used to store messages until they are discarded or flushed to a persistent store.
            buffer = new LogBuffer(this.logBufferFlushFrequency);

            if (handlers != null)
            {
                // Load each module (handler) from the configuration file
                foreach (HandlerConfiguration handler in handlers.Handlers)
                {
                    MessageHandler msgHandler = new MessageHandler(handler.AssemblyName, handler.ParserClassName,
                                                                   handler.StorageClassName, handler.ConnectionString);

                    // If the handler has a storage class then setup the buffer to temporarily store the messages
                    if (handler.StorageClassName != null)
                    {
                        buffer.InitializeBuffer(msgHandler);
                    }

                    // If the handler is configured for specific IP addresses, setup the IP handler lookup list
                    if (handler.FilterIPAddresses != null)
                    {
                        string[] filters = handler.FilterIPAddresses.Split(',', ';');

                        for (int i = 0; i < filters.Length; i++)
                        {
                            ipFilters.Add(filters[i], msgHandler);

                            // If the handler also has IP forwards set, add them to the IP Forwards lookup list
                            if (handler.IPForwards != null && handler.IPForwards.Length > 0)
                            {
                                ipForwards.Add(filters[i], handler.IPForwards.Split(',', ';'));
                            }
                        }
                    }
                }
            }

            // If any handler has an IP forward setup, create a send socket that will be used forward messages
            if (ipForwards.Count > 0)
            {
                try
                {
                    sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sendSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
                }
                catch (Exception ex)
                {
                }
            }

            try
            {
                // Start the listen operation on the socket
                RegisterReceiveOperation();
            }
            catch (Exception ex)
            {
                EventLogger.LogEvent("Could not register socket on data received event because: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return(false);
            }

            return(true);
        }