1 | /** |
---|
2 | * @file PCAPLiveCapture.cpp |
---|
3 | * @author Tomas Urban |
---|
4 | * @version 0.4 |
---|
5 | * @date 23/07/2009 |
---|
6 | */ |
---|
7 | #include <boost/tokenizer.hpp> |
---|
8 | #include "PCAPLiveCapture.h" |
---|
9 | #include "Logger/Logger.h" |
---|
10 | #include <ctime> |
---|
11 | |
---|
12 | PCAPLiveCapture::PCAPLiveCapture() |
---|
13 | { |
---|
14 | } |
---|
15 | |
---|
16 | PCAPLiveCapture::~PCAPLiveCapture(void) |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | ECaptureInitResult PCAPLiveCapture::OpenDevice(const std::string sParams) |
---|
21 | { |
---|
22 | CloseDevice(); |
---|
23 | int nCounter = 1; |
---|
24 | const char * pszData = sParams.c_str(); |
---|
25 | for (int i = 0; pszData[i]; i++) |
---|
26 | if (pszData[i] == ';') |
---|
27 | ++nCounter; |
---|
28 | |
---|
29 | pcap_t ** tmpBuf = new pcap_t*[nCounter]; |
---|
30 | |
---|
31 | std::string sTmp = sParams; |
---|
32 | boost::char_separator<char> sep(";"); |
---|
33 | boost::tokenizer<boost::char_separator<char> > tokens(sTmp, sep); |
---|
34 | nCounter = 0; |
---|
35 | ECaptureInitResult res = ECaptureInit_Successful; |
---|
36 | for(boost::tokenizer<boost::char_separator<char> >::iterator it = tokens.begin(); |
---|
37 | it != tokens.end(); ++it) |
---|
38 | { |
---|
39 | pcap_t * pHandle = OpenPcapSource(it->c_str()); |
---|
40 | if (pHandle) |
---|
41 | tmpBuf[nCounter++] = pHandle; |
---|
42 | else |
---|
43 | { |
---|
44 | std::string s = "Failed to open adapter \""; |
---|
45 | s += *it; |
---|
46 | s += "\" for packet capture."; |
---|
47 | Logger::Instance().LogWarning(s); |
---|
48 | res = ECaptureInit_PartiallySuccessful; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | bool bRes = true; |
---|
53 | if (nCounter > 0) |
---|
54 | { |
---|
55 | SetPcapHandles(tmpBuf, nCounter); |
---|
56 | std::string s = boost::lexical_cast<std::string>(nCounter); |
---|
57 | s += " adapter(s) available for packet capture"; |
---|
58 | Logger::Instance().LogInfo(s); |
---|
59 | timeval timestamp; |
---|
60 | timestamp.tv_usec = 0; |
---|
61 | #ifdef WIN32 |
---|
62 | timestamp.tv_sec = static_cast<long>(time(0)); |
---|
63 | #else |
---|
64 | timestamp.tv_sec = time(0); |
---|
65 | #endif |
---|
66 | SetStartPoint(timestamp); |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | Logger::Instance().LogError("No adapter available for packet capture"); |
---|
71 | res = ECaptureInit_Failed; |
---|
72 | } |
---|
73 | delete tmpBuf; |
---|
74 | return res; |
---|
75 | } |
---|
76 | |
---|
77 | bool PCAPLiveCapture::SetCaptureFile(const std::string sFile) |
---|
78 | { |
---|
79 | return InitCaptureFile(sFile); |
---|
80 | } |
---|
81 | |
---|