From eb186ddfb43b5b54c2a43731427edb6a26a52c0c Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 17:08:20 +0700 Subject: [PATCH 1/4] Initial pepoluan Commit Modify .hgignore to exclude PyCharm working dirs. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index eeb8a34..739a2eb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ /Vagrantfile /.vagrant /tags + +# Added exclusion for PyCharm files +.idea/* From 2d9eb2b7fad7a1b57287972eb83f765c86b766f4 Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 17:18:19 +0700 Subject: [PATCH 2/4] Implement -g Support In general, changes made are: * Change constructor of Target to allow 'goto' option * Change create_target method of Rule, same purpose * Change how IPT_F_FRAG gets set in rule.entry.ip --- iptc/ip4tc.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 3c6d42a..35ae04c 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -662,7 +662,7 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string i.e. "" should be used. """ - def __init__(self, rule, name=None, target=None, revision=None): + def __init__(self, rule, name=None, target=None, revision=None, goto=None): """ *rule* is the Rule object this match belongs to; it can be changed later via *set_rule()*. *name* is the name of the iptables target @@ -672,6 +672,7 @@ def __init__(self, rule, name=None, target=None, revision=None): should be used; different revisions use different structures in C and they usually only work with certain kernel versions. Python-iptables by default will use the latest revision available. + If goto is True, then it converts '-j' to '-g'. """ if name is None and target is None: raise ValueError("can't create target based on nothing") @@ -682,6 +683,17 @@ def __init__(self, rule, name=None, target=None, revision=None): self._orig_parse = None self._orig_options = None + if target is not None or goto is None: + # We are 'decoding' existing Target + self._goto = bool(rule.entry.ip.flags & ipt_ip.IPT_F_GOTO) + if goto is not None: + assert isinstance(goto, bool) + self._goto = goto + if goto: + rule.entry.ip.flags |= ipt_ip.IPT_F_GOTO + else: + rule.entry.ip.flags &= ~ipt_ip.IPT_F_GOTO + self._xt = xtables(rule.nfproto) module = (self._is_standard_target() and @@ -831,6 +843,10 @@ def _get_target(self): target = property(_get_target) """This is the C structure used by the extension.""" + def _get_goto(self): + return self._goto + goto = property(_get_goto) + class Policy(object): """ @@ -960,11 +976,11 @@ def create_match(self, name, revision=None): self.add_match(match) return match - def create_target(self, name, revision=None): + def create_target(self, name, revision=None, goto=False): """Create a new *target*, and set it as this rule's target. *name* is the name of the target extension, *revision* is the revision to - use.""" - target = Target(self, name=name, revision=revision) + use. *goto* determines if target uses '-j' (default) or '-g'.""" + target = Target(self, name=name, revision=revision, goto=False) self.target = target return target @@ -1207,7 +1223,10 @@ def get_fragment(self): def set_fragment(self, frag): self.entry.ip.invflags &= ~ipt_ip.IPT_INV_FRAG & ipt_ip.IPT_INV_MASK - self.entry.ip.flags = int(bool(frag)) + if frag: + self.entry.ip.flags |= ipt_ip.IPT_F_FRAG + else: + self.entry.ip.flags &= ~ipt_ip.IPT_F_FRAG fragment = property(get_fragment, set_fragment) """This means that the rule refers to the second and further fragments of From c9da2e93a3a8503fb7aa7a01854cd7d65f341d8d Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 19:18:38 +0700 Subject: [PATCH 3/4] Bugfix #1 in this branch So create_target()'s' goto parameter did not get passed to Target()... fixed. --- iptc/ip4tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 35ae04c..f43df7d 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -980,7 +980,7 @@ def create_target(self, name, revision=None, goto=False): """Create a new *target*, and set it as this rule's target. *name* is the name of the target extension, *revision* is the revision to use. *goto* determines if target uses '-j' (default) or '-g'.""" - target = Target(self, name=name, revision=revision, goto=False) + target = Target(self, name=name, revision=revision, goto=goto) self.target = target return target From d3df22b2520fc68c4eaa3852b571704d2d9787eb Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Fri, 16 Dec 2016 01:25:59 +0700 Subject: [PATCH 4/4] Make Compatible with ip6tc Now properly use get_ip() and introspection to ensure compatibility with ip6tc, and future. (IPv8, anyone?) --- iptc/ip4tc.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index f43df7d..19599a9 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -683,16 +683,27 @@ def __init__(self, rule, name=None, target=None, revision=None, goto=None): self._orig_parse = None self._orig_options = None + # NOTE: + # get_ip() returns the 'ip' structure that contains (1)the 'flags' field, and + # (2)the value for the GOTO flag. + # We *must* use get_ip() because the actual name of the field containing the + # structure apparently differs between implementation + ipstruct = rule.get_ip() + f_goto_attrs = [a for a in dir(ipstruct) if a.endswith('_F_GOTO')] + if len(f_goto_attrs) == 0: + raise RuntimeError('What kind of struct is this? It does not have "*_F_GOTO" constant!') + _F_GOTO = getattr(ipstruct, f_goto_attrs[0]) + if target is not None or goto is None: # We are 'decoding' existing Target - self._goto = bool(rule.entry.ip.flags & ipt_ip.IPT_F_GOTO) + self._goto = bool(ipstruct.flags & _F_GOTO) if goto is not None: assert isinstance(goto, bool) self._goto = goto if goto: - rule.entry.ip.flags |= ipt_ip.IPT_F_GOTO + ipstruct.flags |= _F_GOTO else: - rule.entry.ip.flags &= ~ipt_ip.IPT_F_GOTO + ipstruct.flags &= ~_F_GOTO self._xt = xtables(rule.nfproto)