rule.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // Rule represents a netlink rule.
  7. type Rule struct {
  8. Priority int
  9. Family int
  10. Table int
  11. Mark int
  12. Mask int
  13. Tos uint
  14. TunID uint
  15. Goto int
  16. Src *net.IPNet
  17. Dst *net.IPNet
  18. Flow int
  19. IifName string
  20. OifName string
  21. SuppressIfgroup int
  22. SuppressPrefixlen int
  23. Invert bool
  24. Dport *RulePortRange
  25. Sport *RulePortRange
  26. IPProto int
  27. }
  28. func (r Rule) String() string {
  29. from := "all"
  30. if r.Src != nil && r.Src.String() != "<nil>" {
  31. from = r.Src.String()
  32. }
  33. to := "all"
  34. if r.Dst != nil && r.Dst.String() != "<nil>" {
  35. to = r.Dst.String()
  36. }
  37. return fmt.Sprintf("ip rule %d: from %s to %s table %d",
  38. r.Priority, from, to, r.Table)
  39. }
  40. // NewRule return empty rules.
  41. func NewRule() *Rule {
  42. return &Rule{
  43. SuppressIfgroup: -1,
  44. SuppressPrefixlen: -1,
  45. Priority: -1,
  46. Mark: -1,
  47. Mask: -1,
  48. Goto: -1,
  49. Flow: -1,
  50. }
  51. }
  52. // NewRulePortRange creates rule sport/dport range.
  53. func NewRulePortRange(start, end uint16) *RulePortRange {
  54. return &RulePortRange{Start: start, End: end}
  55. }
  56. // RulePortRange represents rule sport/dport range.
  57. type RulePortRange struct {
  58. Start uint16
  59. End uint16
  60. }