This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcapobj.c
447 lines (362 loc) · 9.94 KB
/
pcapobj.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 2003 CORE Security Technologies
*
* This software is provided under under a slightly modified version
* of the Apache Software License. See the accompanying LICENSE file
* for more information.
*
* $Id: pcapobj.cc 45 2009-02-05 14:11:33Z gera $
*/
#include <Python.h>
#include <pcap.h>
#include "pcapobj.h"
#include "pcapy.h"
#include "pcapdumper.h"
#include "pcap_pkthdr.h"
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
/* internal pcapobject */
typedef struct {
PyObject_HEAD
pcap_t *pcap;
bpf_u_int32 net;
bpf_u_int32 mask;
} pcapobject;
/* PcapType */
static void
pcap_dealloc(register pcapobject* pp)
{
if ( pp->pcap )
pcap_close(pp->pcap);
pp->pcap = NULL;
PyObject_Del(pp);
}
/* pcap methods */
static PyObject* p_getnet(register pcapobject* pp, PyObject* args);
static PyObject* p_getmask(register pcapobject* pp, PyObject* args);
static PyObject* p_setfilter( register pcapobject* pp, PyObject* args );
static PyObject* p_next(register pcapobject* pp, PyObject* args);
static PyObject* p_dispatch(register pcapobject* pp, PyObject* args);
static PyObject* p_loop(register pcapobject* pp, PyObject* args);
static PyObject* p_datalink(register pcapobject* pp, PyObject* args);
static PyObject* p_setnonblock(register pcapobject* pp, PyObject* args);
static PyObject* p_getnonblock(register pcapobject* pp, PyObject* args);
static PyObject* p_dump_open(register pcapobject* pp, PyObject* args);
#ifdef HAVE_PCAP_SENDPACKET
static PyObject* p_sendpacket(register pcapobject* pp, PyObject* args);
#endif
static PyMethodDef p_methods[] = {
{"loop", (PyCFunction) p_loop, METH_VARARGS, "loops packet dispatching"},
{"dispatch", (PyCFunction) p_dispatch, METH_VARARGS, "dispatchs packets"},
{"next", (PyCFunction) p_next, METH_NOARGS, "returns next packet"},
{"setfilter", (PyCFunction) p_setfilter, METH_VARARGS, "compiles and sets a BPF capture filter"},
{"getnet", (PyCFunction) p_getnet, METH_VARARGS, "returns the network address for the device"},
{"getmask", (PyCFunction) p_getmask, METH_VARARGS, "returns the netmask for the device"},
{"datalink", (PyCFunction) p_datalink, METH_VARARGS, "returns the link layer type"},
{"getnonblock", (PyCFunction) p_getnonblock, METH_VARARGS, "returns the current `non-blocking' state"},
{"setnonblock", (PyCFunction) p_setnonblock, METH_VARARGS, "puts into `non-blocking' mode, or take it out, depending on the argument"},
{"dump_open", (PyCFunction) p_dump_open, METH_VARARGS, "creates a dumper object"},
#ifdef HAVE_PCAP_SENDPACKET
{"sendpacket", (PyCFunction) p_sendpacket, METH_VARARGS, "sends a packet through the interface"},
#endif
{NULL, NULL} /* sentinel */
};
static PyObject*
pcap_getattr(pcapobject* pp, char* name)
{
return Py_FindMethod(p_methods, (PyObject*)pp, name);
}
PyTypeObject Pcaptype = {
PyObject_HEAD_INIT(NULL)
0,
"Reader",
sizeof(pcapobject),
0,
/* methods */
(destructor)pcap_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)pcap_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};
PyObject*
new_pcapobject(pcap_t *pcap, bpf_u_int32 net, bpf_u_int32 mask)
{
pcapobject *pp;
pp = PyObject_New(pcapobject, &Pcaptype);
if (pp == NULL)
return NULL;
pp->pcap = pcap;
pp->net = net;
pp->mask = mask;
return (PyObject*)pp;
}
static void ntos(char* dst, unsigned int n, int ip)
{
ip = htonl(ip);
snprintf(dst, n, "%i.%i.%i.%i",
((ip >> 24) & 0xFF),
((ip >> 16) & 0xFF),
((ip >> 8) & 0xFF),
(ip & 0xFF));
}
static PyObject*
p_getnet(register pcapobject* pp, PyObject* args)
{
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
char ip_str[20];
ntos(ip_str, sizeof(ip_str), pp->net);
return Py_BuildValue("s", ip_str);
}
static PyObject*
p_getmask(register pcapobject* pp, PyObject* args)
{
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
char ip_str[20];
ntos(ip_str, sizeof(ip_str), pp->mask);
return Py_BuildValue("s", ip_str);
}
static PyObject*
p_setfilter(register pcapobject* pp, PyObject* args)
{
struct bpf_program bpfprog;
int status;
char* str;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!PyArg_ParseTuple(args,"s:setfilter",&str))
return NULL;
status = pcap_compile(pp->pcap, &bpfprog, str, 1, pp->mask);
if (status)
{
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
status = pcap_setfilter(pp->pcap, &bpfprog);
if (status)
{
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
p_next(register pcapobject* pp, PyObject* args)
{
struct pcap_pkthdr hdr;
const unsigned char *buf;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
/* allow threads as this might block */
Py_BEGIN_ALLOW_THREADS;
buf = pcap_next(pp->pcap, &hdr);
Py_END_ALLOW_THREADS;
if(!buf)
{
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
PyObject *pkthdr = new_pcap_pkthdr(&hdr);
if (pkthdr)
{
PyObject *ret = NULL;
ret = Py_BuildValue("(Os#)", pkthdr, buf, hdr.caplen);
Py_DECREF(pkthdr);
return ret;
}
PyErr_SetString(PcapError, "Can't build pkthdr");
return NULL;
}
struct PcapCallbackContext {
pcap_t* ppcap_t;
PyObject* pyfunc;
PyThreadState* thread_state;
};
static void
PythonCallBack(u_char *user,
const struct pcap_pkthdr *header,
const u_char *packetdata)
{
PyObject *arglist, *result;
unsigned int *len;
struct PcapCallbackContext *pctx;
len = (unsigned int *)&header->caplen;
pctx = (struct PcapCallbackContext *)user;
PyEval_RestoreThread(pctx->thread_state);
PyObject *hdr = new_pcap_pkthdr(header);
arglist = Py_BuildValue("Os#", hdr, packetdata, *len);
result = PyEval_CallObject(pctx->pyfunc,arglist);
Py_XDECREF(arglist);
if (result)
Py_DECREF(result);
Py_DECREF(hdr);
if (!result)
pcap_breakloop(pctx->ppcap_t);
pctx->thread_state = PyEval_SaveThread();
}
static PyObject*
p_dispatch(register pcapobject* pp, PyObject* args)
{
int cant, ret;
PyObject *PyFunc;
struct PcapCallbackContext ctx;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if(!PyArg_ParseTuple(args,"iO:dispatch",&cant,&PyFunc))
return NULL;
ctx.ppcap_t = pp->pcap;
ctx.pyfunc = PyFunc;
Py_INCREF(ctx.pyfunc);
ctx.thread_state = PyEval_SaveThread();
ret = pcap_dispatch(pp->pcap, cant, PythonCallBack, (u_char*)&ctx);
PyEval_RestoreThread(ctx.thread_state);
Py_DECREF(ctx.pyfunc);
if(ret<0) {
if (ret!=-2)
/* pcap error, pcap_breakloop was not called so error is not set */
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
return Py_BuildValue("i", ret);
}
static PyObject*
p_dump_open(register pcapobject* pp, PyObject* args)
{
char *filename;
pcap_dumper_t *ret;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if(!PyArg_ParseTuple(args,"s",&filename))
return NULL;
ret = pcap_dump_open(pp->pcap, filename);
if (ret==NULL) {
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
return new_pcapdumper(ret);
}
static PyObject*
p_loop(register pcapobject* pp, PyObject* args)
{
int cant, ret;
PyObject *PyFunc;
struct PcapCallbackContext ctx;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if(!PyArg_ParseTuple(args,"iO:loop",&cant,&PyFunc))
return NULL;
ctx.ppcap_t = pp->pcap;
ctx.pyfunc = PyFunc;
Py_INCREF(ctx.pyfunc);
ctx.thread_state = PyEval_SaveThread();
ret = pcap_loop(pp->pcap, cant, PythonCallBack, (u_char*)&ctx);
PyEval_RestoreThread(ctx.thread_state);
Py_DECREF(ctx.pyfunc);
if(ret<0) {
if (ret!=-2)
/* pcap error, pcap_breakloop was not called so error is not set */
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
p_datalink(register pcapobject* pp, PyObject* args)
{
if (pp->ob_type != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
int type = pcap_datalink(pp->pcap);
return Py_BuildValue("i", type);
}
static PyObject*
p_setnonblock(register pcapobject* pp, PyObject* args)
{
if (pp->ob_type != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
int state;
if (!PyArg_ParseTuple(args, "i", &state))
return NULL;
char errbuf[PCAP_ERRBUF_SIZE];
int ret = pcap_setnonblock(pp->pcap, state, errbuf);
if (-1 == ret) {
PyErr_SetString(PcapError, errbuf);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
p_getnonblock(register pcapobject* pp, PyObject* args)
{
if (pp->ob_type != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
char errbuf[PCAP_ERRBUF_SIZE];
int state = pcap_getnonblock(pp->pcap, errbuf);
if (-1 == state) {
PyErr_SetString(PcapError, errbuf);
return NULL;
}
return Py_BuildValue("i", state);
}
#ifdef HAVE_PCAP_SENDPACKET
static PyObject*
p_sendpacket(register pcapobject* pp, PyObject* args)
{
int status;
unsigned char* str;
unsigned int length;
if (pp->ob_type != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!PyArg_ParseTuple(args,"s#", &str, &length))
return NULL;
status = pcap_sendpacket(pp->pcap, str, length);
if (status)
{
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
#endif