endpoints.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "github.com/google/gopacket"
  10. "net"
  11. "strconv"
  12. )
  13. var (
  14. // We use two different endpoint types for IPv4 vs IPv6 addresses, so that
  15. // ordering with endpointA.LessThan(endpointB) sanely groups all IPv4
  16. // addresses and all IPv6 addresses, such that IPv6 > IPv4 for all addresses.
  17. EndpointIPv4 = gopacket.RegisterEndpointType(1, gopacket.EndpointTypeMetadata{Name: "IPv4", Formatter: func(b []byte) string {
  18. return net.IP(b).String()
  19. }})
  20. EndpointIPv6 = gopacket.RegisterEndpointType(2, gopacket.EndpointTypeMetadata{Name: "IPv6", Formatter: func(b []byte) string {
  21. return net.IP(b).String()
  22. }})
  23. EndpointMAC = gopacket.RegisterEndpointType(3, gopacket.EndpointTypeMetadata{Name: "MAC", Formatter: func(b []byte) string {
  24. return net.HardwareAddr(b).String()
  25. }})
  26. EndpointTCPPort = gopacket.RegisterEndpointType(4, gopacket.EndpointTypeMetadata{Name: "TCP", Formatter: func(b []byte) string {
  27. return strconv.Itoa(int(binary.BigEndian.Uint16(b)))
  28. }})
  29. EndpointUDPPort = gopacket.RegisterEndpointType(5, gopacket.EndpointTypeMetadata{Name: "UDP", Formatter: func(b []byte) string {
  30. return strconv.Itoa(int(binary.BigEndian.Uint16(b)))
  31. }})
  32. EndpointSCTPPort = gopacket.RegisterEndpointType(6, gopacket.EndpointTypeMetadata{Name: "SCTP", Formatter: func(b []byte) string {
  33. return strconv.Itoa(int(binary.BigEndian.Uint16(b)))
  34. }})
  35. EndpointRUDPPort = gopacket.RegisterEndpointType(7, gopacket.EndpointTypeMetadata{Name: "RUDP", Formatter: func(b []byte) string {
  36. return strconv.Itoa(int(b[0]))
  37. }})
  38. EndpointUDPLitePort = gopacket.RegisterEndpointType(8, gopacket.EndpointTypeMetadata{Name: "UDPLite", Formatter: func(b []byte) string {
  39. return strconv.Itoa(int(binary.BigEndian.Uint16(b)))
  40. }})
  41. EndpointPPP = gopacket.RegisterEndpointType(9, gopacket.EndpointTypeMetadata{Name: "PPP", Formatter: func([]byte) string {
  42. return "point"
  43. }})
  44. )
  45. // NewIPEndpoint creates a new IP (v4 or v6) endpoint from a net.IP address.
  46. // It returns gopacket.InvalidEndpoint if the IP address is invalid.
  47. func NewIPEndpoint(a net.IP) gopacket.Endpoint {
  48. ipv4 := a.To4()
  49. if ipv4 != nil {
  50. return gopacket.NewEndpoint(EndpointIPv4, []byte(ipv4))
  51. }
  52. ipv6 := a.To16()
  53. if ipv6 != nil {
  54. return gopacket.NewEndpoint(EndpointIPv6, []byte(ipv6))
  55. }
  56. return gopacket.InvalidEndpoint
  57. }
  58. // NewMACEndpoint returns a new MAC address endpoint.
  59. func NewMACEndpoint(a net.HardwareAddr) gopacket.Endpoint {
  60. return gopacket.NewEndpoint(EndpointMAC, []byte(a))
  61. }
  62. func newPortEndpoint(t gopacket.EndpointType, p uint16) gopacket.Endpoint {
  63. return gopacket.NewEndpoint(t, []byte{byte(p >> 8), byte(p)})
  64. }
  65. // NewTCPPortEndpoint returns an endpoint based on a TCP port.
  66. func NewTCPPortEndpoint(p TCPPort) gopacket.Endpoint {
  67. return newPortEndpoint(EndpointTCPPort, uint16(p))
  68. }
  69. // NewUDPPortEndpoint returns an endpoint based on a UDP port.
  70. func NewUDPPortEndpoint(p UDPPort) gopacket.Endpoint {
  71. return newPortEndpoint(EndpointUDPPort, uint16(p))
  72. }
  73. // NewSCTPPortEndpoint returns an endpoint based on a SCTP port.
  74. func NewSCTPPortEndpoint(p SCTPPort) gopacket.Endpoint {
  75. return newPortEndpoint(EndpointSCTPPort, uint16(p))
  76. }
  77. // NewRUDPPortEndpoint returns an endpoint based on a RUDP port.
  78. func NewRUDPPortEndpoint(p RUDPPort) gopacket.Endpoint {
  79. return gopacket.NewEndpoint(EndpointRUDPPort, []byte{byte(p)})
  80. }
  81. // NewUDPLitePortEndpoint returns an endpoint based on a UDPLite port.
  82. func NewUDPLitePortEndpoint(p UDPLitePort) gopacket.Endpoint {
  83. return newPortEndpoint(EndpointUDPLitePort, uint16(p))
  84. }