private static void UpdateNetwork()
        {
            int index = 0;

            System.Net.NetworkInformation.NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            foreach (var iface in interfaces)
            {
                UnicastIPAddressInformation ipInfo = (from unicastAddress in iface.GetIPProperties().UnicastAddresses
                                                      where unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork
                                                      select unicastAddress).FirstOrDefault();

                if (ipInfo != null && ipInfo.Address.ToString() != "127.0.0.1")
                {
                    string macAddress = iface.GetPhysicalAddress().ToString();
                    string ipAddress  = ipInfo.Address.ToString();
                    string subnet     = ipInfo.IPv4Mask.ToString();
                    string gateway    = (from gatewayAddress in iface.GetIPProperties().GatewayAddresses
                                         where gatewayAddress.Address.AddressFamily == AddressFamily.InterNetwork
                                         select gatewayAddress.Address.ToString()).FirstOrDefault();
                    InterfaceType type = iface.Name.StartsWith("w") ? InterfaceType.WiFi : InterfaceType.LAN;
                    // uint speed = (uint)(iface.Speed / 1000000),                // Unsupported in .NET Core 2.2 on Linux

                    if (index >= Provider.Get.Network.Interfaces.Count)
                    {
                        // Add new network interface
                        Provider.Get.Network.Interfaces.Add(new DuetAPI.Machine.NetworkInterface
                        {
                            MacAddress   = macAddress,
                            ActualIP     = ipAddress,
                            ConfiguredIP = ipAddress,
                            Subnet       = subnet,
                            Gateway      = gateway,
                            Type         = type
                        });
                    }
                    else
                    {
                        // Update existing entry
                        DuetAPI.Machine.NetworkInterface existing = Provider.Get.Network.Interfaces[index];
                        existing.MacAddress   = macAddress;
                        existing.ActualIP     = ipAddress;
                        existing.ConfiguredIP = ipAddress;
                        existing.Subnet       = subnet;
                        existing.Type         = type;
                    }
                    index++;
                }
            }

            for (int i = Provider.Get.Network.Interfaces.Count - 1; i > index; i--)
            {
                Provider.Get.Network.Interfaces.RemoveAt(i);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Update network interfaces
        /// </summary>
        private static void UpdateNetwork()
        {
            Provider.Get.Network.Hostname = Environment.MachineName;

            int index = 0;

            foreach (var iface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
            {
                UnicastIPAddressInformation ipInfo = (from unicastAddress in iface.GetIPProperties().UnicastAddresses
                                                      where unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork
                                                      select unicastAddress).FirstOrDefault();
                if (ipInfo != null && !System.Net.IPAddress.IsLoopback(ipInfo.Address))
                {
                    DuetAPI.Machine.NetworkInterface networkInterface;
                    if (index >= Provider.Get.Network.Interfaces.Count)
                    {
                        networkInterface = new DuetAPI.Machine.NetworkInterface();
                        Provider.Get.Network.Interfaces.Add(networkInterface);
                    }
                    else
                    {
                        networkInterface = Provider.Get.Network.Interfaces[index];
                    }
                    index++;

                    networkInterface.MacAddress   = iface.GetPhysicalAddress().ToString();
                    networkInterface.ActualIP     = ipInfo.Address.ToString();
                    networkInterface.ConfiguredIP = ipInfo.Address.ToString();
                    networkInterface.Subnet       = ipInfo.IPv4Mask.ToString();
                    networkInterface.Gateway      = (from gatewayAddress in iface.GetIPProperties().GatewayAddresses
                                                     where gatewayAddress.Address.AddressFamily == AddressFamily.InterNetwork
                                                     select gatewayAddress.Address.ToString()).FirstOrDefault();
                    networkInterface.Type = iface.Name.StartsWith("w") ? InterfaceType.WiFi : InterfaceType.LAN;
                    // networkInterface.Speed = (uint)(iface.Speed / 1000000);                // Unsupported in .NET Core 2.2 on Linux
                }
            }

            for (int i = Provider.Get.Network.Interfaces.Count; i > index; i--)
            {
                Provider.Get.Network.Interfaces.RemoveAt(i - 1);
            }
        }