libDwm-0.9.45
DwmPcap.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2005-2007, 2016
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMPCAP_HH_
42#define _DWMPCAP_HH_
43
44extern "C" {
45 #include <pcap.h>
46}
47
48#include <map>
49#include <string>
50
51namespace Dwm {
52
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
56 class Pcap
57 {
58 public:
59 //------------------------------------------------------------------------
61 //------------------------------------------------------------------------
63
64 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
68
69 int SetBufferSize(int bufferSize);
70
71 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
78 bool OpenLive(const std::string & device, int snaplen,
79 bool promisc, int timeout_msecs);
80
81 //------------------------------------------------------------------------
86 //------------------------------------------------------------------------
87 bool SetDirection(pcap_direction_t direction);
88
89 //------------------------------------------------------------------------
92 //------------------------------------------------------------------------
93 bool OpenOffline(const std::string & fname);
94
95 //------------------------------------------------------------------------
99 //------------------------------------------------------------------------
100 bool SetFilter(const std::string & filter, bool optimize);
101
102 //------------------------------------------------------------------------
104 //------------------------------------------------------------------------
105 int Dispatch(int count, pcap_handler callback, uint8_t *user);
106
107 //------------------------------------------------------------------------
109 //------------------------------------------------------------------------
110 int Loop(int count, pcap_handler callback, uint8_t *user);
111
112 //------------------------------------------------------------------------
114 //------------------------------------------------------------------------
115 void BreakLoop();
116
117 //------------------------------------------------------------------------
120 //------------------------------------------------------------------------
121 bool Close();
122
123 //------------------------------------------------------------------------
126 //------------------------------------------------------------------------
127 const std::string & LastError() const;
128
129 //------------------------------------------------------------------------
134 //------------------------------------------------------------------------
135 int DataLinkOffset() const;
136
137 private:
138 static std::map<int,int> _dataLinkOffsets;
139
140 pcap_t *_pcap;
141 std::string _errbuf;
142 };
143
144} // namespace Dwm
145
146#endif // _DWMPCAP_HH_
147
148//---------------------------- emacs settings -----------------------------
149// Local Variables:
150// mode: C++
151// tab-width: 2
152// indent-tabs-mode: nil
153// c-basic-offset: 2
154// End:
155//-------------------------------------------------------------------------
This is a pcap wrapper/utility class.
Definition DwmPcap.hh:57
bool OpenOffline(const std::string &fname)
Opens the file fname for reading.
int Loop(int count, pcap_handler callback, uint8_t *user)
A wrapper for pcap_loop(). See the pcap(3) manpage.
bool Close()
Closes the capture device.
Pcap()
Constructor.
const std::string & LastError() const
Returns the last error string from a failed OpenLive() or OpenOffline() call.
~Pcap()
Destructor. Will close the contained pcap device if it's open.
int DataLinkOffset() const
Returns the length of the link layer header for the capture device.
bool SetFilter(const std::string &filter, bool optimize)
Sets the filter for the capture device.
bool SetDirection(pcap_direction_t direction)
Specify whether to capture incoming packets, outgoing packets, or both.
bool OpenLive(const std::string &device, int snaplen, bool promisc, int timeout_msecs)
Open device for live packet capture.
int Dispatch(int count, pcap_handler callback, uint8_t *user)
A wrapper for pcap_dispatch(). See the pcap(3) manpage.
void BreakLoop()
A wrapper for pcap_breakloop().