2020-01-29 21:23:07 +00:00
|
|
|
#!/usr/bin/env racket
|
|
|
|
#lang racket
|
2020-01-30 23:29:08 +00:00
|
|
|
; vim:syntax=racket
|
2020-01-29 21:23:07 +00:00
|
|
|
|
2020-01-30 23:33:13 +00:00
|
|
|
(require json
|
|
|
|
"iputil.rkt"
|
2020-01-31 00:51:35 +00:00
|
|
|
"unix-socket.rkt"
|
2020-02-01 03:35:34 +00:00
|
|
|
"msg.rkt"
|
|
|
|
"radix-tree.rkt"
|
|
|
|
)
|
2020-01-30 23:33:13 +00:00
|
|
|
|
|
|
|
;; info : Peer
|
|
|
|
;; sock-in : Input-Port
|
|
|
|
;; sock-out : Output-Port
|
|
|
|
(struct peer-conn
|
|
|
|
[info sock-in sock-out]
|
|
|
|
#:transparent)
|
|
|
|
|
2020-02-01 03:35:34 +00:00
|
|
|
;; a Router is a [RTof [Listof [Cons Peer Route]]]
|
2020-02-01 02:31:56 +00:00
|
|
|
|
|
|
|
;; -> Router
|
|
|
|
(define (make-router)
|
2020-02-01 03:35:34 +00:00
|
|
|
(make-rt))
|
|
|
|
|
|
|
|
;; Router Route Peer -> Void
|
|
|
|
(define (router-add! rt r peer)
|
|
|
|
(rt-update! rt
|
|
|
|
(subnet->bl (route-subnet r))
|
|
|
|
(lambda (rst) (cons (cons peer r) rst))
|
|
|
|
(lambda () '())))
|
|
|
|
|
|
|
|
(define (route< r1 r2)
|
|
|
|
#|
|
|
|
|
(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.
|
|
|
|
|#
|
2020-02-01 04:13:49 +00:00
|
|
|
(let/ec meow
|
|
|
|
(define (cmp mapper)
|
|
|
|
(let ([x2 (mapper r1)]
|
|
|
|
[y2 (mapper r2)])
|
|
|
|
(cond [(< x2 y2) (meow #t)]
|
|
|
|
[(> x2 y2) (meow #f)])))
|
|
|
|
(cmp (compose - route-pref))
|
|
|
|
(cmp (lambda (x) (if (route-self-origin? x) 1 0)))
|
|
|
|
(cmp (compose length route-as-path))
|
|
|
|
(cmp (lambda (x) (match (route-origin x) ['IGP 3] ['EGP 2] ['UNK 1])))
|
|
|
|
(cmp route-nexthop)
|
|
|
|
(error "your router is angery...")))
|
2020-02-01 03:35:34 +00:00
|
|
|
|
|
|
|
;; 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]))
|
2020-02-01 02:31:56 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
;; Str [Listof Peer-Conn] -> Void
|
2020-01-30 23:33:13 +00:00
|
|
|
;; --
|
2020-01-31 00:51:35 +00:00
|
|
|
;; Runs router logic, given a list of peer connections.
|
2020-01-30 23:33:13 +00:00
|
|
|
(define (run-router/conns ans peer-conns)
|
|
|
|
(define mail
|
|
|
|
(make-channel))
|
|
|
|
|
|
|
|
(define peer-threads
|
|
|
|
(for/list ([pc (in-list peer-conns)])
|
|
|
|
(match-define (peer-conn peer sock-in sock-out) pc)
|
|
|
|
(thread (λ ()
|
|
|
|
(define buf (make-bytes 65536))
|
|
|
|
(let loop ()
|
|
|
|
(define len (read-bytes-avail! buf sock-in))
|
2020-01-30 23:56:17 +00:00
|
|
|
(printf "got ~a bytes from ~a...\n" len peer)
|
2020-01-31 00:51:35 +00:00
|
|
|
(define msg (bytes->msg (subbytes buf 0 len)))
|
|
|
|
(channel-put mail (list peer msg))
|
2020-01-30 23:33:13 +00:00
|
|
|
(loop))))))
|
|
|
|
|
2020-02-01 03:35:34 +00:00
|
|
|
(define (ip->peer ip)
|
|
|
|
(findf (λ (peer)
|
|
|
|
(equal? ip (peer-ip peer)))
|
|
|
|
(map peer-conn-info peer-conns)))
|
|
|
|
|
|
|
|
(define (peer->peer-conn peer)
|
|
|
|
(findf (λ (pc)
|
|
|
|
(equal? peer (peer-conn-info pc)))
|
2020-02-01 02:31:56 +00:00
|
|
|
peer-conns))
|
|
|
|
|
|
|
|
(define router
|
|
|
|
(make-router))
|
|
|
|
|
2020-02-01 03:35:34 +00:00
|
|
|
(let loop ()
|
|
|
|
(match-define (list src-peer-conn msg) (channel-get mail))
|
|
|
|
(printf "====\nfrom ~a:\n~s\n" src-peer-conn msg)
|
2020-02-01 02:31:56 +00:00
|
|
|
(match msg
|
|
|
|
|
|
|
|
[(msg:update src dst r)
|
2020-02-01 03:35:34 +00:00
|
|
|
(router-add! router
|
|
|
|
r
|
|
|
|
(ip->peer src))
|
|
|
|
(rt-dump router)]
|
2020-02-01 02:31:56 +00:00
|
|
|
|
|
|
|
[(msg:data src dst data)
|
2020-02-01 03:35:34 +00:00
|
|
|
(define-values [dst-peer-conn resp-msg]
|
2020-02-01 02:31:56 +00:00
|
|
|
(match (router-find-best router dst)
|
|
|
|
[#f
|
2020-02-01 03:35:34 +00:00
|
|
|
(values src-peer-conn
|
|
|
|
(msg:no-route src dst))]
|
|
|
|
[dst-peer
|
|
|
|
(values (peer->peer-conn dst-peer)
|
2020-02-01 02:31:56 +00:00
|
|
|
msg)]))
|
2020-02-01 03:35:34 +00:00
|
|
|
(printf "----\nwant to send back to ~a:\n~a\n"
|
|
|
|
(peer-conn-info dst-peer-conn)
|
2020-02-01 02:31:56 +00:00
|
|
|
resp-msg)]
|
|
|
|
|
|
|
|
[_
|
|
|
|
(printf "----\nignored\n")])
|
2020-02-01 03:35:34 +00:00
|
|
|
(loop)))
|
2020-01-29 21:23:07 +00:00
|
|
|
|
|
|
|
;; Str [Listof Peer] ->
|
|
|
|
;; Router main
|
|
|
|
(define (run-router asn peers)
|
|
|
|
(displayln asn)
|
|
|
|
(map displayln peers)
|
2020-01-30 23:56:17 +00:00
|
|
|
(displayln "------------")
|
2020-01-31 00:51:35 +00:00
|
|
|
(with-handlers ([exn:break? (λ (e) (printf "time to die.\n"))])
|
2020-01-30 23:56:17 +00:00
|
|
|
(run-router/conns
|
|
|
|
asn
|
|
|
|
(for/list ([peer (in-list peers)])
|
|
|
|
(define-values [sock-in sock-out]
|
|
|
|
(unix-socket-connect (ip->string (peer-ip peer))
|
|
|
|
'SOCK-SEQPACKET))
|
|
|
|
(peer-conn peer
|
|
|
|
sock-in
|
|
|
|
sock-out)))))
|
2020-01-29 21:23:07 +00:00
|
|
|
|
|
|
|
(module+ main
|
2020-01-30 23:33:13 +00:00
|
|
|
(command-line
|
|
|
|
#:program "router"
|
|
|
|
#:args
|
|
|
|
(asn . peers)
|
2020-01-30 23:56:17 +00:00
|
|
|
(with-output-to-file "log.txt"
|
|
|
|
#:exists 'replace
|
|
|
|
(λ ()
|
|
|
|
;; Run the router
|
|
|
|
(run-router asn (map string->peer peers))))))
|
2020-01-30 23:33:13 +00:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(define-values [in1 out1] (make-pipe))
|
|
|
|
(define-values [in2 out2] (make-pipe))
|
|
|
|
(define p1 (peer-conn (string->peer "1.2.3.4-cust") in1 out1))
|
|
|
|
(define p2 (peer-conn (string->peer "1.2.3.5-peer") in2 out2))
|
|
|
|
|
2020-01-30 23:56:17 +00:00
|
|
|
; (define abort-router
|
2020-01-30 23:33:13 +00:00
|
|
|
(run-router/conns "123"
|
2020-01-30 23:56:17 +00:00
|
|
|
(list p1 p2))
|
2020-01-30 23:33:13 +00:00
|
|
|
|
|
|
|
(void
|
|
|
|
(write-string "{\"a\": 1, \"b\": [1,2,3]}" out1)))
|