From dbd915351316ec2cc899db1757b3ae13c809ea0b Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 18 Apr 2022 13:53:18 +0200 Subject: [PATCH] Get default router and gateways, IPv4 & IPv6 --- cmd/jailhost.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 cmd/jailhost.go diff --git a/cmd/jailhost.go b/cmd/jailhost.go new file mode 100644 index 0000000..dbef5ac --- /dev/null +++ b/cmd/jailhost.go @@ -0,0 +1,109 @@ +package cmd + +import ( + "fmt" + "net" + "golang.org/x/net/route" +) + +var defaultRoute4 = [4]byte{0, 0, 0, 0} +var defaultRoute6 = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} +var local6 = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + +func Inet4AddrToString(ip4 route.Inet4Addr) string { + return fmt.Sprintf("%v.%v.%v.%v", ip4.IP[0], ip4.IP[1], ip4.IP[2], ip4.IP[3]) +} + +func Inet6AddrToString(ip6 route.Inet6Addr) string { + return fmt.Sprintf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + ip6.IP[0], ip6.IP[1], ip6.IP[2], ip6.IP[3], ip6.IP[4], ip6.IP[5], ip6.IP[6], ip6.IP[7], + ip6.IP[8], ip6.IP[9], ip6.IP[10], ip6.IP[11], ip6.IP[12], ip6.IP[13], ip6.IP[14], ip6.IP[15]) +} + +/***************************************************************************** +* Initialize default_interface, default_gatway4, default_gateway6 +*****************************************************************************/ +func (jh *JailHost) InitNetworkProperties() { + rib, _ := route.FetchRIB(0, route.RIBTypeRoute, 0) + messages, err := route.ParseRIB(route.RIBTypeRoute, rib) + if err != nil { + panic(err) + } + + for _, message := range messages { + route_message := message.(*route.RouteMessage) + addresses := route_message.Addrs + card_index := route_message.Index + + if addresses[0].Family() == 2 { + var destination4, gateway4 *route.Inet4Addr + ok := false + + if destination4, ok = addresses[0].(*route.Inet4Addr); !ok { + continue + } + + if gateway4, ok = addresses[1].(*route.Inet4Addr); !ok { + continue + } + + if destination4 == nil || gateway4 == nil { + continue + } + + if destination4.IP == defaultRoute4 { + card, _ := net.InterfaceByIndex(card_index) + //fmt.Printf("Default IPv4 gateway is %v on card %s\n", Inet4AddrToString(*gateway4), card.Name) + jh.default_interface = card.Name + jh.default_gateway4 = Inet4AddrToString(*gateway4) + } + } else if addresses[0].Family() == 28 { + var destination6, gateway6 *route.Inet6Addr + ok := false + + if destination6, ok = addresses[0].(*route.Inet6Addr); !ok { + continue + } + + if gateway6, ok = addresses[1].(*route.Inet6Addr); !ok { + continue + } + + if destination6 == nil || gateway6 == nil { + continue + } + + if destination6.IP == defaultRoute6 && gateway6.IP != local6 { + card, _ := net.InterfaceByIndex(card_index) + //fmt.Printf("Default IPv6 gateway is %v on card %s\n", Inet6AddrToString(*gateway6), card.Name) + jh.default_interface = card.Name + jh.default_gateway6 = Inet6AddrToString(*gateway6) + } + } + } +} + + +func (jh *JailHost) GetDefaultInterface() string { + if len(jh.default_interface) == 0 { + jh.InitNetworkProperties() + } + return jh.default_interface +} + + +func (jh *JailHost) GetDefaultGateway4() string { + if len(jh.default_gateway4) == 0 { + jh.InitNetworkProperties() + } + return jh.default_gateway4 +} + + +func(jh *JailHost) GetDefaultGateway6() string { + if len(jh.default_gateway6) == 0 { + jh.InitNetworkProperties() + } + return jh.default_gateway6 +} +