2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2014-09-15 00:44:56 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Scope is an enum representing a route scope.
|
|
|
|
type Scope uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE
|
|
|
|
SCOPE_SITE Scope = syscall.RT_SCOPE_SITE
|
|
|
|
SCOPE_LINK Scope = syscall.RT_SCOPE_LINK
|
|
|
|
SCOPE_HOST Scope = syscall.RT_SCOPE_HOST
|
|
|
|
SCOPE_NOWHERE Scope = syscall.RT_SCOPE_NOWHERE
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
2015-10-09 08:09:44 +00:00
|
|
|
type NextHopFlag int
|
|
|
|
|
|
|
|
const (
|
|
|
|
FLAG_ONLINK NextHopFlag = syscall.RTNH_F_ONLINK
|
|
|
|
FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
|
|
|
|
)
|
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
// Route represents a netlink route.
|
2014-09-01 03:27:34 +00:00
|
|
|
type Route struct {
|
2015-12-08 22:35:55 +00:00
|
|
|
LinkIndex int
|
|
|
|
ILinkIndex int
|
|
|
|
Scope Scope
|
|
|
|
Dst *net.IPNet
|
|
|
|
Src net.IP
|
|
|
|
Gw net.IP
|
|
|
|
Protocol int
|
|
|
|
Priority int
|
|
|
|
Table int
|
|
|
|
Type int
|
|
|
|
Tos int
|
|
|
|
Flags int
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Route) String() string {
|
2016-04-28 20:16:53 +00:00
|
|
|
return fmt.Sprintf("{Ifindex: %d Dst: %s Src: %s Gw: %s Flags: %s Table: %d}", r.LinkIndex, r.Dst,
|
|
|
|
r.Src, r.Gw, r.ListFlags(), r.Table)
|
2015-10-09 08:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) SetFlag(flag NextHopFlag) {
|
|
|
|
r.Flags |= int(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) ClearFlag(flag NextHopFlag) {
|
|
|
|
r.Flags &^= int(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
type flagString struct {
|
|
|
|
f NextHopFlag
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
var testFlags = []flagString{
|
2016-01-28 19:27:24 +00:00
|
|
|
{f: FLAG_ONLINK, s: "onlink"},
|
|
|
|
{f: FLAG_PERVASIVE, s: "pervasive"},
|
2015-10-09 08:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) ListFlags() []string {
|
|
|
|
var flags []string
|
|
|
|
for _, tf := range testFlags {
|
|
|
|
if r.Flags&int(tf.f) != 0 {
|
|
|
|
flags = append(flags, tf.s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2015-08-19 14:42:33 +00:00
|
|
|
|
|
|
|
// RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
|
|
|
|
type RouteUpdate struct {
|
|
|
|
Type uint16
|
|
|
|
Route
|
|
|
|
}
|