HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/pyroute2/__pycache__/ipset.cpython-37.pyc
B

��fD^�@sXdZddlZddlZddlmZddlmZmZmZm	Z	m
Z
mZddlm
Z
mZddlmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8ddl9m:Z:d	d
�Z;Gdd�de<�Z=Gd
d�de<�Z>Gdd�de:�Z?Gdd�de
�Z@dS)a.
ipset support.

This module is tested with hash:ip, hash:net, list:set and several
other ipset structures (like hash:net,iface). There is no guarantee
that this module is working with all available ipset modules.

It supports almost all kernel commands (create, destroy, flush,
rename, swap, test...)
�N)�
basestring)�NETLINK_NETFILTER�	NLM_F_ACK�
NLM_F_DUMP�
NLM_F_EXCL�
NLM_F_REQUEST�NLMSG_ERROR)�
IPSetError�NetlinkError)�NFNL_SUBSYS_IPSET)'�
IPSET_CMD_ADD�IPSET_CMD_CREATE�
IPSET_CMD_DEL�IPSET_CMD_DESTROY�IPSET_CMD_FLUSH�IPSET_CMD_GET_BYINDEX�IPSET_CMD_GET_BYNAME�IPSET_CMD_HEADER�IPSET_CMD_LIST�IPSET_CMD_PROTOCOL�IPSET_CMD_RENAME�IPSET_CMD_SWAP�IPSET_CMD_TEST�IPSET_CMD_TYPE�IPSET_ERR_BUSY�IPSET_ERR_COMMENT�IPSET_ERR_COUNTER�IPSET_ERR_EXIST�IPSET_ERR_EXIST_SETNAME2�IPSET_ERR_FIND_TYPE�IPSET_ERR_INVALID_CIDR�IPSET_ERR_INVALID_FAMILY�IPSET_ERR_INVALID_MARKMASK�IPSET_ERR_INVALID_NETMASK�IPSET_ERR_IPADDR_IPV4�IPSET_ERR_IPADDR_IPV6�IPSET_ERR_MAX_SETS�IPSET_ERR_PROTOCOL�IPSET_ERR_REFERENCED�IPSET_ERR_SKBINFO�IPSET_ERR_TIMEOUT�IPSET_ERR_TYPE_MISMATCH�IPSET_FLAG_IFACE_WILDCARD�IPSET_FLAG_PHYSDEV�IPSET_FLAG_WITH_COMMENT�IPSET_FLAG_WITH_COUNTERS�IPSET_FLAG_WITH_FORCEADD�IPSET_FLAG_WITH_SKBINFO�	ipset_msg)�
NetlinkSocketcCs|ddtkS)N�header�type)r)�msg�r7�?/opt/alt/python37/lib/python3.7/site-packages/pyroute2/ipset.py�_nlmsg_errorFsr9c@seZdZdZddd�ZdS)�	PortRangeaFA simple container for port range with optional protocol

    Note that optional protocol parameter is not supported by all
    kernel ipset modules using ports. On the other hand, it's sometimes
    mandatory to set it (like for hash:net,port ipsets)

    Example::

        udp_proto = socket.getprotobyname("udp")
        port_range = PortRange(1000, 2000, protocol=udp_proto)
        ipset.create("foo", stype="hash:net,port")
        ipset.add("foo", ("192.0.2.0/24", port_range), etype="net,port")
        ipset.test("foo", ("192.0.2.0/24", port_range), etype="net,port")
    NcCs||_||_||_dS)N)�begin�end�protocol)�selfr;r<r=r7r7r8�__init__ZszPortRange.__init__)N)�__name__�
__module__�__qualname__�__doc__r?r7r7r7r8r:Jsr:c@seZdZdZddd�ZdS)�	PortEntryz8A simple container for port entry with optional protocolNcCs||_||_dS)N)�portr=)r>rEr=r7r7r8r?cszPortEntry.__init__)N)r@rArBrCr?r7r7r7r8rD`srDcs\eZdZdZeeeeeeeee	ee
eiZdddddddd	d
ddd
�Zd?�fdd�	Z
eeBdfdd�Zdd�Zd@dd�Zdd�ZdAdd�ZdBdd�Zdejd d!d!dd!ddddd!fd"d#�Zed$d%��Zd&d'�ZdCd)d*�Zejd ddd(dddd!f	d+d,�Zejd d(fd-d.�Zejd(fd/d0�Zd1d2�Z dDd3d4�Z!d5d6�Z"d7d8�Z#d9d:�Z$d;d<�Z%ejfd=d>�Z&�Z'S)E�IPSetzf
    NFNetlink socket (family=NETLINK_NETFILTER).

    Implements API to the ipset functionality.
    ZIPSET_ATTR_IFACEZIPSET_ATTR_MARK�IPSET_ATTR_NAMEZIPSET_ATTR_ETHER�IPSET_ATTR_PORTZIPSET_ATTR_IP_FROMZIPSET_ATTR_IP2ZIPSET_ATTR_CIDRZIPSET_ATTR_CIDR2ZIPSET_ATTR_IP_TOZIPSET_ATTR_IP2_TO)Ziface�mark�set�macrE)�ip_from�)rL�)�cidrrM)rOrN)�ip_torM)rPrNNrNcshtt|�jtd�tdd�|j��D��}|�|�||_|dkrX|�	�}|d�
d�}||_||_dS)N)�familycSs g|]\}}|td>B|f�qS)�)r)�.0�x�yr7r7r8�
<listcomp>�sz"IPSet.__init__.<locals>.<listcomp>r�IPSET_ATTR_PROTOCOL)
�superrFr?r�dict�policy�itemsZregister_policy�
_nfgen_family�get_proto_version�get_attr�_proto_version�_attr_revision)r>�versionZ
attr_revision�nfgen_familyrZr6)�	__class__r7r8r?�s
zIPSet.__init__c
Cs^|j|d<yt|j||td>B||d��Stk
rX}zt|j|d��Wdd}~XYnXdS)NrbrR)�	terminate)�cmd)r\�tupleZnlm_requestrr
�_IPSetError�code)r>r6Zmsg_type�	msg_flagsrd�errr7r7r8�request�s


z
IPSet.requestcKs|jtfd|i|��S)z�
        Get headers of the named ipset. It can be used to test if one ipset
        exists, since it returns a no such file or directory.
        �name)�_list_or_headersr)r>rl�kwargsr7r7r8�headers�sz
IPSet.headers�cCs t�}d|gg|d<|�|t�S)z�
        Get supported protocol version by kernel.

        version parameter allow to set mandatory (but unused?)
        IPSET_ATTR_PROTOCOL netlink attribute in the request.
        rW�attrs)r2rkr)r>rar6r7r7r8r]�szIPSet.get_proto_versioncOs|r|d|d<|jtf|�S)z�
        List installed ipsets. If `name` is provided, list
        the named ipset or return an empty list.

        Be warned: netlink does not return an error if given name does not
        exit, you will receive an empty list.
        rrl)rmr)r>�argvrnr7r7r8�list�sz
IPSet.listcCsVt�}d|jgg|d<|dk	r0|d�d|g�|dk	rJ|d�d|g�|�||�S)NrWrq�IPSET_ATTR_SETNAMEZIPSET_ATTR_FLAGS)r2r_�appendrk)r>rerl�flagsr6r7r7r8rm�szIPSet._list_or_headerscCsJt�}d|jgg|d<|dk	r0|d�d|g�|j|tttBtBtd�S)zQ
        Destroy one (when name is set) or all ipset (when name is None)
        rWrqNrt)rird)	r2r_rurkrrrrr9)r>rlr6r7r7r8�destroy�s
z
IPSet.destroyzhash:ipTFcCs�|rtnd}t�}d}|r"|tO}|r.|tO}|r:|tO}|
rF|tO}|dkr^|dkr^td��dgi}|r�|dd|gg7<|dk	r�|dd|gg7<|	dk	r�|dd|	gg7<n&|dk	r�|d	kr�|dd
|gg7<|
dk	�r|dd|
gg7<|dk	�r�|dk�r�t|t��rV|dd|j	gg7<|dd
|j
gg7<n4|dd|dgg7<|dd
|dgg7<|jdk�r�|�|�d}n|j}d|j
gd|gd|gd|gd|gd|gg|d<|j|tttB|Btd�S)a
        Create an ipset `name` of type `stype`, by default
        `hash:ip`.

        Common ipset options are supported:

        * exclusive -- if set, raise an error if the ipset exists
        * counters -- enable data/packets counters
        * comment -- enable comments capability
        * maxelem -- max size of the ipset
        * forceadd -- you should refer to the ipset manpage
        * hashsize -- size of the hashtable (if any)
        * timeout -- enable and set a default value for entries (if not None)
        * bitmap_ports_range -- set the specified inclusive portrange for
                                the bitmap ipset structure (0, 65536)
        * size -- Size of the list:set, the default is 8
        * skbinfo -- enable skbinfo capability
        rzbitmap:portNz Missing value bitmap_ports_rangerq�IPSET_ATTR_CADT_FLAGSZIPSET_ATTR_MAXELEMZIPSET_ATTR_HASHSIZEzlist:setZIPSET_ATTR_SIZE�IPSET_ATTR_TIMEOUT�IPSET_ATTR_PORT_FROM�IPSET_ATTR_PORT_TOrMrWrt�IPSET_ATTR_TYPENAME�IPSET_ATTR_FAMILY�IPSET_ATTR_REVISION�IPSET_ATTR_DATA)rird)rr2r/r.r0r1�
ValueError�
isinstancer:r;r<r`�get_supported_revisionsr_rkr
rrr9)r>rl�styperQ�	exclusiveZcounters�commentZmaxelemZforceaddZhashsize�timeoutZbitmap_ports_range�sizeZskbinfo�	excl_flagr6Z
cadt_flags�data�revisionr7r7r8�create�s`"

zIPSet.createcCs>|dk	r:|tjkrdS|tjkr$dS|tjkr2dStd��dS)NZIPSET_ATTR_IPADDR_IPV4ZIPSET_ATTR_IPADDR_IPV6zunknown family)�socket�AF_INET�AF_INET6�	AF_UNSPEC�	TypeError)rQr7r7r8�_family_to_versionCs


zIPSet._family_to_versionc
Cs�g}d}|dkr"|d|gg7}|St|t�r6|�d�}t|tttf�rL|g}�x�t||�d��D�]j\}}|dk�r|d7}|dkr�d|kr�|�d�\}}||jd	|ft|�gg7}n8d
|kr�|�d
�\}}	||jd|fd||	ggigg7}||jd
|fd||ggigg7}q`|dk�r�t|t��rh|d|jgg7}|d|j	gg7}|j
dk	�r�|d|j
gg7}nNt|t��r�|d|jgg7}|j
dk	�r�|d|j
gg7}n||j||gg7}q`||j||gg7}q`W|S)NrrJrG�,)�ip�netrMr��/rO�-rPrqrLrErzr{ZIPSET_ATTR_PROTOrH)r�r�split�intr:rD�zip�attr_mapr;r<r=rE)
r>�entry�etype�
ip_versionrqZip_count�e�trO�tor7r7r8�_entry_to_data_attrsNsL



zIPSet._entry_to_data_attrsr�cCs2|rtnd}d}|r|tO}|r(|tO}|�|�}|�|||�}|dk	r\|d|gddgg7}|dk	rr|d|gg7}|
dk	r�|d|
gg7}|	dk	r�|d|	gg7}|dk	r�|d|gg7}|dk	r�|d|gg7}|
dk	r�|d	|
gg7}|r�|d
|gg7}t�}d|jgd|gd
d|igg|d<|j||tt	B|Bt
d�S)NrZIPSET_ATTR_COMMENTZIPSET_ATTR_CADT_LINENOryZIPSET_ATTR_BYTESZIPSET_ATTR_PACKETSZIPSET_ATTR_SKBMARKZIPSET_ATTR_SKBPRIOZIPSET_ATTR_SKBQUEUErxrWrtrrq)rird)rr,r-r�r�r2r_rkrrr9)r>rlr�rQrer�r�r�r�Zpackets�bytes�skbmark�skbprio�skbqueue�wildcardZphysdevr�Z	adt_flagsr�Z
data_attrsr6r7r7r8�_add_delete_test�sF

zIPSet._add_delete_testc

Ks*|j|||t|f|||||	|
|d�|��S)a�
        Add a member to the ipset.

        etype is the entry type that you add to the ipset. It's related to
        the ipset type. For example, use "ip" for one hash:ip or bitmap:ip
        ipset.

        When your ipset store a tuple, like "hash:net,iface", you must use a
        comma a separator (etype="net,iface")

        entry is a string for "ip" and "net" objects. For ipset with several
        dimensions, you must use a tuple (or a list) of objects.

        "port" type is specific, since you can use integer of specialized
        containers like :class:`PortEntry` and :class:`PortRange`

        Examples::

            ipset = IPSet()
            ipset.create("foo", stype="hash:ip")
            ipset.add("foo", "198.51.100.1", etype="ip")

            ipset = IPSet()
            ipset.create("bar", stype="bitmap:port",
                         bitmap_ports_range=(1000, 2000))
            ipset.add("bar", 1001, etype="port")
            ipset.add("bar", PortRange(1500, 2000), etype="port")

            ipset = IPSet()
            import socket
            protocol = socket.getprotobyname("tcp")
            ipset.create("foobar", stype="hash:net,port")
            port_entry = PortEntry(80, protocol=protocol)
            ipset.add("foobar", ("198.51.100.0/24", port_entry),
                      etype="net,port")

        wildcard option enable kernel wildcard matching on interface
        name for net,iface entries.
        )r�r�r�r�r�r�r�)r�r)
r>rlr�rQr�r�r�r�r�r�r�r�rnr7r7r8�add�s6z	IPSet.addcCs|j|||t||d�S)zp
        Delete a member from the ipset.

        See :func:`add` method for more information on etype.
        )r�)r�r)r>rlr�rQr�r�r7r7r8�deleteszIPSet.deletec
CsTy|j|||td|d�dStk
rN}z|jtkr:dS|�Wdd}~XYnXdS)zr
        Test if entry is part of an ipset

        See :func:`add` method for more information on etype.
        F)r�TN)r�rr	rhr)r>rlr�rQr�r�r7r7r8�tests
z
IPSet.testcCs8t�}d|jgd|gd|gg|d<|j|tttBtd�S)zJ
        Swap two ipsets. They must have compatible content type.
        rWrtr|rq)rird)r2r_rkrrrr9)r>Zset_aZset_br6r7r7r8�swap#sz
IPSet.swapcCsFt�}d|jgg|d<|dk	r0|d�d|g�|j|tttBtd�S)zL
        Flush all ipsets. When name is set, flush only this ipset.
        rWrqNrt)rird)r2r_rurkrrrr9)r>rlr6r7r7r8�flush4szIPSet.flushcCs8t�}d|jgd|gd|gg|d<|j|tttBtd�S)z#
        Rename the ipset.
        rWrtr|rq)rird)r2r_rkrrrr9)r>Zname_srcZname_dstr6r7r7r8�renameCszIPSet.renamecCs^|jdkrt��t�}|tkr4d|jgd|gg|d<|tkrRd|jgd|gg|d<|�||�S)N�rWrtrqZIPSET_ATTR_INDEX)r_�NotImplementedErrorr2rrrk)r>re�valuer6r7r7r8�_get_set_byTs
zIPSet._get_set_bycCs|�t|�S)z'
        Get a set by its name
        )r�r)r>rlr7r7r8�get_set_bynamegszIPSet.get_set_bynamecCs|�t|�S)z(
        Get a set by its index
        )r�r)r>�indexr7r7r8�get_set_byindexnszIPSet.get_set_byindexcCs\t�}d|jgd|gd|gg|d<|j|tttBtd�}|d�d�}|d�d�}||fS)	a5
        Return minimum and maximum of revisions supported by the kernel.

        Each ipset module (like hash:net, hash:ip, etc) has several
        revisions. Newer revisions often have more features or more
        performances. Thanks to this call, you can ask the kernel
        the list of supported revisions.

        You can manually set/force revisions used in IPSet constructor.

        Example::

            ipset = IPSet()
            ipset.get_supported_revisions("hash:net")

            ipset.get_supported_revisions("hash:net,port,net")
        rWr|r}rq)rirdrZIPSET_ATTR_PROTOCOL_MINr~)r2r_rkrrrr9r^)r>r�rQr6�responseZmin_revisionZmax_revisionr7r7r8r�uszIPSet.get_supported_revisions)NNrN)rp)NN)N)
NNr�NNNNNFF)N)(r@rArBrCrr2rrrrrrZr�r?rrrkror]rsrmrwr�r�r��staticmethodr�r�r�r�r�r�r�r�r�r�r�r�r��
__classcell__r7r7)rcr8rFhs�

	
U>
0;
rFcs�eZdZdZd�fdd�	Zedededede	d	e
d
edediZ
ejd
ededededediZediZedediZedediZediZediZe ee!ee"ee#ee$ee%eiZ&�Z'S)rgz�
    Proxy class to not import all specifics ipset code in exceptions.py

    Out of the ipset module, a caller should use parent class instead
    NcsP||jkr|j|}n$||jkr:|j|}||kr:||}tt|��||�dS)N)�base_map�cmd_maprXrgr?)r>rhr6reZ	error_map)rcr7r8r?�s


z_IPSetError.__init__z+Kernel error received: ipset protocol errorz<The value of the CIDR parameter of the IP address is invalidz?Timeout cannot be used: set was created without timeout supportz-An IPv4 address is expected, but not receivedz-An IPv6 address is expected, but not receivedzLPacket/byte counters cannot be used: set was created without counter supportzComment string is too long!zHSkbinfo mapping cannot be used:  set was created without skbinfo supportz<Set cannot be created: set with the same name already existsz-Kernel error received: set type not supportedzJKernel error received: maximal number of sets reached, cannot create more.z-The value of the netmask parameter is invalidz.The value of the markmask parameter is invalidz-Protocol family not supported by the set typez;Set cannot be destroyed: it is in use by a kernel componentz=Set cannot be renamed: a set with the new name already existsz5Set cannot be renamed: it is in use by another systemz5Sets cannot be swapped: the second set does not existz5The sets cannot be swapped: their type does not matchz6Element cannot be added to the set: it's already addedz6Element cannot be deleted from the set: it's not added)NN)(r@rArBrCr?r'r r*r$r%rrr)r��errno�EEXISTrr&r#r"r!Zc_maprZdestroy_maprr(Zr_mapr+Zs_maprZa_mapZdel_mapr
rrrrrr�r�r7r7)rcr8rg�s:
rg)ArCr�r�Zpyroute2.commonrZpyroute2.netlinkrrrrrrZpyroute2.netlink.exceptionsr	r
Zpyroute2.netlink.nfnetlinkrZ pyroute2.netlink.nfnetlink.ipsetrr
rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2Zpyroute2.netlink.nlsocketr3r9�objectr:rDrFrgr7r7r7r8�<module>
s" �)5