doc.go 4.0 KB

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