/// <summary> /// IComparer against another IPV4Range /// </summary> /// <param name="other">Other range</param> /// <returns></returns> public int CompareTo(IPV6Range other) { int cmp = End.CompareTo(other.Begin); if (cmp < 0) { return(cmp); } cmp = Begin.CompareTo(other.End); if (cmp > 0) { return(cmp); } // inside range return(0); }
public MemoryFirewallRuleRanges(IEnumerable <IPAddressRange> ipRanges, List <PortRange> allowedPorts, bool block, string name) { List <IPAddressRange> ipRangesSorted = new List <IPAddressRange>(ipRanges); ipRangesSorted.Sort(); allowedPorts ??= emptyPortRanges; Block = block; Name = name; foreach (IPAddressRange range in ipRangesSorted) { // optimized storage, no pointers or other overhead if (range.Begin.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { uint begin = range.Begin.ToUInt32(); uint end = range.End.ToUInt32(); Debug.Assert(end >= begin); ipv4.Add(new IPV4Range { Begin = begin, End = end }); } else { UInt128 begin = range.Begin.ToUInt128(); UInt128 end = range.End.ToUInt128(); Debug.Assert(end.CompareTo(begin) >= 0); ipv6.Add(new IPV6Range { Begin = begin, End = end }); } } ipv4.TrimExcess(); ipv6.TrimExcess(); if (block) { string portString = IPBanFirewallUtility.GetBlockPortRangeString(allowedPorts); this.portRanges = (string.IsNullOrWhiteSpace(portString) ? new List <PortRange>(0) : portString.Split(',').Select(s => PortRange.Parse(s)).ToList()); } else { this.portRanges = allowedPorts; } }