diff --git a/cmd/api/v5/system.go b/cmd/api/v5/system.go index 21280b3..3b3ae7f 100644 --- a/cmd/api/v5/system.go +++ b/cmd/api/v5/system.go @@ -19,9 +19,9 @@ func (r Prefix) Url(prefix string) string { func (r Prefix) Tmpl() string { return `# total {{ len . }} -{{ps -18 "destination"}} {{ps -15 "nexthop"}} {{ps -16 "link"}} {{ps -15 "source"}} {{"metric"}} +{{ps -18 "destination"}} {{ps -15 "nexthop"}} {{ps -16 "link"}} {{ps -15 "source"}} {{ps -6 "metric" }} {{ "protocol" }} {{- range . }} -{{ps -18 .Prefix}} {{ps -15 .NextHop}} {{ps -16 .Link}} {{ps -15 .Source}} {{.Metric}} +{{ps -18 .Prefix}} {{ps -15 .NextHop}} {{ps -16 .Link}} {{ps -15 .Source}} {{pi -6 .Metric}} {{ .Protocol }} {{- end }} ` } diff --git a/pkg/api/system.go b/pkg/api/system.go index c034e41..cc2ca39 100755 --- a/pkg/api/system.go +++ b/pkg/api/system.go @@ -24,12 +24,13 @@ func (l Prefix) List(w http.ResponseWriter, r *http.Request) { routes, _ := libol.ListRoutes() for _, prefix := range routes { item := schema.PrefixRoute{ - Link: prefix.Link, - Metric: prefix.Priority, - Table: prefix.Table, - Source: prefix.Src, - NextHop: prefix.Gw, - Prefix: prefix.Dst, + Link: prefix.Link, + Metric: prefix.Priority, + Table: prefix.Table, + Source: prefix.Src, + NextHop: prefix.Gw, + Prefix: prefix.Dst, + Protocol: prefix.Protocol, } items = append(items, item) diff --git a/pkg/libol/nl_linux.go b/pkg/libol/nl_linux.go index ee8b39b..e7f8ed7 100755 --- a/pkg/libol/nl_linux.go +++ b/pkg/libol/nl_linux.go @@ -1,6 +1,7 @@ package libol import ( + "fmt" "net" "github.com/vishvananda/netlink" @@ -45,6 +46,44 @@ func GetLocalByGw(addr string) (net.IP, error) { return local, nil } +const ( + RTPROT_BGP = 0xba + RTPROT_BIRD = 0xc + RTPROT_KERNEL = 0x2 + RTPROT_BOOT = 0x3 + RTPROT_DHCP = 0x10 + RTPROT_ISIS = 0xbb + RTPROT_OSPF = 0xbc + RTPROT_REDIRECT = 0x1 + RTPROT_RIP = 0xbd + RTPROT_STATIC = 0x4 + RTPROT_UNSPEC = 0x0 + RTPROT_ZEBRA = 0xb +) + +func RouteProtocol(code int) string { + switch code { + case RTPROT_BGP: + return "bgp" + case RTPROT_KERNEL: + return "kernel" + case RTPROT_BOOT: + return "boot" + case RTPROT_DHCP: + return "dhcp" + case RTPROT_ISIS: + return "isis" + case RTPROT_OSPF: + return "ospf" + case RTPROT_RIP: + return "rip" + case RTPROT_STATIC: + return "static" + default: + return fmt.Sprintf("%d", code) + } +} + func ListRoutes() ([]Prefix, error) { routes, err := netlink.RouteList(nil, netlink.FAMILY_V4) if err != nil { @@ -53,17 +92,14 @@ func ListRoutes() ([]Prefix, error) { var data []Prefix for _, rte := range routes { - link, err := netlink.LinkByIndex(rte.LinkIndex) - if err != nil { - continue - } - entry := Prefix{ - Protocol: rte.Protocol, + Protocol: RouteProtocol(rte.Protocol), Priority: rte.Priority, - Link: link.Attrs().Name, } - + link, err := netlink.LinkByIndex(rte.LinkIndex) + if err == nil { + entry.Link = link.Attrs().Name + } if rte.Dst == nil { entry.Dst = "0.0.0.0/0" } else { @@ -71,13 +107,13 @@ func ListRoutes() ([]Prefix, error) { } if len(rte.Gw) == 0 { - entry.Gw = "" + entry.Gw = "0.0.0.0" } else { entry.Gw = rte.Gw.String() } if len(rte.Src) == 0 { - entry.Src = "" + entry.Src = "0.0.0.0" } else { entry.Src = rte.Src.String() } diff --git a/pkg/libol/struct.go b/pkg/libol/struct.go index 223d16a..fb705a9 100644 --- a/pkg/libol/struct.go +++ b/pkg/libol/struct.go @@ -5,7 +5,7 @@ type Prefix struct { Dst string Src string Gw string - Protocol int + Protocol string Priority int Table int } diff --git a/pkg/schema/network.go b/pkg/schema/network.go index f49cbd1..369c6c6 100755 --- a/pkg/schema/network.go +++ b/pkg/schema/network.go @@ -16,6 +16,7 @@ type PrefixRoute struct { Link string `json:"link,omitempty"` Table int `json:"table,omitempty"` Source string `json:"source,omitempty"` + Protocol string `json:"protocol,omitempty"` MultiPath []MultiPath `json:"multipath,omitempty"` }