1 // Copyright 2012 Google, Inc. All rights reserved.
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
13 "github.com/google/gopacket"
16 // TCPPort is a port in a TCP layer.
19 // UDPPort is a port in a UDP layer.
22 // RUDPPort is a port in a RUDP layer.
25 // SCTPPort is a port in a SCTP layer.
28 // UDPLitePort is a port in a UDPLite layer.
29 type UDPLitePort uint16
31 // RUDPPortNames contains the string names for all RUDP ports.
32 var RUDPPortNames = map[RUDPPort]string{}
34 // UDPLitePortNames contains the string names for all UDPLite ports.
35 var UDPLitePortNames = map[UDPLitePort]string{}
37 // {TCP,UDP,SCTP}PortNames can be found in iana_ports.go
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
42 func (a TCPPort) String() string {
43 if name, ok := TCPPortNames[a]; ok {
44 return fmt.Sprintf("%d(%s)", a, name)
46 return strconv.Itoa(int(a))
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
53 // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
54 func (a TCPPort) LayerType() gopacket.LayerType {
55 lt := tcpPortLayerType[uint16(a)]
59 return gopacket.LayerTypePayload
62 var tcpPortLayerType = [65536]gopacket.LayerType{
66 // RegisterTCPPortLayerType creates a new mapping between a TCPPort
67 // and an underlaying LayerType.
68 func RegisterTCPPortLayerType(port TCPPort, layerType gopacket.LayerType) {
69 tcpPortLayerType[port] = layerType
72 // String returns the port as "number(name)" if there's a well-known port name,
73 // or just "number" if there isn't. Well-known names are stored in
75 func (a UDPPort) String() string {
76 if name, ok := UDPPortNames[a]; ok {
77 return fmt.Sprintf("%d(%s)", a, name)
79 return strconv.Itoa(int(a))
82 // LayerType returns a LayerType that would be able to decode the
83 // application payload. It uses some well-known ports such as 53 for
86 // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
87 func (a UDPPort) LayerType() gopacket.LayerType {
88 lt := udpPortLayerType[uint16(a)]
92 return gopacket.LayerTypePayload
95 var udpPortLayerType = [65536]gopacket.LayerType{
101 6343: LayerTypeSFlow,
102 6081: LayerTypeGeneve,
106 // RegisterUDPPortLayerType creates a new mapping between a UDPPort
107 // and an underlaying LayerType.
108 func RegisterUDPPortLayerType(port UDPPort, layerType gopacket.LayerType) {
109 udpPortLayerType[port] = layerType
112 // String returns the port as "number(name)" if there's a well-known port name,
113 // or just "number" if there isn't. Well-known names are stored in
115 func (a RUDPPort) String() string {
116 if name, ok := RUDPPortNames[a]; ok {
117 return fmt.Sprintf("%d(%s)", a, name)
119 return strconv.Itoa(int(a))
122 // String returns the port as "number(name)" if there's a well-known port name,
123 // or just "number" if there isn't. Well-known names are stored in
125 func (a SCTPPort) String() string {
126 if name, ok := SCTPPortNames[a]; ok {
127 return fmt.Sprintf("%d(%s)", a, name)
129 return strconv.Itoa(int(a))
132 // String returns the port as "number(name)" if there's a well-known port name,
133 // or just "number" if there isn't. Well-known names are stored in
135 func (a UDPLitePort) String() string {
136 if name, ok := UDPLitePortNames[a]; ok {
137 return fmt.Sprintf("%d(%s)", a, name)
139 return strconv.Itoa(int(a))