netmask-ip

This commit is contained in:
Milo 2020-01-31 23:38:32 -05:00
parent 901cd5beee
commit 38be205744
1 changed files with 8 additions and 0 deletions

View File

@ -4,6 +4,7 @@
string->ip
ip->string
ip-netmask
netmask-ip
string->subnet
subnet->string
string->peer
@ -51,6 +52,10 @@
#:break (positive? (bitwise-and ip (arithmetic-shift 1 i)))
1)))
(define (netmask-ip mask)
(arithmetic-shift (sub1 (arithmetic-shift 1 mask))
(- 32 mask)))
(define (string->subnet str)
(match-define (list ipstr maskstr) (string-split str "/"))
(subnet (string->ip ipstr)
@ -78,6 +83,9 @@
(check-equal? (ip->string (string->ip "123.84.0.67")) "123.84.0.67")
(check-equal? (ip->string (string->ip "0.0.0.0")) "0.0.0.0")
(check-equal? (ip->string (netmask-ip 23)) "255.255.254.0")
(check-equal? (ip->string (netmask-ip 8)) "255.0.0.0")
(check-equal? (ip-netmask (string->ip "255.255.254.0")) 23)
(check-equal? (ip-netmask (string->ip "255.0.0.0")) 8)
(check-equal? (subnet->bl (string->subnet "3.3.0.0/24"))