Revision: 440 http://vde.svn.sourceforge.net/vde/?rev=440&view=rev Author: danielel Date: 2010-08-13 09:10:00 +0000 (Fri, 13 Aug 2010)
Log Message: ----------- Added rudimental Python libvdeplug wrapper Modified Paths: -------------- trunk/vde-2/configure.ac trunk/vde-2/src/lib/Makefile.am Added Paths: ----------- trunk/vde-2/src/lib/python/ trunk/vde-2/src/lib/python/Makefile trunk/vde-2/src/lib/python/VdePlug.py trunk/vde-2/src/lib/python/VdePlug_example.py trunk/vde-2/src/lib/python/vdeplug_python.c Modified: trunk/vde-2/configure.ac =================================================================== --- trunk/vde-2/configure.ac 2010-08-04 14:42:59 UTC (rev 439) +++ trunk/vde-2/configure.ac 2010-08-13 09:10:00 UTC (rev 440) @@ -23,6 +23,10 @@ AC_CHECK_LIB([crypto], [EVP_EncryptInit], [add_cryptcab_support=yes], [add_cryptcab_support=no ; warn_cryptcab=yes]) + +PYTHONINCLUDEDIR=`python -c "from distutils.sysconfig import get_python_inc; print get_python_inc();"` +AC_CHECK_HEADERS([$PYTHONINCLUDEDIR/Python.h], [add_python_support=yes], + [add_python_support=no; warn_python=no]) AC_CHECK_LIB([pcap], [pcap_open_dead], [add_pcap=yes], [add_pcap=no ; warn_pcap=yes]) @@ -177,6 +181,7 @@ fi]) AM_CONDITIONAL(ENABLE_CRYPTCAB, test "$add_cryptcab_support" = yes) +AM_CONDITIONAL(ENABLE_PYTHON, test "$add_python_support" = yes) AM_CONDITIONAL(ENABLE_PCAP, test "$add_pcap" = yes) AM_CONDITIONAL(CAN_MAKE_LIBVDETAP, test "$can_make_libvdetap" = yes) AM_CONDITIONAL(CAN_MAKE_VDETUNCTL, test "$can_make_vdetunctl" = yes) @@ -223,6 +228,12 @@ echo " - VDE CryptCab............ disabled" fi +if test x$add_python_support = "xyes" ; then + echo " + Python Libraries........ enabled" +else + echo " - Python Libraries........ disabled" +fi + if test x$warn_tuntap = "xyes" ; then echo " - TAP support............. disabled" else @@ -263,6 +274,14 @@ fi fi +if ! test x$add_python_support = "xyes" ; then + if test x$warn_python = "xyes" ; then + AC_MSG_WARN([Python libraries support has been disabled because python is +not installed on your system, or because it could not be +found. Please install it if you want Python libraries to be compiled and installed.]) + fi +fi + if ! test x$add_pcap = "xyes" ; then if test x$warn_pcap = "xyes" ; then AC_MSG_WARN([VDE vde_pcapplug and packet dump plugin have been disabled because Modified: trunk/vde-2/src/lib/Makefile.am =================================================================== --- trunk/vde-2/src/lib/Makefile.am 2010-08-04 14:42:59 UTC (rev 439) +++ trunk/vde-2/src/lib/Makefile.am 2010-08-13 09:10:00 UTC (rev 440) @@ -27,6 +27,10 @@ libvdehist_la_LIBADD = $(LIBADD) libvdehist_la_LDFLAGS = $(AM_LDFLAGS) -version-number 0:0:1 -export-dynamic +if ENABLE_PYTHON + SUBDIRS = python +endif + pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = vdesnmp.pc vdemgmt.pc vdeplug.pc vdehist.pc Added: trunk/vde-2/src/lib/python/Makefile =================================================================== --- trunk/vde-2/src/lib/python/Makefile (rev 0) +++ trunk/vde-2/src/lib/python/Makefile 2010-08-13 09:10:00 UTC (rev 440) @@ -0,0 +1,27 @@ +PYTHONINCLUDEDIR=`python -c "from distutils.sysconfig import get_python_inc; print get_python_inc();"` +PYTHONLIBDIR=`python -c "from distutils.sysconfig import get_python_lib; print get_python_lib();"` + + + +all: vdeplug_python.so + +vdeplug_python.o: vdeplug_python.c + gcc -c vdeplug_python.c -I /usr/include/python2.5 + +vdeplug_python.so: vdeplug_python.o + gcc -shared vdeplug_python.o /usr/lib/libvdeplug.so -o vdeplug_python.so + +distclean: clean + +clean: + rm -f *.so *.o *.pyc + +install: + install vdeplug_python.so $(PYTHONLIBDIR) + @echo INCLUDES are in $(PYTHONINCLUDEDIR) + @echo LIBS are in $(PYTHONLIBDIR) + install VdePlug.py $(PYTHONLIBDIR) + +uninstall: + rm -f $(PREFIX)/lib/vdeplug_python.so + rm -f $(PYTHONLIBDIR)/VdePlug.py Added: trunk/vde-2/src/lib/python/VdePlug.py =================================================================== --- trunk/vde-2/src/lib/python/VdePlug.py (rev 0) +++ trunk/vde-2/src/lib/python/VdePlug.py 2010-08-13 09:10:00 UTC (rev 440) @@ -0,0 +1,46 @@ +#!/usr/bin/python + +''' + + LibVdePlug/python wrapper + Copyright 2010 Daniele Lacamera + + Released under the terms of GNU GPL v.2 + +''' + +import vdeplug_python +import os +from array import array + + +class VdePlug: + + def __init__(self, sock=None, descr="Python", port=0, group=None, mode=0): + self._magic = vdeplug_python.open(sock, descr) + + self._ctl = os.fdopen(vdeplug_python.ctlfd(self._magic)) + self._data = os.fdopen(vdeplug_python.datafd(self._magic), 'wb+', os.O_NONBLOCK) + + def ctlfd(self): + return self._ctl + + def datafd(self): + return self._data + + def send(self, buffer): + a = array('B', buffer) + r = self._data.write(a) + self._data.flush() + return r + + def recv(self, size): + return os.read(self._data.fileno(), size) + + def close(self): + vdeplug_python.close(self._magic) + self._magic = None + + + + Property changes on: trunk/vde-2/src/lib/python/VdePlug.py ___________________________________________________________________ Added: svn:executable + * Added: trunk/vde-2/src/lib/python/VdePlug_example.py =================================================================== --- trunk/vde-2/src/lib/python/VdePlug_example.py (rev 0) +++ trunk/vde-2/src/lib/python/VdePlug_example.py 2010-08-13 09:10:00 UTC (rev 440) @@ -0,0 +1,30 @@ +#!/usr/bin/python +from VdePlug import VdePlug +from select import poll +import os, sys, struct +from select import POLLIN, POLLOUT, POLLHUP, POLLERR, POLLNVAL + + +v = VdePlug(sys.argv[1]) +p = poll() +p.register(sys.stdin.fileno(), POLLIN) +p.register(v.datafd().fileno(), POLLIN) +while(True): + pollret = p.poll() + for (f,e) in pollret: + if f == v.datafd().fileno() and (e & POLLIN): + buffer = v.recv(2000) + lh = (len(buffer)>>8) & 0xFF + ll = len(buffer) & 0xFF + a = struct.pack("BB", lh, ll) + sys.stdout.write(a) + sys.stdout.write(buffer) + sys.stdout.flush() + elif f == sys.stdin.fileno() and (e & POLLIN): + hdr = os.read(f, 2) + (toth, totl) = struct.unpack("BB", hdr) + tot = (toth << 8) + totl + buffer = os.read(f, tot) + v.send(buffer) + + Property changes on: trunk/vde-2/src/lib/python/VdePlug_example.py ___________________________________________________________________ Added: svn:executable + * Added: trunk/vde-2/src/lib/python/vdeplug_python.c =================================================================== --- trunk/vde-2/src/lib/python/vdeplug_python.c (rev 0) +++ trunk/vde-2/src/lib/python/vdeplug_python.c 2010-08-13 09:10:00 UTC (rev 440) @@ -0,0 +1,103 @@ +/* + * LibVdePlug/python wrapper + * Copyright © 2010 Daniele Lacamera + * + * Released under the terms of GNU GPL v.2 + * + */ +#include "Python.h" +#include <stdio.h> +#include "libvdeplug.h" + + +static PyObject *vdeplug_open(PyObject *self, PyObject *args) +{ + struct vde_open_args vde_args = {0,NULL,0}; + char *vde_sock = NULL, *vde_descr = NULL; + VDECONN *ret; + int e; + + if (!PyArg_ParseTuple(args, "ss|isi", &vde_sock, &vde_descr, &vde_args.port, &vde_args.group, &vde_args.mode)) + goto failure; + + ret = vde_open_real(vde_sock, vde_descr, 1, &vde_args); + e = errno; + if (!ret) + goto failure; + else + return PyLong_FromUnsignedLong((unsigned long) ret); + + +failure: + return PyErr_SetFromErrno(PyExc_RuntimeError); +} + +static PyObject *vdeplug_ctlfd(PyObject *self, PyObject *args) +{ + VDECONN *conn; + unsigned long vde_magic = 0; + + if (!PyArg_ParseTuple(args, "k", &vde_magic)) + goto failure; + conn = (VDECONN *) vde_magic; + + if (!conn) + goto failure; + + return Py_BuildValue("i", vde_ctlfd(conn)); + +failure: + return PyErr_SetFromErrno(PyExc_RuntimeError); +} + +static PyObject *vdeplug_datafd(PyObject *self, PyObject *args) +{ + VDECONN *conn; + unsigned long vde_magic = 0; + + if (!PyArg_ParseTuple(args, "k", &vde_magic)) + goto failure; + conn = (VDECONN *) vde_magic; + + if (!conn) + goto failure; + + return Py_BuildValue("i", vde_datafd(conn)); + +failure: + return PyErr_SetFromErrno(PyExc_RuntimeError); +} + +static PyObject *vdeplug_close(PyObject *self, PyObject *args) +{ + VDECONN *conn; + unsigned long vde_magic = 0; + + if (!PyArg_ParseTuple(args, "k", &vde_magic)) + goto failure; + conn = (VDECONN *) vde_magic; + + if (!conn) + goto failure; + + return Py_BuildValue("i", vde_close(conn)); + +failure: + return PyErr_SetFromErrno(PyExc_RuntimeError); +} + + + +static PyMethodDef vdeplug_methods[] = { + {"open", vdeplug_open, METH_VARARGS}, + {"ctlfd", vdeplug_ctlfd, METH_VARARGS}, + {"datafd", vdeplug_datafd, METH_VARARGS}, + {"close", vdeplug_close, METH_VARARGS}, + {NULL, NULL} /* Sentinel */ +}; + +void initvdeplug_python(void) +{ + (void) Py_InitModule("vdeplug_python", vdeplug_methods); +// PyErr_SetString(PyExc_RuntimeError,"vdeplug error"); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ This SF.net email is sponsored by Make an app they can't live without Enter the BlackBerry Developer Challenge http://p.sf.net/sfu/RIM-dev2dev _______________________________________________ vde-users mailing list vde-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vde-users