doc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. Package pcap allows users of gopacket to read packets off the wire or from
  8. pcap files.
  9. This package is meant to be used with its parent,
  10. http://github.com/google/gopacket, although it can also be used independently
  11. if you just want to get packet data from the wire.
  12. Reading PCAP Files
  13. The following code can be used to read in data from a pcap file.
  14. if handle, err := pcap.OpenOffline("/path/to/my/file"); err != nil {
  15. panic(err)
  16. } else {
  17. packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
  18. for packet := range packetSource.Packets() {
  19. handlePacket(packet) // Do something with a packet here.
  20. }
  21. }
  22. Reading Live Packets
  23. The following code can be used to read in data from a live device, in this case
  24. "eth0".
  25. if handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever); err != nil {
  26. panic(err)
  27. } else if err := handle.SetBPFFilter("tcp and port 80"); err != nil { // optional
  28. panic(err)
  29. } else {
  30. packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
  31. for packet := range packetSource.Packets() {
  32. handlePacket(packet) // Do something with a packet here.
  33. }
  34. }
  35. Inactive Handles
  36. Newer PCAP functionality requires the concept of an 'inactive' PCAP handle.
  37. Instead of constantly adding new arguments to pcap_open_live, users now call
  38. pcap_create to create a handle, set it up with a bunch of optional function
  39. calls, then call pcap_activate to activate it. This library mirrors that
  40. mechanism, for those that want to expose/use these new features:
  41. inactive, err := pcap.NewInactiveHandle(deviceName)
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. defer inactive.CleanUp()
  46. // Call various functions on inactive to set it up the way you'd like:
  47. if err = inactive.SetTimeout(time.Minute); err != nil {
  48. log.Fatal(err)
  49. } else if err = inactive.SetTimestampSource("foo"); err != nil {
  50. log.Fatal(err)
  51. }
  52. // Finally, create the actual handle by calling Activate:
  53. handle, err := inactive.Activate() // after this, inactive is no longer valid
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. defer handle.Close()
  58. // Now use your handle as you see fit.
  59. PCAP Timeouts
  60. pcap.OpenLive and pcap.SetTimeout both take timeouts.
  61. If you don't care about timeouts, just pass in BlockForever,
  62. which should do what you expect with minimal fuss.
  63. A timeout of 0 is not recommended. Some platforms, like Macs
  64. (http://www.manpages.info/macosx/pcap.3.html) say:
  65. The read timeout is used to arrange that the read not necessarily return
  66. immediately when a packet is seen, but that it wait for some amount of time
  67. to allow more packets to arrive and to read multiple packets from the OS
  68. kernel in one operation.
  69. This means that if you only capture one packet, the kernel might decide to wait
  70. 'timeout' for more packets to batch with it before returning. A timeout of
  71. 0, then, means 'wait forever for more packets', which is... not good.
  72. To get around this, we've introduced the following behavior: if a negative
  73. timeout is passed in, we set the positive timeout in the handle, then loop
  74. internally in ReadPacketData/ZeroCopyReadPacketData when we see timeout
  75. errors.
  76. PCAP File Writing
  77. This package does not implement PCAP file writing. However, gopacket/pcapgo
  78. does! Look there if you'd like to write PCAP files.
  79. Note For Windows 10 Users
  80. If you're trying to use 64-bit winpcap on Windows 10, you might have to do
  81. the crazy hijinks detailed at
  82. http://stackoverflow.com/questions/38047858/compile-gopacket-on-windows-64bit
  83. */
  84. package pcap