Skip to content

Commit 49bfa61

Browse files
changed NextLayer method
1 parent 0ba2de9 commit 49bfa61

File tree

18 files changed

+87
-70
lines changed

18 files changed

+87
-70
lines changed

layers/arp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ func (ap *ARPPacket) Parse(data []byte) error {
187187
return ap.UnmarshalBinary(data)
188188
}
189189

190-
func (ap *ARPPacket) NextLayer() (layer string, payload []byte) { return }
190+
func (ap *ARPPacket) NextLayer() Layer { return nil }
191+
func (ap *ARPPacket) Name() string { return "ARP" }
191192

192193
func ptypedesc(pt uint16) string {
193194
var proto string

layers/dns.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ func (d *DNSMessage) Parse(data []byte) error {
286286
return d.UnmarshalBinary(data)
287287
}
288288

289-
func (d *DNSMessage) NextLayer() (layer string, payload []byte) { return }
289+
func (d *DNSMessage) NextLayer() Layer { return nil }
290+
291+
func (d *DNSMessage) Name() string { return "DNS" }
290292

291293
func (d *DNSMessage) printRecords() string {
292294
var sb strings.Builder

layers/ethernet.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ func (ef *EthernetFrame) Parse(data []byte) error {
110110
return ef.UnmarshalBinary(data)
111111
}
112112

113-
// NextLayer returns the name and payload of the next layer protocol based on the EtherType field of the EthernetFrame.
114-
func (ef *EthernetFrame) NextLayer() (string, []byte) {
115-
return ef.EtherType.Desc, ef.Payload
113+
func (ef *EthernetFrame) NextLayer() Layer {
114+
if next := GetNextLayer(ef.EtherType.Desc); next != nil {
115+
if err := next.Parse(ef.Payload); err == nil {
116+
return next
117+
}
118+
}
119+
return ParseNextLayer(ef.Payload, nil, nil)
116120
}
117121

122+
func (ef *EthernetFrame) Name() string { return "ETH" }
123+
118124
func ethertypedesc(et EtherType) string {
119125
var etdesc string
120126
switch et {

layers/ftp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ func (f *FTPMessage) Parse(data []byte) error {
4141
return nil
4242
}
4343

44-
func (f *FTPMessage) NextLayer() (layer string, payload []byte) { return }
44+
func (f *FTPMessage) NextLayer() Layer { return nil }
45+
func (f *FTPMessage) Name() string { return "FTP" }

layers/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ func (h *HTTPMessage) Parse(data []byte) error {
8686
return nil
8787
}
8888

89-
func (h *HTTPMessage) NextLayer() (layer string, payload []byte) { return }
89+
func (h *HTTPMessage) NextLayer() Layer { return nil }
90+
func (h *HTTPMessage) Name() string { return "HTTP" }
9091

9192
type HTTPRequestWrapper struct {
9293
Request HTTPRequest `json:"http_request"`

layers/icmp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func (i *ICMPSegment) Parse(data []byte) error {
6767
i.TypeDesc, i.CodeDesc = i.typecode()
6868
return nil
6969
}
70-
func (i *ICMPSegment) NextLayer() (layer string, payload []byte) { return }
70+
71+
func (i *ICMPSegment) NextLayer() Layer { return nil }
72+
func (i *ICMPSegment) Name() string { return "ICMP" }
7173

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

layers/icmpv6.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func (i *ICMPv6Segment) Parse(data []byte) error {
7070
return nil
7171
}
7272

73-
func (i *ICMPv6Segment) NextLayer() (layer string, payload []byte) { return }
73+
func (i *ICMPv6Segment) NextLayer() Layer { return nil }
74+
func (i *ICMPv6Segment) Name() string { return "ICMPv6" }
7475

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

layers/ipv4.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,17 @@ func protodesc(proto IPProto) string {
214214
return protodesc
215215
}
216216

217-
func (p *IPv4Packet) NextLayer() (string, []byte) {
218-
layer := p.Protocol.Desc
219-
if layer == "Unknown" {
220-
layer = ""
217+
func (p *IPv4Packet) NextLayer() Layer {
218+
if next := GetNextLayer(p.Protocol.Desc); next != nil {
219+
if err := next.Parse(p.Payload); err == nil {
220+
return next
221+
}
221222
}
222-
return layer, p.Payload
223+
return ParseNextLayer(p.Payload, nil, nil)
223224
}
224225

226+
func (p *IPv4Packet) Name() string { return "IPv4" }
227+
225228
func dscpdesc(dscp uint8) string {
226229
// https://en.wikipedia.org/wiki/Differentiated_services
227230
var dscpdesc string

layers/ipv6.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type IPv6Packet struct {
4444
HopLimit uint8
4545
SrcIP netip.Addr // The unicast IPv6 address of the sending node.
4646
DstIP netip.Addr // The IPv6 unicast or multicast address of the destination node(s).
47-
payload []byte
47+
Payload []byte
4848
}
4949

5050
func (p *IPv6Packet) String() string {
@@ -67,7 +67,7 @@ func (p *IPv6Packet) String() string {
6767
p.HopLimit,
6868
p.SrcIP,
6969
p.DstIP,
70-
len(p.payload),
70+
len(p.Payload),
7171
)
7272
}
7373

@@ -92,11 +92,11 @@ func (p *IPv6Packet) Parse(data []byte) error {
9292
p.HopLimit = buf[7]
9393
p.SrcIP, _ = netip.AddrFromSlice(buf[8:24])
9494
p.DstIP, _ = netip.AddrFromSlice(buf[24:headerSizeIPv6])
95-
p.payload = buf[headerSizeIPv6:]
95+
p.Payload = buf[headerSizeIPv6:]
9696
return nil
9797
}
9898

99-
func (p *IPv6Packet) NextLayer() (string, []byte) {
99+
func (p *IPv6Packet) nextLayer() string {
100100
// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
101101
var layer string
102102
switch p.NextHeader {
@@ -109,9 +109,20 @@ func (p *IPv6Packet) NextLayer() (string, []byte) {
109109
default:
110110
layer = ""
111111
}
112-
return layer, p.payload
112+
return layer
113113
}
114114

115+
func (p *IPv6Packet) NextLayer() Layer {
116+
if next := GetNextLayer(p.nextLayer()); next != nil {
117+
if err := next.Parse(p.Payload); err == nil {
118+
return next
119+
}
120+
}
121+
return ParseNextLayer(p.Payload, nil, nil)
122+
}
123+
124+
func (p *IPv6Packet) Name() string { return "IPv6" }
125+
115126
func (p *IPv6Packet) nextHeader() string {
116127
// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
117128
var header string

layers/ipv6_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ func TestParseIPv6(t *testing.T) {
3131
HopLimit: 64,
3232
SrcIP: netip.AddrFrom16([16]byte{
3333
0xFD, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34-
0xF2, 0x3F, 0xD1, 0x59, 0x50, 0x48, 0x9C, 0x14}),
34+
0xF2, 0x3F, 0xD1, 0x59, 0x50, 0x48, 0x9C, 0x14,
35+
}),
3536
DstIP: netip.AddrFrom16([16]byte{
3637
0x26, 0x20, 0x00, 0x2D, 0x40, 0x00, 0x00, 0x01,
37-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B}),
38-
payload: []byte{},
38+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B,
39+
}),
40+
Payload: []byte{},
3941
}
4042
ip := &IPv6Packet{}
4143
packet, close := testPacket(t, "ipv6")

0 commit comments

Comments
 (0)