Skip to content

Commit b1ad40a

Browse files
committed
Add Subnet Mask conversion
1 parent dd3d92d commit b1ad40a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

iptc/easy.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,15 @@ def decode_iptc_rule(iptc_rule, ipv6=False):
385385
""" Return a dictionary representation of an iptc_rule """
386386
d = {}
387387
if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0':
388-
d['src'] = iptc_rule.src
388+
_ip, _netmask = iptc_rule.src.split('/')
389+
_netmask = _netmask_v4_to_cidr(_netmask)
390+
d['src'] = '{}/{}'.format(_ip, _netmask)
389391
elif ipv6==True and iptc_rule.src != '::/0':
390-
d['src'] = iptc_rule.src
392+
d['src'] = iptc_rule.src.rstrip('/128')
391393
if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0':
392-
d['dst'] = iptc_rule.dst.rstrip('/255.255.255.255')
394+
_ip, _netmask = iptc_rule.dst.split('/')
395+
_netmask = _netmask_v4_to_cidr(_netmask)
396+
d['dst'] = '{}/{}'.format(_ip, _netmask)
393397
elif ipv6==True and iptc_rule.dst != '::/0':
394398
d['dst'] = iptc_rule.dst.rstrip('/128')
395399
if iptc_rule.protocol != 'ip':
@@ -446,4 +450,8 @@ def _filter_empty_field(data_d):
446450
data_d[k] = ''
447451
return data_d
448452

453+
def _netmask_v4_to_cidr(netmask_addr):
454+
# Implement Subnet Mask conversion without dependencies
455+
return sum([bin(int(x)).count('1') for x in netmask_addr.split('.')])
456+
449457
### /INTERNAL FUNCTIONS ###

0 commit comments

Comments
 (0)