router registration and lookup

This commit is contained in:
Milo 2020-01-31 22:35:34 -05:00
parent 133c18551a
commit 90275458ed
1 changed files with 56 additions and 33 deletions

89
router
View File

@ -5,7 +5,9 @@
(require json (require json
"iputil.rkt" "iputil.rkt"
"unix-socket.rkt" "unix-socket.rkt"
"msg.rkt") "msg.rkt"
"radix-tree.rkt"
)
;; info : Peer ;; info : Peer
;; sock-in : Input-Port ;; sock-in : Input-Port
@ -14,24 +16,41 @@
[info sock-in sock-out] [info sock-in sock-out]
#:transparent) #:transparent)
;; alist : [Listof [Cons IP Route]] ;; a Router is a [RTof [Listof [Cons Peer Route]]]
(struct router
[(alist #:mutable)]
#:transparent)
;; -> Router ;; -> Router
(define (make-router) (define (make-router)
(router '())) (make-rt))
;; Router Route IP -> Void ;; Router Route Peer -> Void
(define (router-add! rs r src) (define (router-add! rt r peer)
(set-router-alist! rs (rt-update! rt
(cons (cons src r) (subnet->bl (route-subnet r))
(router-alist rs)))) (lambda (rst) (cons (cons peer r) rst))
(lambda () '())))
;; Router IP -> (U IP #f) (define (route< r1 r2)
(define (router-find-best rs src) #|
#f) (route< r1 r2) iff "r1 wins"
1. The path with the highest "localpref" wins. If the "localpref"s are equal...
2. The path with "selfOrigin" = true wins. If all selfOrigins are the equal...
3. The path with the shortest "ASPath" wins. If multiple routes have the shortest length...
4. The path with the best "origin" wins, were IGP > EGP > UNK.
If multiple routes have the best origin...
5. The path from the neighbor router with the lowest IP address.
|#
#t)
;; Router IP -> (U Peer #f)
(define (router-find-best rt src-ip)
(match (sort (rt-lookup rt
(ip->bl src-ip)
(λ () '()))
route<
#:key cdr)
[(list* (cons peer _) _) peer]
[_ #f]))
;; Str [Listof Peer-Conn] -> Void ;; Str [Listof Peer-Conn] -> Void
;; -- ;; --
@ -52,42 +71,46 @@
(channel-put mail (list peer msg)) (channel-put mail (list peer msg))
(loop)))))) (loop))))))
(define (ip->peer-conn ip) (define (ip->peer ip)
(findf (λ (p) (findf (λ (peer)
(equal? ip (peer-ip (peer-conn-info p)))) (equal? ip (peer-ip peer)))
(map peer-conn-info peer-conns)))
(define (peer->peer-conn peer)
(findf (λ (pc)
(equal? peer (peer-conn-info pc)))
peer-conns)) peer-conns))
(define router (define router
(make-router)) (make-router))
(define (loop) (let loop ()
(match-define (list src-peer msg) (channel-get mail)) (match-define (list src-peer-conn msg) (channel-get mail))
(printf "====\nfrom ~a:\n~s\n" src-peer msg) (printf "====\nfrom ~a:\n~s\n" src-peer-conn msg)
(match msg (match msg
[(msg:update src dst r) [(msg:update src dst r)
(router-add! router r src)] (router-add! router
r
(ip->peer src))
(rt-dump router)]
[(msg:data src dst data) [(msg:data src dst data)
(define-values [dst-peer resp-msg] (define-values [dst-peer-conn resp-msg]
(match (router-find-best router dst) (match (router-find-best router dst)
[#f [#f
(values peer (values src-peer-conn
(msg:no-route src (msg:no-route src dst))]
dst))] [dst-peer
[dst-peer-ip (values (peer->peer-conn dst-peer)
(values (ip->peer-conn dst-peer-ip)
msg)])) msg)]))
(printf "----\nwant to send back ~a to ~a\n" (printf "----\nwant to send back to ~a:\n~a\n"
dst-peer (peer-conn-info dst-peer-conn)
resp-msg)] resp-msg)]
[_ [_
(printf "----\nignored\n")]) (printf "----\nignored\n")])
(loop)) (loop)))
(printf "waiting for messages...\n")
(loop))
;; Str [Listof Peer] -> ;; Str [Listof Peer] ->
;; Router main ;; Router main