Implement actual dump response

This commit is contained in:
xenia 2020-02-01 22:21:25 -05:00
parent d24df65c34
commit 222d9ce31c
3 changed files with 33 additions and 17 deletions

View File

@ -116,6 +116,12 @@
'ASPath as
'origin (symbol->string org))]
[(msg:table _ _ networks)
(for/list ([entry (in-list networks)])
(hash 'network (ip->string (subnet-ip (first entry)))
'netmask (ip->string (netmask-ip (subnet-mask (first entry))))
'peer (ip->string (second entry))))]
[_ (error 'msg->jsexpr
(format "unimplemented ~a" (msg-type msg)))])))

View File

@ -83,13 +83,12 @@
visited])))
(define (rt-flatten node)
(if node
(let* ([e1 (rt-node-edge0 node)]
[e2 (rt-node-edge1 node)]
[data (rt-node-data node)]
[rst (append (rt-flatten e1) (rt-flatten e2))])
(if (equal? data empty-node-data) rst (cons data rst)))
'()))
(define (flatten-edge e) (if e (rt-flatten (rt-edge-target e)) '()))
(let* ([e1 (rt-node-edge0 node)]
[e2 (rt-node-edge1 node)]
[data (rt-node-data node)]
[rst (apply append (map flatten-edge (list e1 e2)))])
(if (equal? data empty-node-data) rst (cons data rst))))
; (define test (make-rt))
; (define (test-insert! x)

31
router
View File

@ -1,6 +1,7 @@
#!/usr/bin/env racket
#lang racket
; vim:syntax=racket
; vim:ft=racket
(require json
"iputil.rkt"
@ -29,6 +30,10 @@
(lambda (rst) (cons (cons peer r) rst))
(lambda () '())))
;; IP -> IP
(define (peer-ip->own-ip p)
(sub1 p))
(define (route< r1 r2)
#|
(route< r1 r2) iff "r1 wins"
@ -66,9 +71,13 @@
;; Router -> Msg
;; Dumps routing table
(define (router-dump rt src dst)
(msg:table (sub1 src) src
(map (lambda (route) (list (route-subnet route) (route-nexthop route)))
(rt-flatten rt))))
(define routing-table (rt-flatten rt))
(msg:table (peer-ip->own-ip src) src
(for/fold ([entries '()]) ([routes (in-list routing-table)])
(append entries (map (lambda (route)
(list (route-subnet (cdr route))
(route-nexthop (cdr route))))
routes)))))
;; Str [Listof Peer-Conn] -> Void
;; --
@ -103,8 +112,8 @@
(make-router))
(let loop ()
(match-define (list src-peer-conn msg) (channel-get mail))
(printf "====\nfrom ~a:\n~s\n" src-peer-conn msg)
(match-define (list src-peer msg) (channel-get mail))
(printf "====\nfrom ~a:\n~s\n" src-peer msg)
;; [Listof [Cons Peer-Conn Msg]]
(define to-send
@ -116,19 +125,21 @@
(ip->peer src))
(rt-dump router)
(for/list ([pc (in-list peer-conns)]
#:when (not (eq? pc src-peer-conn)))
(cons pc msg))]
#:when (not (eq? (peer-conn-info pc) src-peer)))
(match-define (peer-conn peer _ _) pc)
(cons pc (msg:update (peer-ip->own-ip (peer-ip peer)) (peer-ip peer) r)))]
[(msg:data src dst data)
(match (router-find-best router dst)
[#f
(list (cons src-peer-conn
(msg:no-route (sub1 src)
(list (cons (peer->peer-conn src-peer)
(msg:no-route (peer-ip->own-ip src)
src)))]
[dst-peer
(list (cons (peer->peer-conn dst-peer)
msg))])]
[(msg:dump src dst)
(list (cons (peer->peer-conn src-peer) (router-dump router src dst)))]
[_
(printf "----\nignored\n")
'()]))