o
    CD©i¡  ã                   @   sh   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZmZ ddl	m
Z
mZmZ G dd„ deƒZdS )é    Né   )ÚNBNSÚNotConnectedError)ÚTYPE_CLIENTÚTYPE_SERVERÚTYPE_WORKSTATIONc                   @   sT   e Zd Ze d¡Zddd„Zdd„ Zdd	„ Zddd„Z	ddd„Z
dd„ Zdd„ ZdS )ÚNetBIOSzNMB.NetBIOSTr   c                 C   sN   || _ t tjtj¡| _| j r| j tjtjd¡ |r%| j d|f¡ dS dS )a|  
        Instantiate a NetBIOS instance, and creates a IPv4 UDP socket to listen/send NBNS packets.

        :param boolean broadcast: A boolean flag to indicate if we should setup the listening UDP port in broadcast mode
        :param integer listen_port: Specifies the UDP port number to bind to for listening. If zero, OS will automatically select a free port number.
        r   Ú N)	Ú	broadcastÚsocketÚAF_INETÚ
SOCK_DGRAMÚsockÚ
setsockoptÚ
SOL_SOCKETÚSO_BROADCASTÚbind)Úselfr
   Úlisten_port© r   úD/var/www/Datamplify/venv/lib/python3.10/site-packages/nmb/NetBIOS.pyÚ__init__
   s   ÿzNetBIOS.__init__c                 C   s   | j  ¡  d| _ dS )z·
        Close the underlying and free resources.

        The NetBIOS instance should not be used to perform any operations after this method returns.

        :return: None
        N)r   Úclose)r   r   r   r   r      s   

zNetBIOS.closec                 C   s$   | j sJ dƒ‚| j  |||f¡ d S )NúSocket is already closed)r   Úsendto)r   ÚdataÚipÚportr   r   r   Úwrite#   s   zNetBIOS.writer	   é‰   é   c                 C   s`   | j sJ dƒ‚t dd¡}|  ||¡}| jr|sd}n|s#| j d¡ |  |||¡ |  ||¡S )ac  
        Send a query on the network and hopes that if machine matching the *name* will reply with its IP address.

        :param string ip: If the NBNSProtocol instance was instianted with broadcast=True, then this parameter can be an empty string. We will leave it to the OS to determine an appropriate broadcast address.
                          If the NBNSProtocol instance was instianted with broadcast=False, then you should provide a target IP to send the query.
        :param integer port: The NetBIOS-NS port (IANA standard defines this port to be 137). You should not touch this parameter unless you know what you are doing.
        :param integer/float timeout: Number of seconds to wait for a reply, after which the method will return None
        :return: A list of IP addresses in dotted notation (aaa.bbb.ccc.ddd). On timeout, returns None.
        r   r   éÿÿ  z<broadcast>zQqueryName: ip parameter is empty. OS might not transmit this query to the network)	r   ÚrandomÚrandintÚprepareNameQueryr
   ÚlogÚwarningr   Ú_pollForNetBIOSPacket)r   Únamer   r   ÚtimeoutÚtrn_idr   r   r   r   Ú	queryName'   s   

zNetBIOS.queryNamec                 C   sd   | j sJ dƒ‚t dd¡}|  |d¡}|  |||¡ |  ||¡}|r0ttdd„ tdd„ |ƒƒƒS dS )	a©  
        Send a query to the machine with *ip* and hopes that the machine will reply back with its name.

        The implementation of this function is contributed by Jason Anderson.

        :param string ip: If the NBNSProtocol instance was instianted with broadcast=True, then this parameter can be an empty string. We will leave it to the OS to determine an appropriate broadcast address.
                          If the NBNSProtocol instance was instianted with broadcast=False, then you should provide a target IP to send the query.
        :param integer port: The NetBIOS-NS port (IANA standard defines this port to be 137). You should not touch this parameter unless you know what you are doing.
        :param integer/float timeout: Number of seconds to wait for a reply, after which the method will return None
        :return: A list of string containing the names of the machine at *ip*. On timeout, returns None.
        r   r   r!   Fc                 S   s   | d S )Nr   r   ©Úsr   r   r   Ú<lambda>Q   s    z(NetBIOS.queryIPForName.<locals>.<lambda>c                 S   s   | d t kS )Nr   )r   r,   r   r   r   r.   Q   s    N)	r   r"   r#   ÚprepareNetNameQueryr   Ú_pollForQueryPacketÚlistÚmapÚfilter)r   r   r   r)   r*   r   Úretr   r   r   ÚqueryIPForName>   s   zNetBIOS.queryIPForNamec              
   C   óì   t   ¡ | }	 zAt   ¡ | }|dkrW d S t | j ¡ gg g |¡\}}}|s)W d S | j d¡\}}t|ƒdkr9t‚|  |¡\}}	||krG|	W S W n, tjyt }
 zt	|
ƒt
u rh|
d tjkrg|
d tjkrg|
‚n|
‚W Y d }
~
nd }
~
ww q©NTr   r!   )ÚtimeÚselectr   ÚfilenoÚrecvfromÚlenr   ÚdecodePacketÚerrorÚtypeÚtupleÚerrnoÚEINTRÚEAGAIN©r   Úwait_trn_idr)   Úend_timeÚ_timeoutÚreadyÚ_r   r*   r4   Úexr   r   r   r'   Y   ó2   ÿ€€ûîzNetBIOS._pollForNetBIOSPacketc              
   C   r6   r7   )r8   r9   r   r:   r;   r<   r   ÚdecodeIPQueryPacketr>   r?   r@   rA   rB   rC   rD   r   r   r   r0   w   rK   zNetBIOS._pollForQueryPacketN)Tr   )r	   r   r    )r   r    )Ú__name__Ú
__module__Ú__qualname__ÚloggingÚ	getLoggerr%   r   r   r   r+   r5   r'   r0   r   r   r   r   r      s    



r   )ÚosrP   r"   r   r8   r9   Úbaser   r   Únmb_constantsr   r   r   r   r   r   r   r   Ú<module>   s   0