-
Notifications
You must be signed in to change notification settings - Fork 105
/
pcapobj.cc
822 lines (669 loc) · 19.8 KB
/
pcapobj.cc
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
* Copyright (c) 2014 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.
*
*/
#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 PyObject*
p_close(register pcapobject* pp, PyObject*)
{
if ( pp->pcap )
pcap_close(pp->pcap);
pp->pcap = NULL;
Py_RETURN_NONE;
}
static void
pcap_dealloc(register pcapobject* pp)
{
p_close(pp, NULL);
PyObject_Del(pp);
}
static PyObject *
err_closed(void)
{
PyErr_SetString(PyExc_ValueError, "pcap is closed");
return NULL;
}
// 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*);
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_setdirection(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);
static PyObject* p_sendpacket(register pcapobject* pp, PyObject* args);
static PyObject* p_stats( register pcapobject* pp, PyObject*);
static PyObject* p__enter__( register pcapobject* pp, PyObject*);
static PyObject* p_getfd(register pcapobject* pp, PyObject* args);
static PyObject* p_set_snaplen(register pcapobject* pp, PyObject* args);
static PyObject* p_set_promisc(register pcapobject* pp, PyObject* args);
static PyObject* p_set_timeout(register pcapobject* pp, PyObject* args);
static PyObject* p_set_buffer_size(register pcapobject* pp, PyObject* args);
static PyObject* p_set_rfmon(register pcapobject* pp, PyObject* args);
static PyObject* p_activate(register pcapobject* pp, PyObject* args);
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"},
{"setdirection", (PyCFunction) p_setdirection, METH_VARARGS, "set the direction for which packets will be captured"},
{"dump_open", (PyCFunction) p_dump_open, METH_VARARGS, "creates a dumper object"},
{"sendpacket", (PyCFunction) p_sendpacket, METH_VARARGS, "sends a packet through the interface"},
{"stats", (PyCFunction) p_stats, METH_NOARGS, "returns capture statistics"},
{"close", (PyCFunction) p_close, METH_NOARGS, "close the capture"},
{"set_snaplen", (PyCFunction)p_set_snaplen, METH_VARARGS, "set the snapshot length for a not-yet-activated capture handle"},
{"set_promisc", (PyCFunction)p_set_promisc, METH_VARARGS, "set promiscuous mode for a not-yet-activated capture handle"},
{"set_timeout", (PyCFunction)p_set_timeout, METH_VARARGS, "set the read timeout for a not-yet-activated capture handle"},
{"set_buffer_size", (PyCFunction)p_set_buffer_size, METH_VARARGS, "set the buffer size for a not-yet-activated capture handle"},
{"activate", (PyCFunction)p_activate, METH_NOARGS, "activate a capture handle created using create()"},
{"__enter__", (PyCFunction) p__enter__, METH_NOARGS, NULL},
{"__exit__", (PyCFunction) p_close, METH_VARARGS, NULL},
#ifndef WIN32
{"getfd", (PyCFunction) p_getfd, METH_VARARGS, "get selectable pcap fd"},
{"set_rfmon", (PyCFunction)p_set_rfmon, METH_VARARGS, "set monitor mode for a not-yet-activated capture handle"}, /* Available on Npcap, not on Winpcap. */
#endif
{NULL, NULL} /* sentinel */
};
static PyObject*
pcap_getattr(pcapobject* pp, char* name)
{
#if PY_MAJOR_VERSION >= 3
PyObject *nameobj = PyUnicode_FromString(name);
PyObject *attr = PyObject_GenericGetAttr((PyObject *)pp, nameobj);
Py_DECREF(nameobj);
return attr;
#else
return Py_FindMethod(p_methods, (PyObject*)pp, name);
#endif
}
PyTypeObject Pcaptype = {
#if PY_MAJOR_VERSION >= 3
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"Reader", /* tp_name */
sizeof(pcapobject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pcap_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc)pcap_getattr, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
NULL, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
p_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
#else
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*/
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
NULL, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
p_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
#endif
};
PyObject*
new_pcapobject(pcap_t *pcap, bpf_u_int32 net, bpf_u_int32 mask)
{
if (PyType_Ready(&Pcaptype) < 0)
return NULL;
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 (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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 (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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 (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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*)
{
struct pcap_pkthdr *hdr = NULL;
const unsigned char *buf = (const unsigned char*)"";
int err_code = 1;
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
// allow threads as this might block
Py_BEGIN_ALLOW_THREADS;
err_code = pcap_next_ex(pp->pcap, &hdr, &buf);
Py_END_ALLOW_THREADS;
if(err_code == -1)
{
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
PyObject *pkthdr;
int _caplen = 0;
if (err_code == 1) {
pkthdr = new_pcap_pkthdr(hdr);
_caplen = hdr->caplen;
} else {
pkthdr = Py_None;
Py_INCREF(pkthdr);
_caplen = 0;
}
if (pkthdr)
{
PyObject *ret = NULL;
#if PY_MAJOR_VERSION >= 3
/* return bytes */
ret = Py_BuildValue("(Oy#)", pkthdr, buf, _caplen);
#else
ret = Py_BuildValue("(Os#)", pkthdr, buf, _caplen);
#endif
Py_DECREF(pkthdr);
return ret;
}
PyErr_SetString(PcapError, "Can't build pkthdr");
return NULL;
}
struct PcapCallbackContext {
PcapCallbackContext(pcap_t* p, PyObject* f, PyThreadState* ts)
: ppcap_t(p), pyfunc(f), thread_state(ts)
{
Py_INCREF(pyfunc);
}
~PcapCallbackContext()
{
Py_DECREF(pyfunc);
}
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;
PcapCallbackContext *pctx;
len = (unsigned int *)&header->caplen;
pctx = (PcapCallbackContext *)user;
PyEval_RestoreThread(pctx->thread_state);
PyObject *hdr = new_pcap_pkthdr(header);
#if PY_MAJOR_VERSION >= 3
/* pass bytes */
arglist = Py_BuildValue("Oy#", hdr, packetdata, *len);
#else
arglist = Py_BuildValue("Os#", hdr, packetdata, *len);
#endif
result = PyEval_CallObject(pctx->pyfunc,arglist);
Py_XDECREF(arglist);
if (result)
Py_DECREF(result);
Py_DECREF(hdr);
if (!result)
pcap_breakloop(pctx->ppcap_t);
PyEval_SaveThread();
}
static PyObject*
p_dispatch(register pcapobject* pp, PyObject* args)
{
int cant, ret;
PyObject *PyFunc;
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
if(!PyArg_ParseTuple(args,"iO:dispatch",&cant,&PyFunc))
return NULL;
PcapCallbackContext ctx(pp->pcap, PyFunc, PyThreadState_Get());
PyEval_SaveThread();
ret = pcap_dispatch(pp->pcap, cant, PythonCallBack, (u_char*)&ctx);
PyEval_RestoreThread(ctx.thread_state);
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_stats(register pcapobject* pp, PyObject*)
{
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
struct pcap_stat stats;
if (-1 == pcap_stats(pp->pcap, &stats)) {
PyErr_SetString(PcapError, pcap_geterr(pp->pcap));
return NULL;
}
return Py_BuildValue("III", stats.ps_recv, stats.ps_drop, stats.ps_ifdrop);
}
static PyObject*
p__enter__( register pcapobject* pp, PyObject*)
{
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
Py_INCREF(pp);
return (PyObject*)pp;
}
static PyObject*
p_dump_open(register pcapobject* pp, PyObject* args)
{
char *filename;
pcap_dumper_t *ret;
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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;
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
if(!PyArg_ParseTuple(args,"iO:loop",&cant,&PyFunc))
return NULL;
PcapCallbackContext ctx(pp->pcap, PyFunc, PyThreadState_Get());
PyEval_SaveThread();
ret = pcap_loop(pp->pcap, cant, PythonCallBack, (u_char*)&ctx);
PyEval_RestoreThread(ctx.thread_state);
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 (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int type = pcap_datalink(pp->pcap);
return Py_BuildValue("i", type);
}
static PyObject*
p_setdirection(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
pcap_direction_t direction;
if (!PyArg_ParseTuple(args, "i", &direction))
return NULL;
int ret = pcap_setdirection(pp->pcap, direction);
if (-1 == ret) {
PyErr_SetString(PcapError, "Failed setting direction");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
p_setnonblock(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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 (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
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);
}
static PyObject*
p_set_snaplen(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int snaplen;
if (!PyArg_ParseTuple(args, "i", &snaplen))
return NULL;
int ret = pcap_set_snaplen(pp->pcap, snaplen);
return Py_BuildValue("i", ret);
}
static PyObject*
p_set_promisc(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int promisc;
if (!PyArg_ParseTuple(args, "i", &promisc))
return NULL;
int ret = pcap_set_promisc(pp->pcap, promisc);
return Py_BuildValue("i", ret);
}
static PyObject*
p_set_timeout(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int to_ms;
if (!PyArg_ParseTuple(args, "i", &to_ms))
return NULL;
int ret = pcap_set_timeout(pp->pcap, to_ms);
return Py_BuildValue("i", ret);
}
static PyObject*
p_set_buffer_size(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int buffer_size;
if (!PyArg_ParseTuple(args, "i", &buffer_size))
return NULL;
int ret = pcap_set_buffer_size(pp->pcap, buffer_size);
return Py_BuildValue("i", ret);
}
static PyObject*
p_set_rfmon(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int rfmon;
if (!PyArg_ParseTuple(args, "i", &rfmon))
return NULL;
int ret = pcap_set_rfmon(pp->pcap, rfmon);
return Py_BuildValue("i", ret);
}
static PyObject*
p_activate(register pcapobject* pp, PyObject*)
{
if (Py_TYPE(pp) != &Pcaptype) {
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int ret = pcap_activate(pp->pcap);
return Py_BuildValue("i", ret);
}
static PyObject*
p_sendpacket(register pcapobject* pp, PyObject* args)
{
int status;
unsigned char* str;
unsigned int length;
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
#if PY_MAJOR_VERSION >= 3
/* accept bytes */
if (!PyArg_ParseTuple(args,"y#", &str, &length)) {
return NULL;
}
#else
if (!PyArg_ParseTuple(args,"s#", &str, &length)) {
return NULL;
}
#endif
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;
}
#ifndef WIN32
static PyObject*
p_getfd(register pcapobject* pp, PyObject* args)
{
if (Py_TYPE(pp) != &Pcaptype)
{
PyErr_SetString(PcapError, "Not a pcap object");
return NULL;
}
if (!pp->pcap)
return err_closed();
int fd = pcap_get_selectable_fd(pp->pcap);
return Py_BuildValue("i", fd);
}
#endif