~e&dZddlZddlZddlZddlZddlZddlZddlZddlm Z ddlm Z ddgZ dZ dZ d Zd Zd Zd Zd ZdZdZdZdZdZdZddZ ddZddZdZdZy)zG Network introspection utilities using ioctl and the /proc filesystem. N)_PY3)longget_active_device_infoget_network_trafficiiFc2tjddk(S)z>Returns C{True} if the platform is 64-bit, otherwise C{False}.l)structcalcsize7/usr/lib/python3/dist-packages/landscape/lib/network.pyis_64rs ??3 1 $$rc |dzS)zReturns C{True} if the interface is up, otherwise C{False}. @param flags: the integer value of an interface's flags. @see /usr/include/linux/if.h for the meaning of the flags. rr )flagss ris_uprs 19rc|jtjigdjd}|jtjigdjd}t |xs|S)zChecks if interface address data has an IP address @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} raddr)get netifacesAF_INETAF_INET6bool) ifaddresses inet_addr inet6_addrs r is_activer&sd  1 1B48;??GI!3!3bT:1=AA&IJ  'Z ((rc6i}tj|vr$|tj|tj<tj|vrI|tjDcgc]}|djds|}}|r||tj<|Scc}w)zReturn all IP addresses of an interfaces. Returns the same structure as L{ifaddresses}, but filtered to keep IP addresses only. @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} rzfe80:)rrr startswith)rresultsr global_addrss rget_ip_addressesr#0sGK'%01B1B%C !!"[($I$6$67 <**73   *6GI&& ' N s BcN|tjdjddS)zReturn the broadcast address associated to an interface. @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} r broadcastz0.0.0.0rrrrs rget_broadcast_addressr(Hs' y(( )! , 0 0i HHrcN|tjdjddS)zReturn the network mask associated to an interface. @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} rnetmaskr&r's r get_netmaskr,Ps' y(( )! , 0 0B ??rc4|tjddS)zReturn the first IPv4 address associated to the interface. @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} rr)rrr's rget_ip_addressr.Xs y(( )! ,V 44rcttj|vr&|tjdjddSy)a Return the hardware MAC address for an interface in human friendly form, ie. six colon separated groups of two hexadecimal digits, if available; otherwise an empty string. @param ifaddresses: a dict as returned by L{netifaces.ifaddresses} rrr+)rAF_LINKrr's rget_mac_addressr1`s8K'9,,-a044VR@@ rc tj|jtt j d|dd}t j d|dddS)zReturn the integer value of the interface flags for the given interface. @param sock: a socket instance. @param interface: The name of the interface. @see /usr/include/linux/if.h for the meaning of the flags. 256sNHr)fcntlioctlfileno SIOCGIFFLAGSr packunpack)sock interfacedatas r get_flagsrAmsP ;;  FIcrN+ D ==d2bk *1 --rc~tjd}|jDcgc]}|d }}|Scc}w)z: Returns a list of interfaces with default routes defaultr)rgatewaysvalues) default_tablegateway interfacess rget_default_interfacesrI|sC&&(3M,9,@,@,BC'!*CJC Ds :c g} tjtjtjtj}t j D] t fd|Drt j }t|s: j}t||}t|sbt|}|stj|vrd i}||d<t||\|d<|d<|r||d<tj|vr8t||d<t||d<t!||d <t#||d <|j%| |j'|S#j'wxYw) z Returns a dictionary containing info on each active network interface that passes all `filters`. A filter is a callable that returns True if the interface should be skipped. c3.K|] }|ywNr ).0fr?s r z'get_filtered_if_info..s1A1Y<1sr?rspeedduplex ip_addresses ip_address mac_addressbroadcast_addressr*)socketr SOCK_DGRAM IPPROTO_IPrrHanyrrencoderArr#get_network_interface_speedr.r1r(r,appendclose) filtersextendedr!r>r ifencodedrrRifinfor?s @rget_filtered_if_inforbszG/}} NN        #--/& #I111#// :K[)!((*IdI.E<+K8L 1 1 E!9-F#F7O0K1 -F7OVH- )5~&  L0'5k'B|$(7 (D}%.C/*+%0 $<y! NN6 "M& #P N s EE22Fch fd}d}d}t fd}d} t| ||||f|S)Nc |vSrLr )r?skipped_interfacess r filter_localz,get_active_device_info..filter_locals...rc d|vS)N.r r?s r filter_vlanz+get_active_device_info..filter_vlan irc d|vS)N:r ris r filter_aliasz,get_active_device_info..filter_aliasrkrcxr|vSrLr )r? default_ifs default_onlys rfilter_defaultz.get_active_device_info..filter_defaults<  <.filter_taps##E**r)r^r_)rIrb) re skip_vlan skip_aliasr_rqrfrjrnrrrurps ` ` @rrrsN/  )*K= +         rc t|d5}|j}ddddjd\}}}|jDcgc]}d| }}|j|jDcgc]}d| c}i}|ddD]_} d| vr| jd\} } | j } t t |tt| j|| <a|S#1swYxYwcc}wcc}w) ze Retrieves an array of information regarding the network activity per network interface. rNr|recv_send_rm) open readlinessplitextendstripdictzipmapr) source_filenetdevlines_receive_columnstransmit_columnscolumncolumnsdeviceslinedevicer@s rrrs k3 #6  "#,18>>#+>(A(.=.C.C.EFFvhFGF NN3C3I3I3KLeF8$LMGab F d? zz# s7Cdjjl,CDE F N!## GLsC9 D; D 9Dc:tj}d|vrtjtjdtjtj tj tjdd}d|vrtj}|S)a Return the current fqdn of the machine, trying hard to return a meaningful name. In particular, it means working against a NetworkManager bug which seems to make C{getfqdn} return localhost6.localdomain6 for machine without a domain since Maverick. localhostNr)rVgetfqdn getaddrinfo gethostnamerrWrX AI_CANONNAME)fqdns rget_fqdnrs >> Dd!!     NN              $ %%'D Krctjdtd}tjd|}tjd||j d}d} t j |t|tr|j}n|j}tjd|\}}|d k(rd}|d k(rd }t)|}||fS#ttf$r}}|jtjk(rt!j"dn<|jtj$k7r|jtj&k7r|d}d }Yd }~d }~wwxYw) a Return the ethernet device's advertised link speed. The return value can be one of: * 10, 100, 1000, 2500, 10000: The interface speed in Mbps * -1: The interface does not support querying for max speed, such as virtio devices for instance. * 0: The cable is not connected to the interface. We cannot measure interface speed, but could if it was plugged in. I39ss'B16sPr12xHB28xzECould not determine network interface speed, operation not permitted.FNi)r r< ETHTOOL_GSETarray buffer_infor8r9 SIOCETHTOOLrtobytestostringr=IOErrorOSErrorerrnoEPERMloggingwarning EOPNOTSUPPEINVALr) r>interface_name cmd_struct status_cmdpackedrPresrQes rr[r[s,V\<@JS*-J [[1G1G1I!1L MF E D+v. $$&C%%'C j#6 v ~} &\F &=- W   77ekk ! OO+ WW(( (QWW -DG sACE!$A3EE!)r F))loTTFF)z /proc/net/dev)__doc__rrr8rrVr rlandscape.lib.compatrr__all__r;rrrrrr#r(r,r.r1rArIrbrrrr[r rrrs  %% #%: ;   % )0I@5  .;~  %P04-r