Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pynetfilter_conntrack/conntrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def register_callback(self, callback, event_type=NFCT_T_ALL, data=None):
return: NFCT_CB_CONTINUE, NFCT_CB_FAILURE, NFCT_CB_STOP,
or NFCT_CB_STOLEN (like continue, but ct is not freed).
"""
self.event_type = event_type
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indent here too.

self.callback = nfct_callback_t(callback)
self.callback_arg = data
nfct_callback_register(self.handle, event_type, self.callback, self.callback_arg)
ret = nfct_callback_register(self.handle, self.event_type, self.callback, self.callback_arg)
if ret == -1:
self._error('nfct_callback_register')
elif ret != 0:
self._error('nfct_callback_register unknown return code')

def unregister_callback(self):
"""Unregister callback"""
Expand Down Expand Up @@ -90,9 +95,13 @@ def query(self, command, argument):

May raise a RuntimeError.
"""
ret = nfct_query(self.handle, command, argument)
if ret != 0:
self.query_type = command
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indenting looks weird here ? That should confuse Python.


ret = nfct_query(self.handle, self.query_type, byref(argument))
if ret == -1:
self._error('nfct_query')
elif ret != 0:
self._error('nfct_query unknown return code')

def catch(self, callback):
"""
Expand Down
7 changes: 5 additions & 2 deletions pynetfilter_conntrack/conntrack_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def __init__(self, subsys, subscriptions):
self.conntrack = None
self.handle = None

self.subsys = subsys
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it use ? Do you plan to do something with it ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used on next list (open a conntrack handler), however it is not really used in the class.

The reason is that I saw several reports of dereferencements of data when using python clib because the garbage collector lost track of a variable used in the c library and cleaned it up.
I did not analysed this carefully, but I try to make sure the garbage collector will not dereference the data by storing systematically in the class variables that are used in the c library calls.

self.subscriptions = subscriptions
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


# Open a conntrack handler
self.handle = nfct_open(subsys, subscriptions)
if not self.handle:
self.handle = nfct_open(self.subsys, self.subscriptions)
if self.handle == None:
self._error('nfct_new')

def _error(self, func_name):
Expand Down
12 changes: 7 additions & 5 deletions pynetfilter_conntrack/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#
#nf_conntrack_p = POINTER(nf_conntrack)
#nfct_handle_p = POINTER(nfct_handle)
nf_conntrack_p = c_int
nfct_handle_p = c_int
#nf_conntrack_p = c_int
#nfct_handle_p = c_int
nf_conntrack_p = c_void_p
nfct_handle_p = c_void_p
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is a fix, it is a fix: you can remove commented lines.


class nfct_conntrack_compare_t(Structure):
_fields_ = (
Expand All @@ -33,7 +35,7 @@ class nfct_conntrack_compare_t(Structure):
# void *data)
#
# Callback type
nfct_callback_t = CFUNCTYPE(c_int, c_int, nf_conntrack_p, c_void_p)
nfct_callback_t = CFUNCTYPE(c_int, c_uint, nf_conntrack_p, c_void_p)

#--------------------------------------------------------------------------
# struct nf_conntrack *nfct_new(void): Allocate a new conntrack
Expand Down Expand Up @@ -81,7 +83,7 @@ class nfct_conntrack_compare_t(Structure):
# On error, -1 is returned and errno is explicitely set. On success, 0
# is returned
nfct_query = library.nfct_query
nfct_query.argtypes = (nfct_handle_p, c_int, c_void_p)
nfct_query.argtypes = (nfct_handle_p, c_uint, c_void_p)
nfct_query.restype = c_int

#--------------------------------------------------------------------------
Expand All @@ -93,7 +95,7 @@ class nfct_conntrack_compare_t(Structure):
# void *data);
# Register callback.
nfct_callback_register = library.nfct_callback_register
nfct_callback_register.argtypes = (nfct_handle_p, c_int, nfct_callback_t, c_void_p)
nfct_callback_register.argtypes = (nfct_handle_p, c_uint, nfct_callback_t, c_void_p)
nfct_callback_register.restype = c_int

#--------------------------------------------------------------------------
Expand Down