431358c12d8c6fa5c2c570dec45474a2942b36ac
[govpp.git] / vendor / github.com / google / gopacket / layers / ports.go
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/google/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         lt := tcpPortLayerType[uint16(a)]
56         if lt != 0 {
57                 return lt
58         }
59         return gopacket.LayerTypePayload
60 }
61
62 var tcpPortLayerType = [65536]gopacket.LayerType{
63         53: LayerTypeDNS,
64 }
65
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
70 }
71
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
74 // UDPPortNames.
75 func (a UDPPort) String() string {
76         if name, ok := UDPPortNames[a]; ok {
77                 return fmt.Sprintf("%d(%s)", a, name)
78         }
79         return strconv.Itoa(int(a))
80 }
81
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
84 // DNS.
85 //
86 // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers.
87 func (a UDPPort) LayerType() gopacket.LayerType {
88         lt := udpPortLayerType[uint16(a)]
89         if lt != 0 {
90                 return lt
91         }
92         return gopacket.LayerTypePayload
93 }
94
95 var udpPortLayerType = [65536]gopacket.LayerType{
96         53:   LayerTypeDNS,
97         123:  LayerTypeNTP,
98         4789: LayerTypeVXLAN,
99         67:   LayerTypeDHCPv4,
100         68:   LayerTypeDHCPv4,
101         6343: LayerTypeSFlow,
102         6081: LayerTypeGeneve,
103         3784: LayerTypeBFD,
104 }
105
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
110 }
111
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
114 // RUDPPortNames.
115 func (a RUDPPort) String() string {
116         if name, ok := RUDPPortNames[a]; ok {
117                 return fmt.Sprintf("%d(%s)", a, name)
118         }
119         return strconv.Itoa(int(a))
120 }
121
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
124 // SCTPPortNames.
125 func (a SCTPPort) String() string {
126         if name, ok := SCTPPortNames[a]; ok {
127                 return fmt.Sprintf("%d(%s)", a, name)
128         }
129         return strconv.Itoa(int(a))
130 }
131
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
134 // UDPLitePortNames.
135 func (a UDPLitePort) String() string {
136         if name, ok := UDPLitePortNames[a]; ok {
137                 return fmt.Sprintf("%d(%s)", a, name)
138         }
139         return strconv.Itoa(int(a))
140 }