82 lines
2.3 KiB
Racket
82 lines
2.3 KiB
Racket
#lang racket
|
|
|
|
(provide (struct-out peer) (struct-out subnet)
|
|
string->ip
|
|
ip->string
|
|
ip-netmask
|
|
string->subnet
|
|
subnet->string
|
|
string->peer)
|
|
|
|
(require racket/struct)
|
|
|
|
(module+ test
|
|
(require rackunit))
|
|
|
|
(struct peer [ip type]
|
|
#:transparent
|
|
#:methods gen:custom-write
|
|
[(define write-proc
|
|
(make-constructor-style-printer
|
|
(lambda (x) 'peer)
|
|
(lambda (x) (list (ip->string (peer-ip x)) (peer-type x)))))])
|
|
|
|
(struct subnet [ip mask]
|
|
#:transparent
|
|
#:methods gen:custom-write
|
|
[(define write-proc
|
|
(make-constructor-style-printer
|
|
(lambda (x) 'subnet)
|
|
(lambda (x) (list (subnet->string x)))))])
|
|
|
|
(define (string->ip str)
|
|
(define parts (reverse (string-split str ".")))
|
|
(for/sum ([part (in-list parts)]
|
|
[i (in-naturals)])
|
|
(arithmetic-shift (string->number part)
|
|
(* i 8))))
|
|
|
|
(define (ip->string ip)
|
|
(define parts
|
|
(reverse (for/list ([i (in-range 4)])
|
|
(number->string (bitwise-and (arithmetic-shift ip (* i -8))
|
|
255)))))
|
|
(string-join parts "."))
|
|
|
|
(define (ip-netmask ip)
|
|
(- 32
|
|
(for/sum ([i (in-range 32)])
|
|
#:break (positive? (bitwise-and ip (arithmetic-shift 1 i)))
|
|
1)))
|
|
|
|
(define (string->subnet str)
|
|
(match-define (list ipstr maskstr) (string-split str "/"))
|
|
(subnet (string->ip ipstr)
|
|
(string->number maskstr)))
|
|
|
|
(define (subnet->string sub)
|
|
(format "~a/~a"
|
|
(ip->string (subnet-ip sub))
|
|
(subnet-mask sub)))
|
|
|
|
(define (subnet->bl sub)
|
|
(for/list ([i (in-range (subnet-mask sub))])
|
|
(= 1 (bitwise-and 1 (arithmetic-shift (subnet-ip sub) (- i 31))))))
|
|
|
|
(define (string->peer str)
|
|
(match-define (list ip type) (string-split str "-"))
|
|
(peer (string->ip ip) (string->symbol type)))
|
|
|
|
;; ================================================================================
|
|
|
|
(module+ test
|
|
(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-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"))
|
|
'(#f #f #f #f #f #f #t #t
|
|
#f #f #f #f #f #f #t #t
|
|
#f #f #f #f #f #f #f #f)))
|