2020-07-22 09:24:43 +00:00
|
|
|
use re
|
|
|
|
use str
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn is-ipv4 {|arg|
|
2020-03-13 14:03:36 +00:00
|
|
|
if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) {
|
|
|
|
put $false
|
|
|
|
} else {
|
|
|
|
use ./list
|
2021-12-11 21:09:02 +00:00
|
|
|
not (str:split '.' $arg | list:contains {|p| > $p 255 })
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn is-ipv6 {|arg|
|
2021-08-27 20:06:49 +00:00
|
|
|
var p = '[0-9a-fA-F]{1,4}'
|
|
|
|
var g1 g2 = '('$p':)' '(:'$p')'
|
|
|
|
var cases = [
|
2020-03-13 14:03:36 +00:00
|
|
|
$g1'{7,7}'$p
|
|
|
|
$g1'{1,7}:'
|
|
|
|
$g1'{1,6}:'$p
|
|
|
|
$g1'{1,5}'$g2'{1,2}'
|
|
|
|
$g1'{1,4}'$g2'{1,3}'
|
|
|
|
$g1'{1,3}'$g2'{1,4}'
|
|
|
|
$g1'{1,2}'$g2'{1,5}'
|
|
|
|
$p':'$g2'{1,6}'
|
|
|
|
':'$g2'{1,7}'
|
|
|
|
'::'
|
|
|
|
]
|
2021-08-27 20:06:49 +00:00
|
|
|
var r = '^('(str:join '|' $cases)')$'
|
2020-03-13 14:03:36 +00:00
|
|
|
put (re:match $r $arg)
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
|
2020-07-22 09:24:43 +00:00
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn -long-part6 {|p|
|
2020-03-13 14:03:36 +00:00
|
|
|
use str
|
2021-10-13 18:49:48 +00:00
|
|
|
set p = (str:to-lower $p)
|
|
|
|
var c = (- 4 (count $p))
|
2020-07-22 09:24:43 +00:00
|
|
|
put (repeat $c '0' | str:join '')$p
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn -middle-part6 {|p|
|
2020-03-13 14:03:36 +00:00
|
|
|
while (and (> (count $p) 1) (eq $p[0] 0)) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set p = $p[1..]
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
put $p
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn -find-max0 {|parts|
|
2020-03-13 14:03:36 +00:00
|
|
|
use ./list
|
2021-08-27 20:06:49 +00:00
|
|
|
var idx s = -1 1
|
|
|
|
var ci cs f = -1 0 $false
|
2021-12-11 21:09:02 +00:00
|
|
|
list:loop {|i p|
|
2020-03-13 14:03:36 +00:00
|
|
|
if (eq $p 0) {
|
2021-10-13 18:49:48 +00:00
|
|
|
condition:call $f { set cs = (+ $cs 1) } { set ci cs f = $i 1 $true }
|
2020-03-13 14:03:36 +00:00
|
|
|
} elif $f {
|
2021-10-13 18:49:48 +00:00
|
|
|
set f = $false
|
2020-03-13 14:03:36 +00:00
|
|
|
if (> $cs $s) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set idx s = $ci $cs
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} $parts
|
|
|
|
if (and $f (> $cs $s)) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set idx s = $ci $cs
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
put $idx $s
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn long6 {|ip|
|
2020-03-13 14:03:36 +00:00
|
|
|
if (not (is-ipv6 $ip)) {
|
|
|
|
fail 'Not an IPv6'
|
|
|
|
}
|
|
|
|
if (eq $ip '::') {
|
2020-07-22 09:24:43 +00:00
|
|
|
repeat 8 '0000' | str:join ':'
|
2020-03-13 14:03:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-27 20:06:49 +00:00
|
|
|
var c = (- 7 (str:count $ip ':'))
|
2020-03-13 14:03:36 +00:00
|
|
|
if (> $c 0) {
|
2021-08-27 20:06:49 +00:00
|
|
|
var i = (str:index $ip '::')
|
|
|
|
var z = (repeat $c ':' | str:join '')
|
2021-10-13 18:49:48 +00:00
|
|
|
set ip = (str:join '' [$ip[..$i] $z $ip[{$i}..]])
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
2020-07-22 09:24:43 +00:00
|
|
|
str:split ':' $ip | each $-long-part6~ | str:join ':'
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn middle6 {|ip|
|
2020-07-22 09:24:43 +00:00
|
|
|
str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':'
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn short6 {|ip|
|
2021-08-27 20:06:49 +00:00
|
|
|
var parts = [(str:split ':' (middle6 $ip))]
|
|
|
|
var i s = (-find-max0 $parts)
|
2020-03-13 14:03:36 +00:00
|
|
|
if (>= $i 0) {
|
2021-08-27 20:06:49 +00:00
|
|
|
var left right = $parts[..$i] $parts[(+ $i $s)..]
|
2020-03-13 14:03:36 +00:00
|
|
|
if (== (count $left) 0) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set left = ['']
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
if (== (count $right) 0) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set right = ['']
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
2021-10-13 18:49:48 +00:00
|
|
|
set parts = [$@left '' $@right]
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
2020-07-22 09:24:43 +00:00
|
|
|
str:join ':' $parts
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
|
2020-03-13 14:03:36 +00:00
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn -cmp {|e1 e2|
|
2020-03-13 14:03:36 +00:00
|
|
|
use ./list
|
|
|
|
use ./condition
|
2021-08-27 20:06:49 +00:00
|
|
|
var c = 0
|
2021-12-11 21:09:02 +00:00
|
|
|
list:loop {|i p1|
|
2021-08-27 20:06:49 +00:00
|
|
|
var p2 = $e2[$i]
|
2021-10-13 18:49:48 +00:00
|
|
|
set c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0))
|
2020-03-13 14:03:36 +00:00
|
|
|
if (!= $c 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} $e1
|
|
|
|
put $c
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn cmp4 {|ip1 ip2|
|
2020-03-13 14:03:36 +00:00
|
|
|
if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) {
|
2020-07-22 09:24:43 +00:00
|
|
|
fail 'Not an IPv4 '$ip1' → '$ip2
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
2020-07-22 09:24:43 +00:00
|
|
|
-cmp [(str:split . $ip1)] [(str:split . $ip2)]
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn cmp6 {|ip1 ip2|
|
2020-03-13 14:03:36 +00:00
|
|
|
if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) {
|
2020-07-22 09:24:43 +00:00
|
|
|
fail 'Not an IPv6: '$ip1' → '$ip2
|
|
|
|
}
|
|
|
|
-cmp [(str:split : (middle6 $ip1))] [(str:split : (middle6 $ip2))]
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
fn cmp {|ip1 ip2|
|
2020-07-22 09:24:43 +00:00
|
|
|
if (is-ipv4 $ip1) {
|
|
|
|
if (is-ipv4 $ip2) {
|
|
|
|
cmp4 $ip1 $ip2
|
|
|
|
} else {
|
|
|
|
put -1
|
|
|
|
}
|
|
|
|
} elif (is-ipv6 $ip1) {
|
|
|
|
if (is-ipv4 $ip2) {
|
|
|
|
put 1
|
|
|
|
} elif (is-ipv6 $ip2) {
|
|
|
|
cmp6 $ip1 $ip2
|
|
|
|
} else {
|
|
|
|
put -1
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-24 18:44:01 +00:00
|
|
|
fail 'Not an IP: '$ip1' → '$ip2
|
2020-03-13 14:03:36 +00:00
|
|
|
}
|
|
|
|
}
|