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
9 // This binary tests that PCAP packet capture is working correctly by issuing
10 // HTTP requests, then making sure we actually capture data off the wire.
23 "github.com/google/gopacket/pcap"
26 var mode = flag.String("mode", "basic", "One of: basic,filtered,timestamp")
28 func generatePackets() {
29 if resp, err := http.Get("http://code.google.com"); err != nil {
30 log.Printf("Could not get HTTP: %v", err)
38 ifaces, err := net.Interfaces()
42 for _, iface := range ifaces {
43 log.Printf("Trying capture on %q", iface.Name)
44 if err := tryCapture(iface); err != nil {
45 log.Printf("Error capturing on %q: %v", iface.Name, err)
47 log.Printf("Successfully captured on %q", iface.Name)
54 func tryCapture(iface net.Interface) error {
55 if iface.Name[:2] == "lo" {
56 return errors.New("skipping loopback")
62 h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3)
64 return fmt.Errorf("openlive: %v", err)
68 h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3)
70 return fmt.Errorf("openlive: %v", err)
73 if err := h.SetBPFFilter("port 80 or port 443"); err != nil {
74 return fmt.Errorf("setbpf: %v", err)
77 u, err := pcap.NewInactiveHandle(iface.Name)
82 if err = u.SetSnapLen(65536); err != nil {
84 } else if err = u.SetPromisc(false); err != nil {
86 } else if err = u.SetTimeout(time.Second * 3); err != nil {
89 sources := u.SupportedTimestamps()
90 if len(sources) == 0 {
91 return errors.New("no supported timestamp sources")
92 } else if err := u.SetTimestampSource(sources[0]); err != nil {
93 return fmt.Errorf("settimestampsource(%v): %v", sources[0], err)
94 } else if h, err = u.Activate(); err != nil {
95 return fmt.Errorf("could not activate: %v", err)
99 panic("Invalid --mode: " + *mode)
102 h.ReadPacketData() // Do one dummy read to clear any timeouts.
103 data, ci, err := h.ReadPacketData()
105 return fmt.Errorf("readpacketdata: %v", err)
107 log.Printf("Read packet, %v bytes, CI: %+v", len(data), ci)