Skip to content

Commit c4284e7

Browse files
updated level name logic
1 parent adad59e commit c4284e7

File tree

16 files changed

+82
-63
lines changed

16 files changed

+82
-63
lines changed

layers/arp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (ap *ARPPacket) Parse(data []byte) error {
188188
}
189189

190190
func (ap *ARPPacket) NextLayer() Layer { return nil }
191-
func (ap *ARPPacket) Name() string { return "ARP" }
191+
func (ap *ARPPacket) Name() LayerName { return LayerARP }
192192

193193
func ptypedesc(pt uint16) string {
194194
var proto string

layers/dns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (d *DNSMessage) Parse(data []byte) error {
288288

289289
func (d *DNSMessage) NextLayer() Layer { return nil }
290290

291-
func (d *DNSMessage) Name() string { return "DNS" }
291+
func (d *DNSMessage) Name() LayerName { return LayerDNS }
292292

293293
func (d *DNSMessage) printRecords() string {
294294
var sb strings.Builder

layers/ethernet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ func (ef *EthernetFrame) Parse(data []byte) error {
111111
}
112112

113113
func (ef *EthernetFrame) NextLayer() Layer {
114-
if next := GetNextLayer(ef.EtherType.Desc); next != nil {
114+
if next := GetNextLayer(LayerName(ef.EtherType.Desc)); next != nil {
115115
if err := next.Parse(ef.Payload); err == nil {
116116
return next
117117
}
118118
}
119119
return ParseNextLayer(ef.Payload, nil, nil)
120120
}
121121

122-
func (ef *EthernetFrame) Name() string { return "ETH" }
122+
func (ef *EthernetFrame) Name() LayerName { return LayerETH }
123123

124124
func ethertypedesc(et EtherType) string {
125125
var etdesc string

layers/ftp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ func (f *FTPMessage) Parse(data []byte) error {
4242
}
4343

4444
func (f *FTPMessage) NextLayer() Layer { return nil }
45-
func (f *FTPMessage) Name() string { return "FTP" }
45+
func (f *FTPMessage) Name() LayerName { return LayerFTP }

layers/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (h *HTTPMessage) Parse(data []byte) error {
8787
}
8888

8989
func (h *HTTPMessage) NextLayer() Layer { return nil }
90-
func (h *HTTPMessage) Name() string { return "HTTP" }
90+
func (h *HTTPMessage) Name() LayerName { return LayerHTTP }
9191

9292
type HTTPRequestWrapper struct {
9393
Request HTTPRequest `json:"http_request"`

layers/icmp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (i *ICMPSegment) Parse(data []byte) error {
6969
}
7070

7171
func (i *ICMPSegment) NextLayer() Layer { return nil }
72-
func (i *ICMPSegment) Name() string { return "ICMP" }
72+
func (i *ICMPSegment) Name() LayerName { return LayerICMP }
7373

7474
func (i *ICMPSegment) typecode() (string, string) {
7575
// https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol

layers/icmpv6.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (i *ICMPv6Segment) Parse(data []byte) error {
7171
}
7272

7373
func (i *ICMPv6Segment) NextLayer() Layer { return nil }
74-
func (i *ICMPv6Segment) Name() string { return "ICMPv6" }
74+
func (i *ICMPv6Segment) Name() LayerName { return LayerICMPv6 }
7575

7676
func (i *ICMPv6Segment) typecode() (string, string) {
7777
// https://en.wikipedia.org/wiki/ICMPv6

layers/ipv4.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ func protodesc(proto IPProto) string {
218218
}
219219

220220
func (p *IPv4Packet) NextLayer() Layer {
221-
if next := GetNextLayer(p.Protocol.Desc); next != nil {
221+
if next := GetNextLayer(LayerName(p.Protocol.Desc)); next != nil {
222222
if err := next.Parse(p.Payload); err == nil {
223223
return next
224224
}
225225
}
226226
return ParseNextLayer(p.Payload, nil, nil)
227227
}
228228

229-
func (p *IPv4Packet) Name() string { return "IPv4" }
229+
func (p *IPv4Packet) Name() LayerName { return LayerIPv4 }
230230

231231
func dscpdesc(dscp uint8) string {
232232
// https://en.wikipedia.org/wiki/Differentiated_services

layers/ipv6.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ func (p *IPv6Packet) nextLayer() string {
120120
}
121121

122122
func (p *IPv6Packet) NextLayer() Layer {
123-
if next := GetNextLayer(p.nextLayer()); next != nil {
123+
if next := GetNextLayer(LayerName(p.nextLayer())); next != nil {
124124
if err := next.Parse(p.Payload); err == nil {
125125
return next
126126
}
127127
}
128128
return ParseNextLayer(p.Payload, nil, nil)
129129
}
130130

131-
func (p *IPv6Packet) Name() string { return "IPv6" }
131+
func (p *IPv6Packet) Name() LayerName { return LayerIPv6 }
132132

133133
func (p *IPv6Packet) nextHeader() string {
134134
// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers

layers/layers.go

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,40 @@ import (
1010

1111
const maxLenSummary = 110
1212

13-
var Layers = []string{
14-
"ETH",
15-
"IPv4",
16-
"IPv6",
17-
"ARP",
18-
"TCP",
19-
"UDP",
20-
"ICMP",
21-
"ICMPv6",
22-
"DNS",
23-
"FTP",
24-
"HTTP",
25-
"SNMP",
26-
"SSH",
27-
"TLS",
13+
type LayerName string
14+
15+
const (
16+
LayerETH LayerName = "ETH"
17+
LayerIPv4 LayerName = "IPv4"
18+
LayerIPv6 LayerName = "IPv6"
19+
LayerARP LayerName = "ARP"
20+
LayerTCP LayerName = "TCP"
21+
LayerUDP LayerName = "UDP"
22+
LayerICMP LayerName = "ICMP"
23+
LayerICMPv6 LayerName = "ICMPv6"
24+
LayerDNS LayerName = "DNS"
25+
LayerFTP LayerName = "FTP"
26+
LayerHTTP LayerName = "HTTP"
27+
LayerSNMP LayerName = "SNMP"
28+
LayerSSH LayerName = "SSH"
29+
LayerTLS LayerName = "TLS"
30+
)
31+
32+
var Layers = []LayerName{
33+
LayerETH,
34+
LayerIPv4,
35+
LayerIPv6,
36+
LayerTLS,
37+
LayerHTTP,
38+
LayerDNS,
39+
LayerARP,
40+
LayerTCP,
41+
LayerUDP,
42+
LayerICMP,
43+
LayerICMPv6,
44+
LayerSNMP,
45+
LayerSSH,
46+
LayerFTP,
2847
}
2948

3049
var (
@@ -46,7 +65,7 @@ type Layer interface {
4665
Parse(data []byte) error
4766
NextLayer() Layer
4867
Summary() string
49-
Name() string
68+
Name() LayerName
5069
}
5170

5271
func parseNextLayerFallback(data []byte) Layer {
@@ -71,25 +90,25 @@ func parseNextLayerFromBytes(data []byte) Layer {
7190
var next Layer
7291
firstByte := buf[0]
7392
if firstByte >= 0x45 && firstByte <= 0x4F {
74-
next = GetNextLayer("IPv4")
93+
next = GetNextLayer(LayerIPv4)
7594
if err := next.Parse(buf); err == nil {
7695
return next
7796
}
7897
}
7998
if firstByte>>4 == 6 {
80-
next = GetNextLayer("IPv6")
99+
next = GetNextLayer(LayerIPv6)
81100
if err := next.Parse(buf); err == nil {
82101
return next
83102
}
84103
}
85104
if firstByte == HandshakeTLSVal {
86-
next = GetNextLayer("TLS")
105+
next = GetNextLayer(LayerTLS)
87106
if err := next.Parse(buf); err == nil {
88107
return next
89108
}
90109
}
91110
if firstByte == 0x30 {
92-
next = GetNextLayer("SNMP")
111+
next = GetNextLayer(LayerSNMP)
93112
if err := next.Parse(buf); err == nil {
94113
return next
95114
}
@@ -98,7 +117,7 @@ func parseNextLayerFromBytes(data []byte) Layer {
98117
b1 := binary.BigEndian.Uint16(buf[0:2])
99118
b2 := binary.BigEndian.Uint16(buf[2:4])
100119
if b1 == 1 && (b2 == 0x0800 || b2 == 0x86dd) {
101-
next = GetNextLayer("ARP")
120+
next = GetNextLayer(LayerARP)
102121
if err := next.Parse(buf); err == nil {
103122
return next
104123
}
@@ -107,20 +126,20 @@ func parseNextLayerFromBytes(data []byte) Layer {
107126
if len(buf) > 15 {
108127
b1 := binary.BigEndian.Uint16(buf[12:14])
109128
if b1 == 0x0806 || b1 == 0x0800 || b1 == 0x86dd {
110-
next = GetNextLayer("ETH")
129+
next = GetNextLayer(LayerETH)
111130
if err := next.Parse(buf); err == nil {
112131
return next
113132
}
114133
}
115134
}
116135
if bytes.Contains(buf, protohttp10) || bytes.Contains(buf, protohttp11) {
117-
next = GetNextLayer("HTTP")
136+
next = GetNextLayer(LayerHTTP)
118137
if err := next.Parse(buf); err == nil {
119138
return next
120139
}
121140
}
122141
if bytes.Contains(buf, protoSSH) {
123-
next = GetNextLayer("SSH")
142+
next = GetNextLayer(LayerSSH)
124143
if err := next.Parse(buf); err == nil {
125144
return next
126145
}
@@ -151,17 +170,17 @@ func parseNextLayerFromPorts(data []byte, src, dst *uint16) Layer {
151170
var next Layer
152171
switch {
153172
case addrMatch(src, dst, []uint16{53, 5353, 853, 5355}):
154-
next = GetNextLayer("DNS")
173+
next = GetNextLayer(LayerDNS)
155174
case addrMatch(src, dst, []uint16{80, 8080, 8000, 8888, 81, 591, 5911}):
156-
next = GetNextLayer("HTTP")
175+
next = GetNextLayer(LayerHTTP)
157176
case addrMatch(src, dst, []uint16{161, 162, 10161, 10162, 1161, 2161}):
158-
next = GetNextLayer("SNMP")
177+
next = GetNextLayer(LayerSNMP)
159178
case addrMatch(src, dst, []uint16{21, 20, 2121, 8021}):
160-
next = GetNextLayer("FTP")
179+
next = GetNextLayer(LayerFTP)
161180
case addrMatch(src, dst, []uint16{22, 2222, 2200, 222, 2022}):
162-
next = GetNextLayer("SSH")
181+
next = GetNextLayer(LayerSSH)
163182
case addrMatch(src, dst, []uint16{443, 465, 993, 995, 8443, 9443, 10443, 8444, 5228}):
164-
next = GetNextLayer("TLS")
183+
next = GetNextLayer(LayerTLS)
165184
default:
166185
return nil
167186
}
@@ -187,35 +206,35 @@ func ParseNextLayer(data []byte, src, dst *uint16) Layer {
187206
return parseNextLayerFallback(buf)
188207
}
189208

190-
func GetNextLayer(layer string) Layer {
209+
func GetNextLayer(layer LayerName) Layer {
191210
switch layer {
192-
case "ETH":
211+
case LayerETH:
193212
return &EthernetFrame{}
194-
case "IPv4":
213+
case LayerIPv4:
195214
return &IPv4Packet{}
196-
case "IPv6":
215+
case LayerIPv6:
197216
return &IPv6Packet{}
198-
case "ARP":
217+
case LayerARP:
199218
return &ARPPacket{}
200-
case "TCP":
219+
case LayerTCP:
201220
return &TCPSegment{}
202-
case "UDP":
221+
case LayerUDP:
203222
return &UDPSegment{}
204-
case "ICMP":
223+
case LayerICMP:
205224
return &ICMPSegment{}
206-
case "ICMPv6":
225+
case LayerICMPv6:
207226
return &ICMPv6Segment{}
208-
case "DNS":
227+
case LayerDNS:
209228
return &DNSMessage{}
210-
case "FTP":
229+
case LayerFTP:
211230
return &FTPMessage{}
212-
case "HTTP":
231+
case LayerHTTP:
213232
return &HTTPMessage{}
214-
case "SNMP":
233+
case LayerSNMP:
215234
return &SNMPMessage{}
216-
case "SSH":
235+
case LayerSSH:
217236
return &SSHMessage{}
218-
case "TLS":
237+
case LayerTLS:
219238
return &TLSMessage{}
220239
default:
221240
return nil

0 commit comments

Comments
 (0)