/// <summary> /// adds a new DhcpdLease to the collection /// </summary> /// <param name="dhcpdLease">DhcpdLease object</param> public void AddDhcpdLease(DhcpdLease dhcpdLease) { this.dhcpdLeases.Add(dhcpdLease.MACAddress, dhcpdLease); DhcpdLeasesChangedEvt(this.dhcpdLeases, new PropertyChangedEventArgs("DhcpdLeases")); }
/// <summary> /// parses the leases from the filecontent of the dhcpd.leasses file /// </summary> /// <param name="result"></param> private void parseDhcpdLeasesFile(IAsyncResult result) { IOController.Log(this, "parseDhcpdLeasesFile start", Flag.debug); System.Collections.Generic.Dictionary <String, DhcpdLease> dhcpdLeasesList = new System.Collections.Generic.Dictionary <String, DhcpdLease>(); DhcpdLease dhcpdLease = new DhcpdLease(); try { AsyncResult aResult = (AsyncResult)result; ReadDhcpdLeasesFileDelegate readDhcpdLeasesFileDelegate = (ReadDhcpdLeasesFileDelegate)aResult.AsyncDelegate; ArrayList filecontent = readDhcpdLeasesFileDelegate.EndInvoke(result); this.settings.DhcpdLeases.Clear(); //IOController.Log(this, "parseDhcpdLeasesFile finished EndInvoke", Flag.debug); foreach (string line in filecontent) { string cleanline = Regex.Replace(line, @"\s+", " "); cleanline = cleanline.Trim(); if (cleanline.StartsWith("lease")) { dhcpdLease = new DhcpdLease(); int endindex = cleanline.IndexOf("{") - 1; int startindex = 6; dhcpdLease.IPAddress = cleanline.Substring(startindex, endindex - startindex); } else if (cleanline.StartsWith("hardware ethernet")) { int endindex = cleanline.IndexOf(";"); int startindex = cleanline.IndexOf("hardware ethernet") + 18; dhcpdLease.MACAddress = cleanline.Substring(startindex, endindex - startindex); } else if (cleanline.StartsWith("client-hostname")) { int endindex = cleanline.IndexOf(";") - 1; int startindex = cleanline.IndexOf("client-hostname") + 17; dhcpdLease.DeviceName = cleanline.Substring(startindex, endindex - startindex); } else if (cleanline.StartsWith("starts")) { string[] strArr = cleanline.Split(' '); if (strArr.Length > 3) { strArr[3] = strArr[3].TrimEnd(';'); dhcpdLease.LeaseStart = strArr[2] + " " + strArr[3]; } } else if (cleanline.StartsWith("ends")) { string[] strArr = cleanline.Split(' '); if (strArr.Length > 3) { strArr[3] = strArr[3].TrimEnd(';'); dhcpdLease.LeaseEnd = strArr[2] + " " + strArr[3]; } } else if (cleanline.StartsWith("binding state")) { string[] strArr = cleanline.Split(' '); if (strArr.Length > 2) { strArr[2] = strArr[2].TrimEnd(';'); dhcpdLease.LeaseState = strArr[2]; } } else if (cleanline.Contains("}")) { dhcpdLeasesList[dhcpdLease.MACAddress] = dhcpdLease; //IOController.Log(this, "found dhcpdLease: " + dhcpdLease.ToString(), Flag.debug); } } } catch (Exception e) { IOController.Log(this, "parsing failed " + e.ToString()); } this.settings.DhcpdLeases = dhcpdLeasesList; IOController.Log(this, "Active Leases found:\n" + string.Join("\n", dhcpdLeasesList.Select(x => x.Key + "=" + x.Value).ToArray()), Flag.status); }