[pypy-issue] Issue #3114: SO_ATTACH_FILTER (pypy/pypy)

gpotter2 issues-reply at bitbucket.org
Wed Nov 13 07:26:06 EST 2019

New issue 3114: SO_ATTACH_FILTER



The following call is made on a raw socket:

sock.setsockopt(socket.SOL_SOCKET, SO_ATTACH_FILTER, bp)

Where bp is a `bpf_program` C structure \( generated using libpcap’s `compile_filter`\) that we recreated using

You import ctypes

class bpf_insn(ctypes.Structure):
    """"The BPF instruction data structure"""
    _fields_ = [("code", ctypes.c_ushort),
                ("jt", ctypes.c_ubyte),
                ("jf", ctypes.c_ubyte),
                ("k", ctypes.c_int)]

class bpf_program(ctypes.Structure):
    """"Structure for BIOCSETF"""
    _fields_ = [('bf_len', ctypes.c_int),
                ('bf_insns', ctypes.POINTER(bpf_insn))]

This approach \(following Linux' docs for : `SO_ATTACH_FILTER`[http://man7.org/linux/man-pages/man7/socket.7.html](http://man7.org/linux/man-pages/man7/socket.7.html)\) works in both 2.7 and 3 versions of CPython, and fails with 

> TypeError: expected bytes, got bpf\_program object

On Pypy3 \(7.1.1 beta0\) \(note actually all pypy versions\).


The full code is actually quite long, as it contains libpcap bindings, socket creation… 

To debug the issue, you can get this modified version of Scapy:


Make sure libpcap is installed and run `sudo pypy3 -m scapy` then type `sniff(filter=”tcp”)`.


FTR on master, we have to carry around old compatibility functions to make this work for PyPy using bytes based filters \(`https://github.com/secdev/scapy/blob/43fda76e560e3c94ab64fc23f8ee29c582b459be/scapy/arch/common.py#L111`\) which we would like to be able to drop.


Thanks for the support

More information about the pypy-issue mailing list