Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- got pylibpcap to compile for python 3. took moderate modifications. had to do the following
- notes for converting python 2 code to python 3
- 1) change print stmts from '''print x''' to '''print (x)''' where x is generalized
- 2) change places where maps and filters are used. python 2, these return lists, python 3, these return the respective classes. the fix is [i for i in map(func, iterable)] and [i for i in filter(func, iterable)]
- --- map(lambda x: (x, None), config_defines) ==> [i for i in map(lambda x: (x, None), config_defines)]
- 3) in setup.py add '-py3' to the swig command parameters, update include dirs to contain python3 headers on the mac, and also the libdirs for python:
- <snip line: 23>
- # specify its directory here, otherwise set this to None
- libpcap_dir = None
- # libpcap_dir = "/home/wiml/netsrc/libpcap/libpcap-0.7.2"
- # libpcap_dir = "/home/wiml/netsrc/libpcap/libpcap-0.8.3"
- # libpcap_dir = "/home/wiml/netsrc/libpcap/cvs.tcpdump.org/libpcap"
- library_dirs = ["/Library/Frameworks/Python.framework/Versions/3.2/lib/" ]
- if not libpcap_dir is None:
- library_dirs.append(libpcap_dir)
- include_dirs = ["/Library/Frameworks/Python.framework/Versions/3.2/Headers" ]
- </snip>
- Also updated library_dirs and include_dirs, accordingly:<snip: line 153>
- if libpcap_dir is None:
- pcap_extension = Extension("_pcapmodule",
- sourcefiles,
- include_dirs = include_dirs,
- define_macros = defines,
- library_dirs = library_dirs,
- libraries = [ "pcap" ]
- )
- else:
- include_dirs.append( libpcap_dir )
- # extension_objects=[ os.path.join(libpcap_dir, 'libpcap.a') ]
- pcap_extension = Extension("_pcapmodule",
- sourcefiles,
- include_dirs = include_dirs,
- define_macros = defines,
- library_dirs = library_dirs,
- libraries = [ "pcap" ]
- )
- 4) update docify.py and docify-shadow.py with the above. Also in docify.py, need to perform the following change (from http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=542984 ):
- if re.search('swigregister|SWIG_PyInstanceMethod_New',fname):
- #if re.search('swigregister',fname):
- 5) update mk-constants.py (strings module no longer has replace, so the line needs to be modified (i just comment out the string.replace and write the file out...laziness):
- rcs = ' Id: mk-constants.py,v 1.7 2008/01/26 01:31:55 wiml Exp \n'
- fp.write(rcs)#string.replace(rcs, '$', ''))
- 6) had to perform some basic foo to get the right Python stuff working:python3 setup.py build
- sudo python3 setup.py install --prefix=/Library/Frameworks/Python.framework/Versions/3.2/ #mac
- Ok, so it appears 3.2 changes the C-API signifigantly, Surprise!
- http://paste.pocoo.org/show/253494/ <-- swig patch that helped fix one problem, but I ran into another issue with PyLong_AS_INT
- So, Now I have run into a number of deprecations that is super frustrating. I think I need to revert the swig file and patch from the beginning, but the interface file may be an issue.
- Converted the following thus far:
- PyString_* -> PyUnicode_* ===> Wrong needed to convert this to PyBytes_*
- PyInt_* -> PyLong_*
- SUCCESS!!
- [Ones on the left are my modfied versions, ones on the right are clean]
- Output from diff of pcap_interface.c:
- 0:pylibpcap-0.6.2 apridgen$ diff pcap_interface.c ~/pylibpcap-0.6.2/pcap_interface.c
- 248c248
- < outObject = Py_BuildValue("iy#f", header.len, buf, header.caplen,
- ---
- > outObject = Py_BuildValue("is#f", header.len, buf, header.caplen,
- 285c285
- < PyObject *linktype = PyLong_FromLong( (long) (links[i]) );
- ---
- > PyObject *linktype = PyInt_FromLong( (long) (links[i]) );
- 428c428
- < str = PyBytes_FromString(buf);
- ---
- > str = PyString_FromString(buf);
- 484c484
- < return PyBytes_FromStringAndSize( (const char *)sa, length );
- ---
- > return PyString_FromStringAndSize( (const char *)sa, length );
- 521c521
- < return PyBytes_FromFormat("<AF %d>", sa->sa_family);
- ---
- > return PyString_FromFormat("<AF %d>", sa->sa_family);
- 534c534
- < result = PyBytes_FromString(buf);
- ---
- > result = PyString_FromString(buf);
- 674c674
- < out=PyLong_FromLong(addr.s_addr);
- ---
- > out=PyInt_FromLong(addr.s_addr);
- 700c700
- < arglist = Py_BuildValue("iy#f", header->len, packetdata, header->caplen,
- ---
- > arglist = Py_BuildValue("is#f", header->len, packetdata, header->caplen,
- Output of diff for pcap.i:
- 0:pylibpcap-0.6.2 apridgen$ diff pcap.i ~/pylibpcap-0.6.2/pcap.i
- 53c53
- < PyObject *v = PyLong_FromLong(pcapmodule_DLT[constantIndex].value);
- ---
- > PyObject *v = PyInt_FromLong(pcapmodule_DLT[constantIndex].value);
- 91,92c91,92
- < if (PyLong_CheckExact($input)) {
- < $1 = (unsigned long)PyLong_AsLong($input);
- ---
- > if (PyInt_CheckExact($input)) {
- > $1 = (unsigned long)PyInt_AS_LONG($input);
- Output for changes that need to be made for swig, as mentioned above:
- 0:pylibpcap-0.6.2 apridgen$ diff pcap.c ~/pylibpcap-0.6.2/pcap.c
- 2427,2429c2427,2428
- < type_pointer = PyCapsule_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer" SWIG_TYPE_TABLE_NAME, 0);
- < // type_pointer = PyCObject_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION,
- < // (char*)"type_pointer" SWIG_TYPE_TABLE_NAME);
- ---
- > type_pointer = PyCObject_Import((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION,
- > (char*)"type_pointer" SWIG_TYPE_TABLE_NAME);
- 2472c2471
- < SWIG_Python_DestroyModule(PyObject *capsule)
- ---
- > SWIG_Python_DestroyModule(void *vptr)
- 2474,2477c2473
- < swig_module_info *swig_module = (swig_module_info *)PyCapsule_GetPointer(capsule, (char*)"swig_runtime_data" SWIG_RUNTIME_VERSION);
- < if (swig_module == NULL)
- < return;
- <
- ---
- > swig_module_info *swig_module = (swig_module_info *) vptr;
- 2502,2503c2498
- < PyObject *pointer = PyCapsule_New((void *) swig_module, (char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer" SWIG_TYPE_TABLE_NAME, SWIG_Python_DestroyModule);
- < //PyObject *pointer = PyCObject_FromVoidPtr((void *) swig_module, SWIG_Python_DestroyModule);
- ---
- > PyObject *pointer = PyCObject_FromVoidPtr((void *) swig_module, SWIG_Python_DestroyModule);
- 2526,2527c2521
- < descriptor = (swig_type_info *) PyCapsule_GetPointer(obj, (char*)"swig_type_info");
- < //descriptor = (swig_type_info *) PyCObject_AsVoidPtr(obj);
- ---
- > descriptor = (swig_type_info *) PyCObject_AsVoidPtr(obj);
- 2532,2533c2526
- < obj = PyCapsule_New(descriptor, (char*)"swig_type_info", NULL);
- < //obj = PyCObject_FromVoidPtr(descriptor, NULL);
- ---
- > obj = PyCObject_FromVoidPtr(descriptor, NULL);
- 0:pylibpcap-0.6.2 apridgen$ diff pcap.py ~/pylibpcap-0.6.2/pcap.py
- 81a82
- > if int(sys.version[0])>='2':
- PyString_* -> PyUnicode_* ===> Wrong needed to convert this to PyBytes_*
- I also needed to change the s# to y#.
- I had some other problems, but i got this little script to run, so that works for me.
- building and installing on a mac:
- python3 setup.py build
- sudo python3 setup.py install --prefix=/Library/Frameworks/Python.framework/Versions/3.2/
- python3
- My obviously weak test script:
- import pcap
- p = pcap.pcapObject()
- p.open_offline("../../traffic/lbl-internal.20041004-1303.port001.dump.anon")
- f = []
- while True:
- c = p.next()
- if c is None:
- break
- f.append(c)
- print(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement