forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dissect.go
77 lines (69 loc) · 2.01 KB
/
dissect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2016-2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bpfdebug
import (
"encoding/hex"
"fmt"
"sync"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
var (
eth layers.Ethernet
ip4 layers.IPv4
ip6 layers.IPv6
icmp4 layers.ICMPv4
icmp6 layers.ICMPv6
tcp layers.TCP
udp layers.UDP
parser = gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ð, &ip4, &ip6, &icmp4, &icmp6, &tcp, &udp)
decoded = []gopacket.LayerType{}
lock sync.Mutex
)
// Dissect parses and prints the provided data if dissect is set to true,
// otherwise the data is printed as HEX output
func Dissect(dissect bool, data []byte) {
if dissect {
lock.Lock()
defer lock.Unlock()
parser.DecodeLayers(data, &decoded)
for _, typ := range decoded {
switch typ {
case layers.LayerTypeEthernet:
fmt.Println(gopacket.LayerString(ð))
case layers.LayerTypeIPv4:
fmt.Println(gopacket.LayerString(&ip4))
case layers.LayerTypeIPv6:
fmt.Println(gopacket.LayerString(&ip6))
case layers.LayerTypeTCP:
fmt.Println(gopacket.LayerString(&tcp))
case layers.LayerTypeUDP:
fmt.Println(gopacket.LayerString(&udp))
case layers.LayerTypeICMPv4:
fmt.Println(gopacket.LayerString(&icmp4))
case layers.LayerTypeICMPv6:
fmt.Println(gopacket.LayerString(&icmp6))
default:
fmt.Println("Unknown layer")
}
}
if parser.Truncated {
fmt.Println(" Packet has been truncated")
}
} else {
fmt.Println(hex.Dump(data))
}
}