Exemplo n.º 1
0
        /// <summary>
        /// edites the config of /etc/default/isc-dhcp-server to set the seletc interface
        /// </summary>
        /// <param name="ar"></param>
        private void processEtcDefaultConfigFile(IAsyncResult ar)
        {
            IOController.Log(this, "processEtcDefaultConfigFile entered", IOController.Flag.debug);
            AsyncResult            aResult = (AsyncResult)ar;
            ReadConfigFileDelegate readConfigFileDelegate = (ReadConfigFileDelegate)aResult.AsyncDelegate;
            ArrayList filecontent = readConfigFileDelegate.EndInvoke(ar);

            IOController.Log(this, "processDefaultConfigFile filecontent: " + String.Join(",", filecontent), Flag.debug);
            newEtcDefaultConfig = new ArrayList();
            newEtcDefaultConfig.Add("#configfile /etc/default/isc-dhcp-server modified by GM4D");
            foreach (string line in filecontent)
            {
                string trimmedline = line.Trim();
                if (trimmedline.StartsWith("#INTERFACES"))
                {
                    trimmedline = "INTERFACES=\"" + this.settings.OverviewSelectedInterfaceName + "\"";
                }
                else if (trimmedline.StartsWith("INTERFACES"))
                {
                    trimmedline = "INTERFACES=\"" + this.settings.OverviewSelectedInterfaceName + "\"";
                }
                newEtcDefaultConfig.Add(trimmedline);
            }
            IOController.Log(this, "newEtcDefaultConfig created:\n" + string.Join("\n", newEtcDefaultConfig), Flag.debug);
        }
Exemplo n.º 2
0
 /// <summary>
 /// loads /etc/default/isc-dhcp-server file
 /// </summary>
 public void LoadEtcDefaultConfigFile()
 {
     this.newEtcDefaultConfig = null;
     IOController.Log(this, "LoadEtcDefaultConfigFile enter", Flag.debug);
     if (OsIsUnix)
     {
         if (this.settings.IsDHCPServerInstalled)
         {
             if (File.Exists("/etc/default/isc-dhcp-server"))
             {
                 IOController.Log(this, "LoadEtcDefaultConfig", Flag.debug);
                 ReadConfigFileDelegate readConfigFileDelegate       = new ReadConfigFileDelegate(ProcessConfigFile);
                 IAsyncResult           readConfigFileDelegateResult = readConfigFileDelegate.BeginInvoke("/etc/default/isc-dhcp-server", processEtcDefaultConfigFile, null);
             }
             else
             {
                 IOController.Log(this, "FileNotFoundException /etc/default/isc-dhcp-server", Flag.error);
                 throw new FileNotFoundException("/etc/default/isc-dhcp-server" + " not found.");
             }
         }
         else
         {
             IOController.Log(this, "DHCP Server not installed", Flag.error);
             throw new System.Exception("DHCP Server not installed");
         }
     }
     else
     {
         IOController.Log(this, "System in not a Unix environment", Flag.error);
         throw new System.Exception("System in not a Unix environment");
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// loads DHCP setting from file with filename
 /// </summary>
 /// <param name="filename">path and filename as string</param>
 public void LoadSettingsFile(String filename)
 {
     if (File.Exists(filename))
     {
         IOController.Log(this, "LoadSettingsFile filename: " + filename, Flag.status);
         ReadConfigFileDelegate readConfigFileDelegate       = new ReadConfigFileDelegate(ProcessConfigFile);
         IAsyncResult           readConfigFileDelegateResult = readConfigFileDelegate.BeginInvoke(filename, parseConfig, null);
     }
     else
     {
         throw new FileNotFoundException(filename + " not found.");
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// takes the filecontent from IAsyncResult und parses DHCP settings to the settings object
        /// </summary>
        /// <param name="result">takes an IAsyncResult containing the file content as string</param>
        public void parseConfig(IAsyncResult result)
        {
            AsyncResult            aResult = (AsyncResult)result;
            ReadConfigFileDelegate readConfigFileDelegate = (ReadConfigFileDelegate)aResult.AsyncDelegate;
            ArrayList filecontent = readConfigFileDelegate.EndInvoke(result);

            IOController.Log(this, "parseConfig filecontent: " + String.Join(",", filecontent), Flag.debug);
            StaticLease staticLease = null;

            this.settings.StaticLeases.Clear();
            foreach (string line in filecontent)
            {
                // clean line from tabs, break, whitespaces etc.
                string cleanline = Regex.Replace(line, @"\s+", " ");
                cleanline = cleanline.Trim();
                // check for valid tags
                if (cleanline.StartsWith("default-lease-time"))
                {
                    int endindex   = cleanline.IndexOf(";");
                    int startindex = 19;
                    int tmp;
                    if (int.TryParse(cleanline.Substring(startindex, endindex - startindex), out tmp))
                    {
                        this.settings.DefaultLeaseTime = tmp;
                    }
                }
                else if (cleanline.StartsWith("max-lease-time"))
                {
                    int endindex   = cleanline.IndexOf(";");
                    int startindex = 15;
                    int tmp;
                    if (int.TryParse(cleanline.Substring(startindex, endindex - startindex), out tmp))
                    {
                        this.settings.MaxLeaseTime = tmp;
                    }
                }
                else if (cleanline.StartsWith("subnet"))
                {
                    string[]             strArr = cleanline.Split(' ');
                    System.Net.IPAddress tmp;
                    if (strArr.Length > 1)
                    {
                        if (System.Net.IPAddress.TryParse(strArr[1], out tmp))
                        {
                            this.settings.Subnet = tmp.ToString();
                        }
                        if (strArr.Length > 3)
                        {
                            if (strArr[2].Contains("netmask"))
                            {
                                if (System.Net.IPAddress.TryParse(strArr[3], out tmp))
                                {
                                    this.settings.SubnetMask = tmp.ToString();
                                }
                            }
                        }
                    }
                }
                else if (cleanline.StartsWith("range"))
                {
                    string[]             strArr = cleanline.Split(' ');
                    System.Net.IPAddress tmp;
                    if (strArr.Length > 1)
                    {
                        if (System.Net.IPAddress.TryParse(strArr[1], out tmp))
                        {
                            this.settings.IpRangeStart = tmp.ToString();
                        }
                    }
                    if (strArr.Length > 2)
                    {
                        strArr[2] = strArr[2].TrimEnd(';');
                        if (System.Net.IPAddress.TryParse(strArr[2], out tmp))
                        {
                            this.settings.IpRangeEnd = tmp.ToString();
                        }
                    }
                }
                else if (cleanline.StartsWith("option"))
                {
                    string[]             strArr = cleanline.Split(' ');
                    System.Net.IPAddress tmpIp;
                    if (strArr.Length > 2)
                    {
                        switch (strArr[1])
                        {
                        case "routers":
                            strArr[2] = strArr[2].TrimEnd(';');
                            if (System.Net.IPAddress.TryParse(strArr[2], out tmpIp))
                            {
                                this.settings.Gateway = tmpIp.ToString();
                            }
                            break;

                        case "domain-name-servers":
                            strArr[2] = strArr[2].TrimEnd(';');
                            strArr[2] = strArr[2].TrimEnd(',');
                            if (System.Net.IPAddress.TryParse(strArr[2], out tmpIp))
                            {
                                this.settings.PrimaryDNS = tmpIp.ToString();
                            }
                            if (strArr.Length > 3)
                            {
                                strArr[3] = strArr[3].TrimEnd(';');
                                if (System.Net.IPAddress.TryParse(strArr[3], out tmpIp))
                                {
                                    this.settings.SecondaryDNS = tmpIp.ToString();
                                }
                            }
                            break;

                        default: break;
                        }
                    }
                }
                else if (cleanline.StartsWith("host"))
                {
                    staticLease = new StaticLease();
                    string[] strArr = cleanline.Split(' ');
                    if (strArr.Length > 1)
                    {
                        staticLease.DeviceName = strArr[1];
                    }
                }
                else if (cleanline.StartsWith("hardware ethernet"))
                {
                    int endindex   = cleanline.IndexOf(";");
                    int startindex = 18;
                    staticLease.MACAddress = cleanline.Substring(startindex, endindex - startindex);
                }
                else if (cleanline.StartsWith("fixed-address"))
                {
                    int endindex   = cleanline.IndexOf(";");
                    int startindex = 14;
                    staticLease.IPAddress = cleanline.Substring(startindex, endindex - startindex);
                }
                if (staticLease != null && cleanline.Contains("}"))
                {
                    staticLease.ID = (this.settings.GetStaticLeases().Count + 1).ToString();
                    this.settings.AddStaticLease(staticLease);
                    staticLease = null;
                }
            }
            if (SettingsFileLoadedEvt != null)
            {
                SettingsFileLoadedEvt(this, new EventArgs());
            }
        }