pcap_tester.go 2.7 KB

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