o
    b                     @   s   d dgZ ddlmZ ddlmZmZ ddlmZmZm	Z	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 )SecondaryAuthoritySecondaryAuthorityService    )service)defertask)clientcommondnsresolve)FileAuthority)failurelog)nativeStringc                   @   sD   e Zd 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   a:  
    A service that keeps one or more authorities up to date by doing hourly
    zone transfers from a master.

    @ivar primary: IP address of the master.
    @type primary: L{str}

    @ivar domains: An authority for each domain mirrored from the master.
    @type domains: L{list} of L{SecondaryAuthority}
    N5   c                    s"   t  | _ 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   r   domains)selfr   r   r   r   r   __init__    s   

z"SecondaryAuthorityService.__init__c                    s0    \}}| |g }||_  fdd|D |_|S )a  
        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 domains: Domain names for which to perform zone transfers.
        @type domains: sequence of L{bytes}

        @return: A new instance of L{SecondaryAuthorityService}.
        c                    s   g | ]}t  |qS r   )r   fromServerAddressAndDomainr   serverAddressr   r   r   A   s    
zISecondaryAuthorityService.fromServerAddressAndDomains.<locals>.<listcomp>)_portr   )clsr   r   r   portr   r   r   r   fromServerAddressAndDomains-   s   

z5SecondaryAuthorityService.fromServerAddressAndDomainsc                 C   s   t | jS )z_
        Get a resolver for the transferred domains.

        @rtype: L{ResolverChain}
        )r
   ResolverChainr   r   r   r   r   getAuthorityG   s   z&SecondaryAuthorityService.getAuthorityc                 C   sV   t j|  dd | jD | _d}ddlm} | jD ]}|||jd |d7 }qd S )Nc                 S   s   g | ]}t |jqS r   )r   LoopingCalltransferr   r   r   r   r   Q   s    z:SecondaryAuthorityService.startService.<locals>.<listcomp>r   reactori     )	r   ServicestartServicer   callstwisted.internetr(   	callLaterstart)r   ir(   cr   r   r   r+   O   s   

z&SecondaryAuthorityService.startServicec                 C   s$   t j|  | jD ]}|  q	d S N)r   r*   stopServicer,   stop)r   r1   r   r   r   r3   Z   s   

z%SecondaryAuthorityService.stopService)__name__
__module____qualname____doc__r,   r   r   classmethodr!   r$   r+   r3   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: L{str}

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

    @ivar domain: The domain for which this is the secondary authority.
    @type domain: L{bytes}

    @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|  t|| _t|| _dS )z
        @param domain: The domain for which this will be the secondary
            authority.
        @type domain: L{bytes} or L{str}
        N)r   ResolverBaser   r   r   r	   domainStringdomain)r   	primaryIPr<   r   r   r   r   x   s   	
zSecondaryAuthority.__init__c                 C   s   |\}}| ||}||_ |S )af  
        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.
        @type domain: L{bytes}

        @return: A new instance of L{SecondaryAuthority}.
        )r   )r   r   r<   r   r    	secondaryr   r   r   r      s   
z-SecondaryAuthority.fromServerAddressAndDomainc                 C   s^   | j rdS d| _| j}|du rddlm} tj| j| jfg|d}|	| j
| j| jS )z
        Attempt a zone transfer.

        @returns: A L{Deferred} that fires with L{None} when attempted zone
            transfer has completed.
        NTr   r'   )serversr(   )transferringtransfering_reactorr-   r(   r   Resolverr   r   
lookupZoner<   addCallback_cbZone
addErrback_ebZone)r   r(   resolverr   r   r   r&      s   	
zSecondaryAuthority.transferc                 C   s4   | j r| jsttt|S t	| ||||S r2   )
soarecordsr   failr   Failurer	   DomainErrorr   _lookup)r   namer   typetimeoutr   r   r   rO      s   zSecondaryAuthority._lookupc                 C   sf   |\}}}i  | _ }|D ]$}| js"|jtjkr"|jj |jf| _q||jj g 	|j qd S r2   )
rK   rJ   rQ   r	   SOArP   lowerpayload
setdefaultappend)r   zoneans_rrecr   r   r   rF      s   

zSecondaryAuthority._cbZonec                 C   s$   t d| j| jf  t | d S )Nz/Updating %s from %s failed during zone transfer)r   msgr<   r   errr   r   r   r   r   rH      s   
zSecondaryAuthority._ebZonec                 C   s   |   | j| j d S r2   )r&   addCallbacks_cbTransferred_ebTransferredr#   r   r   r   update   s   zSecondaryAuthority.updatec                 C   s
   d| _ d S )NF)r@   )r   resultr   r   r   ra      s   
z!SecondaryAuthority._cbTransferredc                 C   s*   d| _ td| j| jf  t| d S )NFz2Transferring %s from %s failed after zone transfer)transferredr   r]   r<   r   r^   r_   r   r   r   rb      s   
z!SecondaryAuthority._ebTransferredr2   )r5   r6   r7   r8   r@   rJ   rK   r   rB   r   r9   r   r&   rO   rF   rH   rc   ra   rb   r   r   r   r   r   `   s     

	N)__all__twisted.applicationr   r-   r   r   twisted.namesr   r   r	   r
   twisted.names.authorityr   twisted.pythonr   r   twisted.python.compatr   r*   r   r   r   r   r   r   <module>   s   P