elvish_config/lib/moi/util/ip.elv

154 lines
2.9 KiB
Plaintext
Raw Normal View History

2020-07-22 09:24:43 +00:00
use re
use str
2020-03-13 14:03:36 +00:00
fn is-ipv4 [arg]{
if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) {
put $false
} else {
use ./list
2020-07-22 09:24:43 +00:00
not (str:split '.' $arg | list:contains [p]{ > $p 255 })
2020-03-13 14:03:36 +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)
}
2020-07-22 09:24:43 +00:00
fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) }
2020-03-13 14:03:36 +00:00
fn -long-part6 [p]{
use str
p = (str:to-lower $p)
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
}
fn -middle-part6 [p]{
while (and (> (count $p) 1) (eq $p[0] 0)) {
p = $p[1..]
2020-03-13 14:03:36 +00:00
}
put $p
}
fn -find-max0 [parts]{
use ./list
2021-08-27 20:06:49 +00:00
var idx s = -1 1
var ci cs f = -1 0 $false
2020-03-13 14:03:36 +00:00
list:loop [i p]{
if (eq $p 0) {
condition:call $f { cs = (+ $cs 1) } { ci cs f = $i 1 $true }
} elif $f {
f = $false
if (> $cs $s) {
idx s = $ci $cs
}
}
} $parts
if (and $f (> $cs $s)) {
idx s = $ci $cs
}
put $idx $s
}
fn long6 [ip]{
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 '')
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
}
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
}
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) {
left = ['']
}
if (== (count $right) 0) {
right = ['']
}
parts = [$@left '' $@right]
}
2020-07-22 09:24:43 +00:00
str:join ':' $parts
2020-03-13 14:03:36 +00:00
}
fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) }
fn -cmp [e1 e2]{
use ./list
use ./condition
2021-08-27 20:06:49 +00:00
var c = 0
2020-03-13 14:03:36 +00:00
list:loop [i p1]{
2021-08-27 20:06:49 +00:00
var p2 = $e2[$i]
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
}
fn cmp4 [ip1 ip2]{
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
}
fn cmp6 [ip1 ip2]{
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))]
}
fn cmp [ip1 ip2]{
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 {
fail 'Not an IP: '$ip1' → '$ip2
2020-03-13 14:03:36 +00:00
}
}