/// <summary> /// adds a new static lease to the dictionary collection /// </summary> /// <param name="staticLease">StaticLease object</param> public void AddStaticLease(StaticLease staticLease) { this.staticLeases[staticLease.MACAddress] = staticLease; this.StaticLeasesIsSet = true; StaticLeasesChangedEvt(this, new PropertyChangedEventArgs("StaticLeases")); }
/// <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()); } }