diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 561a473..efc2167 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,9 +6,10 @@ on: tags: - "v*" pull_request: + jobs: build-and-test: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: matrix: python-version: @@ -19,9 +20,9 @@ jobs: - "3.11" steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Build package @@ -35,15 +36,15 @@ jobs: run: sudo PATH=$PATH coverage run setup.py test release: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') needs: - build-and-test steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: "3.x" - name: Build package diff --git a/doc/conf.py b/doc/conf.py index 6abeedf..ea780c0 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -196,6 +196,6 @@ # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'http://docs.python.org/': None} +intersphinx_mapping = {'python': ('http://docs.python.org/', None)} autoclass_content="both" diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 4ddd2dc..90f5ee7 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -42,10 +42,14 @@ def is_table_available(name): try: + if name in Table.existing_table_names: + return Table.existing_table_names[name] Table(name) + Table.existing_table_names[name] = True return True except IPTCError: pass + Table.existing_table_names[name] = False return False @@ -669,6 +673,11 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string i.e. "" should be used. """ + + STANDARD_TARGETS = ["", "ACCEPT", "DROP", "REJECT", "RETURN", "REDIRECT", "SNAT", "DNAT", \ + "MASQUERADE", "MIRROR", "TOS", "MARK", "QUEUE", "LOG"] + """This is the constant for all standard targets.""" + 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 @@ -784,6 +793,8 @@ def _create_buffer(self, target): self.reset() def _is_standard_target(self): + if self._name in Target.STANDARD_TARGETS: + return False for t in self._rule.tables: if t.is_chain(self._name): return True @@ -1572,6 +1583,8 @@ class Table(object): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table._cache.get(name, None) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index ed10e17..e0d8787 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -19,10 +19,14 @@ def is_table6_available(name): try: + if name in Table6.existing_table_names: + return Table6.existing_table_names[name] Table6(name) + Table6.existing_table_names[name] = True return True except IPTCError: pass + Table6.existing_table_names[name] = False return False @@ -412,22 +416,19 @@ def set_dst(self, dst): def get_in_interface(self): intf = "" if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_IN: - intf = "".join(["!", intf]) - iface = bytearray(_IFNAMSIZ) - iface[:len(self.entry.ipv6.iniface)] = self.entry.ipv6.iniface - mask = bytearray(_IFNAMSIZ) - mask[:len(self.entry.ipv6.iniface_mask)] = self.entry.ipv6.iniface_mask - if mask[0] == 0: + intf = "!" + + iface = self.entry.ipv6.iniface.decode() + mask = self.entry.ipv6.iniface_mask + + if len(mask) == 0: return None - for i in range(_IFNAMSIZ): - if mask[i] != 0: - intf = "".join([intf, chr(iface[i])]) - else: - if iface[i - 1] != 0: - intf = "".join([intf, "+"]) - else: - intf = intf[:-1] - break + + intf += iface + if len(iface) == len(mask): + intf += '+' + intf = intf[:_IFNAMSIZ] + return intf def set_in_interface(self, intf): @@ -456,23 +457,19 @@ def set_in_interface(self, intf): def get_out_interface(self): intf = "" if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_OUT: - intf = "".join(["!", intf]) - iface = bytearray(_IFNAMSIZ) - iface[:len(self.entry.ipv6.outiface)] = self.entry.ipv6.outiface - mask = bytearray(_IFNAMSIZ) - mask[:len(self.entry.ipv6.outiface_mask)] = \ - self.entry.ipv6.outiface_mask - if mask[0] == 0: + intf = "!" + + iface = self.entry.ipv6.outiface.decode() + mask = self.entry.ipv6.outiface_mask + + if len(mask) == 0: return None - for i in range(_IFNAMSIZ): - if mask[i] != 0: - intf = "".join([intf, chr(iface[i])]) - else: - if iface[i - 1] != 0: - intf = "".join([intf, "+"]) - else: - intf = intf[:-1] - break + + intf += iface + if len(iface) == len(mask): + intf += '+' + intf = intf[:_IFNAMSIZ] + return intf def set_out_interface(self, intf): @@ -579,6 +576,8 @@ class Table6(Table): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table6._cache.get(name, None) diff --git a/iptc/util.py b/iptc/util.py index 04fe905..6c1592c 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -3,7 +3,6 @@ import sys import ctypes import ctypes.util -from distutils.sysconfig import get_python_lib from itertools import product from subprocess import Popen, PIPE from sys import version_info @@ -14,6 +13,12 @@ def get_config_var(name): if name == 'SO': return '.so' raise Exception('Not implemented') +try: + from distutils.sysconfig import get_python_lib +except ModuleNotFoundError: + import sysconfig + def get_python_lib(): + return sysconfig.get_path("purelib") def _insert_ko(modprobe, modname): diff --git a/iptc/version.py b/iptc/version.py index 8a2f162..f6a5157 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.0.2-dev" +__version__ = "1.2.0" diff --git a/setup.py b/setup.py index c3e4a46..1929f93 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,6 @@ """python-iptables setup script""" from setuptools import setup, Extension -#from distutils.core import setup, Extension # make pyflakes happy __pkgname__ = None diff --git a/tests/test_iptc.py b/tests/test_iptc.py index d630c87..816df86 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -423,7 +423,9 @@ def test_rule_address(self): def test_rule_interface(self): # valid interfaces rule = iptc.Rule6() - for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+"]: + + max_length_valid_interface_name = "0123456789abcde" + for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+", max_length_valid_interface_name]: rule.in_interface = intf self.assertEquals(intf, rule.in_interface) rule.out_interface = intf