Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 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 | // +build ignore |
| 8 | |
| 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. |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "errors" |
| 15 | "flag" |
| 16 | "fmt" |
| 17 | "log" |
| 18 | "net/http" |
| 19 | "os" |
| 20 | "time" |
| 21 | |
| 22 | "github.com/google/gopacket/pcap" |
| 23 | ) |
| 24 | |
| 25 | var mode = flag.String("mode", "basic", "One of: basic,filtered,timestamp") |
| 26 | |
| 27 | func generatePackets() { |
| 28 | if resp, err := http.Get("http://code.google.com"); err != nil { |
| 29 | log.Printf("Could not get HTTP: %v", err) |
| 30 | } else { |
| 31 | resp.Body.Close() |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func main() { |
| 36 | flag.Parse() |
| 37 | ifaces, err := pcap.FindAllDevs() |
| 38 | if err != nil { |
| 39 | log.Fatal(err) |
| 40 | } |
| 41 | for _, iface := range ifaces { |
| 42 | log.Printf("Trying capture on %q", iface.Name) |
| 43 | if err := tryCapture(iface); err != nil { |
| 44 | log.Printf("Error capturing on %q: %v", iface.Name, err) |
| 45 | } else { |
| 46 | log.Printf("Successfully captured on %q", iface.Name) |
| 47 | return |
| 48 | } |
| 49 | } |
| 50 | os.Exit(1) |
| 51 | } |
| 52 | |
| 53 | func tryCapture(iface pcap.Interface) error { |
| 54 | if iface.Name[:2] == "lo" { |
| 55 | return errors.New("skipping loopback") |
| 56 | } |
| 57 | var h *pcap.Handle |
| 58 | var err error |
| 59 | switch *mode { |
| 60 | case "basic": |
| 61 | h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3) |
| 62 | if err != nil { |
| 63 | return fmt.Errorf("openlive: %v", err) |
| 64 | } |
| 65 | defer h.Close() |
| 66 | case "filtered": |
| 67 | h, err = pcap.OpenLive(iface.Name, 65536, false, time.Second*3) |
| 68 | if err != nil { |
| 69 | return fmt.Errorf("openlive: %v", err) |
| 70 | } |
| 71 | defer h.Close() |
| 72 | if err := h.SetBPFFilter("port 80 or port 443"); err != nil { |
| 73 | return fmt.Errorf("setbpf: %v", err) |
| 74 | } |
| 75 | case "timestamp": |
| 76 | u, err := pcap.NewInactiveHandle(iface.Name) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | defer u.CleanUp() |
| 81 | if err = u.SetSnapLen(65536); err != nil { |
| 82 | return err |
| 83 | } else if err = u.SetPromisc(false); err != nil { |
| 84 | return err |
| 85 | } else if err = u.SetTimeout(time.Second * 3); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | sources := u.SupportedTimestamps() |
| 89 | if len(sources) == 0 { |
| 90 | return errors.New("no supported timestamp sources") |
| 91 | } else if err := u.SetTimestampSource(sources[0]); err != nil { |
| 92 | return fmt.Errorf("settimestampsource(%v): %v", sources[0], err) |
| 93 | } else if h, err = u.Activate(); err != nil { |
| 94 | return fmt.Errorf("could not activate: %v", err) |
| 95 | } |
| 96 | defer h.Close() |
| 97 | default: |
| 98 | panic("Invalid --mode: " + *mode) |
| 99 | } |
| 100 | go generatePackets() |
| 101 | h.ReadPacketData() // Do one dummy read to clear any timeouts. |
| 102 | data, ci, err := h.ReadPacketData() |
| 103 | if err != nil { |
| 104 | return fmt.Errorf("readpacketdata: %v", err) |
| 105 | } |
| 106 | log.Printf("Read packet, %v bytes, CI: %+v", len(data), ci) |
| 107 | return nil |
| 108 | } |