How to resolve the algorithm Canonicalize CIDR step by step in the Nim programming language
How to resolve the algorithm Canonicalize CIDR step by step in the Nim programming language
Table of Contents
Problem Statement
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form. That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Given 87.70.141.1/22, your code should output 87.70.140.0/22
An Internet Protocol version 4 address is a 32-bit value, conventionally represented as a number in base 256 using dotted-decimal notation, where each base-256 digit is given in decimal and the digits are separated by periods. Logically, this 32-bit value represents two components: the leftmost (most-significant) bits determine the network portion of the address, while the rightmost (least-significant) bits determine the host portion. Classless Internet Domain Routing block notation indicates where the boundary between these two components is for a given address by adding a slash followed by the number of bits in the network portion. In general, CIDR blocks stand in for the entire set of IP addresses sharing the same network component, so it's common to see access control lists that specify individual IP addresses using /32 to indicate that only the one address is included. Software accepting this notation as input often expects it to be entered in canonical form, in which the host bits are all zeroes. But network admins sometimes skip this step and just enter the address of a specific host on the subnet with the network size, resulting in a non-canonical entry. The example address, 87.70.141.1/22, represents binary 0101011101000110100011 / 0100000001, with the / indicating the network/host division. To canonicalize, clear all the bits to the right of the / and convert back to dotted decimal: 0101011101000110100011 / 0000000000 → 87.70.140.0.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Canonicalize CIDR step by step in the Nim programming language
Source code in the nim programming language
import net
import strutils
proc canonicalize*(address: var IpAddress; nbits: Positive) =
## Canonicalize an IP address.
var zbits = 32 - nbits # Number of bits to reset.
# We process byte by byte which avoids byte order issues.
for idx in countdown(address.address_v4.high, address.address_v4.low):
if zbits == 0:
# No more bits to reset.
break
if zbits >= 8:
# Reset the current byte and continue with the remaining bits.
address.address_v4[idx] = 0
dec zbits, 8
else:
# Use a mask to reset the bits.
address.address_v4[idx] = address.address_v4[idx] and (0xff'u8 shl zbits)
zbits = 0
#———————————————————————————————————————————————————————————————————————————————————————————————————
when isMainModule:
import strformat
var ipAddress: IpAddress
var nbits: int
for address in ["87.70.141.1/22", "36.18.154.103/12", "62.62.197.11/29",
"67.137.119.181/4", "161.214.74.21/24", "184.232.176.184/18"]:
# Parse the address.
let parts = address.split('/')
try:
ipAddress = parseIpAddress(parts[0])
if ipAddress.family == IPV6:
raise newException(ValueError, "")
except ValueError:
echo "Invalid IP V4 address: ", parts[0]
quit(QuitFailure)
# Check the number of bits.
try:
nbits = parseInt(parts[1])
if nbits notin 1..32:
raise newException(ValueError, "")
except ValueError:
echo "Invalid number of bits: ", parts[1]
quit(QuitFailure)
# Canonicalize the address and display the result.
ipAddress.canonicalize(nbits)
echo &"{address:<18} ⇢ {ipAddress}/{nbits}"
You may also check:How to resolve the algorithm Casting out nines step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Plain English programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Harbour programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Ruby programming language