ó i'dWc@sŸdZddlZddlZddlmZddlmZmZddl m Z ddl m Z ddl mZmZmZddlmZejd d eƒZd Zd ejfd „ƒYZdejfd„ƒYZejeƒZdejfd„ƒYZejeƒZdejfd„ƒYZdejfd„ƒYZ dejfd„ƒYZ!eje!ƒZ"dejfd„ƒYZ#eje#ƒZ$de$fdej%fdej&fdefdefdefd ej'ffe#_(d!Z)d"Z*d#Z+d$Z,ej-Z.ejeƒe._/de._1ej2Z3de3_/ejeƒfe3_4ej5Z6ej%e6_/ej7ej8efe6_4ej9Z:ej7e:_/ej%fe:_4ej;Z<ej=e<_/ej>eje$ƒej?ej7fe<_4ej@ZAdeA_/e$feA_4ejBZCej=eC_/eej7ej%ej7ej%ej7ej=feC_4d%„ZDd&„ZEd'„ZFd(„Z-d)„Z5d*„Z9ed+d,d-d.gƒZGd/„Z;d0„ZHdS(1sö netif.py - private interface to /usr/lib/libsocket.so for the following four functions if_nameindex, if_indextoname, if_nametoindex, and getallifaddrs. Once netifaces or similar is in Solaris this module should not be required. iÿÿÿÿN(t namedtuple(tENOMEMtENXIO(tIntegral(tstrerror(tAF_INETtAF_INET6t inet_aton(tunpacks/usr/lib/libsocket.sot use_errnoi t_nameidxcBs,eZdZdejfdejffZRS(s0 struct if_nameindex from /usr/include/net/if.h tif_indextif_name(t__name__t __module__t__doc__tCtc_uint32tc_char_pt_fields_(((snetif.pyR 1s t _sockaddrcBs0eZdZdejfdejdffZRS(s3 struct sockaddr from /usr/include/sys/sock_impl.h t sa_familytsa_datai(R RRRtc_uint16tc_charR(((snetif.pyR9s t _sockaddr_incBsHeZdZdejfdejfdejfdejdffZRS(s3 struct sockaddr_in from /usr/include/netinet/in.h t sin_familytsin_porttsin_addrtsin_zeroi(R RRRRRRR(((snetif.pyRBs    t_in6_ucBsDeZdZdejdfdejdfdejdffZRS(s0 struct in6_addr from /usr/include/netinet/in.h tu6_addr8it u6_addr16it u6_addr32i(R RRRtc_uint8RRR(((snetif.pyRMst _in6_addrcBseZdZdeffZRS(s0 struct in6_addr from /usr/include/netinet/in.h tin6_u(R RRRR(((snetif.pyR#Vst _sockaddr_in6cBsYeZdZdejfdejfdejfdefdejfdejffZRS(s4 struct sockaddr_in6 from /usr/include/netinet/in.h t sin6_familyt sin6_portt sin6_flowinfot sin6_addrt sin6_scope_idt __sin6_src_id(R RRRRRR#R(((snetif.pyR%]s     t_ifaddrscBseZdZRS(s, struct ifaddrs from /usr/include/ifaddrs.h (R RR(((snetif.pyR,jstifa_nexttifa_namet ifa_flagstifa_addrt ifa_netmasktifu_addrtifa_dataiiiicCs'tjdkr|dkS|dkSdS(sã in4_is_addr_loopback(address) -> boolean Determine whether an address is a loopback address for IPv4 Input: addr - a IPv4 address Returns True if interface has a loopback address, False otherwise tbigiiN(tsyst byteorder(taddr((snetif.pytin4_is_addr_loopback¤s cCs¿tjdkre|jjddkod|jjddkod|jjddkod|jjddkS|jjddkoº|jjddkoº|jjddkoº|jjddkSdS(s; IN6_IS_ADDR_LOOPBACK(addr) from /usr/include/netinet/in.h R4iiiiiN(R5R6R$R!(R7((snetif.pytin6_is_addr_loopback²scCsl|jjtkr4tj|tƒ}t|jjƒS|jjtkrhtj|t ƒ}t |jj ƒSt S(sã if_nameindex(address) -> boolean Determine whether an address is a loopback address for IPv4 or IPv6 Input: sa - a socket address Returns True if interface has a loopback address, False otherwise ( tcontentsRRRtcastt _sockaddr_inpR8RRt_sockaddr_in6pR9R)tFalse(tsats4ts6((snetif.pytis_loopback_addressÀs cCs¾z¬tƒ}|dkr[tjƒ}|tkr?tdƒ‚nt|dt|ƒƒ‚ntƒd}}x9||j dk r¦||j |||j <|d7}qnW|SWdt |ƒXdS(sp if_nameindex(None) -> dict Returns a dictionary with int keys mapping to str names of interfaces. s#if_nameindex(): insufficient memorysif_nameindex(): %siiN( t _if_nameindextNoneRt get_errnoRt MemoryErrortOSErrorRtdictR R t_if_freenameindex(tidxlistterrtresulttidx((snetif.pyt if_nameindexÓs    cCsët|tƒ st|tƒr8tdt|ƒƒ‚n|dksWtd|ƒ‚ntjtƒ}t ||ƒ}|dkrätj ƒ}|t kr­td|ƒ‚n|t krÈtdƒ‚nt|dt|ƒƒ‚n|jS(sm if_indextoname(int) -> str Given an index, return the interface name. idx must be > 0. s idx: '%s' object not integer > 0isidx = %d, not > 0s!if_indextoname(idx=%d), not valids%if_indextoname(): insufficient memorysif_indextoname(): %sN(t isinstanceRtboolt TypeErrorttypet ValueErrorRRt _IF_NAMESIZEt_if_indextonameRDRERRRFRGRtvalue(RMtnameRLRK((snetif.pytif_indextonameìs     cCs¦t|tƒs(tdt|ƒƒ‚nt|ƒ}|dkr¢tjƒ}|tkrktd|ƒ‚n|t kr†t dƒ‚nt |dt |ƒƒ‚n|S(sX if_nametoindex(str) ->int Given a name of an interface, return its index. sname: '%s' object not stris"if_nametoindex(name=%s), not valids%if_nametoindex(): insufficient memorysif_nametoindex(): %s( ROtstrRQRRt_if_nametoindexRRERRSRRFRGR(RWRMRK((snetif.pytif_nametoindexs     t InterfaceInfotaddresstnetmasktcidrcCsœd„}tƒd }}d}d}d}t|tj|ƒ||Bdƒ}|dkrœtjƒ}|tkr€tdƒ‚nt|dt |ƒƒ‚nzît ƒ}t ƒ} tj t ƒ} |}tj tƒ} x¢|rz|jjt@st|jjƒr|jj}qÙn|jj} t| | | td dtƒ}|dkr[|jj}qÙn|jj} | j}|dkrŽ|jj}qÙn|jj} t| | | td dtƒ}|dkråt|| j|| jƒƒ}nt|d d ƒ}|jjt@r| sk|| | dict Retrieve the available physical interfaces on a system and stores those in a dictionary with the name of the interface as the key and an InterfaceInfo as the value. If all the interfaces on the system are deprecated, then it returns 1 deprecated interface as last resort cSs,td„ttdt|ƒƒdƒdƒS(NcSs|t|dkƒS(Nt1(tint(txty((snetif.pyt-stIi(treducetbinRR(tm((snetif.pyRd-s iiiiÿÿÿÿs$getallifaddrs(): insufficient memorysgetallifaddrs(): %ss0.0.0.0t:N(t _ifaddrspRDt_getallifaddrsRtbyrefRERRFRGRRHtsizeofRRt _NI_MAXHOSTR:R/t_IFF_POINTOPOINTRBR0R-t _getnameinfot_NI_NUMERICHOSTR.RVR1R\t_IFF_DEPRECATEDtsplitRYt _freeifaddrs(t netmask2cidrtifaptwalkert _AF_UNSPECt _LIFC_NOXMITt_LIFC_TEMPORARYRLRKtacct deprecated_iftsockszRWtsocktkeyR7RVtcount((snetif.pyt getallifaddrs sp                    ! cCs%d„}tt|tƒjƒƒƒS(sê getallifaddrs(None) -> dict Retrieve the available physical interfaces on a system and stores those in a dictionary with the name of the interface as the key and the address/cidr or address as the value. cSs>|\}}|jr1|d|j|jffp=||jfS(Ns%s/%d(R_R](t.0tkti((snetif.pyRdŠs.(RHtmapRt iteritems(t info2cidr((snetif.pytgetallifaddrs_cidrƒs (IRR5tctypesRt collectionsRterrnoRRtnumbersRtosRtsocketRRRtstructRtCDLLtTruet _LIBSOCKETRTt StructureR RtPOINTERt _sockaddrpRR<tUnionRR#R%R=R,RjRtc_uint64tc_void_pRRoRrRqRnRNRCtrestypeRDt argstypestif_freenameindexRItargtypesRXRURRR[RZRRktc_inttc_ushorttc_int64t freeifaddrsRtt getnameinfoRpR8R9RBR\Rˆ(((snetif.pyt s€                                c