Skip to content

Commit 7fe9a99

Browse files
authored
Extended rule counters capabilities (#289)
* Extended rule counters capabilities * Remove counters in tests when comparing rules
1 parent d46b145 commit 7fe9a99

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ designed to simplify the interaction with the library, for example:
148148
{'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []}
149149
>>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False)
150150
[{'comment': {'comment': 'DNS traffic to Google'},
151+
'counters': (1, 56),
151152
'dst': '8.8.8.8/32',
152153
'protocol': 'udp',
153154
'target': 'ACCEPT',

doc/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ designed to simplify the interaction with the library, for example:
1414
{'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []}
1515
>>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False)
1616
[{'comment': {'comment': 'DNS traffic to Google'},
17+
'counters': (1, 56),
1718
'dst': '8.8.8.8/32',
1819
'protocol': 'udp',
1920
'target': 'ACCEPT',

iptc/easy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ def encode_iptc_rule(rule_d, ipv6=False):
305305
try:
306306
if name in rule_attr:
307307
continue
308+
elif name == 'counters':
309+
_iptc_setcounters(iptc_rule, value)
308310
elif name == 'target':
309311
_iptc_settarget(iptc_rule, value)
310312
else:
@@ -353,6 +355,8 @@ def decode_iptc_rule(iptc_rule, ipv6=False):
353355
d['target'] = {'goto':iptc_rule.target.name}
354356
else:
355357
d['target'] = iptc_rule.target.name
358+
# Get counters
359+
d['counters'] = iptc_rule.counters
356360
# Return a filtered dictionary
357361
return _filter_empty_field(d)
358362

@@ -388,6 +392,10 @@ def _iptc_getchain(table, chain, ipv6=False, raise_exc=True):
388392
except Exception as e:
389393
if raise_exc: raise
390394

395+
def _iptc_setcounters(iptc_rule, value):
396+
# Value is a tuple (numberOfBytes, numberOfPackets)
397+
iptc_rule.counters = value
398+
391399
def _iptc_setmatch(iptc_rule, name, value):
392400
# Iterate list/tuple recursively
393401
if isinstance(value, list) or isinstance(value, tuple):

iptc/ip4tc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,15 @@ def get_counters(self):
12861286
counters = self.entry.counters
12871287
return counters.pcnt, counters.bcnt
12881288

1289+
def set_counters(self, counters):
1290+
"""This method set a tuple pair of the packet and byte counters of
1291+
the rule."""
1292+
self.entry.counters.pcnt = counters[0]
1293+
self.entry.counters.bcnt = counters[1]
1294+
1295+
counters = property(get_counters, set_counters)
1296+
"""This is the packet and byte counters of the rule."""
1297+
12891298
# override the following three for the IPv6 subclass
12901299
def _entry_size(self):
12911300
return xt_align(ct.sizeof(ipt_entry))

tests/test_iptc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ def test_rule_to_dict(self):
581581
target = iptc.Target(rule, "ACCEPT")
582582
rule.target = target
583583
rule_d = iptc.easy.decode_iptc_rule(rule, ipv6=True)
584+
# Remove counters when comparing rules
585+
rule_d.pop('counters', None)
584586
self.assertEqual(rule_d, {"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"})
585587

586588
def test_rule_from_dict(self):
@@ -939,6 +941,8 @@ def test_rule_to_dict(self):
939941
target = iptc.Target(rule, "ACCEPT")
940942
rule.target = target
941943
rule_d = iptc.easy.decode_iptc_rule(rule)
944+
# Remove counters when comparing rules
945+
rule_d.pop('counters', None)
942946
self.assertEqual(rule_d, {"protocol": "tcp", "src": "127.0.0.1/32", "target": "ACCEPT"})
943947

944948
def test_rule_from_dict(self):

tests/test_matches.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ def test_recent(self):
499499
}
500500
iptc.easy.add_rule(self.table, self.chain, rule_d)
501501
rule2_d = iptc.easy.get_rule(self.table, self.chain, -1)
502+
# Remove counters when comparing rules
503+
rule2_d.pop('counters', None)
502504
self.assertEqual(rule_d, rule2_d)
503505

504506
def suite():

0 commit comments

Comments
 (0)