/// <summary> /// Subnet a network into multiple nets of cidr mask /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25 /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9 /// </summary> public static bool TrySubnet(IPNetwork network, byte cidr, out IPNetworkCollection ipnetworkCollection) { IPNetworkCollection inc; InternalSubnet(true, network, cidr, out inc); if (inc == null) { ipnetworkCollection = null; return(false); } ipnetworkCollection = inc; return(true); }
private static void InternalSubnet(bool trySubnet, IPNetwork network, byte cidr, out IPNetworkCollection ipnetworkCollection) { if (network == null) { if (trySubnet == false) { throw new ArgumentNullException("network"); } ipnetworkCollection = null; return; } if (cidr > 32) { if (trySubnet == false) { throw new ArgumentOutOfRangeException("cidr"); } ipnetworkCollection = null; return; } if (cidr < network.Cidr) { if (trySubnet == false) { throw new ArgumentException("cidr"); } ipnetworkCollection = null; return; } ipnetworkCollection = new IPNetworkCollection(network, cidr); return; }