pcap-helper.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NFD_DAEMON_FACE_PCAP_HELPER_HPP
27 #define NFD_DAEMON_FACE_PCAP_HELPER_HPP
28 
29 #include "core/common.hpp"
30 
31 #ifndef HAVE_LIBPCAP
32 #error "Cannot include this file when libpcap is not available"
33 #endif
34 
35 // forward declarations
36 struct pcap;
37 typedef pcap pcap_t;
38 
39 namespace nfd {
40 namespace face {
41 
45 class PcapHelper : noncopyable
46 {
47 public:
48  class Error : public std::runtime_error
49  {
50  public:
51  explicit
52  Error(const std::string& what)
53  : std::runtime_error(what)
54  {
55  }
56  };
57 
63  explicit
64  PcapHelper(const std::string& interfaceName);
65 
66  ~PcapHelper();
67 
74  void
75  activate(int dlt);
76 
81  void
82  close();
83 
91  int
92  getFd() const;
93 
100  std::string
101  getLastError() const;
102 
108  size_t
109  getNDropped() const;
110 
118  void
119  setPacketFilter(const char* filter) const;
120 
131  std::tuple<const uint8_t*, size_t, std::string>
132  readNextPacket() const;
133 
134  operator pcap_t*() const
135  {
136  return m_pcap;
137  }
138 
139 private:
140  pcap_t* m_pcap;
141 };
142 
143 } // namespace face
144 } // namespace nfd
145 
146 #endif // NFD_DAEMON_FACE_PCAP_HELPER_HPP
PcapHelper(const std::string &interfaceName)
Create a libpcap context for live packet capture on a network interface.
Definition: pcap-helper.cpp:39
STL namespace.
Error(const std::string &what)
Definition: pcap-helper.hpp:52
size_t getNDropped() const
Get the number of packets dropped by the kernel, as reported by libpcap.
std::string getLastError() const
Get last error message.
Definition: pcap-helper.cpp:99
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::tuple< const uint8_t *, size_t, std::string > readNextPacket() const
Read the next packet captured on the interface.
pcap pcap_t
Definition: pcap-helper.hpp:36
void activate(int dlt)
Start capturing packets.
Definition: pcap-helper.cpp:64
int getFd() const
Obtain a file descriptor that can be used in calls such as select(2) and poll(2). ...
Definition: pcap-helper.cpp:87
void close()
Stop capturing and close the handle.
Definition: pcap-helper.cpp:78
void setPacketFilter(const char *filter) const
Install a BPF filter on the receiving socket.
Helper class for dealing with libpcap handles.
Definition: pcap-helper.hpp:45