route.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "syscall"
  6. )
  7. // Scope is an enum representing a route scope.
  8. type Scope uint8
  9. const (
  10. SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE
  11. SCOPE_SITE Scope = syscall.RT_SCOPE_SITE
  12. SCOPE_LINK Scope = syscall.RT_SCOPE_LINK
  13. SCOPE_HOST Scope = syscall.RT_SCOPE_HOST
  14. SCOPE_NOWHERE Scope = syscall.RT_SCOPE_NOWHERE
  15. )
  16. type NextHopFlag int
  17. const (
  18. FLAG_ONLINK NextHopFlag = syscall.RTNH_F_ONLINK
  19. FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
  20. )
  21. // Route represents a netlink route.
  22. type Route struct {
  23. LinkIndex int
  24. ILinkIndex int
  25. Scope Scope
  26. Dst *net.IPNet
  27. Src net.IP
  28. Gw net.IP
  29. Protocol int
  30. Priority int
  31. Table int
  32. Type int
  33. Tos int
  34. Flags int
  35. }
  36. func (r Route) String() string {
  37. return fmt.Sprintf("{Ifindex: %d Dst: %s Src: %s Gw: %s Flags: %s}", r.LinkIndex, r.Dst,
  38. r.Src, r.Gw, r.ListFlags())
  39. }
  40. func (r *Route) SetFlag(flag NextHopFlag) {
  41. r.Flags |= int(flag)
  42. }
  43. func (r *Route) ClearFlag(flag NextHopFlag) {
  44. r.Flags &^= int(flag)
  45. }
  46. type flagString struct {
  47. f NextHopFlag
  48. s string
  49. }
  50. var testFlags = []flagString{
  51. flagString{f: FLAG_ONLINK, s: "onlink"},
  52. flagString{f: FLAG_PERVASIVE, s: "pervasive"},
  53. }
  54. func (r *Route) ListFlags() []string {
  55. var flags []string
  56. for _, tf := range testFlags {
  57. if r.Flags&int(tf.f) != 0 {
  58. flags = append(flags, tf.s)
  59. }
  60. }
  61. return flags
  62. }
  63. // RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE
  64. type RouteUpdate struct {
  65. Type uint16
  66. Route
  67. }