2023-10-25 19:34:38 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/lib"
|
2023-11-25 04:15:58 +01:00
|
|
|
"golang.org/x/sys/unix"
|
2023-10-25 19:34:38 +02:00
|
|
|
)
|
|
|
|
|
2024-01-04 14:12:07 +01:00
|
|
|
// RouteInstaller: install the routes to the given interface
|
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
|
|
|
}
|
|
|
|
|
2024-01-04 14:12:07 +01:00
|
|
|
defer rtnl.Close()
|
|
|
|
|
2023-12-10 16:10:36 +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{}
|
|
|
|
}
|