Support asynchronous packet handling.

This commit is contained in:
Brian Goldman 2015-11-17 14:07:50 -08:00
parent 53a55fd588
commit b14fa6484d
3 changed files with 3960 additions and 2727 deletions

File diff suppressed because it is too large Load Diff

View File

@ -138,7 +138,8 @@ cdef enum: # Protocol families, same as address families.
cdef extern from "sys/socket.h":
ssize_t recv(int __fd, void *__buf, size_t __n, int __flags) nogil
int MSG_DONTWAIT
# Dummy defines from linux/netfilter.h
cdef enum:
NF_DROP

View File

@ -171,18 +171,24 @@ cdef class NetfilterQueue:
if self.qh != NULL:
nfq_destroy_queue(self.qh)
# See warning about nfq_unbind_pf in __dealloc__ above.
def run(self):
def get_fd(self):
"""Get the file descriptor of the queue handler."""
return nfq_fd(self.h)
def run(self, block=True):
"""Begin accepting packets."""
cdef int fd = nfq_fd(self.h)
cdef char buf[BufferSize]
cdef int rv
cdef int recv_flags
recv_flags = 0 if block else MSG_DONTWAIT
with nogil:
rv = recv(fd, buf, sizeof(buf), 0)
rv = recv(fd, buf, sizeof(buf), recv_flags)
while rv >= 0:
nfq_handle_packet(self.h, buf, rv)
with nogil:
rv = recv(fd, buf, sizeof(buf), 0)
rv = recv(fd, buf, sizeof(buf), recv_flags)
PROTOCOLS = {
0: "HOPOPT",