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
|
/*
* 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: pcap_pkthdr.cc 21 2003-10-23 20:00:54Z jkohen $
*/
#include <Python.h>
#include <pcap.h>
#include "pcapy.h"
#include "pcap_pkthdr.h"
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
// internal pcapobject
typedef struct {
PyObject_HEAD
struct timeval ts;
bpf_u_int32 caplen;
bpf_u_int32 len;
} pkthdr;
// Pkthdr_Type
static void
pcap_dealloc(register pkthdr* pp)
{
PyObject_Del(pp);
}
// pcap methods
static PyObject* p_getts(register pkthdr* pp, PyObject* args);
static PyObject* p_getcaplen(register pkthdr* pp, PyObject* args);
static PyObject* p_getlen(register pkthdr* pp, PyObject* args);
static PyMethodDef p_methods[] = {
{"getts", (PyCFunction) p_getts, METH_VARARGS, "get timestamp tuple (seconds, microseconds) since the Epoch"},
{"getcaplen", (PyCFunction) p_getcaplen, METH_VARARGS, "returns the length of portion present"},
{"getlen", (PyCFunction) p_getlen, METH_VARARGS, "returns the length of the packet (off wire)"},
{NULL, NULL} /* sentinel */
};
static PyObject*
pcap_getattr(pkthdr* pp, char* name)
{
return Py_FindMethod(p_methods, (PyObject*)pp, name);
}
PyTypeObject Pkthdr_type = {
PyObject_HEAD_INIT(NULL)
0,
"Pkthdr",
sizeof(pkthdr),
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_pcap_pkthdr(const struct pcap_pkthdr* hdr)
{
pkthdr *pp;
pp = PyObject_New(pkthdr, &Pkthdr_type);
if (pp == NULL)
return NULL;
pp->ts = hdr->ts;
pp->caplen = hdr->caplen;
pp->len = hdr->len;
return (PyObject*)pp;
}
static PyObject*
p_getts(register pkthdr* pp, PyObject* args)
{
if (pp->ob_type != &Pkthdr_type) {
PyErr_SetString(PcapError, "Not a pkthdr object");
return NULL;
}
return Py_BuildValue("(ll)", pp->ts.tv_sec, pp->ts.tv_usec);
}
static PyObject*
p_getcaplen(register pkthdr* pp, PyObject* args)
{
if (pp->ob_type != &Pkthdr_type) {
PyErr_SetString(PcapError, "Not a pkthdr object");
return NULL;
}
return Py_BuildValue("l", pp->caplen);
}
static PyObject*
p_getlen(register pkthdr* pp, PyObject* args)
{
if (pp->ob_type != &Pkthdr_type) {
PyErr_SetString(PcapError, "Not a pkthdr object");
return NULL;
}
return Py_BuildValue("l", pp->len);
}
int
pkthdr_to_native(PyObject *pyhdr, struct pcap_pkthdr *hdr)
{
if (pyhdr->ob_type != &Pkthdr_type) {
PyErr_SetString(PcapError, "Not a pkthdr object");
return -1;
}
pkthdr *pp = (pkthdr *) pyhdr;
hdr->ts = pp->ts;
hdr->caplen = pp->caplen;
hdr->len = pp->len;
return 0;
}
|