This repository was archived by the owner on Nov 8, 2024. It is now read-only.
forked from frankie-zeng/node_npcap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpcap_session.cpp
More file actions
316 lines (261 loc) · 11.6 KB
/
npcap_session.cpp
File metadata and controls
316 lines (261 loc) · 11.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "npcap_session.h"
#include "stdio.h"
Napi::Object PcapSession::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func =
DefineClass(env,
"PcapSession",
{
InstanceMethod("open_live", &PcapSession::OpenLive),
InstanceMethod("open_offline", &PcapSession::OpenOffline),
InstanceMethod("load_buffer", &PcapSession::LoadBuffer),
InstanceMethod("close",&PcapSession::Close),
InstanceMethod("stats",&PcapSession::Stats),
InstanceMethod("inject",&PcapSession::Inject),
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
// constructor->SuppressDestruct();
exports.Set("PcapSession", func);
return exports;
}
PcapSession::PcapSession(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<PcapSession>(info) {
this->opened=false;
return;
}
DWORD WINAPI PcapSession::PacketThread(LPVOID lpParam)
{
PcapSession* the=(PcapSession*)lpParam;
int res;
struct pcap_pkthdr* pkthdr;
const u_char* packet;
the->opened=true;
while((res = pcap_next_ex( the->pcap_handle, &pkthdr,&packet)) >= 0){
if(res == 0)
/* Timeout elapsed */
continue;
size_t copy_len = pkthdr->caplen;
if (copy_len > the->buffer_length) {
copy_len = the->buffer_length;
}
memcpy(the->buffer_data, packet, copy_len);
memcpy(the->header_data, &(pkthdr->ts.tv_sec), 4);
memcpy(the->header_data + 4, &(pkthdr->ts.tv_usec), 4);
memcpy(the->header_data + 8, &(pkthdr->caplen), 4);
memcpy(the->header_data + 12, &(pkthdr->len), 4);
the->tsFn.BlockingCall();
}
pcap_close(the->pcap_handle);
the->opened=false;
return 0;
}
Napi::Value PcapSession::Stats(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
struct pcap_stat ps;
if (this->pcap_handle == NULL) {
Napi::TypeError::New(env, "Error: pcap session already closed").ThrowAsJavaScriptException();
}
if (pcap_stats(this->pcap_handle, &ps) == -1) {
Napi::TypeError::New(env, "Error in pcap_stats").ThrowAsJavaScriptException();
}
Napi::Object stats_obj=Napi::Object::New(env);
stats_obj.Set("ps_recv",Napi::Number::New(env,ps.ps_recv));
stats_obj.Set("ps_drop",Napi::Number::New(env,ps.ps_drop));
stats_obj.Set("ps_ifdrop",Napi::Number::New(env,ps.ps_ifdrop));
return stats_obj;
}
Napi::Value PcapSession::Inject(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
if (info.Length() != 1) {
Napi::TypeError::New(env, "Inject takes exactly one argument").ThrowAsJavaScriptException();
}
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "First argument must be a buffer").ThrowAsJavaScriptException();
}
if (this->pcap_handle == NULL) {
Napi::TypeError::New(env, "Error: pcap session already closed").ThrowAsJavaScriptException();
}
Napi::Buffer<char> buffer_obj=info[0].As<Napi::Buffer<char>>();
char* bufferData = buffer_obj.Data();
size_t bufferLength = buffer_obj.Length();
if (pcap_inject(this->pcap_handle, bufferData, bufferLength) != (int)bufferLength) {
Napi::TypeError::New(env, "Pcap inject failed.").ThrowAsJavaScriptException();
}
return Napi::Number::New(env,bufferLength);
}
Napi::Value PcapSession::LoadBuffer(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
if (info.Length() != 2) {
Napi::TypeError::New(env, "Dispatch takes exactly two arguments").ThrowAsJavaScriptException();
}
if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "First argument must be a buffer").ThrowAsJavaScriptException();
}
if (!info[1].IsBuffer()) {
Napi::TypeError::New(env, "Second argument must be a buffer").ThrowAsJavaScriptException();
}
Napi::Buffer<char> buffer_obj=info[0].As<Napi::Buffer<char>>();
this->buffer_data = buffer_obj.Data();
this->buffer_length = buffer_obj.Length();
Napi::Buffer<char> header_obj=info[1].As<Napi::Buffer<char>>();
this->header_data = header_obj.Data();
return Napi::Number::New(info.Env(),0);
}
Napi::Value PcapSession::Close(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
if (this->pcap_dump_handle != NULL) {
pcap_dump_close(this->pcap_dump_handle);
this->pcap_dump_handle = NULL;
}
if (this->pcap_handle != NULL) {
pcap_breakloop(this->pcap_handle);
}
return Napi::Number::New(env,0);
}
Napi::Value PcapSession::Open(bool live, const Napi::CallbackInfo& info)
{
char errbuf[PCAP_ERRBUF_SIZE];
Napi::Env env = info.Env();
if(this->opened)
return Napi::Number::New(env,0);
if (info.Length() == 10) {
if (!info[0].IsString()) {
Napi::TypeError::New(env, "pcap Open: info[0] must be a String").ThrowAsJavaScriptException();
}
if (!info[1].IsString()) {
Napi::TypeError::New(env, "pcap Open: info[1] must be a String").ThrowAsJavaScriptException();
}
if (!info[2].IsNumber()) {
Napi::TypeError::New(env, "pcap Open: info[2] must be a Number").ThrowAsJavaScriptException();
}
if (!info[3].IsNumber()) {
Napi::TypeError::New(env, "pcap Open: info[3] must be a Number").ThrowAsJavaScriptException();
}
if (!info[4].IsString()) {
Napi::TypeError::New(env, "pcap Open: info[4] must be a String").ThrowAsJavaScriptException();
}
if (!info[5].IsFunction()) {
Napi::TypeError::New(env, "pcap Open: info[5] must be a Function").ThrowAsJavaScriptException();
}
if (!info[6].IsBoolean()) {
Napi::TypeError::New(env, "pcap Open: info[6] must be a Boolean").ThrowAsJavaScriptException();
}
if (!info[7].IsNumber()) {
Napi::TypeError::New(env, "pcap Open: info[7] must be a Number").ThrowAsJavaScriptException();
}
if (!info[8].IsFunction()) { // warning function
Napi::TypeError::New(env, "pcap Open: info[8] must be a Function").ThrowAsJavaScriptException();
}
if (!info[9].IsBoolean()) {
Napi::TypeError::New(env, "pcap Open: info[9] must be a Boolean").ThrowAsJavaScriptException();
}
} else {
Napi::TypeError::New(env, "pcap Open: expecting 8 arguments").ThrowAsJavaScriptException();
}
std::string device = info[0].As<Napi::String>().Utf8Value().data();
std::string filter = info[1].As<Napi::String>().Utf8Value().data();
int buffer_size =info[2].As<Napi::Number>().Int32Value();
int snap_length = info[3].As<Napi::Number>().Int32Value();
int buffer_timeout = info[7].As<Napi::Number>().Int32Value();
std::string pcap_output_filename = info[4].As<Napi::String>().Utf8Value().data();
this->tsFn=Napi::ThreadSafeFunction::New(env,info[5].As<Napi::Function>(),"callback",0,1);
this->pcap_dump_handle = NULL;
if (live) {
if (pcap_lookupnet(device.c_str(), &this->net, &this->mask, errbuf) == -1) {
this->net = 0;
this->mask = 0;
info[8].As<Napi::Function>().Call(env.Global(),{Napi::String::New(env,errbuf)});
}
this->pcap_handle = pcap_create(device.c_str(),errbuf);
if (this->pcap_handle == NULL) {
Napi::TypeError::New(env, errbuf).ThrowAsJavaScriptException();
}
// 64KB is the max IPv4 packet size
if (pcap_set_snaplen(this->pcap_handle, snap_length) != 0) {
Napi::TypeError::New(env, "error setting snaplen").ThrowAsJavaScriptException();
}
if (info[9].As<Napi::Boolean>()) {
if (pcap_set_promisc(this->pcap_handle, 1) != 0) {
Napi::TypeError::New(env, "error setting promiscuous mode").ThrowAsJavaScriptException();
}
}
// Try to set buffer size. Sometimes the OS has a lower limit that it will silently enforce.
if (pcap_set_buffer_size(this->pcap_handle, buffer_size) != 0) {
Napi::TypeError::New(env, "error setting buffer size").ThrowAsJavaScriptException();
}
if (buffer_timeout > 0) {
// set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
if (pcap_set_timeout(this->pcap_handle, buffer_timeout) != 0) {
Napi::TypeError::New(env, "error setting read timeout").ThrowAsJavaScriptException();
}
}
// timeout <= 0 is undefined behaviour, we'll set immediate mode instead. (timeout is ignored in immediate mode)
if (pcap_set_immediate_mode(this->pcap_handle, (buffer_timeout <= 0)) != 0) {
Napi::TypeError::New(env, "error setting immediate mode").ThrowAsJavaScriptException();
}
if (info[6].As<Napi::Boolean>()) {
if (pcap_set_rfmon(this->pcap_handle, 1) != 0) {
Napi::TypeError::New(env, pcap_geterr(this->pcap_handle)).ThrowAsJavaScriptException();
}
}
if (pcap_activate(this->pcap_handle) != 0) {
Napi::TypeError::New(env, pcap_geterr(this->pcap_handle)).ThrowAsJavaScriptException();
}
if (pcap_output_filename.length() > 0) {
this->pcap_dump_handle = pcap_dump_open(this->pcap_handle, (char *) pcap_output_filename.c_str());
if (this->pcap_dump_handle == NULL) {
Napi::TypeError::New(env, "error opening dump").ThrowAsJavaScriptException();
}
}
if (pcap_setnonblock(this->pcap_handle, 1, errbuf) == -1) {
Napi::TypeError::New(env, errbuf).ThrowAsJavaScriptException();
}
} else {
// Device is the path to the savefile
this->pcap_handle = pcap_open_offline(device.c_str(), errbuf);
if (this->pcap_handle == NULL) {
Napi::TypeError::New(env, errbuf).ThrowAsJavaScriptException();
}
}
if (filter.length() > 0) {
if (pcap_compile(this->pcap_handle, &this->fp, filter.c_str(), 1, this->net) == -1) {
Napi::TypeError::New(env, pcap_geterr(this->pcap_handle)).ThrowAsJavaScriptException();
}
if (pcap_setfilter(this->pcap_handle, &this->fp) == -1) {
Napi::TypeError::New(env, pcap_geterr(this->pcap_handle)).ThrowAsJavaScriptException();
}
pcap_freecode(&this->fp);
}
int link_type = pcap_datalink(this->pcap_handle);
Napi::String ret;
switch (link_type) {
case DLT_NULL:
ret = Napi::String::New(info.Env(),"LINKTYPE_NULL");
break;
case DLT_EN10MB: // most wifi interfaces pretend to be "ethernet"
ret = Napi::String::New(info.Env(),"LINKTYPE_ETHERNET");
break;
case DLT_IEEE802_11_RADIO: // 802.11 "monitor mode"
ret = Napi::String::New(info.Env(),"LINKTYPE_IEEE802_11_RADIO");
break;
case DLT_RAW: // "raw IP"
ret = Napi::String::New(info.Env(),"LINKTYPE_RAW");
break;
case DLT_LINUX_SLL: // "Linux cooked capture"
ret = Napi::String::New(info.Env(),"LINKTYPE_LINUX_SLL");
break;
default:
snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unknown linktype %d", link_type);
ret = Napi::String::New(info.Env(),errbuf);
break;
}
this->rThread=CreateThread(NULL, NULL, PacketThread, (LPVOID)this, NULL, NULL);
return ret;
}
Napi::Value PcapSession::OpenLive(const Napi::CallbackInfo& info){
return this->Open(true, info);
}
Napi::Value PcapSession::OpenOffline(const Napi::CallbackInfo& info){
return this->Open(false, info);
}