allow RouteDel to work on kernel routes

This commit is contained in:
Vishvananda Ishaya 2015-07-27 19:29:45 -07:00
parent ea0402b9db
commit 27f66da7b4
2 changed files with 12 additions and 4 deletions

View File

@ -20,6 +20,15 @@ func NewRtMsg() *RtMsg {
} }
} }
func NewRtDelMsg() *RtMsg {
return &RtMsg{
RtMsg: syscall.RtMsg{
Table: syscall.RT_TABLE_MAIN,
Scope: syscall.RT_SCOPE_NOWHERE,
},
}
}
func (msg *RtMsg) Len() int { func (msg *RtMsg) Len() int {
return syscall.SizeofRtMsg return syscall.SizeofRtMsg
} }

View File

@ -14,22 +14,21 @@ import (
// Equivalent to: `ip route add $route` // Equivalent to: `ip route add $route`
func RouteAdd(route *Route) error { func RouteAdd(route *Route) error {
req := nl.NewNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK) req := nl.NewNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
return routeHandle(route, req) return routeHandle(route, req, nl.NewRtMsg())
} }
// RouteAdd will delete a route from the system. // RouteAdd will delete a route from the system.
// Equivalent to: `ip route del $route` // Equivalent to: `ip route del $route`
func RouteDel(route *Route) error { func RouteDel(route *Route) error {
req := nl.NewNetlinkRequest(syscall.RTM_DELROUTE, syscall.NLM_F_ACK) req := nl.NewNetlinkRequest(syscall.RTM_DELROUTE, syscall.NLM_F_ACK)
return routeHandle(route, req) return routeHandle(route, req, nl.NewRtDelMsg())
} }
func routeHandle(route *Route, req *nl.NetlinkRequest) error { func routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil { if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil {
return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil") return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
} }
msg := nl.NewRtMsg()
msg.Scope = uint8(route.Scope) msg.Scope = uint8(route.Scope)
family := -1 family := -1
var rtAttrs []*nl.RtAttr var rtAttrs []*nl.RtAttr