ports.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "fmt"
  9. "strconv"
  10. "github.com/google/gopacket"
  11. )
  12. // TCPPort is a port in a TCP layer.
  13. type TCPPort uint16
  14. // UDPPort is a port in a UDP layer.
  15. type UDPPort uint16
  16. // RUDPPort is a port in a RUDP layer.
  17. type RUDPPort uint8
  18. // SCTPPort is a port in a SCTP layer.
  19. type SCTPPort uint16
  20. // UDPLitePort is a port in a UDPLite layer.
  21. type UDPLitePort uint16
  22. // RUDPPortNames contains the string names for all RUDP ports.
  23. var RUDPPortNames = map[RUDPPort]string{}
  24. // UDPLitePortNames contains the string names for all UDPLite ports.
  25. var UDPLitePortNames = map[UDPLitePort]string{}
  26. // {TCP,UDP,SCTP}PortNames can be found in iana_ports.go
  27. // String returns the port as "number(name)" if there's a well-known port name,
  28. // or just "number" if there isn't. Well-known names are stored in
  29. // TCPPortNames.
  30. func (a TCPPort) String() string {
  31. if name, ok := TCPPortNames[a]; ok {
  32. return fmt.Sprintf("%d(%s)", a, name)
  33. }
  34. return strconv.Itoa(int(a))
  35. }
  36. // LayerType returns a LayerType that would be able to decode the
  37. // application payload. It uses some well-known ports such as 53 for
  38. // DNS.
  39. //
  40. // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
  41. func (a TCPPort) LayerType() gopacket.LayerType {
  42. lt := tcpPortLayerType[uint16(a)]
  43. if lt != 0 {
  44. return lt
  45. }
  46. return gopacket.LayerTypePayload
  47. }
  48. var tcpPortLayerType = [65536]gopacket.LayerType{
  49. 53: LayerTypeDNS,
  50. }
  51. // RegisterTCPPortLayerType creates a new mapping between a TCPPort
  52. // and an underlaying LayerType.
  53. func RegisterTCPPortLayerType(port TCPPort, layerType gopacket.LayerType) {
  54. tcpPortLayerType[port] = layerType
  55. }
  56. // String returns the port as "number(name)" if there's a well-known port name,
  57. // or just "number" if there isn't. Well-known names are stored in
  58. // UDPPortNames.
  59. func (a UDPPort) String() string {
  60. if name, ok := UDPPortNames[a]; ok {
  61. return fmt.Sprintf("%d(%s)", a, name)
  62. }
  63. return strconv.Itoa(int(a))
  64. }
  65. // LayerType returns a LayerType that would be able to decode the
  66. // application payload. It uses some well-known ports such as 53 for
  67. // DNS.
  68. //
  69. // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
  70. func (a UDPPort) LayerType() gopacket.LayerType {
  71. lt := udpPortLayerType[uint16(a)]
  72. if lt != 0 {
  73. return lt
  74. }
  75. return gopacket.LayerTypePayload
  76. }
  77. var udpPortLayerType = [65536]gopacket.LayerType{
  78. 53: LayerTypeDNS,
  79. 123: LayerTypeNTP,
  80. 4789: LayerTypeVXLAN,
  81. 67: LayerTypeDHCPv4,
  82. 68: LayerTypeDHCPv4,
  83. 6343: LayerTypeSFlow,
  84. 6081: LayerTypeGeneve,
  85. 3784: LayerTypeBFD,
  86. }
  87. // RegisterUDPPortLayerType creates a new mapping between a UDPPort
  88. // and an underlaying LayerType.
  89. func RegisterUDPPortLayerType(port UDPPort, layerType gopacket.LayerType) {
  90. udpPortLayerType[port] = layerType
  91. }
  92. // String returns the port as "number(name)" if there's a well-known port name,
  93. // or just "number" if there isn't. Well-known names are stored in
  94. // RUDPPortNames.
  95. func (a RUDPPort) String() string {
  96. if name, ok := RUDPPortNames[a]; ok {
  97. return fmt.Sprintf("%d(%s)", a, name)
  98. }
  99. return strconv.Itoa(int(a))
  100. }
  101. // String returns the port as "number(name)" if there's a well-known port name,
  102. // or just "number" if there isn't. Well-known names are stored in
  103. // SCTPPortNames.
  104. func (a SCTPPort) String() string {
  105. if name, ok := SCTPPortNames[a]; ok {
  106. return fmt.Sprintf("%d(%s)", a, name)
  107. }
  108. return strconv.Itoa(int(a))
  109. }
  110. // String returns the port as "number(name)" if there's a well-known port name,
  111. // or just "number" if there isn't. Well-known names are stored in
  112. // UDPLitePortNames.
  113. func (a UDPLitePort) String() string {
  114. if name, ok := UDPLitePortNames[a]; ok {
  115. return fmt.Sprintf("%d(%s)", a, name)
  116. }
  117. return strconv.Itoa(int(a))
  118. }