Example #1
0
        /// <summary>
        /// Initialize
        /// </summary>
        private void InitializeComponent()
        {
            run = new WinLLDP(OsInfo.GetStaticInfo());

            ServiceName = MyServiceName;
            CanStop     = true;

            EventLog.Source = ServiceName;
            EventLog.Log    = "Application";

            if (!EventLog.SourceExists(EventLog.Source))
            {
                // Create Windows Event Log source if it doesn't exist
                EventLog.CreateEventSource(EventLog.Source, this.EventLog.Log);
            }

            ReduceMemory();

            // Run the LLDP packet sender every 30 seconds
            timer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds)
            {
                AutoReset = true
            };

            timer.Elapsed += SendPacket;
        }
Example #2
0
        public bool Run()
        {
            Debug.IndentLevel = 0;

            List <NetworkInterface> adapters = new List <NetworkInterface>();

            // Get list of connected adapters
            Debug.WriteLine("Getting connected adapter list", EventLogEntryType.Information);
            adapters = NetworkInterface.GetAllNetworkInterfaces()
                       .Where(
                x =>
                x.NetworkInterfaceType != NetworkInterfaceType.Loopback
                &&
                x.OperationalStatus == OperationalStatus.Up
                )
                       .ToList();

            if (0 == adapters.Count)
            {
                Debug.WriteLine("No adapters found.", EventLogEntryType.Information);
                // No available adapters
                return(true);
            }

            // Open network devices for sending raw packets
            OpenDevices(adapters);

            // Get list of users
            List <UserInfo> users = OsInfo.GetUsers();

            if (users.Count == 0)
            {
                Debug.WriteLine("No users found.", EventLogEntryType.Error);
            }

            users.Sort();

            // Get information which doesn't change often or doesn't need to be 100% accurate in realtime when iterating through adapters
            PacketInfo pinfo = new PacketInfo()
            {
                OperatingSystem        = StaticInformation.OperatingSystemFriendlyName,
                OperatingSystemVersion = StaticInformation.OperatingSystemVersion,
                Tag         = StaticInformation.ServiceTag,
                Uptime      = OsInfo.GetUptime(),
                MachineName = StaticInformation.MachineName,
            };

            bool   first     = true;
            string olddomain = "";
            string userlist  = "";

            foreach (UserInfo ui in users)
            {
                string domain = ui.Domain;

                if (domain != olddomain)
                {
                    first = true;
                }

                if (first)
                {
                    userlist += domain + ": ";
                    olddomain = domain;
                    first     = false;
                }

                userlist += ui.Username + ", ";
            }

            pinfo.Username = userlist.Trim().TrimEnd(',');

            foreach (NetworkInterface adapter in adapters)
            {
                Debug.WriteLine(String.Format("Adapter {0}:", adapter.Name), EventLogEntryType.Information);

                try
                {
                    Debug.WriteLine("Generating packet", EventLogEntryType.Information);
                    Packet packet = CreateLLDPPacket(adapter, pinfo);
                    Debug.IndentLevel = 0;
                    Debug.WriteLine("Sending packet", EventLogEntryType.Information);
                    SendRawPacket(adapter, packet);
                    Debug.IndentLevel = 0;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(String.Format("Error sending packet:{0}{1}", Environment.NewLine, e.ToString()), EventLogEntryType.Error);
                }

                Debug.WriteLine("", EventLogEntryType.Information);
                Debug.WriteLine(new String('-', 40), EventLogEntryType.Information);
                Debug.IndentLevel = 0;
            }

            return(true);
        }