Skip to content

Using python-netfilterqueue with gevent #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
remleduff opened this issue Jun 30, 2015 · 5 comments
Closed

Using python-netfilterqueue with gevent #5

remleduff opened this issue Jun 30, 2015 · 5 comments

Comments

@remleduff
Copy link
Contributor

I tried using this in an application that uses gevent, and because you call the recv system call rather than using python's socket.recv, gevent isn't able to monkey patch your library.

This causes my application to block at unpredictable times.

I've worked around this by adding a method to NetfilterQueue, but wondered if you might have a better idea about how to do it.

def run2(self):
    "Version of the run method which uses normal socket.recv so that gevent can monkeypatch it"
    import socket
    cdef int fd = nfq_fd(self.h)
    s = socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM)
    while True:
        d = s.recv(BufferSize)
        if d >= 0:
            nfq_handle_packet(self.h, d, len(d))
        else:
            break
    s.close()
@AkihiroSuda
Copy link

+1

@mattfox mattfox mentioned this issue Jun 28, 2016
@mattfox
Copy link
Collaborator

mattfox commented Jun 29, 2016

Great idea. I'll see what I can do.

For reference, here's a proof of concept gevent echoserver: https://gist.github.com/mattfox/3ea31317a050b163a90b36d88c3e252b (based on https://github.com/gevent/gevent/blob/master/examples/echoserver.py)

Works with the following run2 method (same as yours but works in Python 3): https://gist.github.com/mattfox/46b6b6da081f4cb065f0e40e49cd783f

@mattfox
Copy link
Collaborator

mattfox commented Jun 29, 2016

Oh, and my apologies for being silent for a year.

@mattfox
Copy link
Collaborator

mattfox commented Jun 29, 2016

Just note that I didn't solve this exactly like in your run2() example. You need to make your own socket and provide it to the run_socket() method. See the readme for an example.

@remleduff
Copy link
Contributor Author

This looks great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants