Ë
    R/jó4  ã                  óú   — d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZ ddlmZ dd	lmZmZmZ eeeeefZeez  ez  ez  ez  Zeeeeef   f   ZeZeZ e!Z"e#Z$d
Z%dd„Z& G d„ d«      Z'y)a‡  Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery
using IP multicast.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
é    )Úannotations)ÚIterable)ÚheapifyÚheappopÚheappush)Úcasté   )Ú
DNSAddressÚDNSEntryÚDNSHinfoÚDNSNsecÚ
DNSPointerÚ	DNSRecordÚ
DNSServiceÚDNSText)Úcurrent_time_millis)Ú_MAX_CACHE_RECORDSÚ_ONE_SECONDÚ	_TYPE_PTRéd   c                ó    — | |   }||= |s| |= yy)z[Remove a key from a DNSRecord cache

    This function must be run in from event loop.
    N© )ÚcacheÚkeyÚrecordÚrecord_caches       úA/home/josh/.local/lib/python3.12/site-packages/zeroconf/_cache.pyÚ_remove_keyr   8   s$   € ð
 ˜‘:€LØ�VÐÙØ�#‰Jð ó    c                  óÈ   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z	dd„Z
dd	„Zdd
„Zdd„Zd d„Zd d„Zd!d„Zd"d„Zd#d„Zd$d„Zd d„Zd%d„Zd&d„Z	 	 	 	 	 	 	 	 d'd„Zd(d„Zy))ÚDNSCachezA cache of DNS entries.c                óJ   — i | _         g | _        i | _        i | _        d| _        y )Nr   )r   Ú_expire_heapÚ_expirationsÚservice_cacheÚ_total_records©Úselfs    r   Ú__init__zDNSCache.__init__F   s)   € Ø*,ˆŒ
Ø;=ˆÔØ46ˆÔØ24ˆÔØ#$ˆÕr   c                ó(  — | j                   j                  |j                  «      }|du xs ||v}|rH| j                  t        k\  r5| j                  «        | j                   j                  |j                  «      }|€i x}| j                   |j                  <   |xr t        |t        «       }|r| xj                  dz  c_        |||<   |j                  |j                  dz  z   }| j                  j                  |«      |k7  r7t        | j                  ||f«       || j                  |<   | j                  «        t        |t        «      rI|}| j                  j                  |j                   «      x}€i x}| j                  |j                   <   |||<   |S )z�Adds an entry.

        Returns true if the entry was not already in the cache.

        This function must be run in from event loop.
        Nr	   iè  )r   Úgetr   r&   r   Ú_async_evict_oldestÚ
isinstancer   ÚcreatedÚttlr$   r   r#   Ú_maybe_rebuild_heapr   r%   Ú
server_key)r(   r   ÚstoreÚis_newÚnewÚwhenÚservice_recordÚservice_stores           r   Ú
_async_addzDNSCache._async_addP   sn  € ð —
‘
—‘˜vŸz™zÓ*ˆØ˜$�Ò5 &°Ð"5ˆñ
 �d×)Ñ)Ô-?Ò?Ø×$Ñ$Ô&ð —J‘J—N‘N 6§:¡:Ó.ˆEØˆ=Ø-/Ð/ˆE�D—J‘J˜vŸz™zÑ*ØÒ8œZ¨´Ó8Ð8ˆÙØ×Ò 1Ñ$ÕØˆˆf‰Ø�~‰~ §¡¨dÑ!2Ñ3ˆØ×Ñ× Ñ  Ó(¨DÒ0Ü�T×&Ñ&¨¨v¨Ô7Ø(,ˆD×Ñ˜fÑ%ð ×$Ñ$Ô&ä�fœjÔ)Ø#ˆNØ!%×!3Ñ!3×!7Ñ!7¸×8QÑ8QÓ!RÐR�Ð[ØPRÐR� × 2Ñ 2°>×3LÑ3LÑ MØ,:ˆM˜.Ñ)Øˆ
r   c                ó¸   — | j                   rNt        | j                   «      }|d   }| j                  j                  |«      |d   k7  rŒH| j	                  |«       yy)zADrop the closest-to-expiration record to make room for a new one.r	   r   N)r#   r   r$   r+   Ú_async_remove)r(   Úwhen_recordr   s      r   r,   zDNSCache._async_evict_oldest�   sZ   € à×ÒÜ! $×"3Ñ"3Ó4ˆKØ  ‘^ˆFØ× Ñ ×$Ñ$ VÓ,°¸A±Ò>ØØ×Ñ˜vÔ&Øð  r   c                ó6  — t        | j                  «      }|t        kD  rv|t        | j                  «      dz  kD  rZ| j                  D �cg c])  }| j                  j	                  |d   «      |d   k(  sŒ(|‘Œ+ c}| _        t        | j                  «       yyyc c}w )z?Rebuild ``_expire_heap`` when stale entries dominate live ones.é   r	   r   N)Úlenr#   Ú _MIN_SCHEDULED_RECORD_EXPIRATIONr$   r+   r   )r(   Úexpire_heap_lenÚentrys      r   r0   zDNSCache._maybe_rebuild_heap‹   s–   € ä˜d×/Ñ/Ó0ˆàÔ>Ò>Ø¤# d×&7Ñ&7Ó"8¸1Ñ"<Ò<ð $(×#4Ñ#4ö!Ø¸×8IÑ8I×8MÑ8MÈeÐTUÉhÓ8WÐ[`ÐabÑ[cÓ8c’ò!ˆDÔô �D×%Ñ%Õ&ð =ð ?ùò!s   Á)BÁ2Bc                ó@   — d}|D ]  }| j                  |«      sŒd}Œ |S )z˜Add multiple records.

        Returns true if any of the records were not in the cache.

        This function must be run in from event loop.
        FT)r8   )r(   Úentriesr4   rA   s       r   Úasync_add_recordszDNSCache.async_add_records—   s0   € ð ˆØò 	ˆEØ�‰˜uÕ%Ø‘ð	ð ˆ
r   c                ó  — t        |t        «      r#|}t        | j                  |j                  |«       t        | j
                  |j                  |«       | j                  j                  |d«       | xj                  dz  c_	        y)zQRemoves an entry.

        This function must be run in from event loop.
        Nr	   )
r-   r   r   r%   r1   r   r   r$   Úpopr&   )r(   r   r6   s      r   r:   zDNSCache._async_remove¤   sf   € ô
 �fœjÔ)Ø#ˆNÜ˜×*Ñ*¨N×,EÑ,EÀ~ÔVÜ�D—J‘J §
¡
¨FÔ3Ø×Ñ×Ñ˜f dÔ+Ø×Ò˜qÑ Ör   c                ó4   — |D ]  }| j                  |«       Œ y)zXRemove multiple records.

        This function must be run in from event loop.
        N)r:   )r(   rC   rA   s      r   Úasync_remove_recordszDNSCache.async_remove_records°   s!   € ð
 ò 	&ˆEØ×Ñ˜uÕ%ñ	&r   c                ó`  — | j                   sg S g }| j                   rp| j                   d   }|d   }||kD  rnVt        | j                   «       |d   }| j                  j                  |«      |k(  r|j	                  |«       | j                   rŒp| j                  «        | j                  |«       |S )zœPurge expired entries from the cache.

        This function must be run in from event loop.

        :param now: The current time in milliseconds.
        r   r	   )r#   r   r$   r+   Úappendr0   rH   )r(   ÚnowÚexpiredr;   r5   r   s         r   Úasync_expirezDNSCache.async_expire¸   s­   € ð × Ò ØˆIà#%ˆØ×ÒØ×+Ñ+¨AÑ.ˆKØ˜q‘>ˆDØ�cŠzØÜ�D×%Ñ%Ô&ð ! ‘^ˆFØ× Ñ ×$Ñ$ VÓ,°Ò4Ø—‘˜vÔ&ð ×Óð 	× Ñ Ô"Ø×!Ñ! 'Ô*Øˆr   c                ót   — | j                   j                  |j                  «      }|€y|j                  |«      S )z¶Gets a unique entry by key.  Will return None if there is no
        matching entry.

        This function is not threadsafe and must be called from
        the event loop.
        N)r   r+   r   )r(   rA   r2   s      r   Úasync_get_uniquezDNSCache.async_get_uniqueÓ   s1   € ð —
‘
—‘˜uŸy™yÓ)ˆØˆ=ØØ�y‰y˜ÓÐr   c                óô   — |j                  «       }| j                  j                  |«      }g }|€|S |j                  «       D ]3  }||j                  k(  sŒ||j
                  k(  sŒ#|j                  |«       Œ5 |S )zˆGets all matching entries by details.

        This function is not thread-safe and must be called from
        the event loop.
        )Úlowerr   r+   ÚvaluesÚtypeÚclass_rJ   )r(   ÚnameÚtype_rT   r   ÚrecordsÚmatchesr   s           r   Úasync_all_by_detailszDNSCache.async_all_by_detailsß   sp   € ð �j‰j‹lˆØ—*‘*—.‘. Ó%ˆØ#%ˆØˆ?ØˆNØ—n‘nÓ&ò 	'ˆFØ˜Ÿ™Ó#¨°&·-±-Ó(?Ø—‘˜vÕ&ð	'ð ˆr   c                ó$   — | j                  |«      S )z—Returns a dict of entries whose key matches the name.

        This function is not threadsafe and must be called from
        the event loop.
        )Úentries_with_name©r(   rU   s     r   Úasync_entries_with_namez DNSCache.async_entries_with_nameï   s   € ð ×%Ñ% dÓ+Ð+r   c                ó$   — | j                  |«      S )z™Returns a dict of entries whose key matches the server.

        This function is not threadsafe and must be called from
        the event loop.
        )Úentries_with_serverr\   s     r   Úasync_entries_with_serverz"DNSCache.async_entries_with_server÷   s   € ð ×'Ñ'¨Ó-Ð-r   c                óN  — t        |t        «      r5| j                  j                  |j                  i «      j                  |«      S t        t        | j                  j                  |j                  i «      j                  «       «      «      D ]  }|j                  |«      sŒ|c S  y)zNGets an entry by key.  Will return None if there is no
        matching entry.N)	r-   Ú_UNIQUE_RECORD_TYPESr   r+   r   ÚreversedÚlistrR   Ú__eq__)r(   rA   Úcached_entrys      r   r+   zDNSCache.get  s   € ô �eÔ1Ô2Ø—:‘:—>‘> %§)¡)¨RÓ0×4Ñ4°UÓ;Ð;Ü$¤T¨$¯*©*¯.©.¸¿¹ÀBÓ*G×*NÑ*NÓ*PÓ%QÓRò 	$ˆLØ�|‰|˜LÕ)Ø#Ò#ð	$ð r   c                óô   — |j                  «       }| j                  j                  |«      }|€yt        t	        |j                  «       «      «      D ]%  }||j                  k(  sŒ||j                  k(  sŒ#|c S  y)a´  Gets the first matching entry by details. Returns None if no entries match.

        Calling this function is not recommended as it will only
        return one record even if there are multiple entries.

        For example if there are multiple A or AAAA addresses this
        function will return the last one that was added to the cache
        which may not be the one you expect.

        Use get_all_by_details instead.
        N)rQ   r   r+   rc   rd   rR   rS   rT   )r(   rU   rV   rT   r   rW   rf   s          r   Úget_by_detailszDNSCache.get_by_details  so   € ð �j‰j‹lˆØ—*‘*—.‘. Ó%ˆØˆ?ØÜ$¤T¨'¯.©.Ó*:Ó%;Ó<ò 	$ˆLØ˜×)Ñ)Ó)¨f¸×8KÑ8KÓ.KØ#Ò#ð	$ð r   c                óø   — |j                  «       }| j                  j                  |«      }|€g S t        |j	                  «       «      D �cg c]$  }||j
                  k(  sŒ||j                  k(  sŒ#|‘Œ& c}S c c}w )z%Gets all matching entries by details.)rQ   r   r+   rd   rR   rS   rT   )r(   rU   rV   rT   r   rW   rA   s          r   Úget_all_by_detailszDNSCache.get_all_by_details"  sc   € à�j‰j‹lˆØ—*‘*—.‘. Ó%ˆØˆ?ØˆIÜ#'¨¯©Ó(8Ó#9Öl˜%¸UÀeÇjÁjÓ=PÐU[Ð_d×_kÑ_kÓUk’ÒlÐlùÒls   ÁA7Á A7Á0A7c                óŽ   — | j                   j                  |j                  «       «      x}rt        |j	                  «       «      S g S )z8Returns a list of entries whose server matches the name.)r%   r+   rQ   rd   rR   )r(   ÚserverrC   s      r   r_   zDNSCache.entries_with_server*  s;   € à×(Ñ(×,Ñ,¨V¯\©\«^Ó<Ð<ˆ7Ð<Ü˜Ÿ™Ó(Ó)Ð)Øˆ	r   c                óŽ   — | j                   j                  |j                  «       «      x}rt        |j	                  «       «      S g S )z5Returns a list of entries whose key matches the name.)r   r+   rQ   rd   rR   )r(   rU   rC   s      r   r[   zDNSCache.entries_with_name0  s7   € à—j‘j—n‘n T§Z¡Z£\Ó2Ð2ˆ7Ð2Ü˜Ÿ™Ó(Ó)Ð)Øˆ	r   c                óä   — t        «       }t        | j                  |«      «      D ]I  }|j                  t        k(  sŒ|j                  |«      rŒ)t        t        |«      j                  |k(  sŒG|c S  y )N)	r   rc   r[   rS   r   Ú
is_expiredr   r   Úalias)r(   rU   rp   rK   r   s        r   Ú!current_entry_with_name_and_aliasz*DNSCache.current_entry_with_name_and_alias6  sb   € Ü!Ó#ˆÜ˜t×5Ñ5°dÓ;Ó<ò 	ˆFà—‘œyÓ(Ø×)Ñ)¨#Õ.Üœ VÓ,×2Ñ2°eÓ;à’ð	ð r   c                ó,   — t        | j                  «      S )z1Return a copy of the list of current cache names.)rd   r   r'   s    r   ÚnameszDNSCache.namesA  s   € ä�D—J‘JÓÐr   c                óÂ   — t        |«      }|D ]O  \  }}}| j                  |||«      D ]3  }|j                  }	||	z
  t        kD  sŒ||vsŒ!| j	                  ||d«       Œ5 ŒQ y )Nr	   )ÚsetrY   r.   r   Ú_async_set_created_ttl)
r(   Úunique_typesÚanswersrK   Úanswers_rrsetrU   rV   rT   r   Úcreated_doubles
             r   Ú1async_mark_unique_records_older_than_1s_to_expirez:DNSCache.async_mark_unique_records_older_than_1s_to_expireE  st   € ô ˜G›ˆØ#/ò 	@ÑˆD�%˜Ø×3Ñ3°D¸%ÀÓHò @�Ø!'§¡�Ø˜.Ñ(¬;Ó6¸FÈ-Ò<Wà×/Ñ/°¸¸QÕ?ñ	@ñ	@r   c                óJ   — |j                  ||«       | j                  |«       y)z)Set the created time and ttl of a record.N)Ú_set_created_ttlr8   )r(   r   rK   r/   s       r   rv   zDNSCache._async_set_created_ttlW  s    € ð 	×Ñ  SÔ)Ø�‰˜Õr   N)ÚreturnÚNone)r   Ú
_DNSRecordr~   Úbool)rC   úIterable[DNSRecord]r~   r�   )r   r€   r~   r   )rC   r‚   r~   r   )rK   Ú_floatr~   úlist[DNSRecord])rA   Ú_UniqueRecordsTyper~   úDNSRecord | None)rU   Ú_strrV   Ú_intrT   rˆ   r~   r„   )rU   Ústrr~   r„   )rA   r   r~   r†   )rU   r‰   rV   rˆ   rT   rˆ   r~   r†   )rU   r‰   rV   rˆ   rT   rˆ   r~   r„   )rl   r‰   r~   r„   )rU   r‰   rp   r‰   r~   r†   )r~   z	list[str])rw   zset[tuple[_str, _int, _int]]rx   r‚   rK   rƒ   r~   r   )r   r   rK   rƒ   r/   rˆ   r~   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r8   r,   r0   rD   r:   rH   rM   rO   rY   r]   r`   r+   rh   rj   r_   r[   rq   rs   r{   rv   r   r   r   r!   r!   C   s›   „ Ù!ó%ó/óbó
'óó
!ó&óó6
 óó ,ó.óóó*móóó	ó ð@à2ð@ð %ð@ð ð	@ð
 
ó@ô$ r   r!   N)r   Ú_DNSRecordCacheTyper   r‡   r   r€   r~   r   )(r�   Ú
__future__r   Úcollections.abcr   Úheapqr   r   r   Útypingr   Ú_dnsr
   r   r   r   r   r   r   r   Ú_utils.timer   Úconstr   r   r   rb   r…   Údictr‰   rŽ   r€   r‡   Úfloatrƒ   Úintrˆ   r?   r   r!   r   r   r   ú<module>r™      s§   ðñõ, #å $ß ,Ñ ,Ý ÷	÷ 	ó 	õ -ß =Ñ =à" H¨j¸'À:ÐNÐ Ø (Ñ*¨ZÑ7¸'ÑAÀJÑNÐ Ø˜3  Y°	Ð%9Ñ :Ð:Ñ;Ð Ø€
Ø
€Ø	€Ø
€ð
 $'Ð  ó÷Y ò Y r   