Advertise more routes
This commit is contained in:
parent
1105c1d49a
commit
e20eb9be2d
3 changed files with 42 additions and 10 deletions
8
peer.go
8
peer.go
|
@ -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
|
||||||
|
|
15
route.go
15
route.go
|
@ -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
17
rtmp.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue