1 // Copyright 2012 Google, Inc. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
7 // The pcapdump binary implements a tcpdump-like command line tool with gopacket
8 // using pcap as a backend data collection mechanism.
14 "github.com/google/gopacket/dumpcommand"
15 "github.com/google/gopacket/examples/util"
16 "github.com/google/gopacket/pcap"
23 var iface = flag.String("i", "eth0", "Interface to read packets from")
24 var fname = flag.String("r", "", "Filename to read from, overrides -i")
25 var snaplen = flag.Int("s", 65536, "Snap length (number of bytes max to read per packet")
26 var tstype = flag.String("timestamp_type", "", "Type of timestamps to use")
27 var promisc = flag.Bool("promisc", true, "Set promiscuous mode")
31 var handle *pcap.Handle
34 if handle, err = pcap.OpenOffline(*fname); err != nil {
35 log.Fatal("PCAP OpenOffline error:", err)
38 // This is a little complicated because we want to allow all possible options
39 // for creating the packet capture handle... instead of all this you can
40 // just call pcap.OpenLive if you want a simple handle.
41 inactive, err := pcap.NewInactiveHandle(*iface)
43 log.Fatalf("could not create: %v", err)
45 defer inactive.CleanUp()
46 if err = inactive.SetSnapLen(*snaplen); err != nil {
47 log.Fatalf("could not set snap length: %v", err)
48 } else if err = inactive.SetPromisc(*promisc); err != nil {
49 log.Fatalf("could not set promisc mode: %v", err)
50 } else if err = inactive.SetTimeout(time.Second); err != nil {
51 log.Fatalf("could not set timeout: %v", err)
54 if t, err := pcap.TimestampSourceFromString(*tstype); err != nil {
55 log.Fatalf("Supported timestamp types: %v", inactive.SupportedTimestamps())
56 } else if err := inactive.SetTimestampSource(t); err != nil {
57 log.Fatalf("Supported timestamp types: %v", inactive.SupportedTimestamps())
60 if handle, err = inactive.Activate(); err != nil {
61 log.Fatal("PCAP Activate error:", err)
65 if len(flag.Args()) > 0 {
66 bpffilter := strings.Join(flag.Args(), " ")
67 fmt.Fprintf(os.Stderr, "Using BPF filter %q\n", bpffilter)
68 if err = handle.SetBPFFilter(bpffilter); err != nil {
69 log.Fatal("BPF filter error:", err)
72 dumpcommand.Run(handle)