Debian Codegraph @

protolib/src/common/pcapCap.cpp

raw
1 /**
2 * @file pcapCap.cpp
3 * @brief Implementation of ProtoCap class using "libpcap" for packet capture.
4 */
5
6 #include "protoCap.h" // for ProtoCap definition
7 #include "protoSocket.h"
8 #include "protoDebug.h"
9 #include <errno.h>
10 #ifdef WIN32
11 //#include <winpcap.h>
12 #include <pcap.h>
13 #else
14 #include <pcap.h> // for libpcap routines
15 #include <unistd.h> // for write()
16 #endif // end if/else WIN32/UNIX
17 /**
18 * @class PcapCap
19 *
20 * @brief This is an implementation of the Protolib class ProtoCap
21 * using "libpcap" for packet capture
22 *
23 */
24 class PcapCap : public ProtoCap
25 {
26 public:
27 PcapCap();
28 ~PcapCap();
29
30 bool Open(const char* interfaceName = NULL);
31 bool IsOpen(){return (NULL != pcap_device);}
32 void Close();
33 bool Send(const char* buffer, unsigned int& numBytes);
34 bool Recv(char* buffer, unsigned int& numBytes, Direction* direction = NULL);
35
36 private:
37 pcap_t* pcap_device;
38 }; // end class PcapCap
39
40
41 ProtoCap* ProtoCap::Create()
42 {
43 return (static_cast<ProtoCap*>(new PcapCap));
44 }
45
46 PcapCap::PcapCap()
47 : pcap_device(NULL)
48 {
49 StartInputNotification(); // enable input notification by default
50 }
51
52 PcapCap::~PcapCap()
53 {
54 Close();
55 }
56
57 bool PcapCap::Open(const char* interfaceName)
58 {
59 int ifIndex;
60 char buffer[256];
61 if (NULL == interfaceName)
62 {
63 // Try to determine a "default" interface
64 ProtoAddress localAddress;
65 if (!localAddress.ResolveLocalAddress())
66 {
67 PLOG(PL_ERROR, "PcapCap::Open() error: couldn't auto determine local interface\n");
68 return false;
69 }
70 if (!ProtoSocket::GetInterfaceName(localAddress, buffer, 256))
71 {
72 PLOG(PL_ERROR, "PcapCap::Open() error: couldn't determine local interface name\n");
73 return false;
74 }
75 interfaceName = buffer;
76 ifIndex = ProtoSocket::GetInterfaceIndex(interfaceName);
77 }
78 else
79 {
80 ifIndex = ProtoSocket::GetInterfaceIndex(interfaceName);
81 if (ifIndex != 0)
82 {
83 if(!ProtoSocket::GetInterfaceName(ifIndex ,buffer,256))
84 {
85 PLOG(PL_ERROR,"PcapCap::Open() error: couldn't get interface name of index %d\n",ifIndex);
86 return false;
87 }
88 interfaceName = buffer;
89 }
90 else
91 {
92 PLOG(PL_ERROR,"PcapCap::Open() error: coun't get interface index from interface name %s\n",interfaceName);
93 return false;
94 }
95 }
96 if (!ProtoSocket::GetInterfaceAddress(interfaceName, ProtoAddress::ETH, if_addr))
97 {
98 PLOG(PL_ERROR, "PcapCap::Open() error getting interface MAC address\n");
99 return false;
100 }
101 Close(); // just in case
102 char errbuf[PCAP_ERRBUF_SIZE+1];
103 errbuf[0] = '\0';
104
105 #ifdef WIN32
106 pcap_if_t *alldevs;
107 pcap_if_t *d;
108
109 /* Retrieve the device list to get the device prefix */
110 /* (pcap index is different than windows adapater index! */
111
112 if (pcap_findalldevs(&alldevs,errbuf) == -1)
113 {
114 PLOG(PL_ERROR,"PcapCap::Open pcap_findalldevs failed.\n");
115
116 return false;
117 }
118 /* Find the selected adapter to get the full device name*/
119 int i;
120 for (d=alldevs,i=0;d != NULL;d=d->next,i++)
121 {
122 if (char* namePtr = strchr(d->name,'{'))
123 if (!strcmp(interfaceName,namePtr))
124 break;
125 }
126
127 if (d == NULL)
128 {
129 PLOG(PL_ERROR,"PcapCap::Open() Device (%s) not found in pcap list!\n",interfaceName);
130 pcap_freealldevs(alldevs);
131 return false;
132 }
133
134 TRACE("interfacename before pcap open live %s %s\n",d->name,d->description);
135
136 pcap_device = pcap_open_live(d->name, 65535, 1, 0, errbuf);
137 #else
138 TRACE("interfacename before pcap open live %s\n",interfaceName);
139
140 pcap_device = pcap_open_live((char *)interfaceName, 65535, 1, 0, errbuf);
141 #endif
142 if (NULL == pcap_device)
143 {
144 PLOG(PL_ERROR, "pcapExample: pcap_open_live() error: %s\n", errbuf);
145 #ifdef WIN32
146 pcap_freealldevs(alldevs);
147 #endif
148 return false;
149 }
150
151 #ifdef WIN32 // set a minimum pkt num to copy
152 pcap_setmintocopy(pcap_device,1);
153 #endif
154
155 // set non-blocking for async I/O
156 if (-1 == pcap_setnonblock(pcap_device, 1, errbuf))
157 PLOG(PL_ERROR, "pcapExample: pcap_setnonblock() warning: %s\n", errbuf);
158 #ifdef WIN32
159 input_handle = pcap_getevent(pcap_device);
160 input_event_handle = input_handle;
161 #else
162 descriptor = pcap_get_selectable_fd(pcap_device);
163 #endif // if/else WIN32/UNIX
164
165 if (!ProtoCap::Open(interfaceName))
166 {
167 PLOG(PL_ERROR, "PcapCap::Open() ProtoCap::Open() error\n");
168 Close();
169 #ifdef WIN32
170 pcap_freealldevs(alldevs);
171 #endif
172 return false;
173 }
174 if_index = ifIndex;
175 #ifdef WIN32
176 pcap_freealldevs(alldevs);
177 #endif
178 return true;
179 } // end PcapCap::Open()
180
181 void PcapCap::Close()
182 {
183 if (NULL != pcap_device)
184 {
185 ProtoCap::Close();
186 pcap_close(pcap_device);
187 pcap_device = NULL;
188 #ifdef WIN32
189 input_handle = INVALID_HANDLE_VALUE;
190 #else
191 descriptor = -1;
192 #endif // if/else WIN32/UNIX
193 }
194 } // end PcapCap::Close()
195
196 bool PcapCap::Recv(char* buffer, unsigned int& numBytes, Direction* direction)
197 {
198 struct pcap_pkthdr* hdr;
199 const u_char* data;
200 if (NULL != direction) *direction = UNSPECIFIED; // (TBD) try to get a direction
201 switch (pcap_next_ex(pcap_device, &hdr, &data))
202 {
203 case 1: // pkt read
204 {
205 unsigned int copyLen = (numBytes > hdr->caplen) ? hdr->caplen : numBytes;
206 memcpy(buffer, data, copyLen);
207 numBytes = copyLen;
208 // We may get false INBOUND when raw frames are sent with different src MAC addr
209 if ((NULL != direction) && (0 == memcmp(if_addr.GetRawHostAddress(), buffer + 6, 6)))
210 *direction = OUTBOUND;
211 else
212 *direction = INBOUND;
213 return true;
214 }
215 case 0: // no pkt ready?
216 {
217 numBytes = 0;
218 return true;
219 }
220 default: // error (or EOF for offline)
221 {
222 PLOG(PL_ERROR, "PcapCap::Recv() pcap_next_ex() error\n");
223 numBytes = 0;
224 return false;
225 }
226 }
227 } // end PcapCap::Recv()
228
229 bool PcapCap::Send(const char* buffer, unsigned int& numBytes)
230 {
231 // Make sure packet is a type that is OK for us to send
232 // (Some packets seem to cause a PF_PACKET socket trouble)
233 UINT16 type;
234 memcpy(&type, buffer+12, 2);
235 type = ntohs(type);
236 if (type <= 0x05dc) // assume it's 802.3 Length and ignore
237 {
238 PLOG(PL_DEBUG, "PcapCap::Send() unsupported 802.3 frame (len = %04x)\n", type);
239 return false;
240 }
241
242 #ifdef WIN32
243 int pcapreturn = pcap_sendpacket(pcap_device,(unsigned char*)buffer, numBytes);
244 if (0 != pcapreturn)
245 {
246 switch (errno)
247 {
248 case ENOBUFS:
249 case EWOULDBLOCK:
250 numBytes = 0;
251 default:
252 PLOG(PL_ERROR, "PcapCap::Send() error: %s", GetErrorString());
253 break;
254 }
255 return false;
256 }
257 #else
258 for (;;)
259 {
260 ssize_t result = write(descriptor, buffer, numBytes);
261 if (result < 0)
262 {
263 switch (errno)
264 {
265 case EINTR:
266 continue; // try again
267 case EWOULDBLOCK:
268 numBytes = 0;
269 case ENOBUFS:
270 // since this doesn't block write() or select(), etc
271 default:
272 PLOG(PL_ERROR, "PcapCap::Send() error: %s", GetErrorString());
273 break;
274 }
275 return false;
276 }
277 else
278 {
279 ASSERT(result == numBytes);
280 break;
281 }
282 }
283 #endif
284 return true;
285 } // end PcapCap::Send()