added support for string type
[govpp.git] / vendor / github.com / google / gopacket / examples / pcapdump / main.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 //
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
5 // tree.
6
7 // The pcapdump binary implements a tcpdump-like command line tool with gopacket
8 // using pcap as a backend data collection mechanism.
9 package main
10
11 import (
12         "flag"
13         "fmt"
14         "github.com/google/gopacket/dumpcommand"
15         "github.com/google/gopacket/examples/util"
16         "github.com/google/gopacket/pcap"
17         "log"
18         "os"
19         "strings"
20         "time"
21 )
22
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")
28
29 func main() {
30         defer util.Run()()
31         var handle *pcap.Handle
32         var err error
33         if *fname != "" {
34                 if handle, err = pcap.OpenOffline(*fname); err != nil {
35                         log.Fatal("PCAP OpenOffline error:", err)
36                 }
37         } else {
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)
42                 if err != nil {
43                         log.Fatalf("could not create: %v", err)
44                 }
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)
52                 }
53                 if *tstype != "" {
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())
58                         }
59                 }
60                 if handle, err = inactive.Activate(); err != nil {
61                         log.Fatal("PCAP Activate error:", err)
62                 }
63                 defer handle.Close()
64         }
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)
70                 }
71         }
72         dumpcommand.Run(handle)
73 }