Advertise more routes

This commit is contained in:
Josh Deprez 2024-04-13 15:47:58 +10:00
parent 1105c1d49a
commit e20eb9be2d
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
3 changed files with 42 additions and 10 deletions

View file

@ -277,7 +277,13 @@ func (p *peer) handle(ctx context.Context) error {
log.Printf("Learned about these networks: %v", pkt.Networks) log.Printf("Learned about these networks: %v", pkt.Networks)
for _, nt := range pkt.Networks { for _, nt := range pkt.Networks {
upsertRoutes(ddp.Network(nt.RangeStart), ddp.Network(nt.RangeEnd), p, nt.Distance) upsertRoutes(
nt.Extended,
ddp.Network(nt.RangeStart),
ddp.Network(nt.RangeEnd),
p,
nt.Distance,
)
} }
// TODO: track which networks we don't have zone info for, and // TODO: track which networks we don't have zone info for, and

View file

@ -11,6 +11,9 @@ import (
) )
type route struct { type route struct {
extended bool
netStart ddp.Network
netEnd ddp.Network
peer *peer peer *peer
metric uint8 metric uint8
last time.Time last time.Time
@ -19,6 +22,9 @@ type route struct {
var ( var (
routingTableMu sync.Mutex routingTableMu sync.Mutex
routingTable = make(map[ddp.Network][]*route) routingTable = make(map[ddp.Network][]*route)
allRoutesMu sync.Mutex
allRoutes = make(map[*route]struct{})
) )
func lookupRoute(network ddp.Network) *route { func lookupRoute(network ddp.Network) *route {
@ -32,16 +38,23 @@ func lookupRoute(network ddp.Network) *route {
return rs[0] return rs[0]
} }
func upsertRoutes(netStart, netEnd ddp.Network, peer *peer, metric uint8) error { func upsertRoutes(extended bool, netStart, netEnd ddp.Network, peer *peer, metric uint8) error {
if netStart > netEnd { if netStart > netEnd {
return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd) return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd)
} }
r := &route{ r := &route{
extended: extended,
netStart: netStart,
netEnd: netEnd,
peer: peer, peer: peer,
metric: metric, metric: metric,
last: time.Now(), last: time.Now(),
} }
allRoutesMu.Lock()
allRoutes[r] = struct{}{}
allRoutesMu.Unlock()
routingTableMu.Lock() routingTableMu.Lock()
defer routingTableMu.Unlock() defer routingTableMu.Unlock()
for n := netStart; n <= netEnd; n++ { for n := netStart; n <= netEnd; n++ {

17
rtmp.go
View file

@ -198,7 +198,7 @@ func (m *RTMPMachine) broadcastData(myAddr aarp.AddrPair) error {
} }
func (m *RTMPMachine) dataPacket(myAddr ddp.Addr) *rtmp.DataPacket { func (m *RTMPMachine) dataPacket(myAddr ddp.Addr) *rtmp.DataPacket {
return &rtmp.DataPacket{ p := &rtmp.DataPacket{
RouterAddr: myAddr, RouterAddr: myAddr,
Extended: true, Extended: true,
NetworkTuples: []rtmp.NetworkTuple{ NetworkTuples: []rtmp.NetworkTuple{
@ -213,5 +213,18 @@ func (m *RTMPMachine) dataPacket(myAddr ddp.Addr) *rtmp.DataPacket {
}, },
}, },
} }
// TODO: append more networks! implement a route table! allRoutesMu.Lock()
defer allRoutesMu.Unlock()
for rt := range allRoutes {
if rt.peer == nil {
continue
}
p.NetworkTuples = append(p.NetworkTuples, rtmp.NetworkTuple{
Extended: rt.extended,
RangeStart: rt.netStart,
RangeEnd: rt.netEnd,
Distance: rt.metric,
})
}
return p
} }