U
    
W[Q  ã                   @   s¼  d Z ddlmZmZ ddlZddlZddlmZmZmZ ddl	m
Z
mZmZmZ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 er˜dd	„ Ze
ed
ƒƒZej d¡sºejdkrÌdefdefgZn
defgZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ deƒZ#G dd„ deƒZ$G dd„ deƒZ%ee%ƒZ&de&fdefdefdee"ƒfdee"ƒfd ee"ƒfd!efge%_'ej(Z(ee&ƒge(_)ee(_*ej+Z+e&ge+_)d"d#„ Z,d$d%„ Z-d&d'„ Z.dS )(z>
POSIX implementation of local network interface enumeration.
é    )ÚdivisionÚabsolute_importN)ÚAF_INETÚAF_INET6Ú	inet_ntop)ÚCDLLÚPOINTERÚ	StructureÚc_char_pÚc_ushortÚc_intÚc_uint32Úc_uint8Úc_void_pÚc_ubyteÚpointerÚcast)Úfind_library)Ú_PY3ÚnativeStringc                 C   s
   t | gƒS )zs
        Python 3 implementation of Python 2 chr(), i.e. convert an integer to
        corresponding byte.
        )Úbytes)Úi© r   úD/usr/lib/python3/dist-packages/twisted/internet/test/_posixifaces.pyÚchr   s    r   ÚcÚfreebsdÚdarwinZsin_lenÚ
sin_familyc                   @   s   e Zd Zd ed fgZdS )Úin_addré   N©Ú__name__Ú
__module__Ú__qualname__r   Ú_fields_r   r   r   r   r   -   s   
ÿr   c                   @   s   e Zd Zded fgZdS )Úin6_addrr   é   Nr!   r   r   r   r   r&   4   s   
ÿr&   c                   @   s   e Zd Zedefg ZdS )ÚsockaddrÚsin_portN)r"   r#   r$   Ú_sockaddrCommonr   r%   r   r   r   r   r(   ;   s   ÿr(   c                   @   s    e Zd Zedefdefg ZdS )Úsockaddr_inr)   Úsin_addrN)r"   r#   r$   r*   r   r   r%   r   r   r   r   r+   B   s   þr+   c                   @   s&   e Zd Zedefdefdefg ZdS )Úsockaddr_in6r)   Zsin_flowinfor,   N)r"   r#   r$   r*   r   r   r&   r%   r   r   r   r   r-   J   s
   ýr-   c                   @   s   e Zd ZdS )ÚifaddrsN)r"   r#   r$   r   r   r   r   r.   S   s   r.   Úifa_nextÚifa_nameZ	ifa_flagsÚifa_addrZifa_netmaskZifa_dstaddrZifa_datac                 C   s<   t j d¡r8|dd… dkr8|dd… d |dd…  S |S )až  
    On FreeBSD, kill the embedded interface indices in link-local scoped
    addresses.

    @param family: The address family of the packed address - one of the
        I{socket.AF_*} constants.

    @param packed: The packed representation of the address (ie, the bytes of a
        I{in_addr} field).
    @type packed: L{bytes}

    @return: The packed address with any FreeBSD-specific extra bits cleared.
    @rtype: L{bytes}

    @see: U{https://twistedmatrix.com/trac/ticket/6843}
    @see: U{http://www.freebsd.org/doc/en/books/developers-handbook/ipv6.html#ipv6-scope-index}

    @note: Indications are that the need for this will be gone in FreeBSD >=10.
    r   Né   s   þ€s     r    )ÚsysÚplatformÚ
startswith)ÚfamilyÚpackedr   r   r   Ú_maybeCleanupScopeIndexi   s    r8   c                  C   sè   t ƒ } tt| ƒƒdk rtƒ ‚g }z¸| rÖ| d jrÊ| d jd j}|tkr^t| d jt	t
ƒƒ}n"|tkr|t| d jt	tƒƒ}nd}|rÊd tt|d jjdd… ƒ¡}t||ƒ}| | d j|t||ƒf¡ | d j} q"W 5 t| ƒ X |S )z—
    Call C{getifaddrs(3)} and return a list of tuples of interface name, address
    family, and human-readable address representing its results.
    r   Nó    )Ú	ifaddrs_pÚ
getifaddrsr   ÚOSErrorÚfreeifaddrsr1   r   r   r   r   r+   r   r-   ÚjoinÚmapr   r,   r   r8   Úappendr0   r   r/   )r.   Zresultsr6   Zaddrr7   r   r   r   Ú_interfacesƒ   s0    
 
ý
rA   c                  C   sP   g } t ƒ D ]@\}}}t|ƒ}t|ƒ}|tjkr
| d¡r
|  d||f ¡ q
| S )z£
    Return a list of strings in colon-hex format representing all the link local
    IPv6 addresses available on the system, as reported by I{getifaddrs(3)}.
    zfe80:z%s%%%s)rA   r   Úsocketr   r5   r@   )ZretListZ	interfacer6   Zaddressr   r   r   ÚposixGetLinkLocalIPv6Addresses¦   s    rC   )/Ú__doc__Z
__future__r   r   r3   rB   r   r   r   Zctypesr   r   r	   r
   r   r   r   r   r   r   r   r   Zctypes.utilr   Ztwisted.python.compatr   r   r   Zlibcr4   r5   r*   r   r&   r(   r+   r-   r.   r:   r%   r;   ZargtypesZrestyper=   r8   rA   rC   r   r   r   r   Ú<module>   sL   8þÿ	


ù	#