Skip to content

Commit 2bf0827

Browse files
author
ldx
committed
Handle extensions aliases.
1 parent 296310c commit 2bf0827

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

iptc/ip4tc.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ def __init__(self, rule, name=None, match=None, revision=None):
456456
if self._module.next is not None:
457457
self._store_buffer(module)
458458

459+
self._check_alias(module[0], match)
460+
459461
self._match_buf = (ct.c_ubyte * self.size)()
460462
if match:
461463
ct.memmove(ct.byref(self._match_buf), ct.byref(match), self.size)
@@ -477,6 +479,19 @@ def __eq__(self, match):
477479
def __ne__(self, match):
478480
return not self.__eq__(match)
479481

482+
def _check_alias(self, module, match):
483+
# This is ugly, but there are extensions using an alias name. Check if
484+
# that's the case, and load that extension as well if necessary. It
485+
# will be used to parse parameters, since the 'real' extension
486+
# probably won't understand them.
487+
if getattr(module, "alias", None) is not None and module.alias:
488+
self._alias_name = module.alias(match)
489+
alias = self._xt.find_match(self._alias_name)
490+
if not alias:
491+
raise XTablesError("can't find alias match %s" %
492+
(self._alias_name))
493+
self._alias = alias[0]
494+
480495
def _store_buffer(self, module):
481496
self._buffer = _Buffer()
482497
self._buffer.buffer = ct.cast(module, ct.POINTER(ct.c_ubyte))
@@ -485,7 +500,11 @@ def _final_check(self):
485500
self._xt.final_check_match(self._module)
486501

487502
def _parse(self, argv, inv, entry):
488-
self._xt.parse_match(argv, inv, self._module, entry,
503+
if self._alias is not None:
504+
module = self._alias
505+
else:
506+
module = self._module
507+
self._xt.parse_match(argv, inv, module, entry,
489508
ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p)))
490509

491510
def _get_size(self):
@@ -505,6 +524,8 @@ def _update_pointers(self):
505524
self._ptrptr = ct.cast(ct.pointer(self._ptr),
506525
ct.POINTER(ct.POINTER(xt_entry_match)))
507526
self._module.m = self._ptr
527+
if self._alias is not None:
528+
self._alias.m = self._ptr
508529

509530
def reset(self):
510531
"""Reset the match.
@@ -579,6 +600,8 @@ def __init__(self, rule, name=None, target=None, revision=None):
579600
else:
580601
self._revision = self._module.revision
581602

603+
self._check_alias(module[0], target)
604+
582605
self._create_buffer(target)
583606

584607
if self._is_standard_target():
@@ -605,6 +628,19 @@ def __eq__(self, targ):
605628
def __ne__(self, target):
606629
return not self.__eq__(target)
607630

631+
def _check_alias(self, module, target):
632+
# This is ugly, but there are extensions using an alias name. Check if
633+
# that's the case, and load that extension as well if necessary. It
634+
# will be used to parse parameters, since the 'real' extension
635+
# probably won't understand them.
636+
if getattr(module, "alias", None) is not None and module.alias:
637+
self._alias_name = module.alias(target)
638+
alias = self._xt.find_target(self._alias_name)
639+
if not alias:
640+
raise XTablesError("can't find alias target %s" %
641+
(self._alias_name))
642+
self._alias = alias[0]
643+
608644
def _create_buffer(self, target):
609645
self._buffer = _Buffer(self.size)
610646
self._target_buf = self._buffer.buffer
@@ -625,7 +661,11 @@ def _final_check(self):
625661
self._xt.final_check_target(self._module)
626662

627663
def _parse(self, argv, inv, entry):
628-
self._xt.parse_target(argv, inv, self._module, entry,
664+
if self._alias is not None:
665+
module = self._alias
666+
else:
667+
module = self._module
668+
self._xt.parse_target(argv, inv, module, entry,
629669
ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p)))
630670
self._target_buf = ct.cast(self._module.t, ct.POINTER(ct.c_ubyte))
631671
self._buffer.buffer = self._target_buf
@@ -660,6 +700,8 @@ def _update_pointers(self):
660700
self._ptrptr = ct.cast(ct.pointer(self._ptr),
661701
ct.POINTER(ct.POINTER(xt_entry_target)))
662702
self._module.t = self._ptr
703+
if self._alias is not None:
704+
self._alias.t = self._ptr
663705

664706
def reset(self):
665707
"""Reset the target. Parameters are set to their default values, any

0 commit comments

Comments
 (0)