o inhd@sdZddlmZmZmZddlmZmZ ddl m Z ddl m Z ddlmZddlmZmZmZGdd d eZGd d d eZGd d d eZGdddeZdS)z] Classes and functions for dealing with MAC addresses, EUI-48, EUI-64, OUI, IAB identifiers. )NotRegisteredErrorAddrFormatError DictDotLookup)eui48eui64) mac_eui48) eui64_base) IPAddress) _open_binary_is_int_is_strc@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS)BaseIdentifierz$Base class for all IEEE identifiers.)_value __weakref__cCs d|_dSNrselfr?/usr/local/lib/python3.10/dist-packages/netaddr/eui/__init__.py__init__ zBaseIdentifier.__init__cC|jSz):return: integer value of this identifierrrrrr__int__zBaseIdentifier.__int__cCrrrrrrr__long__rzBaseIdentifier.__long__cCs|jdkrdSd|jS)z8:return: octal string representation of this identifier.r0z0%orrrrr__oct__"s  zBaseIdentifier.__oct__cCs d|jS)z>:return: hexadecimal string representation of this identifier.z0x%xrrrrr__hex__) zBaseIdentifier.__hex__cCr)zx :return: return the integer value of this identifier when passed to hex(), oct() or bin(). rrrrr __index__.szBaseIdentifier.__index__N) __name__ __module__ __qualname____doc__ __slots__rrrrrr!rrrrr s r cszeZdZdZdZfddZddZddZd d Zd d Z d dZ ddZ e ddZ dddZddZddZZS)OUIz An individual IEEE OUI (Organisationally Unique Identifier). For online details see - http://standards.ieee.org/regauth/oui/ )recordscstt|ddlm}g|_t|tr t| ddd|_ n"t |r;d|kr.dkr4nn||_ nt d|ft d|f|j |jvrrttd }|j|j D]\}}||||d }||||qS|d Std |f) z Constructor :param oui: an OUI string ``XX-XX-XX`` or an unsigned integer. Also accepts and parses full MAC/EUI-48 address strings (but not MAC/EUI-48 integers)! rieee-z"OUI int outside expected range: %rzunexpected OUI format: %rzoui.txtUTF-8zOUI %r not registered!N)superr'r netaddr.euir*r( isinstancestrintreplacerr ValueError TypeError OUI_INDEXr __package__seekreaddecode _parse_datacloser)rouir*fhoffsetsizedata __class__rrr@s$      z OUI.__init__cC t|jSr)hashrrrrr__hash__grz OUI.__hash__cC<t|tsz||}Wn tytYSw|j|jkSrr2r'rE ExceptionNotImplementedrrotherrrr__eq__j   z OUI.__eq__cC<t|tsz||}Wn tytYSw|j|jkSrrJrMrrr__ne__rrPz OUI.__ne__cC |j|jfS)z+:returns: Pickled state of an `OUI` object.rr(rrrr __getstate__z zOUI.__getstate__cC|\|_|_dS)z;:param state: data used to unpickle a pickled `OUI` object.NrTrstaterrr __setstate__~zOUI.__setstate__cCsdddg||d}|dD]/}|}|sqd|vr1|j|d<|ddd|d <t||d <qd |vr6q|d |q|j|dS) z.Returns a dict record from raw OUI record datarr,)idxr?orgaddressrArB (hex)r\Nr]r? (base 16)r^)splitstriprr3appendr()rrCrArBrecordlinerrrr=s&  zOUI._parse_datacCrF)z0Number of registered organisations with this OUI)lenr(rrrr reg_countr z OUI.reg_countrcCst|j|S)a The IEEE registration details for this OUI. :param index: the index of record (may contain multiple registrations) (Default: 0 - first registration) :return: Objectified Python data structure containing registration details. )rr()rindexrrr registrations zOUI.registrationcCs(|j}d|d?d@|d?d@|d@fS)z*:return: string representation of this OUI%02X-%02X-%02Xr-rrint_valrrr__str__s   z OUI.__str__cCd|S)@:return: executable Python string to recreate equivalent object.z OUI('%s')rrrrr__repr__z OUI.__repr__)r)r"r#r$r%r&rrHrOrRrUrZr=propertyrirkrqrt __classcell__rrrDrr'7s '   r'csveZdZdZ dZedddZdfdd Zdd Zd d Z d d Z ddZ ddZ ddZ ddZddZZS)IAB)iPiU@)rfFcCsx|d?|jvr |dfSd}d|A}|d?}||B|}|d?|jvr4|r0|dkr0tdt|||fStdt|)z :param eui_int: a MAC IAB as an unsigned integer. :param strict: If True, raises a ValueError if the last 12 bits of IAB MAC/EUI-48 address are non-zero, ignores them otherwise. (Default: False) riz%r is not a strict IAB!z%r is not an IAB address!)IAB_EUI_VALUESr6hex)clseui_intstrict user_maskiab_maskiab_bits user_bitsrrr split_iab_macs   zIAB.split_iab_macc stt|ddlm}dddgddd|_t|tr2t| ddd}|j ||d\}}||_ nt |rC|j ||d\}}||_ nt d|f|j |jvrttd }|j|j d\}} ||jd <| |jd <|||| d } || || |dStd |f)a Constructor :param iab: an IAB string ``00-50-C2-XX-X0-00`` or an unsigned integer. This address looks like an EUI-48 but it should not have any non-zero bits in the last 3 bytes. :param strict: If True, raises a ValueError if the last 12 bits of IAB MAC/EUI-48 address are non-zero, ignores them otherwise. (Default: False) rr)r,)r\iabr]r^rArBr+r-)rzunexpected IAB format: %r!ziab.txtrArBr/zIAB %r not unregistered!N)r0rxrr1r*rfr2r3r4r5rrr r7 IAB_INDEXr r9r:r;r<r=r>r) rrrr*rpiab_intuser_intr@rArBrCrDrrrs6        z IAB.__init__cCrIrr2rxrErKrLrrMrrrrOrPz IAB.__eq__cCrQrrrMrrrrRrPz IAB.__ne__cCrS)z+:returns: Pickled state of an `IAB` object.rrfrrrrrU#rVzIAB.__getstate__cCrW)z;:param state: data used to unpickle a pickled `IAB` object.NrrXrrrrZ'r[zIAB.__setstate__cCsv|dD]3}|}|sqd|vr+|j|jd<|ddd|jd<t||jd<qd|vr0q|jd |qdS) z.Returns a dict record from raw IAB record datar_r`r\Nrar]rrbr^)rcrdrrfr3re)rrCrArBrgrrrr=+s zIAB._parse_datacCrF)z*The IEEE registration details for this IAB)rrfrrrrrk;s zIAB.registrationcCs@|jd>}d|d?d@|d?d@|d?d@|d?d@|d@fS)z*:return: string representation of this IABz%02X-%02X-%02X-%02X-%02X-00 rmr-rnrrorrrrq?s     z IAB.__str__cCrr)rsz IAB('%s')rrrrrrtJruz IAB.__repr__)F)r"r#r$r{r& classmethodrrrOrRrUrZr=rkrqrtrwrrrDrrxs 4 rxcsneZdZdZdZdJfdd ZddZdd Zd d Zd d Z e ee ddZ ddZ ddZ ddZe e eddZe ddZe ddZddZe ddZe ddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdKd2d3Ze d4d5Ze d6d7Z e d8d9Z!d:d;Z"dd?Z$d@dAZ%e dBdCZ&dKdDdEZ'dFdGZ(dHdIZ)Z*S)LEUIz An IEEE EUI (Extended Unique Identifier). Both EUI-48 (used for layer 2 MAC addresses) and EUI-64 are supported. Input parsing for EUI-48 addresses is flexible, supporting many MAC variants. )_module_dialectNcstt|d|_t|tr+|dur||jjkrtd|j|_|j|_|j|_dS|durE|dkr7t |_n1|dkr?t |_n)td|t |rhd|krSdkrYnnt |_nd|krcdkrhnnt |_||_ ||_dS) aJ Constructor. :param addr: an EUI-48 (MAC) or EUI-64 address in string format or an unsigned integer. May also be another EUI object (copy construction). :param version: (optional) the explicit EUI address version, either 48 or 64. Mainly used to distinguish EUI-48 and EUI-64 identifiers specified as integers which may be numerically equivalent. :param dialect: (optional) one of the :ref:`mac_formatting_dialects` to be used to configure the formatting of EUI-48 (MAC) addresses. Nz2cannot switch EUI versions using copy constructor!0@zunsupported EUI version %rrrzl) r0rrrr2versionr6rdialect_eui48_eui64r value)raddrrrrDrrr[s,   z EUI.__init__cCs|j|jj|jfS)z+:returns: Pickled state of an `EUI` object.)rrrrrrrrrUzEUI.__getstate__cCsH|\}}}||_|dkrt|_n|dkrt|_ntd|f||_dS)zN :param state: data used to unpickle a pickled `EUI` object. rrz&unpickling failed for object state: %sN)rrrrr6r)rrYrrrrrrrZs  zEUI.__setstate__cCrrrrrrr _get_valuezEUI._get_valuec Cs(|jdurVttfD]>}z |||_||_Wn0tyGzdt|kr,|jkr:nn t||_||_WYnWn tyDYnwYq w|jdurTtd|fdSt |rvz |j||_WdStyutd||jj fwdt|kr|jjkrnnt||_dStd|f)Nrz failed to detect EUI version: %rzaddress %r is not an EUIv%dzbad address format: %r) rrr str_to_intrrr4max_intr6r r)rrmodulerrr _set_valuesD          zEUI._set_valuezAa positive integer representing the value of this EUI identifier.cCrr)rrrrr _get_dialectrzEUI._get_dialectcCs:|dur |jtur tStSt|drt|dr|Std)N word_sizeword_fmtz*custom dialects should subclass mac_eui48!)rrrrhasattrr7rrrrr_validate_dialects zEUI._validate_dialectcCs|||_dSr)rrrrrr _set_dialectszEUI._set_dialectzXa Python class providing support for the interpretation of various MAC address formats.cCs4|jtkr t|jd?S|jtkrt|jd?SdS)z:The OUI (Organisationally Unique Identifier) for this EUI.r(N)rrr'rrrrrrr?s  zEUI.ouicCs@|jtkrdt|ddS|jtkrdt|ddSdS)z*The EI (Extension Identifier) for this EUIrlz%02X-%02X-%02X-%02X-%02XrnN)rrtuplerrrrreis  zEUI.eicCs|jd?tjvS)z<:return: True if this EUI is an IAB address, False otherwiser)rrxr{rrrris_iabr[z EUI.is_iabcCs|r t|jd?SdS)zr If is_iab() is True, the IAB (Individual Address Block) is returned, ``None`` otherwise. ryN)rrxrrrrrrszEUI.iabcCs|jjS)z/The EUI version represented by this EUI object.)rrrrrrrsz EUI.versioncst|r(|jj}| |kr|dkstdtd|j|j|j|St|trF|j|j|jfddt | t DSt d|f)z :return: The integer value of the word referenced by index (both positive and negative). Raises ``IndexError`` if index is out of bounds. Also supports Python list slices for accessing word groups. z!index out range for address type!csg|]}|qSrr).0iwordsrr sz#EUI.__getitem__..zunsupported type %r!) r r num_words IndexErrorr int_to_wordsrr2slicerangeindicesrhr7)rr\rrrr __getitem__s  zEUI.__getitem__cCst|tr tdt|stdd|kr|jjdks'ntd|ft|s/tdd|kr;|jjksFntd||jj ft |j |j |j}|||<|j ||_ dS) z=Set the value of the word referenced by index in this addressz"settable slices are not supported!zindex not an integer!rrz'index %d outside address type boundary!zvalue not an integer!z.value %d outside word size maximum of %d bits!N)r2rNotImplementedErrorr r7rrrmax_wordrlistrrr words_to_int)rr\rrrrr __setitem__s  zEUI.__setitem__cCst|j|jfS)zA:return: hash of this EUI object suitable for dict keys, sets etc)rGrrrrrrrH.r[z EUI.__hash__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkSzy :return: ``True`` if this EUI object is numerically the same as other, ``False`` otherwise. r2rrErKrLrrrMrrrrO2  z EUI.__eq__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkSrrrMrrrrR>rz EUI.__ne__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically lower in value than other, ``False`` otherwise. rrMrrr__lt__Jrz EUI.__lt__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically lower or equal in value to other, ``False`` otherwise. rrMrrr__le__Vrz EUI.__le__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically greater in value than other, ``False`` otherwise. rrMrrr__gt__brz EUI.__gt__cCsHt|tsz||}Wn tytYSw|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically greater or equal in value to other, ``False`` otherwise. rrMrrr__ge__nrz EUI.__ge__cCs|j|j|S)z :param word_sep: (optional) the separator to insert between words. Default: None - use default separator for address type. :return: human-readable binary digit string of this address. )r int_to_bitsr)rword_seprrrbitszszEUI.bitscC|j|jS)z8The value of this EUI address as a packed binary string.)r int_to_packedrrrrrpackedz EUI.packedcCr)zdB|B}n|j}|j|ddS)a2 - If this object represents an EUI-48 it is converted to EUI-64 as per the standard. - If this object is already an EUI-64, a new, numerically equivalent object is returned instead. :return: The value of this EUI object as a new 64-bit EUI object. rrr.rl|rr)rrrE)r first_three last_three new_valuerrrrs  z EUI.eui64cCs|}|jdN_|S)z - create a new EUI object with a modified EUI-64 as described in RFC 4291 section 2.5.1 :return: a new and modified 64-bit EUI object. l)rr)rrrrrmodified_eui64s zEUI.modified_eui64cCs t|t|}t|ddS)a5 .. note:: This poses security risks in certain scenarios. Please read RFC 4941 for details. Reference: RFCs 4291 and 4941. :param prefix: ipv6 prefix :return: new IPv6 `IPAddress` object based on this `EUI` using the technique described in RFC 4291. rr)r4rr )rprefixrprrripv6s zEUI.ipv6cCs |dS)a .. note:: This poses security risks in certain scenarios. Please read RFC 4941 for details. Reference: RFCs 4291 and 4941. :return: new link local IPv6 `IPAddress` object based on this `EUI` using the technique described in RFC 4291. l @)rrrrripv6_link_locals zEUI.ipv6_link_localcCs,d|ji}|r|j|d<t|S)z A record dict containing IEEE registration details for this EUI (MAC-48) if available, None otherwise. r'rx)r?rkrrr)rrCrrrinfoszEUI.infocCs||}|j|j|S)a: Format the EUI into the representational format according to the given dialect :param dialect: one of the :ref:`mac_formatting_dialects` defining the formatting of EUI-48 (MAC) addresses. :return: EUI in representational format according to the given dialect )rr int_to_strr)rrvalidated_dialectrrrformats z EUI.formatcCs|j|j|jS)z':return: EUI in representational format)rrrrrrrrrqrz EUI.__str__cCrr)rsz EUI('%s')rrrrrrtruz EUI.__repr__)NNr)+r"r#r$r%r&rrUrZrrrvrrrrrr?rrrrrrrHrOrRrrrrrrrrrrrrrrrqrtrwrrrDrrOsf 2"                 rN)r% netaddr.corerrrnetaddr.strategyrrrrnetaddr.strategy.eui48rnetaddr.strategy.eui64r netaddr.ipr netaddr.compatr r r objectr r'rxrrrrrs   $