Skip to content

Commit 6f4ac9c

Browse files
authored
Merge pull request #273 from ldx/issue265
Add support for undefined targets (iptc.easy) - issue265
2 parents 3ebd22b + d5458b1 commit 6f4ac9c

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ High level abstractions
141141
``python-iptables`` implements a low-level interface that tries to closely
142142
match the underlying C libraries. The module ``iptc.easy`` improves the
143143
usability of the library by providing a rich set of high-level functions
144-
designed to simplify the interaction with the library, for example::
144+
designed to simplify the interaction with the library, for example:
145145

146146
>>> import iptc
147147
>>> iptc.easy.dump_table('nat', ipv6=False)
@@ -160,6 +160,11 @@ designed to simplify the interaction with the library, for example::
160160
[{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}]
161161
>>> iptc.easy.delete_chain('filter', 'TestChain', flush=True)
162162

163+
>>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto
164+
>>> iptc.easy.add_chain('filter', 'TestChainGoto')
165+
>>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}}
166+
>>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d)
167+
163168
Rules
164169
-----
165170

doc/examples.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ High level abstractions
77
``python-iptables`` implements a low-level interface that tries to closely
88
match the underlying C libraries. The module ``iptc.easy`` improves the
99
usability of the library by providing a rich set of high-level functions
10-
designed to simplify the interaction with the library, for example::
10+
designed to simplify the interaction with the library, for example:
1111

1212
>>> import iptc
1313
>>> iptc.easy.dump_table('nat', ipv6=False)
@@ -26,6 +26,11 @@ designed to simplify the interaction with the library, for example::
2626
[{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}]
2727
>>> iptc.easy.delete_chain('filter', 'TestChain', flush=True)
2828

29+
>>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto
30+
>>> iptc.easy.add_chain('filter', 'TestChainGoto')
31+
>>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}}
32+
>>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d)
33+
2934
Rules
3035
-----
3136

iptc/easy.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ def encode_iptc_rule(rule_d, ipv6=False):
295295
# Basic rule attributes
296296
rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment')
297297
iptc_rule = Rule6() if ipv6 else Rule()
298+
# Set default target
299+
rule_d.setdefault('target', '')
298300
# Avoid issues with matches that require basic parameters to be configured first
299301
for name in rule_attr:
300302
if name in rule_d:
@@ -347,7 +349,10 @@ def decode_iptc_rule(iptc_rule, ipv6=False):
347349
name = iptc_rule.target.name.replace('-', '_')
348350
d['target'] = {name:iptc_rule.target.get_all_parameters()}
349351
elif iptc_rule.target and iptc_rule.target.name:
350-
d['target'] = iptc_rule.target.name
352+
if iptc_rule.target.goto:
353+
d['target'] = {'goto':iptc_rule.target.name}
354+
else:
355+
d['target'] = iptc_rule.target.name
351356
# Return a filtered dictionary
352357
return _filter_empty_field(d)
353358

@@ -412,10 +417,12 @@ def _iptc_setmatch(iptc_rule, name, value):
412417
def _iptc_settarget(iptc_rule, value):
413418
# Target is dictionary - Use only 1 pair key/value
414419
if isinstance(value, dict):
415-
for k, v in value.items():
416-
iptc_target = iptc_rule.create_target(k)
417-
_iptc_setattr_d(iptc_target, v)
418-
return
420+
t_name, t_value = next(iter(value.items()))
421+
if t_name == 'goto':
422+
iptc_target = iptc_rule.create_target(t_value, goto=True)
423+
else:
424+
iptc_target = iptc_rule.create_target(t_name)
425+
_iptc_setattr_d(iptc_target, t_value)
419426
# Simple target
420427
else:
421428
iptc_target = iptc_rule.create_target(value)

0 commit comments

Comments
 (0)