-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptureFuncs.cpp
More file actions
82 lines (70 loc) · 2.25 KB
/
captureFuncs.cpp
File metadata and controls
82 lines (70 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2021, Pejman Taslimi
// You may distribute under the terms of either the GNU General Public
// License or the Artistic License, as specified in the MYLICENSE file.
/*
* FILENAME :
* DESCRIPTION :
*
* Author : Mani Tasl
* Date : Jun 2021.
*
*/
#include<iostream>
#include<pcap.h>
#include<chrono>
#include<vector>
#include<functional> //For std::function
#include"captureFuncs.h"
PcapLib::PcapLib(){
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&mIfDevs, errbuf) == -1) {
std::cout << "pcap_lookupdev() failed: " << errbuf << std::endl;
exit(1);
}
}
PcapLib::~PcapLib(){
pcap_freealldevs(mIfDevs);
}
std::vector<std::string>& PcapLib::getIfNames(void){
mvwIfNames.clear();
mvwIfNames.push_back(std::string(mIfDevs->name));
for(pcap_if_t* d = mIfDevs->next; d != NULL; d = d->next)
mvwIfNames.push_back(std::string(d->name));
return mvwIfNames;
}
int PcapLib::captureInit(const std::string wDevName, const std::string wFilename){
mwFilename = wFilename;
mcpDev = const_cast<char *>(wDevName.c_str());
char errbuf[PCAP_ERRBUF_SIZE];
mDescr = pcap_open_live(mcpDev, BUFSIZ, 0, -1, errbuf);
if (mDescr == NULL) {
std::cout << "pcap_open_live() failed: " << errbuf << std::endl;
return 1;
}
mDumper = pcap_dump_open(mDescr,mwFilename.c_str());
if (mDumper == NULL) {
std::cout << "pcap_dump_open() failed: " << pcap_geterr(mDescr)
<< "\n filename = " << mwFilename << std::endl;
return 1;
}
return 0;
}
int PcapLib::captureFor(int iDurationSec, StatusUpdateHandler handler){
miDurationSec = iDurationSec;
time_t timEnd = std::time(nullptr) + miDurationSec;
int iCount;
while (timEnd > std::time(nullptr)) {
// if(pcap_dispatch(descr, -1, packetHandler, (u_char*) &iBatch) == PCAP_ERROR){
iCount = pcap_dispatch(mDescr, -1, &pcap_dump, (u_char*)mDumper);
if( iCount == PCAP_ERROR){
std::cout <<pcap_geterr(mDescr);
return 1;
}
// if(iCount)
// std::cout <<iCount <<" packets captured. (in class method)\n";
handler(iCount);
}
pcap_dump_close(mDumper);
pcap_close(mDescr);
return 0;
}