2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
2023-12-08 12:49:24 +01:00
|
|
|
"net"
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ip"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/lib"
|
2023-10-26 17:53:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RouteManager interface {
|
|
|
|
UpdateRoutes() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteManagerImpl struct {
|
2023-11-25 04:15:58 +01:00
|
|
|
meshManager MeshManager
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RouteManagerImpl) UpdateRoutes() error {
|
2023-11-05 19:03:58 +01:00
|
|
|
meshes := r.meshManager.GetMeshes()
|
2023-12-10 16:10:36 +01:00
|
|
|
routes := make(map[string][]Route)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
for _, mesh1 := range meshes {
|
2023-12-12 12:58:47 +01:00
|
|
|
if !*mesh1.GetConfiguration().AdvertiseRoutes {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
self, err := mesh1.GetNode(r.meshManager.GetPublicKey().String())
|
2023-11-25 04:15:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
if _, ok := routes[mesh1.GetMeshId()]; !ok {
|
|
|
|
routes[mesh1.GetMeshId()] = make([]Route, 0)
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
if *mesh1.GetConfiguration().AdvertiseDefaultRoute {
|
|
|
|
_, ipv6Default, _ := net.ParseCIDR("::/0")
|
2023-11-25 04:15:58 +01:00
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
defaultRoute := &RouteStub{
|
|
|
|
Destination: ipv6Default,
|
|
|
|
HopCount: 0,
|
|
|
|
Path: []string{mesh1.GetMeshId()},
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh1.AddRoutes(NodeID(self), defaultRoute)
|
|
|
|
routes[mesh1.GetMeshId()] = append(routes[mesh1.GetMeshId()], defaultRoute)
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
routeMap, err := mesh1.GetRoutes(NodeID(self))
|
2023-12-08 21:02:57 +01:00
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-12-08 12:49:24 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
for _, mesh2 := range meshes {
|
2023-12-10 16:10:36 +01:00
|
|
|
routeValues, ok := routes[mesh2.GetMeshId()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
routeValues = make([]Route, 0)
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
if mesh1 == mesh2 {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
mesh1IpNet, _ := (&ip.ULABuilder{}).GetIPNet(mesh1.GetMeshId())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
routeValues = append(routeValues, &RouteStub{
|
|
|
|
Destination: mesh1IpNet,
|
|
|
|
HopCount: 0,
|
|
|
|
Path: []string{mesh1.GetMeshId()},
|
|
|
|
})
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
routeValues = append(routeValues, lib.MapValues(routeMap)...)
|
|
|
|
mesh2IpNet, _ := (&ip.ULABuilder{}).GetIPNet(mesh2.GetMeshId())
|
|
|
|
routeValues = lib.Filter(routeValues, func(r Route) bool {
|
|
|
|
pathNotMesh := func(s string) bool {
|
|
|
|
return s == mesh2.GetMeshId()
|
|
|
|
}
|
2023-12-08 12:49:24 +01:00
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
// Remove any potential routing loops
|
|
|
|
return !r.GetDestination().IP.Equal(mesh2IpNet.IP) &&
|
|
|
|
!lib.Contains(r.GetPath()[1:], pathNotMesh)
|
2023-12-10 16:10:36 +01:00
|
|
|
})
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
routes[mesh2.GetMeshId()] = routeValues
|
2023-10-27 18:49:18 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
// Calculate the set different of each, working out routes to remove and to keep.
|
|
|
|
for meshId, meshRoutes := range routes {
|
2023-12-30 01:44:57 +01:00
|
|
|
mesh := meshes[meshId]
|
|
|
|
|
|
|
|
self, err := mesh.GetNode(r.meshManager.GetPublicKey().String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
toRemove := make([]Route, 0)
|
2023-12-30 01:44:57 +01:00
|
|
|
prevRoutes, err := mesh.GetRoutes(NodeID(self))
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
for _, route := range prevRoutes {
|
|
|
|
if !lib.Contains(meshRoutes, func(r Route) bool {
|
|
|
|
return RouteEquals(r, route)
|
|
|
|
}) {
|
|
|
|
toRemove = append(toRemove, route)
|
|
|
|
}
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
mesh.RemoveRoutes(NodeID(self), toRemove...)
|
|
|
|
mesh.AddRoutes(NodeID(self), meshRoutes...)
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
2023-12-10 16:10:36 +01:00
|
|
|
|
2023-11-07 20:48:53 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-11 00:35:00 +01:00
|
|
|
func NewRouteManager(m MeshManager) RouteManager {
|
|
|
|
return &RouteManagerImpl{meshManager: m}
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|