From 612d2298ab67c3a2ee85147460217b48564b8207 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Thu, 16 Jan 2014 23:41:29 -0800 Subject: [PATCH 1/3] netlink: add default Route to NetworkGetRoutes Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: proppy) --- netlink/netlink_linux.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/netlink/netlink_linux.go b/netlink/netlink_linux.go index 9a937d1..a65bdaf 100644 --- a/netlink/netlink_linux.go +++ b/netlink/netlink_linux.go @@ -471,9 +471,16 @@ func NetworkLinkAdd(name string, linkType string) error { return s.HandleAck(wb.Seq) } +// A Route is a subnet associated with the interface to reach it. +type Route struct { + *net.IPNet + Iface *net.Interface + Default bool +} + // Returns an array of IPNet for all the currently routed subnets on ipv4 // This is similar to the first column of "ip route" output -func NetworkGetRoutes() ([]*net.IPNet, error) { +func NetworkGetRoutes() ([]Route, error) { native := nativeEndian() s, err := getNetlinkSocket() @@ -496,7 +503,7 @@ func NetworkGetRoutes() ([]*net.IPNet, error) { return nil, err } - res := make([]*net.IPNet, 0) + res := make([]Route, 0) done: for { @@ -525,8 +532,7 @@ done: continue } - var iface *net.Interface = nil - var ipNet *net.IPNet = nil + var r Route msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0])) @@ -546,8 +552,8 @@ done: } if msg.Dst_len == 0 { - // Ignore default routes - continue + // Default routes + r.Default = true } attrs, err := syscall.ParseNetlinkRouteAttr(&m) @@ -558,18 +564,17 @@ done: switch attr.Attr.Type { case syscall.RTA_DST: ip := attr.Value - ipNet = &net.IPNet{ + r.IPNet = &net.IPNet{ IP: ip, Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)), } case syscall.RTA_OIF: index := int(native.Uint32(attr.Value[0:4])) - iface, _ = net.InterfaceByIndex(index) - _ = iface + r.Iface, _ = net.InterfaceByIndex(index) } } - if ipNet != nil { - res = append(res, ipNet) + if r.Default || r.IPNet != nil { + res = append(res, r) } } } From abbdc2b6ce4577e854345b810d49df28d1ca2047 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 17 Jan 2014 13:12:08 -0800 Subject: [PATCH 2/3] netlink: move Route type to common arch file Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: proppy) --- netlink/netlink.go | 15 +++++++++++++++ netlink/netlink_linux.go | 7 ------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 netlink/netlink.go diff --git a/netlink/netlink.go b/netlink/netlink.go new file mode 100644 index 0000000..5098b4b --- /dev/null +++ b/netlink/netlink.go @@ -0,0 +1,15 @@ +// Packet netlink provide access to low level Netlink sockets and messages. +// +// Actual implementations are in: +// netlink_linux.go +// netlink_darwin.go +package netlink + +import "net" + +// A Route is a subnet associated with the interface to reach it. +type Route struct { + *net.IPNet + Iface *net.Interface + Default bool +} diff --git a/netlink/netlink_linux.go b/netlink/netlink_linux.go index a65bdaf..ab572e3 100644 --- a/netlink/netlink_linux.go +++ b/netlink/netlink_linux.go @@ -471,13 +471,6 @@ func NetworkLinkAdd(name string, linkType string) error { return s.HandleAck(wb.Seq) } -// A Route is a subnet associated with the interface to reach it. -type Route struct { - *net.IPNet - Iface *net.Interface - Default bool -} - // Returns an array of IPNet for all the currently routed subnets on ipv4 // This is similar to the first column of "ip route" output func NetworkGetRoutes() ([]Route, error) { From f442c504c3e373e516dcb90b0edd61e730b52cd7 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 17 Jan 2014 16:30:27 -0800 Subject: [PATCH 3/3] netlink: make darwin happy Docker-DCO-1.1-Signed-off-by: Johan Euphrosine (github: proppy) --- netlink/netlink_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlink/netlink_darwin.go b/netlink/netlink_darwin.go index dcc60b6..298508a 100644 --- a/netlink/netlink_darwin.go +++ b/netlink/netlink_darwin.go @@ -5,7 +5,7 @@ import ( "net" ) -func NetworkGetRoutes() ([]*net.IPNet, error) { +func NetworkGetRoutes() ([]Route, error) { return nil, fmt.Errorf("Not implemented") }