Skip to content

Commit 9c9b4b7

Browse files
authored
fix(ip6tc): ipv6 interface name with 15 chars is too big (#348)
* fix(ip6tc): ipv6 interface name with 15 chars is too big * feat(tests): add case for max valid length network interface name
1 parent 794aace commit 9c9b4b7

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

iptc/ip6tc.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,19 @@ def set_dst(self, dst):
412412
def get_in_interface(self):
413413
intf = ""
414414
if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_IN:
415-
intf = "".join(["!", intf])
416-
iface = bytearray(_IFNAMSIZ)
417-
iface[:len(self.entry.ipv6.iniface)] = self.entry.ipv6.iniface
418-
mask = bytearray(_IFNAMSIZ)
419-
mask[:len(self.entry.ipv6.iniface_mask)] = self.entry.ipv6.iniface_mask
420-
if mask[0] == 0:
415+
intf = "!"
416+
417+
iface = self.entry.ipv6.iniface.decode()
418+
mask = self.entry.ipv6.iniface_mask
419+
420+
if len(mask) == 0:
421421
return None
422-
for i in range(_IFNAMSIZ):
423-
if mask[i] != 0:
424-
intf = "".join([intf, chr(iface[i])])
425-
else:
426-
if iface[i - 1] != 0:
427-
intf = "".join([intf, "+"])
428-
else:
429-
intf = intf[:-1]
430-
break
422+
423+
intf += iface
424+
if len(iface) == len(mask):
425+
intf += '+'
426+
intf = intf[:_IFNAMSIZ]
427+
431428
return intf
432429

433430
def set_in_interface(self, intf):
@@ -456,23 +453,19 @@ def set_in_interface(self, intf):
456453
def get_out_interface(self):
457454
intf = ""
458455
if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_OUT:
459-
intf = "".join(["!", intf])
460-
iface = bytearray(_IFNAMSIZ)
461-
iface[:len(self.entry.ipv6.outiface)] = self.entry.ipv6.outiface
462-
mask = bytearray(_IFNAMSIZ)
463-
mask[:len(self.entry.ipv6.outiface_mask)] = \
464-
self.entry.ipv6.outiface_mask
465-
if mask[0] == 0:
456+
intf = "!"
457+
458+
iface = self.entry.ipv6.outiface.decode()
459+
mask = self.entry.ipv6.outiface_mask
460+
461+
if len(mask) == 0:
466462
return None
467-
for i in range(_IFNAMSIZ):
468-
if mask[i] != 0:
469-
intf = "".join([intf, chr(iface[i])])
470-
else:
471-
if iface[i - 1] != 0:
472-
intf = "".join([intf, "+"])
473-
else:
474-
intf = intf[:-1]
475-
break
463+
464+
intf += iface
465+
if len(iface) == len(mask):
466+
intf += '+'
467+
intf = intf[:_IFNAMSIZ]
468+
476469
return intf
477470

478471
def set_out_interface(self, intf):

tests/test_iptc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ def test_rule_address(self):
423423
def test_rule_interface(self):
424424
# valid interfaces
425425
rule = iptc.Rule6()
426-
for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+"]:
426+
427+
max_length_valid_interface_name = "0123456789abcde"
428+
for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+", max_length_valid_interface_name]:
427429
rule.in_interface = intf
428430
self.assertEquals(intf, rule.in_interface)
429431
rule.out_interface = intf

0 commit comments

Comments
 (0)