-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPcap.cpp
More file actions
246 lines (207 loc) · 5.21 KB
/
Copy pathPcap.cpp
File metadata and controls
246 lines (207 loc) · 5.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "Pcap.h"
#include "stun_t.h"
#include "rtp_t.h"
#include "rtcp_t.h"
#include "turn_t.h"
#include "t38_t.h"
#include "mqtt_t.h"
#include "rtsp_t.h"
#include "websocket_t.h"
#include "custom_t.h"
#include <string.h>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
#ifdef WIN32
#else
#include <dlfcn.h>
#endif
using namespace stun;
using namespace rtp;
using namespace rtcp;
using namespace turn;
using namespace t38;
using namespace mqtt;
using namespace websocket;
using namespace rtsp;
using namespace custom;
/**
* @brief if you need your class to be parsed by VParse,
* please specialize it below
*/
template<>
struct is_validator<T38Rfc>
{
static constexpr bool value = true;
};
template<>
struct is_validator<RtcpRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<TurnRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<StunRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<RtpRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<MqttRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<WebSocketRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<RtspRFC>
{
static constexpr bool value = true;
};
template<>
struct is_validator<CustomRFC>
{
static constexpr bool value = true;
};
namespace libnetin {
void Pcap::showAll()
{
static char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* ifaces=NULL;
int n MAYBEUNUSED = pcap_findalldevs(&ifaces, errbuf);
for(pcap_if_t* curr = ifaces; curr ; curr = curr->next) {
std::cout << curr->name << "\r\n";
}
//TODO: check how to free the devices
#if 0 //leak for now
// pcap_freealldevs(ifaces);
for(pcap_if_t* curr = ifaces; curr ; ) {
pcap_if_t* t = curr;
curr = curr->next;
if (t) {
free(t);
}
}
// loop to all devs or capture on a specific one
#endif
}
Pcap::Pcap() : m_stop{1}, p_Cap{nullptr},errbuf{0}/* default mode is offline */
{
memset(&m_options, 0, sizeof(m_options));
memset(&m_nextRes, 0, sizeof(m_nextRes));
// if (!Pcap::m_pcapLoader.LoadAPcap("/home/ilian/storage/Builds/build-libnetin-Desktop_Qt_6_3_0_GCC_64bit-Debug/libpcap.a"))
// throw std::exception{};
}
Pcap::~Pcap()
{
if (p_Cap) {
pcap_close(p_Cap);
}
}
bool Pcap::offline(const char *fname)
{
p_Cap = pcap_open_offline(fname, errbuf);
if (!p_Cap) {
fprintf(stderr, "[ERROR]%s", errbuf);
return false;
}
return true;
}
// \Device\NPF_{8C83AA62-C8C8-42C4-9302-1EDA68EA3F5B}
bool Pcap::live(const char *dev)
{
p_Cap = pcap_open_live(dev,PCAP_BUF_SIZE,0,-1,errbuf);
if (p_Cap) {
m_options.live = true;
std::cout << "Opened " << dev << " in live mode" << std::endl;
return true;
} else {
std::cerr << "Error: " << errbuf << std::endl;
return false;
}
}
Result_t& Pcap::next()
{
m_nextRes.data = pcap_next(p_Cap, &m_nextRes.out);
return m_nextRes;
}
bool Pcap::hasNext() const
{
return m_nextRes.data != nullptr;
}
/**
* @brief Pcap::loop - example loop of packets...
* @todo: Add a file appender to the threaded loop
*/
void Pcap::loop()
{
std::ofstream jsonfile;
jsonfile.open ("out.json");
if (m_options.live) {
std::thread t{[&]() {
jsonfile << serializer.beginSerialize();
for(Result_t& res = next(); m_stop.load(); res = next())
{
MAYBEUNUSED auto resultNwork =
VParse(RtspRFC{res},
MqttRFC{res},
T38Rfc{res},
RtcpRFC{res},
TurnRFC{res},
StunRFC{res},
RtpRFC{res},
WebSocketRFC{res},
CustomRFC{res});
jsonfile << serializer.serialzeNow();
}
}};
t.detach();
std::cout << "Press Q or q to stop..." << std::endl;
char q='a';
std::cin >> q;
switch (q) {
case 'q':
case 'Q': {
//win test - \Device\NPF_{8C83AA62-C8C8-42C4-9302-1EDA68EA3F5B}
m_stop.store(0);
jsonfile << serializer.endSerialize();
jsonfile.close();
break;
}
default:
break;
}
} else {
for(Result_t& res = next(); hasNext(); res = next() )
{
MAYBEUNUSED auto resultNwork =
VParse(WebSocketRFC{res},
MqttRFC{res},
T38Rfc{res},
RtcpRFC{res},
TurnRFC{res},
StunRFC{res},
RtpRFC{res},
RtspRFC{res},
CustomRFC{res});
}
jsonfile << serializer.serialize();
jsonfile.close();
}
std::cout << "Finished capturing... writing out...\r\n";
}
}//libnetin