diff --git a/iputil.rkt b/iputil.rkt index e30ee46..1affb98 100644 --- a/iputil.rkt +++ b/iputil.rkt @@ -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"))