Skip to content

Commit

Permalink
conntrack: fix passing pointers around
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 13, 2017
1 parent dd79059 commit 4b023c8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions broker/conntrack.py
Expand Up @@ -37,6 +37,8 @@
# Support OpenWRT.
libc = CDLL('libc.so.0')

nfct.nfct_new.restype = c_void_p
nfct.nfct_open.restype = c_void_p

NFCT_CALLBACK = CFUNCTYPE(c_int, c_int, c_void_p, c_void_p)

Expand Down Expand Up @@ -239,7 +241,7 @@ def kill(self, proto, src, dst, sport, dport, silent_query = True):
dport: destination port
'''

ct = nfct.nfct_new()
ct = c_void_p(nfct.nfct_new())
if not ct:
raise ConntrackError("nfct_new failed!")

Expand All @@ -263,7 +265,7 @@ def kill(self, proto, src, dst, sport, dport, silent_query = True):
nfct.nfct_set_attr_u16(ct, ATTR_PORT_SRC, libc.htons(sport))
nfct.nfct_set_attr_u16(ct, ATTR_PORT_DST, libc.htons(dport))

h = nfct.nfct_open(CONNTRACK, 0)
h = c_void_p(nfct.nfct_open(CONNTRACK, 0))
if not h:
raise ConntrackError("nfct_open failed!")

Expand All @@ -276,7 +278,7 @@ def kill(self, proto, src, dst, sport, dport, silent_query = True):

def killall(self, proto = None, src = None, dst = None, sport = None, dport = None):
'''Removes specified conntrack entries.'''
tct = nfct.nfct_new()
tct = c_void_p(nfct.nfct_new())
if not tct:
raise ConntrackError("nfct_new failed!")

Expand All @@ -302,22 +304,23 @@ def killall(self, proto = None, src = None, dst = None, sport = None, dport = No
if dport:
nfct.nfct_set_attr_u16(tct, ATTR_PORT_DST, libc.htons(dport))

kh = nfct.nfct_open(CONNTRACK, 0)
kh = c_void_p(nfct.nfct_open(CONNTRACK, 0))

if not kh:
libc.perror("nfct_open")
raise ConntrackError("nfct_open failed!")

@NFCT_CALLBACK
def cb(type, ct, data):
ct = c_void_p(ct)
if not nfct.nfct_cmp(tct, ct, NFCT_CMP_ALL | NFCT_CMP_MASK):
return NFCT_CB_CONTINUE

# Remove the matched item
nfct.nfct_query(kh, NFCT_Q_DESTROY, ct)
return NFCT_CB_CONTINUE

h = nfct.nfct_open(CONNTRACK, 0)
h = c_void_p(nfct.nfct_open(CONNTRACK, 0))

if not h:
nfct.nfct_close(kh)
Expand Down

0 comments on commit 4b023c8

Please sign in to comment.