Ñò i'dWc@sŸdZddkZddkZddklZddklZlZddk l Z ddk l Z ddk lZlZlZddklZeid d eƒZd Zd eifd „ƒYZdeifd„ƒYZeieƒZdeifd„ƒYZeieƒZdeifd„ƒYZdeifd„ƒYZ deifd„ƒYZ!eie!ƒZ"deifd„ƒYZ#eie#ƒZ$de$fdei%fdei&fdefdefdefd ei'ffe#_(d!Z)d"Z*d#Z+d$Z,ei-Z.eieƒe._/de._1ei2Z3de3_/eieƒfe3_4ei5Z6ei%e6_/ei7ei8efe6_4ei9Z:ei7e:_/ei%fe:_4ei;Z<ei=e<_/ei>eie$ƒei?ei7fe<_4ei@ZAdeA_/e$feA_4eiBZCei=eC_/eei7ei%ei7ei%ei7ei=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,eZdZdeifdeiffZRS(s0 struct if_nameindex from /usr/include/net/if.h tif_indextif_name(t__name__t __module__t__doc__tCtc_uint32tc_char_pt_fields_(((s ../netif.pyR 1s t _sockaddrcBs0eZdZdeifdeidffZRS(s3 struct sockaddr from /usr/include/sys/sock_impl.h t sa_familytsa_datai(R RRRtc_uint16tc_charR(((s ../netif.pyR9s t _sockaddr_incBsHeZdZdeifdeifdeifdeidffZRS(s3 struct sockaddr_in from /usr/include/netinet/in.h t sin_familytsin_porttsin_addrtsin_zeroi(R RRRRRRR(((s ../netif.pyRBs    t_in6_ucBsDeZdZdeidfdeidfdeidffZRS(s0 struct in6_addr from /usr/include/netinet/in.h tu6_addr8it u6_addr16it u6_addr32i(R RRRtc_uint8RRR(((s ../netif.pyRMst _in6_addrcBseZdZdeffZRS(s0 struct in6_addr from /usr/include/netinet/in.h tin6_u(R RRRR(((s ../netif.pyR#Vst _sockaddr_in6cBsYeZdZdeifdeifdeifdefdeifdeiffZRS(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(((s ../netif.pyR%]s     t_ifaddrscBseZdZRS(s, struct ifaddrs from /usr/include/ifaddrs.h (R RR(((s ../netif.pyR,jstifa_nexttifa_namet ifa_flagstifa_addrt ifa_netmasktifu_addrtifa_dataiiiicCs)tidjo |djS|djSdS(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((s ../netif.pytin4_is_addr_loopback¤s cCsÇtidjoZ|iiddjoB|iiddjo+|iiddjo|iiddjS|iiddjoB|iiddjo+|iiddjo|iiddjSdS(s; IN6_IS_ADDR_LOOPBACK(addr) from /usr/include/netinet/in.h R4iiiiiN(R5R6R$R!(R7((s ../netif.pytin6_is_addr_loopback²scCsp|iitjo#ti|tƒ}t|iiƒS|iitjo#ti|t ƒ}t |ii ƒ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((s ../netif.pytis_loopback_addressÀs cCsÄz²tƒ}|djoFtiƒ}|tjotdƒ‚nt|dt|ƒƒ‚ntƒd}}x;||i dj o&||i |||i <|d7}qrW|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((s ../netif.pyt if_nameindexÓs    cCsöt|tƒ pt|tƒotdt|ƒƒ‚n|djptd|ƒ‚ntitƒ}t ||ƒ}|djogti ƒ}|t jotd|ƒ‚n|t jotdƒ‚nt|dt|ƒƒ‚n|iS(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((s ../netif.pytif_indextonameìs!     cCs®t|tƒptdt|ƒƒ‚nt|ƒ}|djogtiƒ}|tjotd|ƒ‚n|t jot 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((s ../netif.pytif_nametoindexs     t InterfaceInfotaddresstnetmasktcidrc Cs³d„}tƒd }}d}d}d}t|ti|ƒ||Bdƒ}|djoFtiƒ}|tjotdƒ‚nt|dt |ƒƒ‚nzt ƒ}t ƒ} ti t ƒ} |}ti tƒ} x³|o«|iit@pt|iiƒo|ii}qÝn|ii} t| | | td dtƒ}|djo|ii}qÝn|ii} | i}|djo|ii}qÝn|ii} t| | | td dtƒ}|djo"t|| i|| iƒƒ}nt|d d ƒ}|iit@o| p|| | 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(cSs|t|djƒS(t1(tint(txty((s ../netif.pyt-stIi(treducetbinRR(tm((s ../netif.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((s ../netif.pyt getallifaddrs st                    ! cCs%d„}tt|tƒiƒƒƒ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@|\}}|io|d|i|iffp ||ifS(s%s/%d(R_R](t.0tkti((s ../netif.pyRdŠs0(RHtmapRt iteritems(t info2cidr((s ../netif.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ˆ(((s ../netif.pyt s€                                c