Decoding packet layers

Packets can be decoded layer by layer with the packet.Layer() function. This program will inspect the packets, look for TCP traffic, and then output the Ethernet layer, IP layer, TCP layer, and application layer information. This is useful when you need to inspect the traffic and make a decision based on the information. When it gets to the application layer, it looks for the HTTP keyword and prints a message if one is detected:

package mainimport (   "fmt"   "github.com/google/gopacket"   "github.com/google/gopacket/layers"   "github.com/google/gopacket/pcap"   "log"   "strings"   "time")var (   device            = "eth0"   snapshotLen int32 = 1024   promiscuous       = false   err         error   timeout     = 30 * time.Second   handle      *pcap.Handle)func main() { // Open ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.