How to resolve the algorithm Canonicalize CIDR step by step in the Common Lisp programming language
How to resolve the algorithm Canonicalize CIDR step by step in the Common Lisp 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 Common Lisp programming language
Source code in the common programming language
(defun ip->bit-vector (ip)
(flet ((int->bits (int)
(loop :for i :below 8
:collect (if (logbitp i int) 1 0) :into bits
:finally (return (nreverse bits)))))
(loop :repeat 4
:with start := 0
:for pos := (position #\. ip :start start)
:collect (parse-integer ip :start start :end pos) :into res
:while pos
:do (setf start (1+ pos))
:finally (return (apply #'concatenate 'bit-vector (mapcar #'int->bits res))))))
(defun bit-vector->ip (vec &optional n)
(loop :repeat 4
:for end :from 8 :by 8
:for start := (- end 8)
:for sub := (subseq vec start end)
:collect (parse-integer (map 'string #'digit-char sub) :radix 2) :into res
:finally (return (format nil "~{~D~^.~}~@[/~A~]" res n))))
(defun canonicalize-cidr (cidr)
(let* ((n (position #\/ cidr))
(ip (subseq cidr 0 n))
(sn (parse-integer cidr :start (1+ n)))
(ip* (ip->bit-vector ip))
(canonical-ip (fill ip* 0 :start sn)))
(bit-vector->ip canonical-ip sn)))
(loop :for cidr :in '("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")
:for ccidr := (canonicalize-cidr cidr)
:do (format t "~&~A~20,T→ ~A~%" cidr ccidr))
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the C++ programming language
You may also check:How to resolve the algorithm Nonogram solver step by step in the Go programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Scala programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Swift programming language