route.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // Scope is an enum representing a route scope.
  7. type Scope uint8
  8. type NextHopFlag int
  9. // Route represents a netlink route.
  10. type Route struct {
  11. LinkIndex int
  12. ILinkIndex int
  13. Scope Scope
  14. Dst *net.IPNet
  15. Src net.IP
  16. Gw net.IP
  17. MultiPath []*NexthopInfo
  18. Protocol int
  19. Priority int
  20. Table int
  21. Type int
  22. Tos int
  23. Flags int
  24. }
  25. func (r Route) String() string {
  26. if len(r.MultiPath) > 0 {
  27. return fmt.Sprintf("{Dst: %s Src: %s Gw: %s Flags: %s Table: %d}", r.Dst,
  28. r.Src, r.MultiPath, r.ListFlags(), r.Table)
  29. }
  30. return fmt.Sprintf("{Ifindex: %d Dst: %s Src: %s Gw: %s Flags: %s Table: %d}", r.LinkIndex, r.Dst,
  31. r.Src, r.Gw, r.ListFlags(), r.Table)
  32. }
  33. func (r *Route) SetFlag(flag NextHopFlag) {
  34. r.Flags |= int(flag)
  35. }
  36. func (r *Route) ClearFlag(flag NextHopFlag) {
  37. r.Flags &^= int(flag)
  38. }
  39. type flagString struct {
  40. f NextHopFlag
  41. s string
  42. }
  43. // RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
  44. type RouteUpdate struct {
  45. Type uint16
  46. Route
  47. }
  48. type NexthopInfo struct {
  49. LinkIndex int
  50. Hops int
  51. Gw net.IP
  52. }
  53. func (n *NexthopInfo) String() string {
  54. return fmt.Sprintf("{Ifindex: %d Weight: %d, Gw: %s}", n.LinkIndex, n.Hops+1, n.Gw)
  55. }