Skip to content

Commit

Permalink
RouteGetWithOptions: Add source address option
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Herms authored and aboch committed Nov 22, 2020
1 parent c7261bd commit ffba2c8
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ func deserializeRoute(m []byte) (Route, error) {
// RouteGetWithOptions
type RouteGetOptions struct {
VrfName string
SrcAddr net.IP
}

// RouteGetWithOptions gets a route to a specific destination from the host system.
Expand Down Expand Up @@ -1053,23 +1054,40 @@ func (h *Handle) RouteGetWithOptions(destination net.IP, options *RouteGetOption
msg := &nl.RtMsg{}
msg.Family = uint8(family)
msg.Dst_len = bitlen
if options != nil && options.SrcAddr != nil {
msg.Src_len = bitlen
}
msg.Flags = unix.RTM_F_LOOKUP_TABLE
req.AddData(msg)

rtaDst := nl.NewRtAttr(unix.RTA_DST, destinationData)
req.AddData(rtaDst)

if options != nil {
link, err := LinkByName(options.VrfName)
if err != nil {
return nil, err
if options.VrfName != "" {
link, err := LinkByName(options.VrfName)
if err != nil {
return nil, err
}
var (
b = make([]byte, 4)
native = nl.NativeEndian()
)
native.PutUint32(b, uint32(link.Attrs().Index))

req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
}
var (
b = make([]byte, 4)
native = nl.NativeEndian()
)
native.PutUint32(b, uint32(link.Attrs().Index))

req.AddData(nl.NewRtAttr(unix.RTA_OIF, b))
if options.SrcAddr != nil {
var srcAddr []byte
if family == FAMILY_V4 {
srcAddr = options.SrcAddr.To4()
} else {
srcAddr = options.SrcAddr.To16()
}

req.AddData(nl.NewRtAttr(unix.RTA_SRC, srcAddr))
}
}

msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWROUTE)
Expand Down

0 comments on commit ffba2c8

Please sign in to comment.