netmaker/netclient/local/routes_linux.go
2022-04-25 13:10:54 -04:00

45 lines
1.3 KiB
Go

package local
import (
//"github.com/davecgh/go-spew/spew"
"fmt"
"net"
"strings"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/seancfoley/ipaddress-go/ipaddr"
)
func setRoute(iface string, addr *net.IPNet, address string) error {
out, err := ncutils.RunCmd(fmt.Sprintf("ip route get %s", addr.IP.String()), false)
if err != nil || !strings.Contains(out, iface) {
_, err = ncutils.RunCmd(fmt.Sprintf("ip route add %s dev %s", addr.String(), iface), false)
}
return err
}
func deleteRoute(iface string, addr *net.IPNet, address string) error {
var err error
out, _ := ncutils.RunCmd(fmt.Sprintf("ip route get %s", addr.IP.String()), false)
if strings.Contains(out, iface) {
_, err = ncutils.RunCmd(fmt.Sprintf("ip route del %s dev %s", addr.String(), iface), false)
}
return err
}
func setCidr(iface, address string, addr *net.IPNet) {
cidr := ipaddr.NewIPAddressString(addr.String()).GetAddress()
if cidr.IsIPv4() {
ncutils.RunCmd("ip -4 route add "+addr.String()+" dev "+iface, false)
} else if cidr.IsIPv6() {
ncutils.RunCmd("ip -6 route add "+addr.String()+" dev "+iface, false)
} else {
logger.Log(1, "could not parse address: "+addr.String())
}
}
func removeCidr(iface string, addr *net.IPNet, address string) {
ncutils.RunCmd("ip route delete "+addr.String()+" dev "+iface, false)
}