1
0
forked from extern/smegmesh
smegmesh/pkg/mesh/route.go

94 lines
1.7 KiB
Go
Raw Normal View History

package mesh
import (
2023-10-27 18:49:18 +02:00
"github.com/tim-beatham/wgmesh/pkg/ip"
"github.com/tim-beatham/wgmesh/pkg/lib"
2023-10-27 18:49:18 +02:00
logging "github.com/tim-beatham/wgmesh/pkg/log"
)
type RouteManager interface {
UpdateRoutes() error
RemoveRoutes(meshId string) error
}
type RouteManagerImpl struct {
meshManager MeshManager
}
func (r *RouteManagerImpl) UpdateRoutes() error {
2023-11-05 19:03:58 +01:00
meshes := r.meshManager.GetMeshes()
2023-10-27 18:49:18 +02:00
ulaBuilder := new(ip.ULABuilder)
2023-10-27 18:49:18 +02:00
for _, mesh1 := range meshes {
self, err := r.meshManager.GetSelf(mesh1.GetMeshId())
if err != nil {
return err
}
pubKey, err := self.GetPublicKey()
if err != nil {
return err
}
routes, err := mesh1.GetRoutes(pubKey.String())
if err != nil {
return err
}
2023-10-27 18:49:18 +02:00
for _, mesh2 := range meshes {
if mesh1 == mesh2 {
continue
}
2023-10-27 18:49:18 +02:00
ipNet, err := ulaBuilder.GetIPNet(mesh2.GetMeshId())
2023-10-27 18:49:18 +02:00
if err != nil {
logging.Log.WriteErrorf(err.Error())
return err
}
err = mesh2.AddRoutes(NodeID(self), append(lib.MapValues(routes), &RouteStub{
Destination: ipNet,
HopCount: 0,
Path: make([]string, 0),
})...)
2023-10-27 18:49:18 +02:00
if err != nil {
return err
}
}
}
return nil
}
// removeRoutes: removes all meshes we are no longer a part of
func (r *RouteManagerImpl) RemoveRoutes(meshId string) error {
ulaBuilder := new(ip.ULABuilder)
meshes := r.meshManager.GetMeshes()
ipNet, err := ulaBuilder.GetIPNet(meshId)
if err != nil {
return err
}
for _, mesh1 := range meshes {
self, err := r.meshManager.GetSelf(meshId)
if err != nil {
return err
}
2023-11-24 18:49:06 +01:00
mesh1.RemoveRoutes(NodeID(self), ipNet.String())
}
return nil
}
2023-11-05 19:03:58 +01:00
func NewRouteManager(m MeshManager) RouteManager {
return &RouteManagerImpl{meshManager: m}
}