update existing EtherTalk routes in upsert

This commit is contained in:
Josh Deprez 2024-04-29 18:02:16 +10:00
parent 214c72514b
commit 545087fbe8
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
2 changed files with 27 additions and 4 deletions

View file

@ -456,6 +456,9 @@ func main() {
if err := route.EtherTalkPeer.Forward(ctx, ddpkt); err != nil {
log.Printf("DDP: Couldn't forward packet to EtherTalk peer: %v", err)
}
default:
log.Print("DDP: no forwarding mechanism for route; dropping packet")
}
continue
}

View file

@ -31,6 +31,7 @@ type Route struct {
NetStart ddp.Network
NetEnd ddp.Network
Distance uint8
LastSeen time.Time
// Exactly one of the following should be set
@ -133,6 +134,29 @@ func (rt *RoutingTable) UpsertEthRoute(peer *EtherTalkPeer, extended bool, netSt
return fmt.Errorf("invalid network range [%d, %d] for nonextended network", netStart, netEnd)
}
rt.mu.Lock()
defer rt.mu.Unlock()
// Update?
for r := range rt.routes {
if r.EtherTalkPeer != peer {
continue
}
if r.Extended != extended {
continue
}
if r.NetStart != netStart {
continue
}
if r.NetEnd != netEnd {
continue
}
r.Distance = metric
r.LastSeen = time.Now()
return nil
}
// Insert.
r := &Route{
Extended: extended,
NetStart: netStart,
@ -141,10 +165,6 @@ func (rt *RoutingTable) UpsertEthRoute(peer *EtherTalkPeer, extended bool, netSt
LastSeen: time.Now(),
EtherTalkPeer: peer,
}
rt.mu.Lock()
defer rt.mu.Unlock()
// TODO: update if present rather than insert
rt.routes[r] = struct{}{}
return nil
}