#include #include #include #include #include #include #include #include #include #include #include #include #include /* #include "ebtables_u.h" */ /* #include "ethernetdb.h" */ #include #include #include "Python.h" ebt_ulog_packet_msg_t** ulog_get_packet (void); int ulog_init (int group); /* ----------------------------------------------------- */ static char ebtulog_init__doc__[] = "init(group) -> fd\n" "Initializes ulog capture system, for the given netlink group;" " Returns the netlink socket fd." ; static PyObject * ebtulog_init(PyObject *self /* Not used */, PyObject *args) { int group; int fd; if (!PyArg_ParseTuple(args, "i", &group)) return NULL; if (group <= 0) { PyErr_SetString(PyExc_ValueError, "group must be > 0"); return NULL; } fd = ulog_init(group); return PyInt_FromLong(fd); } /* typedef struct ebt_ulog_packet_msg { */ /* int version; */ /* char indev[IFNAMSIZ]; */ /* char outdev[IFNAMSIZ]; */ /* char physindev[IFNAMSIZ]; */ /* char physoutdev[IFNAMSIZ]; */ /* char prefix[EBT_ULOG_PREFIX_LEN]; */ /* struct timeval stamp; */ /* unsigned long mark; */ /* unsigned int hook; */ /* size_t data_len; */ /* /\* The complete packet, including Ethernet header and perhaps */ /* * the VLAN header is appended *\/ */ /* unsigned char data[0] __attribute__ */ /* ((aligned (__alignof__(struct ebt_ulog_info)))); */ /* } ebt_ulog_packet_msg_t; */ static PyObject * convert_one_packet(ebt_ulog_packet_msg_t *msg) { struct ethhdr *ehdr = (struct ethhdr *) msg->data; /* (indev, outdev, physindev, physoutdev, dst, src, proto, payload) */ return Py_BuildValue("sssss#s#is#", msg->indev, msg->outdev, msg->physindev, msg->physoutdev, ehdr->h_dest, ETH_ALEN, ehdr->h_source, ETH_ALEN, ntohs(ehdr->h_proto), msg->data + sizeof(struct ether_header), msg->data_len - sizeof(struct ether_header)); } static char ebtulog_getnext__doc__[] = "getnext() -> [packet, packet, ...]\n" " packet -> (indev, outdev, physindev, physoutdev,\n" " dst, src, proto, payload)\n" " proto is the protocol in host order\n" "\n" " Gets the next batch of packets from the kernel." ; static PyObject * ebtulog_getnext(PyObject *self /* Not used */, PyObject *args) { ebt_ulog_packet_msg_t **packets, *packet; int i; PyObject *list; list = PyList_New(0); packets = ulog_get_packet(); for (i = 0; (packet = packets[i]); ++i) { PyObject *item = convert_one_packet(packet); PyList_Append(list, item); Py_DECREF(item); } free(packets[i]); return list; } /* List of methods defined in the module */ static struct PyMethodDef ebtulog_methods[] = { {"init", (PyCFunction)ebtulog_init, METH_VARARGS, ebtulog_init__doc__}, {"getnext", (PyCFunction)ebtulog_getnext, METH_NOARGS, ebtulog_getnext__doc__}, {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ }; static char ebtulog_module_documentation[] = "Module to receive packets from ebtables --ulog" ; void initebtulog() { PyObject *m, *d; /* Create the module and add the functions */ m = Py_InitModule4("ebtulog", ebtulog_methods, ebtulog_module_documentation, (PyObject*)NULL, PYTHON_API_VERSION); /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); /* XXXX Add constants here */ /* Check for errors */ if (PyErr_Occurred()) Py_FatalError("can't initialize module ebtulog"); }