-
Notifications
You must be signed in to change notification settings - Fork 4
/
pcap.c
616 lines (488 loc) · 15.9 KB
/
pcap.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
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
/**
py-pcap: pcap file module for Python
Copyright (C) 2007 Neale Pickett
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
**/
#include <Python.h>
#include "obj.h"
/* Define some types (Windows doesn't have stdint.h). Let's hope
everything uses an 8-bit byte. */
#if SIZEOF_LONG == 4
typedef unsigned long _uint32_t;
typedef long _int32_t;
#elif SIZEOF_INT == 4
typedef unsigned int _uint32_t;
typedef int _int32_t;
#else
# error "What's uint32 on this system?"
#endif
#if SIZEOF_INT == 2
typedef unsigned int _uint16_t;
typedef int _int16_t;
#elif SIZEOF_SHORT == 2
typedef unsigned short _uint16_t;
typedef short _int16_t;
#else
# error "What's uint16 on this system?"
#endif
/* We only need two data structures from pcap.h. We do the rest
ourselves. */
struct pcap_file_header {
_uint32_t magic;
_uint16_t version_major;
_uint16_t version_minor;
_int32_t thiszone; /* gmt to local correction */
_uint32_t sigfigs; /* accuracy of timestamps */
_int32_t snaplen; /* max length saved portion of each pkt */
_int32_t linktype; /* data link type (LINKTYPE_*) */
};
/* So things still work on 64-bit CPUs.
Note that this makes the program not Y2038-safe!
*/
struct pcap_timeval {
_uint32_t tv_sec;
_uint32_t tv_usec;
};
struct pcap_pkthdr {
struct pcap_timeval ts; /* time stamp */
_uint32_t caplen; /* length of portion present */
_uint32_t len; /* length this packet (off wire) */
};
/* Endian swap for 32-bit ints */
#define btoll(i) (((i & 0xff000000) >> 030) | \
((i & 0x00ff0000) >> 010) | \
((i & 0x0000ff00) << 010) | \
((i & 0x000000ff) << 030))
#define btols(i) (((i & 0xff00) >> 010) | \
((i & 0x00ff) << 010))
#define sl(i) (self->swap ? btoll(i) : (i))
#define ss(i) (self->swap ? btols(i) : (i))
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
PyObject *pFile;
struct pcap_file_header header;
char mode; /* 'r' or 'w' */
int swap; /* swap endianness? */
} pcap_PcapObject;
static void
pcap_PcapObject_dealloc(PyObject *p)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
Py_CLEAR(self->pFile);
self->ob_type->tp_free(p);
}
static PyObject *
pcap_PcapObject_new(PyTypeObject *type,
PyObject *args,
PyObject *kwds)
{
PyObject *p;
p = type->tp_alloc(type, 0);
if (! p) {
return NULL;
}
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
self->pFile = NULL;
}
return p;
}
static int
pcap_PcapObject_init(PyObject *p,
PyObject *args,
PyObject *kwds)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
static char *kwlist[] = {"src", "mode", "snaplen", "linktype", NULL};
char *mode = NULL;
_uint32_t snaplen = 65535;
_uint32_t linktype = 1;
PyObject *pTmp = NULL;
PyObject *pFile; /* Don't decref, it's borrowed! */
attempt {
int tmp;
tmp = PyArg_ParseTupleAndKeywords(args, kwds, "O|sll", kwlist, &pFile, &mode, &snaplen, &linktype);
if (! tmp) break;
if (PyString_Check(pFile)) {
char *fn;
fn = PyString_AsString(pFile);
if (! fn) break;
if (NULL == mode) {
mode = "rb";
}
pFile = PyFile_FromString(fn, mode);
if (! pFile) break;
self->pFile = pFile;
} else {
self->pFile = pFile;
Py_INCREF(self->pFile);
}
if ((! mode) || ('r' == mode[0])) {
/* Try to read in the header. */
pTmp = PyObject_CallMethod(pFile, "read", "i", sizeof(self->header));
if (0 == mode) {
/* If we're in auto-detect mode... */
if (pTmp) {
/* And it worked, then we become read-only */
self->mode = 'r';
} else {
/* And it didn't work, then we become write-only */
PyErr_Clear();
self->mode = 'w';
}
} else {
self->mode = mode[0];
}
} else {
self->mode = mode[0];
}
if ('r' == self->mode) {
if (! pTmp) break;
{
int tmp;
char *buf;
Py_ssize_t len;
tmp = PyString_AsStringAndSize(pTmp, &buf, &len);
if (-1 == tmp) {
break;
}
if (len != sizeof(self->header)) {
PyErr_Format(PyExc_IOError, "Reading header returned wrong number of bytes");
break;
}
memcpy(&(self->header), buf, len);
}
if (0xa1b2c3d4 == self->header.magic) {
self->swap = 0;
} else if (0xd4c3b2a1 == self->header.magic) {
self->swap = 1;
} else {
PyErr_Format(PyExc_IOError, "Not a pcap file");
break;
}
} else if ('w' == self->mode) {
/* Write out header */
memset(&(self->header), 0, sizeof(self->header));
self->header.magic = 0xa1b2c3d4;
self->header.version_major = 2;
self->header.version_minor = 4;
self->header.snaplen = snaplen;
self->header.linktype = linktype;
self->swap = 0;
pTmp = PyObject_CallMethod(pFile, "write", "s#", &(self->header), sizeof(self->header));
if (! pTmp) break;
} else {
PyErr_Format(PyExc_IOError, "mode must be 'r' or 'w'");
break;
}
}
recover {
Py_CLEAR(self->pFile);
Py_CLEAR(pTmp);
return -1;
}
return 0;
}
static PyObject *
pcap_PcapObject_read(PyObject *p)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
struct pcap_pkthdr *hdr;
PyObject *pTmp = NULL;
PyObject *pBuf = NULL;
PyObject *pRet = NULL;
attempt {
if ('r' != self->mode) {
PyErr_Format(PyExc_IOError, "can not read from writable stream");
break;
}
pTmp = PyObject_CallMethod(self->pFile, "read", "i", sizeof(*hdr));
if (! pTmp) break;
{
char *buf;
Py_ssize_t len;
int tmp;
tmp = PyString_AsStringAndSize(pTmp, &buf, &len);
if (-1 == tmp) {
break;
}
/* 0 bytes means end of file */
if (0 == len) {
pRet = Py_None;
Py_INCREF(pRet);
succeed;
}
if (len != sizeof(*hdr)) {
PyErr_Format(PyExc_IOError, "Read returned wrong number of bytes (%zd)", len);
break;
}
/* Since buf points to something Python owns, we can just make hdr
point there and let buf fall off the stack. */
hdr = (struct pcap_pkthdr *)buf;
}
pBuf = PyObject_CallMethod(self->pFile, "read", "i", sl(hdr->caplen));
if (! pBuf) break;
pRet = Py_BuildValue("(lll)O",
sl(hdr->ts.tv_sec),
sl(hdr->ts.tv_usec),
sl(hdr->len),
pBuf);
if (! pRet) break;
}
Py_CLEAR(pTmp);
Py_CLEAR(pBuf);
recover {
Py_CLEAR(pRet);
return NULL;
}
return pRet;
}
static PyObject *
pcap_PcapObject_write(PyObject *p, PyObject *args)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
char *buf;
struct pcap_pkthdr hdr;
PyObject *pTmp = NULL;
attempt {
int tmp;
if ('w' != self->mode) {
PyErr_Format(PyExc_IOError, "can not write to readable stream");
break;
}
tmp = PyArg_ParseTuple(args, "((lll)s#)",
&(hdr.ts.tv_sec), &(hdr.ts.tv_usec),
&(hdr.len),
&buf, &(hdr.caplen));
if (! tmp) break;
pTmp = PyObject_CallMethod(self->pFile, "write", "s#", &hdr, sizeof(hdr));
if (! pTmp) break;
Py_DECREF(pTmp); pTmp = NULL;
pTmp = PyObject_CallMethod(self->pFile, "write", "s#", buf, hdr.caplen);
if (! pTmp) break;
}
Py_CLEAR(pTmp);
recover {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
pcap_PcapObject_get_version(PyObject *p, void *ignored)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
return Py_BuildValue("(HH)", ss(self->header.version_major), ss(self->header.version_minor));
}
static PyObject *
pcap_PcapObject_get_thiszone(PyObject *p, void *ignored)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
return Py_BuildValue("l", sl(self->header.thiszone));
}
static PyObject *
pcap_PcapObject_get_sigfigs(PyObject *p, void *ignored)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
return Py_BuildValue("l", sl(self->header.sigfigs));
}
static PyObject *
pcap_PcapObject_get_snaplen(PyObject *p, void *ignored)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
return Py_BuildValue("l", sl(self->header.snaplen));
}
static PyObject *
pcap_PcapObject_get_linktype(PyObject *p, void *ignored)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
return Py_BuildValue("l", sl(self->header.linktype));
}
/* Iterator stuff */
static PyObject *pcap_PcapObject_iter(PyObject *);
static struct PyMethodDef
pcap_PcapObject_methods[] = {
{"read", (PyCFunction)pcap_PcapObject_read, METH_NOARGS, "Read a packet"},
{"next", (PyCFunction)pcap_PcapObject_read, METH_NOARGS, "Read a packet"},
{"write", (PyCFunction)pcap_PcapObject_write, METH_VARARGS, "Write a packet"},
{NULL} /* Sentinel */
};
static struct PyGetSetDef
pcap_PcapObject_getset[] = {
{"version", pcap_PcapObject_get_version, NULL, "Version of file", NULL},
{"thiszone", pcap_PcapObject_get_thiszone, NULL, "GMT to local correction", NULL},
{"sigfigs", pcap_PcapObject_get_sigfigs, NULL, "Accuracy of timestamps", NULL},
{"snaplen", pcap_PcapObject_get_snaplen, NULL, "Max length of saved portion of each packet", NULL},
{"linktype", pcap_PcapObject_get_linktype, NULL, "Data link type", NULL},
{NULL} /* Sentinel */
};
static PyTypeObject pcap_PcapType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"pcap.pcap", /* tp_name */
sizeof(pcap_PcapObject), /* tp_basicsize */
0, /* tp_itemsize */
pcap_PcapObject_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* 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 */
"Pcap file reader", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
pcap_PcapObject_iter, /* tp_iter */
0, /* tp_iternext */
pcap_PcapObject_methods, /* tp_methods */
0, /* tp_members */
pcap_PcapObject_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
pcap_PcapObject_init, /* tp_init */
0, /* tp_alloc */
pcap_PcapObject_new, /* tp_new */
0, /* tp_free */
};
/******************** Pcap Iterator ********************/
typedef struct {
PyObject_HEAD
PyObject *pcap;
} pcap_PcapIterObject;
static void pcap_PcapIterObject_dealloc(PyObject *p);
static PyObject *pcap_PcapIterObject_next(PyObject *p);
PyTypeObject pcap_PcapIterType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"pcap.pcap_iterator", /* tp_name */
sizeof(pcap_PcapIterObject), /* tp_basicsize */
0, /* tp_itemsize */
pcap_PcapIterObject_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* 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 */
"Pcap file iterator", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
pcap_PcapIterObject_next, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
};
static PyObject *
pcap_PcapObject_iter(PyObject *p)
{
pcap_PcapObject *self = (pcap_PcapObject *)p;
pcap_PcapIterObject *it = NULL;
PyObject *pIt;
attempt {
if ('r' != self->mode) {
PyErr_Format(PyExc_IOError, "can not read from writable stream");
break;
}
it = PyObject_New(pcap_PcapIterObject, &pcap_PcapIterType);
if (! it) break;
pIt = PyObject_Init((PyObject *)it, &pcap_PcapIterType);
if (pIt != (PyObject *)it) break;
it->pcap = p;
Py_INCREF(it->pcap);
}
recover {
if (it) {
Py_CLEAR(it->pcap);
Py_CLEAR(it);
}
return NULL;
}
return (PyObject *)it;
}
static void
pcap_PcapIterObject_dealloc(PyObject *p)
{
pcap_PcapIterObject *self = (pcap_PcapIterObject *)p;
Py_CLEAR(self->pcap);
PyObject_Del(p);
//self->ob_type->tp_free(p);
}
static PyObject *
pcap_PcapIterObject_next(PyObject *p)
{
pcap_PcapIterObject *self = (pcap_PcapIterObject *)p;
PyObject *pRet = NULL;
pRet = pcap_PcapObject_read(self->pcap);
if (pRet == Py_None) {
Py_DECREF(pRet);
Py_DECREF(self->pcap);
self->pcap = NULL;
return NULL;
}
return pRet;
}
static PyMethodDef pcap_methods[] = {
{NULL} /* Sentinel */
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
initpcap(void)
{
PyObject* m;
pcap_PcapType.tp_new = PyType_GenericNew;
if (PyType_Ready(&pcap_PcapType) < 0)
return;
m = Py_InitModule3("pcap", pcap_methods,
"Reads pcap files in a less-sucky way");
{
PyObject *version;
version = Py_BuildValue("ii", 2, 0);
PyModule_AddObject(m, "version", version); /* Steals reference */
}
/* It's probably not strictly necessary to incref each time, since
it's not possible to delete these. */
Py_INCREF(&pcap_PcapType);
PyModule_AddObject(m, "pcap", (PyObject *)&pcap_PcapType);
Py_INCREF(&pcap_PcapType);
PyModule_AddObject(m, "open", (PyObject *)&pcap_PcapType);
Py_INCREF(&pcap_PcapType);
PyModule_AddObject(m, "open_offline", (PyObject *)&pcap_PcapType);
}