github.com/gopacket/gopacket@v1.1.0/layers/ports.go (about) 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 7 package layers 8 9 import ( 10 "fmt" 11 "strconv" 12 13 "github.com/gopacket/gopacket" 14 ) 15 16 // TCPPort is a port in a TCP layer. 17 type TCPPort uint16 18 19 // UDPPort is a port in a UDP layer. 20 type UDPPort uint16 21 22 // RUDPPort is a port in a RUDP layer. 23 type RUDPPort uint8 24 25 // SCTPPort is a port in a SCTP layer. 26 type SCTPPort uint16 27 28 // UDPLitePort is a port in a UDPLite layer. 29 type UDPLitePort uint16 30 31 // RUDPPortNames contains the string names for all RUDP ports. 32 var RUDPPortNames = map[RUDPPort]string{} 33 34 // UDPLitePortNames contains the string names for all UDPLite ports. 35 var UDPLitePortNames = map[UDPLitePort]string{} 36 37 // {TCP,UDP,SCTP}PortNames can be found in iana_ports.go 38 39 // String returns the port as "number(name)" if there's a well-known port name, 40 // or just "number" if there isn't. Well-known names are stored in 41 // TCPPortNames. 42 func (a TCPPort) String() string { 43 if name, ok := TCPPortNames[a]; ok { 44 return fmt.Sprintf("%d(%s)", a, name) 45 } 46 return strconv.Itoa(int(a)) 47 } 48 49 // LayerType returns a LayerType that would be able to decode the 50 // application payload. It uses some well-known ports such as 53 for 51 // DNS. 52 // 53 // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers. 54 func (a TCPPort) LayerType() gopacket.LayerType { 55 if tcpPortLayerTypeOverride.has(uint16(a)) { 56 return tcpPortLayerType[a] 57 } 58 switch a { 59 case 53: 60 return LayerTypeDNS 61 case 443: // https 62 return LayerTypeTLS 63 case 502: // modbustcp 64 return LayerTypeModbusTCP 65 case 636: // ldaps 66 return LayerTypeTLS 67 case 989: // ftps-data 68 return LayerTypeTLS 69 case 990: // ftps 70 return LayerTypeTLS 71 case 992: // telnets 72 return LayerTypeTLS 73 case 993: // imaps 74 return LayerTypeTLS 75 case 994: // ircs 76 return LayerTypeTLS 77 case 995: // pop3s 78 return LayerTypeTLS 79 case 5061: // ips 80 return LayerTypeTLS 81 } 82 return gopacket.LayerTypePayload 83 } 84 85 var tcpPortLayerTypeOverride bitfield 86 87 var tcpPortLayerType = map[TCPPort]gopacket.LayerType{} 88 89 // RegisterTCPPortLayerType creates a new mapping between a TCPPort 90 // and an underlaying LayerType. 91 func RegisterTCPPortLayerType(port TCPPort, layerType gopacket.LayerType) { 92 tcpPortLayerTypeOverride.set(uint16(port)) 93 tcpPortLayerType[port] = layerType 94 } 95 96 // String returns the port as "number(name)" if there's a well-known port name, 97 // or just "number" if there isn't. Well-known names are stored in 98 // UDPPortNames. 99 func (a UDPPort) String() string { 100 if name, ok := UDPPortNames[a]; ok { 101 return fmt.Sprintf("%d(%s)", a, name) 102 } 103 return strconv.Itoa(int(a)) 104 } 105 106 // LayerType returns a LayerType that would be able to decode the 107 // application payload. It uses some well-known ports such as 53 for 108 // DNS. 109 // 110 // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers. 111 func (a UDPPort) LayerType() gopacket.LayerType { 112 if udpPortLayerTypeOverride.has(uint16(a)) { 113 return udpPortLayerType[a] 114 } 115 switch a { 116 case 53: 117 return LayerTypeDNS 118 case 67: 119 return LayerTypeDHCPv4 120 case 68: 121 return LayerTypeDHCPv4 122 case 123: 123 return LayerTypeNTP 124 case 546: 125 return LayerTypeDHCPv6 126 case 547: 127 return LayerTypeDHCPv6 128 case 623: 129 return LayerTypeRMCP 130 case 1812: 131 return LayerTypeRADIUS 132 case 2152: 133 return LayerTypeGTPv1U 134 case 3784: 135 return LayerTypeBFD 136 case 4789: 137 return LayerTypeVXLAN 138 case 5060: 139 return LayerTypeSIP 140 case 6081: 141 return LayerTypeGeneve 142 case 6343: 143 return LayerTypeSFlow 144 } 145 return gopacket.LayerTypePayload 146 } 147 148 var udpPortLayerTypeOverride bitfield 149 150 var udpPortLayerType = map[UDPPort]gopacket.LayerType{} 151 152 // RegisterUDPPortLayerType creates a new mapping between a UDPPort 153 // and an underlaying LayerType. 154 func RegisterUDPPortLayerType(port UDPPort, layerType gopacket.LayerType) { 155 udpPortLayerTypeOverride.set(uint16(port)) 156 udpPortLayerType[port] = layerType 157 } 158 159 // String returns the port as "number(name)" if there's a well-known port name, 160 // or just "number" if there isn't. Well-known names are stored in 161 // RUDPPortNames. 162 func (a RUDPPort) String() string { 163 if name, ok := RUDPPortNames[a]; ok { 164 return fmt.Sprintf("%d(%s)", a, name) 165 } 166 return strconv.Itoa(int(a)) 167 } 168 169 // String returns the port as "number(name)" if there's a well-known port name, 170 // or just "number" if there isn't. Well-known names are stored in 171 // SCTPPortNames. 172 func (a SCTPPort) String() string { 173 if name, ok := SCTPPortNames[a]; ok { 174 return fmt.Sprintf("%d(%s)", a, name) 175 } 176 return strconv.Itoa(int(a)) 177 } 178 179 // String returns the port as "number(name)" if there's a well-known port name, 180 // or just "number" if there isn't. Well-known names are stored in 181 // UDPLitePortNames. 182 func (a UDPLitePort) String() string { 183 if name, ok := UDPLitePortNames[a]; ok { 184 return fmt.Sprintf("%d(%s)", a, name) 185 } 186 return strconv.Itoa(int(a)) 187 }