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
/
pcapy.c
257 lines (209 loc) · 5.93 KB
/
pcapy.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
/*
* 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: pcapy.cc 46 2010-08-25 18:06:31Z gutes $
*/
#include <pcap.h>
#include <Python.h>
#include "pcapy.h"
#include "pcapobj.h"
#include "bpfobj.h"
#include "pcapdumper.h"
#include "pcap_pkthdr.h"
PyObject *PcapError;
/* module methods */
static PyObject*
lookupdev(PyObject* self, PyObject* args)
{
char errbuff[PCAP_ERRBUF_SIZE];
char* dev;
dev = pcap_lookupdev(errbuff);
if(!dev)
{
PyErr_SetString(PcapError, errbuff);
return NULL;
}
return Py_BuildValue("s", dev);
}
static PyObject*
findalldevs(PyObject *self, PyObject *args)
{
char errbuff[PCAP_ERRBUF_SIZE];
pcap_if_t *devs;
int status = pcap_findalldevs(&devs, errbuff);
if(status)
{
PyErr_SetString(PcapError, errbuff);
return NULL;
}
if(devs==NULL)
{
PyErr_SetString(PcapError, "No valid interfaces to open");
return NULL;
}
pcap_if_t *cursor = devs;
PyObject* list = PyList_New(0);
while(cursor)
{
PyList_Append(list, Py_BuildValue("s", cursor->name));
cursor = cursor->next;
}
pcap_freealldevs(devs);
return list;
}
static PyObject*
open_live(PyObject *self, PyObject *args)
{
char errbuff[PCAP_ERRBUF_SIZE];
char * device;
int snaplen;
int promisc;
int to_ms;
bpf_u_int32 net, mask;
if(!PyArg_ParseTuple(args,"sipi:open_live",&device,&snaplen,&promisc,&to_ms))
return NULL;
int status = pcap_lookupnet(device, &net, &mask, errbuff);
if(status)
{
net = 0;
mask = 0;
}
pcap_t* pt;
pt = pcap_open_live(device, snaplen, promisc!=0, to_ms, errbuff);
if(!pt)
{
PyErr_SetString(PcapError, errbuff);
return NULL;
}
#ifdef WIN32
pcap_setmintocopy(pt, 0);
#endif
return new_pcapobject( pt, net, mask );
}
static PyObject*
open_offline(PyObject *self, PyObject *args)
{
char errbuff[PCAP_ERRBUF_SIZE];
char * filename;
if(!PyArg_ParseTuple(args,"s",&filename))
return NULL;
pcap_t* pt;
pt = pcap_open_offline(filename, errbuff);
if(!pt)
{
PyErr_SetString(PcapError, errbuff);
return NULL;
}
#ifdef WIN32
pcap_setmintocopy(pt, 0);
#endif
return new_pcapobject( pt, /*net=*/0, /*mask=*/0 );
}
static PyObject*
bpf_compile(PyObject* self, PyObject* args)
{
int linktype;
int snaplen;
char *filter;
int optimize;
unsigned int netmask;
if(!PyArg_ParseTuple(args,
"iispI:compile",
&linktype,
&snaplen,
&filter,
&optimize,
&netmask))
return NULL;
pcap_t *pp;
pp = pcap_open_dead(linktype, snaplen);
if(pp == NULL)
return NULL;
struct bpf_program bpf;
int status = pcap_compile(pp, &bpf, filter, optimize, netmask);
pcap_close(pp);
if(status)
{
PyErr_SetString(PcapError, pcap_geterr(pp));
return NULL;
}
return new_bpfobject( &bpf );
}
static PyMethodDef pcap_methods[] = {
{
"open_live", open_live, METH_VARARGS,
"open_live(device, snaplen, promisc, to_ms)\n\n"
"Open a pcap device for capture/injection. "
"Return a new `pcap` object.\n\n"
"device\n symbolic name of the device\n"
"snaplen\n number of bytes to capture (0 = unlimited)\n"
"promisc\n whether to place the device in promiscuous mode (0 = no)\n"
"to_ms\n read timeout in milliseconds"
},
{
"open_offline", open_offline, METH_VARARGS,
"open_offline(filename)\n\n"
"Open a pcap formatted file. Return a new `pcap` object.\n\n"
"filename\n name of the 'savefile'"
},
{
"lookupdev", lookupdev, METH_NOARGS,
"lookupdev()\n\n"
"Return the symbolic name of the default pcap device."
},
{
"findalldevs", findalldevs, METH_NOARGS,
"findalldevs()\n\n"
"Return a list of symbolic names of all available pcap devices."
},
{
"compile", bpf_compile, METH_VARARGS,
"compile(linktype, snaplen, filter, optimize, netmask)\n\n"
"Compile a filter specification to a BPF bytecode program. "
"Return a new `bpf` object.\n\n"
"linktype\n link layer header type ID code*\n"
"snaplen\n number of bytes captured per packet\n"
"filter\n filter specification per pcap-filter(7)\n"
"optimize\n perform optimization on the resulting code?\n"
"netmask\n IPv4 netmask encoded as an int of the capture network\n\n"
"See pcap_compile(3PCAP) for more information about netmask.\n\n"
"*You probably want `pcapy.DLT_EN10MB`."
},
{NULL, NULL}
};
static char *pcap_doc =
"\nA wrapper for the Packet Capture (PCAP) library\n";
void
initpcapy(void)
{
PyObject *m, *d;
Pcaptype.ob_type = &PyType_Type;
Pkthdr_type.ob_type = &PyType_Type;
Pdumpertype.ob_type = &PyType_Type;
m = Py_InitModule3("pcapy", pcap_methods, pcap_doc);
/* Direct from pcap's net/bpf.h. */
PyModule_AddIntConstant(m, "DLT_NULL", 0);
PyModule_AddIntConstant(m, "DLT_EN10MB", 1);
PyModule_AddIntConstant(m, "DLT_IEEE802", 6);
PyModule_AddIntConstant(m, "DLT_ARCNET", 7);
PyModule_AddIntConstant(m, "DLT_SLIP", 8);
PyModule_AddIntConstant(m, "DLT_PPP", 9);
PyModule_AddIntConstant(m, "DLT_FDDI", 10);
PyModule_AddIntConstant(m, "DLT_ATM_RFC1483", 11);
PyModule_AddIntConstant(m, "DLT_RAW", 12);
PyModule_AddIntConstant(m, "DLT_PPP_SERIAL", 50);
PyModule_AddIntConstant(m, "DLT_PPP_ETHER", 51);
PyModule_AddIntConstant(m, "DLT_C_HDLC", 104);
PyModule_AddIntConstant(m, "DLT_IEEE802_11", 105);
PyModule_AddIntConstant(m, "DLT_LOOP", 108);
PyModule_AddIntConstant(m, "DLT_LINUX_SLL", 113);
PyModule_AddIntConstant(m, "DLT_LTALK", 114);
d = PyModule_GetDict(m);
PcapError = PyErr_NewException("pcapy.PcapError", NULL, NULL );
if( PcapError )
PyDict_SetItemString( d, "PcapError", PcapError );
}