ports.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 443: LayerTypeTLS, // https
  51. 502: LayerTypeModbusTCP, // modbustcp
  52. 636: LayerTypeTLS, // ldaps
  53. 989: LayerTypeTLS, // ftps-data
  54. 990: LayerTypeTLS, // ftps
  55. 992: LayerTypeTLS, // telnets
  56. 993: LayerTypeTLS, // imaps
  57. 994: LayerTypeTLS, // ircs
  58. 995: LayerTypeTLS, // pop3s
  59. 5061: LayerTypeTLS, // ips
  60. }
  61. // RegisterTCPPortLayerType creates a new mapping between a TCPPort
  62. // and an underlaying LayerType.
  63. func RegisterTCPPortLayerType(port TCPPort, layerType gopacket.LayerType) {
  64. tcpPortLayerType[port] = layerType
  65. }
  66. // String returns the port as "number(name)" if there's a well-known port name,
  67. // or just "number" if there isn't. Well-known names are stored in
  68. // UDPPortNames.
  69. func (a UDPPort) String() string {
  70. if name, ok := UDPPortNames[a]; ok {
  71. return fmt.Sprintf("%d(%s)", a, name)
  72. }
  73. return strconv.Itoa(int(a))
  74. }
  75. // LayerType returns a LayerType that would be able to decode the
  76. // application payload. It uses some well-known ports such as 53 for
  77. // DNS.
  78. //
  79. // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
  80. func (a UDPPort) LayerType() gopacket.LayerType {
  81. lt := udpPortLayerType[uint16(a)]
  82. if lt != 0 {
  83. return lt
  84. }
  85. return gopacket.LayerTypePayload
  86. }
  87. var udpPortLayerType = [65536]gopacket.LayerType{
  88. 53: LayerTypeDNS,
  89. 123: LayerTypeNTP,
  90. 4789: LayerTypeVXLAN,
  91. 67: LayerTypeDHCPv4,
  92. 68: LayerTypeDHCPv4,
  93. 546: LayerTypeDHCPv6,
  94. 547: LayerTypeDHCPv6,
  95. 5060: LayerTypeSIP,
  96. 6343: LayerTypeSFlow,
  97. 6081: LayerTypeGeneve,
  98. 3784: LayerTypeBFD,
  99. 2152: LayerTypeGTPv1U,
  100. }
  101. // RegisterUDPPortLayerType creates a new mapping between a UDPPort
  102. // and an underlaying LayerType.
  103. func RegisterUDPPortLayerType(port UDPPort, layerType gopacket.LayerType) {
  104. udpPortLayerType[port] = layerType
  105. }
  106. // String returns the port as "number(name)" if there's a well-known port name,
  107. // or just "number" if there isn't. Well-known names are stored in
  108. // RUDPPortNames.
  109. func (a RUDPPort) String() string {
  110. if name, ok := RUDPPortNames[a]; ok {
  111. return fmt.Sprintf("%d(%s)", a, name)
  112. }
  113. return strconv.Itoa(int(a))
  114. }
  115. // String returns the port as "number(name)" if there's a well-known port name,
  116. // or just "number" if there isn't. Well-known names are stored in
  117. // SCTPPortNames.
  118. func (a SCTPPort) String() string {
  119. if name, ok := SCTPPortNames[a]; ok {
  120. return fmt.Sprintf("%d(%s)", a, name)
  121. }
  122. return strconv.Itoa(int(a))
  123. }
  124. // String returns the port as "number(name)" if there's a well-known port name,
  125. // or just "number" if there isn't. Well-known names are stored in
  126. // UDPLitePortNames.
  127. func (a UDPLitePort) String() string {
  128. if name, ok := UDPLitePortNames[a]; ok {
  129. return fmt.Sprintf("%d(%s)", a, name)
  130. }
  131. return strconv.Itoa(int(a))
  132. }