packet.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package firewall
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/slackhq/nebula/iputil"
  6. )
  7. type m map[string]interface{}
  8. const (
  9. ProtoAny = 0 // When we want to handle HOPOPT (0) we can change this, if ever
  10. ProtoTCP = 6
  11. ProtoUDP = 17
  12. ProtoICMP = 1
  13. PortAny = 0 // Special value for matching `port: any`
  14. PortFragment = -1 // Special value for matching `port: fragment`
  15. )
  16. type Packet struct {
  17. LocalIP iputil.VpnIp
  18. RemoteIP iputil.VpnIp
  19. LocalPort uint16
  20. RemotePort uint16
  21. Protocol uint8
  22. Fragment bool
  23. }
  24. func (fp *Packet) Copy() *Packet {
  25. return &Packet{
  26. LocalIP: fp.LocalIP,
  27. RemoteIP: fp.RemoteIP,
  28. LocalPort: fp.LocalPort,
  29. RemotePort: fp.RemotePort,
  30. Protocol: fp.Protocol,
  31. Fragment: fp.Fragment,
  32. }
  33. }
  34. func (fp Packet) MarshalJSON() ([]byte, error) {
  35. var proto string
  36. switch fp.Protocol {
  37. case ProtoTCP:
  38. proto = "tcp"
  39. case ProtoICMP:
  40. proto = "icmp"
  41. case ProtoUDP:
  42. proto = "udp"
  43. default:
  44. proto = fmt.Sprintf("unknown %v", fp.Protocol)
  45. }
  46. return json.Marshal(m{
  47. "LocalIP": fp.LocalIP.String(),
  48. "RemoteIP": fp.RemoteIP.String(),
  49. "LocalPort": fp.LocalPort,
  50. "RemotePort": fp.RemotePort,
  51. "Protocol": proto,
  52. "Fragment": fp.Fragment,
  53. })
  54. }