pcap_tester.go 2.7 KB

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