Exemple #1
0
        public static SubnetInfo CalculateIPv4Subnet(IPAddress ipv4Address, IPAddress subnetmask)
        {
            IPAddress networkAddress = SubnetHelper.GetIPv4NetworkAddress(ipv4Address, subnetmask);
            IPAddress broadcast      = SubnetHelper.GetIPv4Broadcast(ipv4Address, subnetmask);
            int       cidr           = SubnetmaskHelper.ConvertSubnetmaskToCidr(subnetmask);
            int       totalIPs       = SubnetmaskHelper.GetNumberIPv4Addresses(cidr);

            return(new SubnetInfo
            {
                NetworkAddress = networkAddress,
                Broadcast = broadcast,
                TotalIPs = totalIPs,
                Subnetmask = subnetmask,
                CIDR = cidr,
                HostFirstIP = IPv4AddressHelper.IncrementIPv4Address(networkAddress, 1),
                HostLastIP = IPv4AddressHelper.DecrementIPv4Address(broadcast, 1),
                HostIPs = totalIPs - 2
            });
        }
Exemple #2
0
        public static SubnetInfo CalculateIPv4Subnet(IPAddress ipv4Address, IPAddress subnetmask)
        {
            IPAddress networkAddress = GetIPv4NetworkAddress(ipv4Address, subnetmask);
            IPAddress broadcast      = GetIPv4Broadcast(ipv4Address, subnetmask);
            int       cidr           = Subnetmask.ConvertSubnetmaskToCidr(subnetmask);
            long      totalIPs       = Subnetmask.GetNumberIPv4Addresses(cidr);

            // Fix bug when /31 (host first/last can be null)
            IPAddress hostFirstIP = null;
            IPAddress hostLastIP  = null;
            long      hostIPs     = 0;

            if (totalIPs == 1) // Fix bug when /32 (show range for 1 IP)
            {
                hostFirstIP = networkAddress;
                hostLastIP  = broadcast;
                hostIPs     = 0;
            }
            else if (totalIPs > 2) // Calculate for /0-/30
            {
                hostFirstIP = IPv4AddressHelper.IncrementIPv4Address(networkAddress, 1);
                hostLastIP  = IPv4AddressHelper.DecrementIPv4Address(broadcast, 1);
                hostIPs     = totalIPs - 2;
            }

            return(new SubnetInfo
            {
                NetworkAddress = networkAddress,
                Broadcast = broadcast,
                IPAddresses = totalIPs,
                Subnetmask = subnetmask,
                CIDR = cidr,
                HostFirstIP = hostFirstIP,
                HostLastIP = hostLastIP,
                Hosts = hostIPs
            });
        }