Canonicalize CIDR: Difference between revisions
Content added Content deleted
(→{{header|Perl}}: Add note about Net::IP) |
(→{{header|UNIX Shell}}: Add implementation) |
||
Line 1,537: | Line 1,537: | ||
println!("{}", canonical_cidr("127.1.2.3/24").unwrap()); |
println!("{}", canonical_cidr("127.1.2.3/24").unwrap()); |
||
}</lang> |
}</lang> |
||
=={{header|UNIX Shell}}== |
|||
{{works with|Bourne Again SHell}} |
|||
{{works with|Korn Shell}} |
|||
{{works with|Zsh}} |
|||
<lang bash>function inet_aton { |
|||
typeset -i addr byte |
|||
typeset -a bytes |
|||
if [[ -n $BASH_VERSION ]]; then |
|||
IFS=. read -a bytes <<<"$1" |
|||
elif [[ -n $ZSH_VERSION ]]; then |
|||
IFS=. bytes=($=1) |
|||
else |
|||
IFS=. bytes=($1) |
|||
fi |
|||
addr=0 |
|||
for byte in "${bytes[@]}"; do |
|||
(( addr = 256 * addr + byte )) |
|||
done |
|||
printf '%s\n' "$addr" |
|||
} |
|||
function inet_ntoa { |
|||
typeset -i addr=$1 |
|||
typeset dotted i |
|||
for (( i=0; i<4; ++i )); do |
|||
dotted=$(( addr & 255 ))${dotted:+.$dotted} |
|||
(( addr >>= 8 )) |
|||
done |
|||
printf '%s\n' "$dotted" |
|||
} |
|||
function canonicalize_cidr { |
|||
typeset ip prefix fixed |
|||
typeset -i netmask addr |
|||
typeset -i all1s=2#11111111111111111111111111111111 |
|||
while (( $# )); do |
|||
IFS=/ read ip prefix <<<"$1" |
|||
netmask=$(( (all1s << (32-prefix)) & all1s )) |
|||
addr=$(inet_aton "$ip") |
|||
fixed=$(( addr & netmask )) |
|||
printf '%s/%s\n' "$(inet_ntoa $fixed)" "$prefix" |
|||
shift |
|||
done |
|||
} |
|||
# demo code |
|||
if (( ! $# )); then |
|||
set -- 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 |
|||
fi |
|||
canonicalize_cidr "$@"</lang> |
|||
{{Out}} |
|||
<pre>36.16.0.0/12 |
|||
62.62.197.8/29 |
|||
64.0.0.0/4 |
|||
161.214.74.0/24 |
|||
184.232.128.0/18</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |