Beispiel #1
0
 private static bool AllNullValues(NICProperties theProperties)
 {
     return(string.IsNullOrEmpty(theProperties.IPAddress) &&
            string.IsNullOrEmpty(theProperties.IP6Address) &&
            (theProperties.DomainNameServers == null || theProperties.DomainNameServers.Length == 0) &&
            (theProperties.Routers == null || theProperties.Routers.Length == 0));
 }
        public static List <NICProperties> Get()
        {
            ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
            //Network
            ManagementObjectSearcher   searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT Description, IPAddress, IPSubnet FROM Win32_NetworkAdapterConfiguration"));
            ManagementObjectCollection results  = searcher.Get();

            var nicList = new List <NICProperties>();

            foreach (ManagementObject item in results)
            {
                if (null != item["IPAddress"])
                {
                    var nicInfo = new NICProperties();
                    nicInfo.Description = (string)item["Description"];
                    nicInfo.IPs         = new List <IPProperties>();
                    string[] ips     = (string[])item["IPAddress"];
                    string[] subnets = (string[])item["IPSubnet"];
                    for (int index = 0; index < ips.Length; index++)
                    {
                        nicInfo.IPs.Add(new IPProperties()
                        {
                            IP     = ips[index],
                            Subnet = subnets[index]
                        });
                    }
                    nicList.Add(nicInfo);
                }
            }

            return(nicList);
        }
Beispiel #3
0
        private static DHCPcdConfLine[] DeleteLines(DHCPcdConfLine[] theConfigLines, NICProperties theExistingProperties)
        {
            var ConfigLinesList = new List <DHCPcdConfLine>(theConfigLines);

            ConfigLinesList.RemoveAll(Line => Line?.NICProperties?.Id == theExistingProperties.Id);

            return(ConfigLinesList.ToArray());
        }
Beispiel #4
0
        private static DHCPcdConfLine[] AddLines(DHCPcdConfLine[] theConfigLines, NICProperties NewProperties)
        {
            var ConfigLinesList = new List <DHCPcdConfLine>(theConfigLines);

            ConfigLinesList.Add(new DHCPcdConfLine {
                Id = true, NICProperties = NewProperties
            });

            if (!string.IsNullOrEmpty(NewProperties.IPAddress))
            {
                ConfigLinesList.Add(new DHCPcdConfLine {
                    IPAddress = true, NICProperties = NewProperties
                });
            }

            if (!string.IsNullOrEmpty(NewProperties.IP6Address))
            {
                ConfigLinesList.Add(new DHCPcdConfLine {
                    IP6Address = true, NICProperties = NewProperties
                });
            }

            if (NewProperties.Routers != null && NewProperties.Routers.Length > 0)
            {
                ConfigLinesList.Add(new DHCPcdConfLine {
                    Routers = true, NICProperties = NewProperties
                });
            }

            if (NewProperties.DomainNameServers != null && NewProperties.DomainNameServers.Length > 0)
            {
                ConfigLinesList.Add(new DHCPcdConfLine {
                    DomainNameServers = true, NICProperties = NewProperties
                });
            }

            return(ConfigLinesList.ToArray());
        }
Beispiel #5
0
        private static DHCPcdConfLine[] UpdateLines(DHCPcdConfLine[] theConfigLines, NICProperties theExistingProperties, NICProperties theNewProperties)
        {
            var ConfigLinesList = new List <DHCPcdConfLine>(theConfigLines);
            var HeaderSearch    = ConfigLinesList.FirstOrDefault(Line => Line?.NICProperties?.Id == theNewProperties.Id);
            var HeaderIndex     = ConfigLinesList.IndexOf(HeaderSearch);

            if (!string.IsNullOrEmpty(theNewProperties.IPAddress))
            {
                // The New Properties has the IP Address value

                if (theExistingProperties.IPAddress == null)
                {
                    // But the current properties collection dosn't have a IP address value,
                    // so it will have to be injected at the current line.
                    theExistingProperties.IPAddress = theNewProperties.IPAddress;

                    // Now insert the new value below the NIC Id header.
                    ConfigLinesList.Insert(HeaderIndex + 1, new DHCPcdConfLine {
                        IPAddress = true, NICProperties = theExistingProperties
                    });
                }
                else
                {
                    theExistingProperties.IPAddress = theNewProperties.IPAddress;
                }
            }
            else
            {
                ConfigLinesList.RemoveAll(Line => Line?.NICProperties?.Id == theExistingProperties.Id && Line.IPAddress);
            }

            if (!string.IsNullOrEmpty(theNewProperties.IP6Address))
            {
                if (theExistingProperties.IP6Address == null)
                {
                    theExistingProperties.IP6Address = theNewProperties.IP6Address;

                    ConfigLinesList.Insert(HeaderIndex + 1, new DHCPcdConfLine {
                        IP6Address = true, NICProperties = theExistingProperties
                    });
                }
                else
                {
                    theExistingProperties.IP6Address = theNewProperties.IP6Address;
                }
            }
            else
            {
                ConfigLinesList.RemoveAll(Line => Line?.NICProperties?.Id == theExistingProperties.Id && Line.IP6Address);
            }

            if (theNewProperties.Routers != null && theNewProperties.Routers.Length > 0)
            {
                if (theExistingProperties.Routers == null)
                {
                    theExistingProperties.Routers = theNewProperties.Routers;

                    ConfigLinesList.Insert(HeaderIndex + 1, new DHCPcdConfLine {
                        Routers = true, NICProperties = theExistingProperties
                    });
                }
                else
                {
                    theExistingProperties.Routers = theNewProperties.Routers;
                }
            }
            else
            {
                ConfigLinesList.RemoveAll(Line => Line?.NICProperties?.Id == theExistingProperties.Id && Line.Routers);
            }

            if (theNewProperties.DomainNameServers != null && theNewProperties.DomainNameServers.Length > 0)
            {
                if (theExistingProperties.DomainNameServers == null)
                {
                    theExistingProperties.DomainNameServers = theNewProperties.DomainNameServers;

                    ConfigLinesList.Insert(HeaderIndex + 1, new DHCPcdConfLine {
                        DomainNameServers = true, NICProperties = theExistingProperties
                    });
                }
                else
                {
                    theExistingProperties.DomainNameServers = theNewProperties.DomainNameServers;
                }
            }
            else
            {
                ConfigLinesList.RemoveAll(Line => Line?.NICProperties?.Id == theExistingProperties.Id && Line.DomainNameServers);
            }

            return(ConfigLinesList.ToArray());
        }
Beispiel #6
0
        internal static ReadResult Read()
        {
            string line;

            var ConfLines = new List <DHCPcdConfLine>();
            var IPConfigs = new List <NICProperties>();

            NICProperties prop = null;

            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(FileLocation))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        line.Trim();

                        if (prop != null && line.StartsWith("static", StringComparison.OrdinalIgnoreCase))
                        {
                            line = line.Remove(0, 6);
                            line = line.Trim();

                            if (line.StartsWith("ip_address", StringComparison.OrdinalIgnoreCase))
                            {
                                var LineSplit = line.Split(new char[] { '=' });

                                if (LineSplit.Length == 2)
                                {
                                    line = LineSplit[1];
                                    line = line.Trim();

                                    prop.IPAddress = line;
                                    ConfLines.Add(new DHCPcdConfLine {
                                        NICProperties = prop, IPAddress = true
                                    });
                                }
                            }
                            else if (line.StartsWith("ip6_address", StringComparison.OrdinalIgnoreCase))
                            {
                                var LineSplit = line.Split(new char[] { '=' });

                                if (LineSplit.Length == 2)
                                {
                                    line = LineSplit[1];
                                    line = line.Trim();

                                    prop.IP6Address = line;
                                    ConfLines.Add(new DHCPcdConfLine {
                                        NICProperties = prop, IP6Address = true
                                    });
                                }
                            }
                            else if (line.StartsWith("routers", StringComparison.OrdinalIgnoreCase))
                            {
                                var LineSplit = line.Split(new char[] { '=' });

                                if (LineSplit.Length == 2)
                                {
                                    line = LineSplit[1];
                                    line = line.Trim();

                                    LineSplit = line.Split(null);

                                    if (LineSplit.Length > 0)
                                    {
                                        prop.Routers = LineSplit;
                                        ConfLines.Add(new DHCPcdConfLine {
                                            NICProperties = prop, Routers = true
                                        });
                                    }
                                }
                            }
                            else if (line.StartsWith("domain_name_servers", StringComparison.OrdinalIgnoreCase))
                            {
                                var LineSplit = line.Split(new char[] { '=' });

                                if (LineSplit.Length == 2)
                                {
                                    line = LineSplit[1];
                                    line = line.Trim();

                                    LineSplit = line.Split(null);

                                    if (LineSplit.Length > 0)
                                    {
                                        prop.DomainNameServers = LineSplit;
                                        ConfLines.Add(new DHCPcdConfLine {
                                            NICProperties = prop, DomainNameServers = true
                                        });
                                    }
                                }
                            }
                        }
                        else if (line.StartsWith("interface", StringComparison.OrdinalIgnoreCase))
                        {
                            string[] ssize = line.Split(null);

                            if (ssize.Length == 2)
                            {
                                prop = new NICProperties(ssize[1]);

                                IPConfigs.Add(prop);
                                ConfLines.Add(new DHCPcdConfLine {
                                    NICProperties = prop, Id = true
                                });
                            }
                        }
                        else
                        {
                            ConfLines.Add(new DHCPcdConfLine {
                                Line = line
                            });
                        }
                    }
                }
            }
            catch (System.IO.FileNotFoundException)
            {
            }

            return(new ReadResult {
                ConfigLines = ConfLines.ToArray(), Properties = IPConfigs.ToArray()
            });
        }
        internal void UpdateProperties(NetworkProperties theModel)
        {
            List <Task <ProcessResult> > Tasks = new List <Task <ProcessResult> >();
            bool AskToRestart = false;

            bool Eth0Changes  = false;
            bool Wlan0Changes = false;

            NetworkProperties CurrentSettings = Core.Instance.Network.RepopulateAndGetProperties();

            if (theModel.Eth0Routers == null)
            {
                theModel.Eth0Routers = new string[0];
            }
            if (theModel.Eth0DomainNameServers == null)
            {
                theModel.Eth0DomainNameServers = new string[0];
            }
            if (theModel.Wlan0Routers == null)
            {
                theModel.Wlan0Routers = new string[0];
            }
            if (theModel.Wlan0DomainNameServers == null)
            {
                theModel.Wlan0DomainNameServers = new string[0];
            }

            if (CurrentSettings.Eth0IPAddress != theModel.Eth0IPAddress)
            {
                Eth0Changes = true;
                if (string.IsNullOrEmpty(theModel.Eth0IPAddress))
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP4DynamicChanging, new string[] { c_Eth0 });
                }
                else
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP4StaticChanging, new string[] { c_Eth0, theModel.Eth0IPAddress });
                }
            }

            if (CurrentSettings.Eth0IP6Address != theModel.Eth0IP6Address)
            {
                Eth0Changes = true;
                if (string.IsNullOrEmpty(theModel.Eth0IP6Address))
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6DynamicChanging, new string[] { c_Eth0 });
                }
                else
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6StaticChanging, new string[] { c_Eth0, theModel.Eth0IP6Address });
                }
            }

            if (!CurrentSettings.Eth0Routers.OrderBy(e => e).SequenceEqual(theModel.Eth0Routers.OrderBy(e => e)))
            {
                Eth0Changes = true;
                Log?.Invoke(EventLogEntryCodes.NICRoutersChanging, new string[] { c_Eth0, string.Join(" ", theModel.Eth0Routers) });
            }

            if (!CurrentSettings.Eth0DomainNameServers.OrderBy(e => e).SequenceEqual(theModel.Eth0DomainNameServers.OrderBy(e => e)))
            {
                Eth0Changes = true;
                Log?.Invoke(EventLogEntryCodes.NICDNSChanging, new string[] { c_Eth0, string.Join(" ", theModel.Eth0DomainNameServers) });
            }

            if (CurrentSettings.Wlan0IPAddress != theModel.Wlan0IPAddress)
            {
                Wlan0Changes = true;
                if (string.IsNullOrEmpty(theModel.Wlan0IPAddress))
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6DynamicChanging, new string[] { c_Wlan0 });
                }
                else
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6StaticChanging, new string[] { c_Wlan0, theModel.Wlan0IPAddress });
                }
            }

            if (CurrentSettings.Wlan0IP6Address != theModel.Wlan0IP6Address)
            {
                Wlan0Changes = true;
                if (string.IsNullOrEmpty(theModel.Eth0IP6Address))
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6DynamicChanging, new string[] { c_Wlan0 });
                }
                else
                {
                    Log?.Invoke(EventLogEntryCodes.NICIP6StaticChanging, new string[] { c_Wlan0, theModel.Wlan0IP6Address });
                }
            }

            if (!CurrentSettings.Wlan0Routers.OrderBy(e => e).SequenceEqual(theModel.Wlan0Routers.OrderBy(e => e)))
            {
                Wlan0Changes = true;
                Log?.Invoke(EventLogEntryCodes.NICDNSChanging, new string[] { c_Wlan0, string.Join(" ", theModel.Wlan0Routers) });
            }

            if (!CurrentSettings.Wlan0DomainNameServers.OrderBy(e => e).SequenceEqual(theModel.Wlan0DomainNameServers.OrderBy(e => e)))
            {
                Wlan0Changes = true;
                Log?.Invoke(EventLogEntryCodes.NICDNSChanging, new string[] { c_Wlan0, string.Join(" ", theModel.Wlan0DomainNameServers) });
            }

            Task <ProcessResult> IPAddressTask = null;

            if (Eth0Changes || Wlan0Changes)
            {
                NICProperties eth0 = new NICProperties(c_Eth0);
                eth0.IPAddress         = theModel.Eth0IPAddress;
                eth0.IP6Address        = theModel.Eth0IP6Address;
                eth0.Routers           = theModel.Eth0Routers;
                eth0.DomainNameServers = theModel.Eth0DomainNameServers;

                NICProperties wlan0 = new NICProperties(c_Wlan0);
                wlan0.IPAddress         = theModel.Wlan0IPAddress;
                wlan0.IP6Address        = theModel.Wlan0IP6Address;
                wlan0.Routers           = theModel.Wlan0Routers;
                wlan0.DomainNameServers = theModel.Wlan0DomainNameServers;

                IPAddressTask = FlushNetworkSequence(Eth0Changes, Wlan0Changes, eth0, wlan0);
                Tasks.Add(IPAddressTask);
            }

            Task <ProcessResult> HostNameTask = null;

            if (CurrentSettings.HostName != theModel.HostName)
            {
                AskToRestart = true;
                HostNameTask = ProcessRunner.GetProcessResultAsync(c_LinuxRaspconfigCommand, "nonint do_hostname " + theModel.HostName);
                Tasks.Add(HostNameTask);
                Log?.Invoke(EventLogEntryCodes.HostNameChanging, new string[] { theModel.HostName });
            }

            Task <ProcessResult> SetWiFiSSIDPassphrase = null;

            if (!string.IsNullOrEmpty(theModel.NewSSID))
            {
                SetWiFiSSIDPassphrase = SetWiFiSSIDPassphraseSequence(theModel);
                Tasks.Add(SetWiFiSSIDPassphrase);
            }

            if (AskToRestart)
            {
                RestartDue?.Invoke();
            }

            Task.WaitAll(Tasks.ToArray());

            // Check if Tasks have completed Okay and Log result

            LoggingActions.LogTaskResult(Log, IPAddressTask, EventLogEntryCodes.NICIPChangesComplete, EventLogEntryCodes.NICIPChangesError);
            LoggingActions.LogTaskResult(Log, HostNameTask, EventLogEntryCodes.HostNameChanged, EventLogEntryCodes.HostNameChangeError);
            LoggingActions.LogTaskResult(Log, SetWiFiSSIDPassphrase, EventLogEntryCodes.SSIDChanged, EventLogEntryCodes.SSIDChangeError);
        }
        private Task <ProcessResult> FlushNetworkSequence(bool isEth0Changes, bool isWlan0Changes, NICProperties theEth0, NICProperties theWlan0)
        {
            return(Task.Run(async() =>
            {
                await DHCPCD.UpdateResultAsync(new NICProperties[] { theEth0, theWlan0 });
                await ProcessRunner.GetProcessResultAsync(c_SystemCtlCommand, "daemon-reload");
                await ProcessRunner.GetProcessResultAsync(c_SystemCtlCommand, "stop dhcpcd.service");
                if (isEth0Changes)
                {
                    await ProcessRunner.GetProcessResultAsync(c_IPCommand, "addr flush dev eth0");
                }
                if (isWlan0Changes)
                {
                    await ProcessRunner.GetProcessResultAsync(c_IPCommand, "addr flush dev wlan0");
                }
                await ProcessRunner.GetProcessResultAsync(c_SystemCtlCommand, "start dhcpcd.service");
                await ProcessRunner.GetProcessResultAsync(c_SystemCtlCommand, "restart networking.service");

                if (isWlan0Changes)
                {
                    await ProcessRunner.GetProcessResultAsync(c_WPACliCommand, "-i wlan0 reconfigure");
                }

                return new ProcessResult(0, string.Empty, string.Empty);
            }));
        }