Ë '"@ÊðÛ ã óº—dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZmZmZmZmZdd lmZd diZd „ZGd „dedgd¢««ZGd„de«Zededdfd„«Zed d„«Zed!d„«Zed"d„«Zed"d„«Z ed"d„«Z!ed"d„«Z"ed"d„«Z#ed"d„«Z$ed"d„«Z%ed"d„«Z&ed"d„«Z'd„Z(y)#a� High level ipset support. When :doc:`ipset` is providing a direct netlink socket with low level functions, a :class:`WiSet` object is built to map ipset objects from kernel. It helps to add/remove entries, list content, etc. For example, adding an entry with :class:`pyroute2.ipset.IPSet` object implies to set a various number of parameters: .. doctest:: :skipif: True >>> ipset = IPSet() >>> ipset.add("foo", "1.2.3.4/24", etype="net") >>> ipset.close() When they are discovered by a :class:`WiSet`: .. doctest:: :skipif: True >>> wiset = load_ipset("foo") >>> wiset.add("1.2.3.4/24") Listing entries is also easier using :class:`WiSet`, since it parses for you netlink messages: .. doctest:: :skipif: True >>> wiset.content {'1.2.3.0/24': IPStats(packets=None, bytes=None, comment=None, timeout=None, skbmark=None, physdev=False)} éN)Ú namedtuple)Ú getcallargs)ÚAF_INET)Ú basestring©ÚIPSet)Ú IPSetError)ÚIPSET_FLAG_IFACE_WILDCARDÚIPSET_FLAG_PHYSDEVÚIPSET_FLAG_WITH_COMMENTÚIPSET_FLAG_WITH_COUNTERSÚIPSET_FLAG_WITH_SKBINFO)Ú IP_PROTOCOLSÚcountcó‡—ˆfd„}|S)aâDecorator to create netlink socket if needed. In many of our helpers, we need to open a netlink socket. This can be expensive for someone using many times the functions: instead to have only one socket and use several requests, we will open it again and again. This helper allow our functions to be flexible: the caller can pass an optional socket, or do nothing. In this last case, this decorator will open a socket for the caller (and close it after call) It also help to mix helpers. One helper can call another one: the socket will be opened only once. We just have to pass the ipset variable. Note that all functions using this helper *must* use ipset as variable name for the socket. có•—t‰g|¢­i|¤Ž}|d€Vtdxxdz cc<t«5}||d<d|vr |j|j d««‰di|¤Žcddd«S‰|i|¤ŽS#1swYŒxYw)NÚsockréÚkwargs©)rÚCOUNTrÚupdateÚpop)ÚargsrÚcallargsrÚfuns €ú3/usr/lib/python3.12/site-packages/pyroute2/wiset.pyÚwrapzneed_ipset_socket..wrapMsŠø€Ü˜sÐ4 TÒ4¨VÑ4ˆØ �FÑ Ð #ä �'‹N˜aÑ ‹NÜ”˜DØ#'�˜Ñ ð˜xÑ'Ø—O‘O H§L¡L°Ó$:Ô;Ù‘˜X‘÷ ‘ñ�DÐ#˜FÑ#Ð#÷�ús ±1A4Á4A=r)rrs` rÚneed_ipset_socketr;sø€ô$ $ð €Kócó(‡—eZdZdZ dˆfd„ ZˆxZS)ÚIPStatsrc ó:•—tt|� ||||||||¬«S)N)ÚphysdevÚwildcard)Úsuperr"Ú__new__) ÚclsÚpacketsÚbytesÚcommentÚtimeoutÚskbmarkr$r%Ú __class__s €rr'zIPStats.__new__os6ø€ô”W˜cÑ*Ø Ø Ø Ø Ø Ø ØØð+ó  ð r )FF)Ú__name__Ú __module__Ú __qualname__Ú __slots__r'Ú __classcell__)r.s@rr"r"_sø„ð€IðØ÷ ñ r r")r)r*r+r,r-r$r%c óÜ—eZdZdZddedddddddf d„Zd„Zd„Zed„«Z e jd „«Z d „Z d „Z e dd „«Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zed„«Zd„Zd„Zy)ÚWiSeta¶Main high level ipset manipulation class. Every high level ipset operation should be possible with this class, you probably don't need other helpers of this module, except tools to load data from kernel (:func:`load_all_ipsets` and :func:`load_ipset`) For example, you can create and an entry in a ipset just with: .. doctest:: :skipif: True >>> with WiSet(name="mysuperipset") as myset: >>> myset.create() # add the ipset in the kernel >>> myset.add("198.51.100.1") # add one IP to the set Netlink sockets are opened by __enter__ and __exit__ function, so you don't have to manage it manually if you use the "with" keyword. If you want to manage it manually (for example for long operation in a daemon), you can do the following: .. doctest:: :skipif: True >>> myset = WiSet(name="mysuperipset") >>> myset.open_netlink() >>> # do stuff >>> myset.close_netlink() You can also don't initiate at all any netlink socket, this code will work: .. doctest:: :skipif: True >>> myset = WiSet(name="mysuperipset") >>> myset.create() >>> myset.destroy() But do it very carefully. In that case, a netlink socket will be opened in background for any operation. No socket will be leaked, but that can consume resources. You can also instantiate WiSet objects with :func:`load_all_ipsets` and :func:`load_ipset`: .. doctest:: :skipif: True >>> all_sets_dict = load_all_ipsets() >>> one_set = load_ipset(name="myset") Have a look on content variable if you need list of entries in the Set. Nzhash:ipFc óÈ—||_||_d|_d|_||_||_d|_||_||_||_ ||_ | |_ d|_ | |_ y©N)ÚnameÚhashsizeÚ _attr_typeÚ entry_typeÚ attr_typeÚfamilyÚ_contentrr,Úcountersr+ÚrevisionÚindexÚskbinfo) Úselfr8r<r=rr,r?r+r9r@rBs rÚ__init__zWiSet.__init__¾sf€ðˆŒ Ø ˆŒ ؈ŒØˆŒØ"ˆŒØˆŒ ؈Œ ؈Œ ؈Œ Ø ˆŒ ؈Œ Ø ˆŒ ؈Œ ؈� r có<—|j€t«|_yy)zh Open manually a netlink socket. You can use "with WiSet()" statement instead. N)rr©rCs rÚ open_netlinkzWiSet.open_netlinkÚs€ð �9‰9Ð Ü›ˆD�Ið r có`—|j�"|jj«d|_yy)zClone any opened netlink socketN)rÚcloserFs rÚ close_netlinkzWiSet.close_netlinkãs&€à �9‰9Ð Ø �I‰I�O‰OÔ ØˆD�Ið !r có—|jSr7)r:rFs rr<zWiSet.attr_typeés €à�‰Ðr cóF—||_|jdd«d|_y)NÚ:r)r:Úsplitr;)rCÚvalues rr<zWiSet.attr_typeís €àˆŒØŸ+™+ c¨1Ó-¨aÑ0ˆ�r có&—|j«|Sr7)rGrFs rÚ __enter__zWiSet.__enter__òs€Ø ×ÑÔØˆ r có$—|j«yr7)rJ)rCÚexc_typeÚ exc_valueÚ tracebacks rÚ__exit__zWiSet.__exit__ös€Ø ×ÑÕr có@—|«}|jd«|_|jd«|_|jd«|_|jd«|_|jd«|_|jd«|_|jd«}|jd«|_|jd «}|�Et|tz«|_ t|tz«|_ t|tz«|_|r|j|«|S) a Create a ipset objects based on a parsed netlink message :param ndmsg: the netlink message to parse :param content: should we fill (and parse) entries info (can be slow on very large set) :type content: bool ÚIPSET_ATTR_TYPENAMEÚIPSET_ATTR_SETNAMEÚIPSET_ATTR_HASHSIZEÚIPSET_ATTR_FAMILYÚIPSET_ATTR_REVISIONÚIPSET_ATTR_INDEXÚIPSET_ATTR_DATAÚIPSET_ATTR_TIMEOUTÚIPSET_ATTR_CADT_FLAGS)Úget_attrr<r8r9r=r@rAr,Úboolr r?r r+rrBÚupdate_dict_content)r(ÚndmsgÚcontentrCÚdataÚflagss rÚ from_netlinkzWiSet.from_netlinkùsò€ñ‹uˆØŸ™Ð(=Ó>ˆŒØ—N‘NÐ#7Ó8ˆŒ ØŸ™Ð'<Ó=ˆŒ Ø—n‘nÐ%8Ó9ˆŒ ØŸ™Ð'<Ó=ˆŒ Ø—^‘^Ð$6Ó7ˆŒ Ø�~‰~Ð/Ó0ˆØ—}‘}Ð%9Ó:ˆŒ Ø— ‘ Ð5Ó6ˆØ Ð Ü  Ô)AÑ!AÓBˆDŒMÜ Ô(?Ñ ?Ó@ˆDŒLÜ Ô(?Ñ ?Ó@ˆDŒLá Ø × $Ñ $ UÔ +àˆ r c ó—d}d}|j€i|_d}|jd«jd«}|D�]¼}d}|jj d«D�]x}|dk(r'|j|«j|«} || z }�nD|d k(rN|j|«j|«} || z }|jd «} | ��|d j | «z }nñ|d k(r||jd «z }n×|dk(r||jd«z }n½|dk(r'|t t|jd«««z }n‘|dk(rs|jd«} | �Btj| t | ««j«} |dj | ¬«z }|t |jd««z }n|dk(r||jd«z }|dz }�Œ{|jd«}|j�|jd«}|jd«} | �Q| ddk7r2dj| D� cgc]} t t| ««‘Œc} «} nt t| d««} dd i}|jd!«}|�*t|tz«|d<t|t z«|d"<t#d'|jd#«|jd$«|jd%«| |d&œ|¤Ž}||j|<�Œ¿ycc} w)(zœUpdate a dictionary statistics with values sent in netlink message :param ndmsg: the netlink message :type ndmsg: netlink message ÚIPSET_ATTR_IPADDR_IPV4ÚIPSET_ATTR_IP_FROMNÚIPSET_ATTR_ADTr^ÚÚ,ÚipÚnetÚIPSET_ATTR_CIDRz/{0}ÚifaceÚIPSET_ATTR_IFACEÚsetÚIPSET_ATTR_NAMEÚmarkÚIPSET_ATTR_MARKÚportÚIPSET_ATTR_PROTOz{proto}:)ÚprotoÚIPSET_ATTR_PORT_FROMÚmacÚIPSET_ATTR_ETHERr_ÚIPSET_ATTR_SKBMARKrlÿÿÚ/rr$Fr`r%ÚIPSET_ATTR_PACKETSÚIPSET_ATTR_BYTESÚIPSET_ATTR_COMMENT)r)r*r+r-r,r)r>raÚ get_attrsr;rNÚformatÚstrÚhexrÚgetÚlowerÚstripr,Újoinrbr r r")rCrdr=Úip_attrr,ÚentriesÚentryÚkeyÚ parse_typeroÚcidrrzr-rvÚentry_flag_parsedrgrOs rrczWiSet.update_dict_contents€ð*ˆØ&ˆØ �=‰=Р؈DŒMàˆØ—.‘.Ð!1Ó2×<Ñ<Ð=NÓOˆÜˆE؈CØ"Ÿo™o×3Ñ3°C×8� Ø Ò%ØŸ™¨Ó0×9Ñ9¸&ÓA�Bؘ2‘I’CØ 5Ò(ØŸ™¨Ó0×9Ñ9¸&ÓA�Bؘ2‘I�CØ Ÿ>™>Ð*;Ó<�DØÑ'ؘvŸ}™}¨TÓ2Ñ2™Ø 7Ò*ؘ5Ÿ>™>Ð*<Ó=Ñ=‘CØ 5Ò(ؘ5Ÿ>™>Ð*;Ó<Ñ<‘CØ 6Ò)Øœ3œs 5§>¡>Ð2CÓ#DÓEÓFÑF‘CØ 6Ò)Ø!ŸN™NÐ+=Ó>�EØÐ(Ü ,× 0Ñ 0°¼¸E» Ó C× IÑ IÓ K˜Ø˜z×0Ñ0°uÐ0Ó=Ñ=˜Øœ3˜uŸ~™~Ð.DÓEÓFÑF‘CØ 5Ò(ؘ5Ÿ>™>Ð*<Ó=Ñ=�CØ�s‘ ’ð19ð4—)‘)˜C“.ˆCà�|‰|Ð'ØŸ.™.Ð)=Ó>�Ø—n‘nÐ%9Ó:ˆGØÐ"ð˜1‘: )Ò,Ø!Ÿh™hÁ7Ó'KÁ7¸4¬¬C°«I­À7Ñ'KÓL‘Gä!¤# g¨a¡j£/Ó2�GØ!*¨EÐ 2Ð Ø—N‘NÐ#:Ó;ˆEØÐ Ü/3°EÔÚupdate_wiset_contentrrFs rÚupdate_contentzWiSet.update_contentªs€àˆŒ ܘT¨¯ © Ö2r cóF—t|j|j¬«y)zFlush entries of the ipsetr—N)Ú flush_ipsetr8rrFs rÚflushz WiSet.flush¯s€ä�D—I‘I D§I¡IÖ.r cóR—|j€|j«|jS)zqDictionary of entries in the set. Keys are IP addresses (as string), values are IPStats tuples. )r>r²rFs rrez WiSet.content³s$€ð �=‰=Ð Ø × Ñ Ô !à�}‰}Ðr có4—|D]}|j|«Œy)z?Just a small helper to reduce the number of loops in main code.N©r¥)rCrŒr�s rÚ insert_listzWiSet.insert_list¾s€ãˆEØ �H‰H�U�Oñr có\—ttj««dd}t|j|j ¬«}||_|j |_|j «|j|«t|j||j ¬«|j«y)a}Replace the content of an ipset with a new list of entries. This operation is like a flush() and adding all entries one by one. But this call is atomic: it creates a temporary ipset and swap the content. :param new_list: list of entries to add :type new_list: list or :py:class:`set` of basestring or of keyword arguments dict rér—N) r…ÚuuidÚuuid4Ú load_ipsetr8rr•r¹Ú swap_ipsetsr™)rCÚnew_listÚ temp_nameÚtemps rÚreplace_entrieszWiSet.replace_entriesÃsu€ôœŸ ™ › Ó% a¨Ð*ˆ ä˜$Ÿ)™)¨$¯)©)Ô4ˆØˆŒ Ø—I‘IˆŒ Ø � ‰ Œ Ø ×јÔ"Ü�D—I‘I˜y¨t¯y©yÕ9Ø � ‰ �r )F)r/r0r1Ú__doc__rrDrGrJÚpropertyr<ÚsetterrQrVÚ classmethodrhrcr•r™r¥r©r¬r¯r²rµrer¹rÃrr rr5r5†sЄñ4ðrØØØ ØØØØØØóò8 òð ñóðð×Ññ1óð1òòðòóðò8F'òP ò(1ò ò4 ò  ò  ò3ò /ðñóðòó r r5Fc ó2—|j|f|||dœ|¤Žy)zCreate an ipset.)r“r=Ú exclusiveN)r•)r8r“r=rÉrrs rr”r”Øs)€ð €D‡K�KØ ðØ &°IñØAGór có(—i}|j«D]|}|jd«}|�|j|«sŒ(||vr0tj ||¬«}|r||_|||j <Œ\|sŒ_|j j|«Œ~|S)aLList all ipset as WiSet objects. Get full ipset data from kernel and parse it in WiSet objects. Result is a dictionary with ipset names as keys, and WiSet objects as values. :param content: parse the list of entries and fill it in WiSet content dictionary :type content: bool :param inherit_sock: use the netlink sock passed in ipset arg to fill WiSets sock :type inherit_sock: bool :param prefix: filter out all ipset with a name not beginning by this prefix :type prefix: str or None rY©re)ÚlistraÚ startswithr5rhrr8rc)rerÚ inherit_sockÚprefixÚresÚmysetr8Úwisets rÚload_all_ipsetsrÓâs�€ð" €CØ—‘–ˆà�~‰~Ð2Ó3ˆØ Ð  d§o¡o°fÔ&=Ø Ø �s‰?Ü×&Ñ& u°gÐ&Ó>ˆEÙØ!�” Ø#ˆC�— ‘ ŠOÚ Ø �— ‘ ‰O× /Ñ /°Õ 6ðð €Jr có—d} |j|¬«}|D]:}|€"t j ||¬«}|sŒ||_Œ'|sŒ*|j|«Œ<|S#t$r*}|jtjk(r|cYd}~S‚d}~wwxYw)aòGet one ipset as WiSet object Helper to get current WiSet object. More efficient that :func:`load_all_ipsets` since the kernel does the filtering itself. Return None if the ipset does not exist :param name: name of the ipset :type name: str :param content: parse or not content and statistics on entries :type content: bool :param inherit_sock: use the netlink sock passed in ipset arg to fill WiSet sock :type inherit_sock: bool N©r8rË) rÌr ÚcodeÚerrnoÚENOENTr5rhrrc)r8rerrÎrÐÚmessagesÚeÚmsgs rr¾r¾s�€ð" €CðØ—9‘9 $�9Ó'ˆó ˆØ ˆ;Ü×$Ñ$ S°'Ð$Ó:ˆCÚØ�•Ú Ø × #Ñ # CÕ (ð ð €Jøô òØ �6‰6”U—\‘\Ò !Ø�JØ ûðús#„AÁ B Á BÁ>B ÂBÂB cóh—|j|j¬«D]}|j|«Œy)z¹Update content/statistics of a wiset. You should never call yourself this function. It is only a helper to use the :func:`need_ipset_socket` decorator out of WiSet object. rÕN)rÌr8rc)rÒrrÛs rr±r±%s+€ð�y‰y˜eŸj™jˆyÖ)ˆØ ×!Ñ! #Õ&ñ*r có&—|j|«y)zRemove an ipset in the kernel.N)r™©r8rs rr˜r˜0s€ð ‡L�L�Õr c ó,—|j||fi|¤Žy)z Add an entryNr¸©r8r�rrs rr£r£6s€ð €D‡H�HˆT�5Ñ#˜FÓ#r c ó,—|j||fi|¤Žy)zRemove one entryN)r©ràs rr§r§<s€ð€D‡K�K��eÑ&˜vÓ&r có�— |j|«y#t$r(}|jtjk(rYd}~y‚d}~wwxYw)zTest if the given ipset existTNF)Úheadersr rÖr×rØ)r8rrÚs rÚtest_ipset_existräBs<€ðØ � ‰ �TÔØøÜ òØ �6‰6”U—\‘\Ò !ÜØ ûðús‚” A�A¿AÁAc ó*—|j||fi|¤ŽS)z Test if an entry is in one ipset)r¬ràs rr«r«Ns€ð ˆ4�9‰9�T˜5Ñ + FÑ +Ð+r c ót—t«}|D](}|j||fi|¤ŽsŒ|j|«Œ*|S)z"Test a list (or a set) of entries.)rtr¬r¥)r8rŒrrrÐr�s rr®r®Ts<€ô ‹%€CÛˆØ ˆ4�9‰9�T˜5Ñ + FÓ +Ø �G‰G�E�Nðð €Jr có&—|j|«y)zFlush all ipset contentN)rµrÞs rr´r´^s€ð ‡J�JˆtÕr có(—|j||«y)zQSwap the content of ipset a and b. ipsets must have compatible content. N)Úswap)Úname_aÚname_brs rr¿r¿ds€ð  ‡I�Iˆf�fÕr c ó—tdi|¤ŽS)z7Get a socket that one can pass to several WiSet objectsrr)rs rÚget_ipset_socketríms€ä ‰?�6‰?Ðr )FNFN)FNFr7))rÄr×r¼Ú collectionsrÚinspectrÚsocketrÚpyroute2.commonrÚpyroute2.ipsetrÚpyroute2.netlink.exceptionsr Ú pyroute2.netlink.nfnetlink.ipsetr r r r rÚ%pyroute2.netlink.nfnetlink.nfctsocketrrrr"Úobjectr5r”rÓr¾r±r˜r£r§rär«r®r´r¿rírr rÚr÷soðñ"óH Û Ý"ÝÝå&Ý Ý2÷õõ?ð �!ˆ €ò!ôH# ÙØò ó ô# ôNOˆFôOðd à˜W°¸Dòóððòóðð@òóððBò'óð'ðòóðð ò$óð$ð ò'óð'ð òóððò,óð,ð òóððòóðð òóðór