Canonicalize CIDR: Difference between revisions
Content added Content deleted
m (add cases which are accepted by Apache) |
|||
Line 178: | Line 178: | ||
Julia has a Sockets library as a builtin, which has the types IPv4 and IPv6 for single IP addresses. |
Julia has a Sockets library as a builtin, which has the types IPv4 and IPv6 for single IP addresses. |
||
<lang julia>using Sockets |
<lang julia>using Sockets |
||
function canonCIDR(cidr::String) |
|||
cidr = replace(cidr, r"\.(\.|\/)" => s".0\1") # handle .. |
|||
cidr = replace(cidr, r"\.(\.|\/)" => s".0\1") # handle ... |
|||
ip = split(cidr, "/") |
|||
dig = length(ip) > 1 ? 2^(32 - parse(UInt8, ip[2])) : 1 |
|||
ip4 = IPv4(UInt64(IPv4(ip[1])) & (0xffffffff - dig + 1)) |
|||
return length(ip) == 1 ? "$ip4" : "$ip4/$(ip[2])" |
|||
end |
|||
println(canonCIDR("87.70.141.1/22")) |
|||
println(canonCIDR("100.68.0.18/18")) |
|||
println(canonCIDR("10.4.30.77/30")) |
|||
println(canonCIDR("110.200.21/4")) |
|||
println(canonCIDR("10..55/8")) |
|||
println(canonCIDR("10.../8")) |
|||
</lang>{{out}} |
|||
<pre> |
|||
using Sockets |
|||
function canonCIDR(cidr::String) |
function canonCIDR(cidr::String) |