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)
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

View file

@ -11,14 +11,20 @@ import (
)
type route struct {
peer *peer
metric uint8
last time.Time
extended bool
netStart ddp.Network
netEnd ddp.Network
peer *peer
metric uint8
last time.Time
}
var (
routingTableMu sync.Mutex
routingTable = make(map[ddp.Network][]*route)
allRoutesMu sync.Mutex
allRoutes = make(map[*route]struct{})
)
func lookupRoute(network ddp.Network) *route {
@ -32,16 +38,23 @@ func lookupRoute(network ddp.Network) *route {
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 {
return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd)
}
r := &route{
peer: peer,
metric: metric,
last: time.Now(),
extended: extended,
netStart: netStart,
netEnd: netEnd,
peer: peer,
metric: metric,
last: time.Now(),
}
allRoutesMu.Lock()
allRoutes[r] = struct{}{}
allRoutesMu.Unlock()
routingTableMu.Lock()
defer routingTableMu.Unlock()
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 {
return &rtmp.DataPacket{
p := &rtmp.DataPacket{
RouterAddr: myAddr,
Extended: true,
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
}