U
    
W[                     @   s   d dl mZmZ ddgZd dlmZmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d d	lmZ d d
lmZmZ d dlmZ G dd dejZG dd deZdS )    )absolute_importdivisionSecondaryAuthoritySecondaryAuthorityService)taskdefer)dns)common)client)resolve)FileAuthority)logfailure)servicec                   @   s@   e Zd ZdZdZdd Zedd Zdd Zd	d
 Z	dd Z
dS )r   N5   c                    s    | _  fdd|D | _dS )a  
        @param primary: The IP address of the server from which to perform
        zone transfers.
        @type primary: L{str}

        @param domains: A sequence of domain names for which to perform
        zone transfers.
        @type domains: L{list} of L{bytes}
        c                    s   g | ]}t  |qS  )r   .0dprimaryr   9/usr/lib/python3/dist-packages/twisted/names/secondary.py
<listcomp>#   s     z6SecondaryAuthorityService.__init__.<locals>.<listcomp>N)r   domains)selfr   r   r   r   r   __init__   s    
z"SecondaryAuthorityService.__init__c                    s6   | dg } d |_  d |_ fdd|D |_|S )aS  
        Construct a new L{SecondaryAuthorityService} from a tuple giving a
        server address and a C{str} giving the name of a domain for which this
        is an authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domain: A C{bytes} giving the domain to transfer.

        @return: A new instance of L{SecondaryAuthorityService}.
        Nr      c                    s   g | ]}t  |qS r   )r   fromServerAddressAndDomainr   serverAddressr   r   r   9   s   zISecondaryAuthorityService.fromServerAddressAndDomains.<locals>.<listcomp>)r   _portr   )clsr   r   r   r   r   r   fromServerAddressAndDomains&   s    



z5SecondaryAuthorityService.fromServerAddressAndDomainsc                 C   s   t | jS N)r   ZResolverChainr   r   r   r   r   getAuthority?   s    z&SecondaryAuthorityService.getAuthorityc                 C   sV   t j|  dd | jD | _d}ddlm} | jD ]}|||jd |d7 }q4d S )Nc                 S   s   g | ]}t |jqS r   )r   ZLoopingCalltransferr   r   r   r   r   D   s     z:SecondaryAuthorityService.startService.<locals>.<listcomp>r   reactori  r   )	r   ServicestartServicer   callstwisted.internetr(   Z	callLaterstart)r   ir(   cr   r   r   r*   B   s    
z&SecondaryAuthorityService.startServicec                 C   s$   t j|  | jD ]}|  qd S r#   )r   r)   stopServicer+   stop)r   r/   r   r   r   r0   L   s    
z%SecondaryAuthorityService.stopService)__name__
__module____qualname__r+   r    r   classmethodr"   r%   r*   r0   r   r   r   r   r      s   

c                   @   sr   e Zd ZdZdZd ZZdZdZdd Z	e
dd Zd	d
 ZdddZdd Zdd Zdd Zdd Zdd ZdS )r   a  
    An Authority that keeps itself updated by performing zone transfers.

    @ivar primary: The IP address of the server from which zone transfers will
        be attempted.
    @type primary: C{str}

    @ivar _port: The port number of the server from which zone transfers will be
        attempted.
    @type: C{int}

    @ivar _reactor: The reactor to use to perform the zone transfers, or L{None}
        to use the global reactor.
    FNr   c                 C   s   t j|  || _|| _dS )z
        @param domain: The domain for which this will be the secondary
            authority.
        @type domain: L{bytes}
        N)r	   ZResolverBaser   r   domain)r   Z	primaryIPr6   r   r   r   r   h   s    	zSecondaryAuthority.__init__c                 C   s(   | dd}|d |_ |d |_||_|S )aG  
        Construct a new L{SecondaryAuthority} from a tuple giving a server
        address and a C{bytes} giving the name of a domain for which this is an
        authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domain: A C{bytes} giving the domain to transfer.

        @return: A new instance of L{SecondaryAuthority}.
        Nr   r   )r   r    r6   )r!   r   r6   Z	secondaryr   r   r   r   v   s
    


z-SecondaryAuthority.fromServerAddressAndDomainc                 C   s^   | j r
d S d| _| j}|d kr*ddlm} tj| j| jfg|d}|	| j
| j| jS )NTr   r'   )Zserversr(   )transferringZtransfering_reactorr,   r(   r
   ZResolverr   r    Z
lookupZoner6   ZaddCallback_cbZoneZ
addErrback_ebZone)r   r(   Zresolverr   r   r   r&      s     zSecondaryAuthority.transferc                 C   s4   | j r| js"ttt|S t	| ||||S r#   )
soarecordsr   Zfailr   ZFailurer   ZDomainErrorr   _lookup)r   namer!   typeZtimeoutr   r   r   r=      s    zSecondaryAuthority._lookupc                 C   sj   |\}}}i  | _ }|D ]L}| jsF|jtjkrFt|j |jf| _q|	t|j g 
|j qd S r#   )r<   r;   r?   r   ZSOAstrr>   lowerZpayload
setdefaultappend)r   ZzoneZans_rZrecr   r   r   r9      s    

zSecondaryAuthority._cbZonec                 C   s$   t d| j| jf  t | d S )Nz/Updating %s from %s failed during zone transfer)r   msgr6   r   errr   r   r   r   r   r:      s    zSecondaryAuthority._ebZonec                 C   s   |   | j| j d S r#   )r&   ZaddCallbacks_cbTransferred_ebTransferredr$   r   r   r   update   s    zSecondaryAuthority.updatec                 C   s
   d| _ d S )NF)r7   )r   resultr   r   r   rI      s    z!SecondaryAuthority._cbTransferredc                 C   s*   d| _ td| j| jf  t| d S )NFz2Transferring %s from %s failed after zone transfer)Ztransferredr   rF   r6   r   rG   rH   r   r   r   rJ      s    z!SecondaryAuthority._ebTransferred)N)r2   r3   r4   __doc__r7   r;   r<   r    r8   r   r5   r   r&   r=   r9   r:   rK   rI   rJ   r   r   r   r   r   S   s   


N)Z
__future__r   r   __all__r,   r   r   Ztwisted.namesr   r	   r
   r   Ztwisted.names.authorityr   Ztwisted.pythonr   r   Ztwisted.applicationr   r)   r   r   r   r   r   r   <module>   s   @