2
0

routes_freebsd.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package local
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "github.com/c-robinson/iplib"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/netclient/ncutils"
  9. )
  10. // GetDefaultRoute - Gets the default route (ip and interface) on a freebsd machine
  11. func GetDefaultRoute() (string, string, error) {
  12. var ipaddr string
  13. var iface string
  14. var err error
  15. output, err := ncutils.RunCmd("route show default", true)
  16. if err != nil {
  17. return ipaddr, iface, err
  18. }
  19. outFormatted := strings.ReplaceAll(output, "\n", "")
  20. if !strings.Contains(outFormatted, "default") && !strings.Contains(outFormatted, "interface:") {
  21. return ipaddr, iface, fmt.Errorf("could not find default gateway")
  22. }
  23. outputSlice := strings.Split(string(outFormatted), " ")
  24. for i, outString := range outputSlice {
  25. if outString == "gateway:" {
  26. ipaddr = outputSlice[i+1]
  27. }
  28. if outString == "interface:" {
  29. iface = outputSlice[i+1]
  30. }
  31. }
  32. return ipaddr, iface, err
  33. }
  34. func setRoute(iface string, addr *net.IPNet, address string) error {
  35. _, err := ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
  36. return err
  37. }
  38. // SetExplicitRoute - sets route via explicit ip address
  39. func SetExplicitRoute(iface string, destination *net.IPNet, gateway string) error {
  40. _, err := ncutils.RunCmd("route add "+destination.String()+" "+gateway, false)
  41. return err
  42. }
  43. func deleteRoute(iface string, addr *net.IPNet, address string) error {
  44. var err error
  45. _, _ = ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, false)
  46. return err
  47. }
  48. func setCidr(iface, address string, addr *net.IPNet) {
  49. if iplib.Version(addr.IP) == 4 {
  50. ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
  51. } else if iplib.Version(addr.IP) == 6 {
  52. ncutils.RunCmd("route add -net -inet6 "+addr.String()+" -interface "+iface, false)
  53. } else {
  54. logger.Log(1, "could not parse address: "+addr.String())
  55. }
  56. ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, false)
  57. }
  58. func removeCidr(iface string, addr *net.IPNet, address string) {
  59. ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, false)
  60. }