Matches¶
The programmatic interface to packet matching is based on the concept
of a Match object that provides methods returning
Criterion objects which in turn allow for equality (and
inequality) testing against a stored value.
In the following example, PacketMatch (a subclass of
Match) provides matching against packet attributes
such as protocol, source address, etc.
m = PacketMatch()
m.protocol().equals('udp')
The protocol() method returns a
ProtocolCriterion` object which stores the value that
we want to compare against (udp in this case).
A Match object may have multiple criteria; such criteria
are specific to the Match subclass.
Continuing the example:
a = IPv4Network('1.2.3.4/32')
mcast = IPV4Network('224.0.0.0/4')
m.source_address().equals(a).dest_address().not_equals(mcast)
The source_address() method returns a
SourceAddressCriterion object, while
the dest_address() method returns a
DestAddressCriterion object.
The resulting Match object now matches UDP packets with
a source address of 1.2.3.4 and a destination address that is not
a multicast address.
The Match class is the parent class of all match-related classes.
No objects of this class are instantiated.
The Criterion class is the parent class of all classes implementing
match-specific criteria.
No objects of this class are instantiated.
Objects of subclasses of Criterion are never directly instantiated
by the user; they are instantiated
as needed by the Match subclasses.
MatchNone is a special subclass of Match that is used
to indicate the absence of a match.
- class Match[source]¶
Parent class for all match-specific subclasses.
- static build_iptables_args(match_name: Optional[str], crit_iterable) List[str][source]¶
Helper method to build an iptables(8) argument list from a match name and a list of
Criterionobjects. iptables(8) arguments will be extracted from each criterion that is set.- Parameters:
match_name – optional match name that will result in adding ‘-m’
match_nameat the beginning of the argument list; there must be at least one set criterion for the match name to be included in the return valuecrit_iterable – a
Criterioniterator
- class Criterion(match: Match)[source]¶
This class is used to express an iptables(8) match criterion; it does not perform any comparisons.
Criterionis a superclass that serves the following purposes:it provides an
equals()method and anot_equals()method to express comparison against a valueit keeps track of whether the criterion has been set; a criterion is set when either the
equals()ornot_equals()method is invoked; a criterion may only be set onceit keeps track of whether a criterion is negated or not
its
iptables_to_args()method generates the ‘!’ iptables(8) argument, and also checks if the criterion was set
The
equals()/not_equals()methods ofCriterionsubclasses must invoke the_set_polarity()method ofCriterionto indicate the polarity of the test. These methods are also responsible for saving the comparison value in the subclass object.A
Criterionhas an owner which is an object of a subclass ofMatch. Theequals()/not_equals()methods return this object to facilitate building a criteria list:match.crit1().equals('foo').crit2().not_equals('bar')
- Parameters:
match – the
Matchobject that owns thisCriterion
- compare(is_equal: bool, *args, **kwargs) Match[source]¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- equals(*args, **kwargs) Match[source]¶
Express equality comparison against the argument values.
The method implementation in this class expects no arguments and expresses a bool comparison.
Subclasses will implement this method to express comparisons against a specific value (or values). These values will be the arguments of the subclass method and will be stored in the subclass object.
Subclasses overriding this method should invoke the
_set_polarity()method of this class to set the polarity toTrue.Returns this object.
- is_positive() bool[source]¶
Returns the ‘polarity’ of the criterion;
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- not_equals(*args, **kwargs) Match[source]¶
Express inequality comparison against the argument values.
The method implementation in this class invokes the
equals()method and then reverses the polarity; subclasses should not need to override it.Subclasses overriding this method should invoke the
_set_polarity()method of this class to set the polarity toFalse.Returns this object.