netmaker/netclient/local/routes_freebsd.go

69 lines
2 KiB
Go
Raw Normal View History

2022-02-04 08:55:12 +08:00
package local
import (
2022-08-23 23:37:59 +08:00
"fmt"
2022-02-04 08:55:12 +08:00
"net"
2022-08-23 23:37:59 +08:00
"strings"
2022-02-04 08:55:12 +08:00
2022-05-27 05:39:18 +08:00
"github.com/c-robinson/iplib"
2022-04-26 01:22:56 +08:00
"github.com/gravitl/netmaker/logger"
2022-02-04 08:55:12 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
2022-08-23 04:44:04 +08:00
// GetDefaultRoute - Gets the default route (ip and interface) on a freebsd machine
func GetDefaultRoute() (string, string, error) {
var ipaddr string
var iface string
var err error
output, err := ncutils.RunCmd("route show default", true)
if err != nil {
return ipaddr, iface, err
}
outFormatted := strings.ReplaceAll(output, "\n", "")
if !strings.Contains(outFormatted, "default") && !strings.Contains(outFormatted, "interface:") {
return ipaddr, iface, fmt.Errorf("could not find default gateway")
}
outputSlice := strings.Split(string(outFormatted), " ")
for i, outString := range outputSlice {
if outString == "gateway:" {
ipaddr = outputSlice[i+1]
}
if outString == "interface:" {
iface = outputSlice[i+1]
}
}
return ipaddr, iface, err
}
2022-02-04 08:55:12 +08:00
func setRoute(iface string, addr *net.IPNet, address string) error {
2022-08-23 23:37:59 +08:00
_, err := ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
return err
}
func SetExplicitRoute(iface string, destination *net.IPNet, gateway string) error {
_, err := ncutils.RunCmd("route add "+destination.String()+" "+gateway, false)
2022-02-04 08:55:12 +08:00
return err
}
func deleteRoute(iface string, addr *net.IPNet, address string) error {
var err error
2022-02-08 00:29:10 +08:00
_, _ = ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, false)
2022-02-04 08:55:12 +08:00
return err
}
func setCidr(iface, address string, addr *net.IPNet) {
2022-05-27 05:39:18 +08:00
if iplib.Version(addr.IP) == 4 {
2022-04-20 04:18:03 +08:00
ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
2022-05-27 05:39:18 +08:00
} else if iplib.Version(addr.IP) == 6 {
2022-04-20 04:18:03 +08:00
ncutils.RunCmd("route add -net -inet6 "+addr.String()+" -interface "+iface, false)
} else {
logger.Log(1, "could not parse address: "+addr.String())
}
2022-02-06 04:00:26 +08:00
ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
}
func removeCidr(iface string, addr *net.IPNet, address string) {
ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, false)
}