2023-10-25 19:34:38 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2023-11-25 04:15:58 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
|
|
"golang.org/x/sys/unix"
|
2023-10-25 19:34:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RouteInstaller interface {
|
2023-11-25 04:15:58 +01:00
|
|
|
InstallRoutes(devName string, routes ...lib.Route) error
|
2023-10-25 19:34:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type RouteInstallerImpl struct{}
|
|
|
|
|
|
|
|
// InstallRoutes: installs a route into the routing table
|
2023-11-25 04:15:58 +01:00
|
|
|
func (r *RouteInstallerImpl) InstallRoutes(devName string, routes ...lib.Route) error {
|
|
|
|
rtnl, err := lib.NewRtNetlinkConfig()
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-10-25 19:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
err = rtnl.DeleteRoutes(devName, unix.AF_INET6, routes...)
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
for _, route := range routes {
|
|
|
|
err := rtnl.AddRoute(devName, route)
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouteInstaller() RouteInstaller {
|
|
|
|
return &RouteInstallerImpl{}
|
|
|
|
}
|