-
Notifications
You must be signed in to change notification settings - Fork 1
/
err.go
51 lines (38 loc) · 933 Bytes
/
err.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
package main
import (
"fmt"
"log"
)
// START MAIN OMIT
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func main() {
pcapFile, _ := pcap.OpenOffline("packets.pcap")
defer pcapFile.Close()
packetSource := gopacket.NewPacketSource(pcapFile, pcapFile.LinkType())
for packet := range packetSource.Packets() {
s, err := payloadSize(packet)
if err != nil {
log.Printf("ERROR: %s\n", err.Error())
continue
}
log.Printf("Payload Size = %d bytes", s)
}
}
// END MAIN OMIT
// START FUNC OMIT
func payloadSize(packet gopacket.Packet) (int, error) {
ip4Layer := packet.Layer(layers.LayerTypeIPv4)
if ip4Layer == nil {
return 0, fmt.Errorf("Not an IPv4 packet")
}
appLayer := packet.ApplicationLayer()
if appLayer == nil {
return 0, fmt.Errorf("Application Layer not available")
}
return len(appLayer.Payload()), nil
}
// END FUNC OMIT