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 pathpcapdumper.c
120 lines (93 loc) · 2.35 KB
/
pcapdumper.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
/*
* 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: pcapdumper.cc,v 1.3 2003/10/23 20:00:53 jkohen Exp $
*/
#include <Python.h>
#include <pcap.h>
#include "pcapdumper.h"
#include "pcap_pkthdr.h"
#include "pcapy.h"
/* internal pcapdumper */
typedef struct {
PyObject_HEAD
pcap_dumper_t *dumper;
} pcapdumper;
/* Pdumpertype */
static void
pcap_dealloc(register pcapdumper* pp)
{
if ( pp->dumper )
pcap_dump_close(pp->dumper);
pp->dumper = NULL;
PyObject_Del(pp);
}
/* pcap methods */
/*static PyObject* p_close(register pcapdumper* pp, PyObject* args);*/
static PyObject* p_dump(register pcapdumper* pp, PyObject* args);
static PyMethodDef p_methods[] = {
/* {"close", (PyCFunction) p_close, METH_VARARGS, "loops packet dispatching"}, */
{
"dump", (PyCFunction) p_dump, METH_VARARGS,
"dump(self, hdr, data)\n\n"
"Dump a packet to the file.\n\n"
"hdr\n a pkthdr object that specifies the packet's ts, caplen, and len\n"
"data\n raw packet data"
},
{NULL, NULL} /* sentinel */
};
static PyObject*
pcap_getattr(pcapdumper* pp, char* name)
{
return Py_FindMethod(p_methods, (PyObject*)pp, name);
}
PyTypeObject Pdumpertype = {
PyObject_HEAD_INIT(NULL)
0,
"Dumper",
sizeof(pcapdumper),
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_pcapdumper(pcap_dumper_t *dumper)
{
pcapdumper *pp;
pp = PyObject_New(pcapdumper, &Pdumpertype);
if (pp == NULL)
return NULL;
pp->dumper = dumper;
return (PyObject*)pp;
}
static PyObject*
p_dump(register pcapdumper* pp, PyObject* args)
{
PyObject *pyhdr;
u_char *data;
int len;
if (pp->ob_type != &Pdumpertype) {
PyErr_SetString(PcapError, "Not a pcapdumper object");
return NULL;
}
if (!PyArg_ParseTuple(args,"Os#",&pyhdr,&data,&len))
return NULL;
struct pcap_pkthdr hdr;
if (-1 == pkthdr_to_native(pyhdr, &hdr))
return NULL;
pcap_dump((u_char *)pp->dumper, &hdr, data);
Py_INCREF(Py_None);
return Py_None;
}