U .Ѫ_Xd@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)_importlib_resources_is_int_is_strc@sDeZdZdZdZddZddZddZd d Zd d Z d dZ dS)BaseIdentifierz$Base class for all IEEE identifiers.)_value __weakref__cCs d|_dSNrselfr;/Users/cjl/code/aws-vpc-ipam/lambda/netaddr/eui/__init__.py__init__szBaseIdentifier.__init__cCs|jSz):return: integer value of this identifierrrrrr__int__szBaseIdentifier.__int__cCs|jSrrrrrr__long__szBaseIdentifier.__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__)szBaseIdentifier.__hex__cCs|jS)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__rrrrrrrrrrr sr csreZdZdZdZfddZddZddZd d Zd d Z d dZ e ddZ dddZ ddZddZZS)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|_ nFt |rxd|kr\dkrhnn||_ qt d|fnt d|f|j |jkrttd }|j|j D]0\}}||||d }||||q|ntd |fd S) 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 TypeErrorZ OUI_INDEXr open_binary __package__seekreaddecode _parse_datacloser)rouir&fhoffsetsizedata __class__rrr@s$      z OUI.__init__cCs>t|ts2z||}Wntk r0tYSX|j|jkSrr.r#rA ExceptionNotImplementedrrotherrrr__eq__gs   z OUI.__eq__cCs>t|ts2z||}Wntk r0tYSX|j|jkSrrBrErrr__ne__os   z OUI.__ne__cCs |j|jfS)z+:returns: Pickled state of an `OUI` object.rr$rrrr __getstate__wszOUI.__getstate__cCs|\|_|_dS)z;:param state: data used to unpickle a pickled `OUI` object.NrIrstaterrr __setstate__{szOUI.__setstate__cCsdddg||d}|dD]`}|}|s.qd|krb|j|d<|ddd|d <t||d <qd |krnqq|d |q|j|dS) z.Returns a dict record from raw OUI record datarr()idxr;orgaddressr=r> (hex)rNNrOr; (base 16)rP)splitstriprr/appendr$)rr?r=r>recordlinerrrr9s&  zOUI._parse_datacCs t|jS)z0Number of registered organisations with this OUI)lenr$rrrr reg_countsz 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__cCsd|S)@:return: executable Python string to recreate equivalent object.z OUI('%s')rrrrr__repr__sz OUI.__repr__)r)rrr r!r"rrGrHrJrMr9propertyr[r]rcre __classcell__rrr@rr#7s '  r#csteZdZdZdZedddZdfdd Zdd Zd d Z d d Z ddZ ddZ ddZ ddZddZZS)IAB)iPiU@)rXFcCsz|d?|jkr|dfSd}d|A}|d?}||B|}|d?|jkrb|rr|dkrrtdt|ntdt|||fS)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_VALUESr2hex)clsZeui_intstrictZ user_maskZiab_maskZiab_bitsZ user_bitsrrr split_iab_macs   zIAB.split_iab_macc stt|ddlm}dddgddd|_t|trdt| ddd}|j ||d\}}||_ n0t |r|j ||d\}}||_ nt d|f|j |jkrttd }|j|j d\}} ||jd <| |jd <|||| d } || || |ntd |fdS)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()rNiabrOrPr=r>r'r))rnzunexpected IAB format: %r!ziab.txtr=r>r+zIAB %r not unregistered!N)r,rhrr-r&rXr.r/r0r1rorr r3Z IAB_INDEXr r4r5r6r7r8r9r:r) rrprnr&rbZiab_intZuser_intr<r=r>r?r@rrrs6        z IAB.__init__cCs>t|ts2z||}Wntk r0tYSX|j|jkSrr.rhrArCrDrrErrrrGs   z IAB.__eq__cCs>t|ts2z||}Wntk r0tYSX|j|jkSrrqrErrrrHs   z IAB.__ne__cCs |j|jfS)z+:returns: Pickled state of an `IAB` object.rrXrrrrrJ szIAB.__getstate__cCs|\|_|_dS)z;:param state: data used to unpickle a pickled `IAB` object.NrrrKrrrrM$szIAB.__setstate__cCsx|dD]h}|}|sq d|krV|j|jd<|ddd|jd<t||jd<q d|krbq q |jd |q dS) z.Returns a dict record from raw IAB record datarQrRrNNrSrOrprTrP)rUrVrrXr/rW)rr?r=r>rYrrrr9(s zIAB._parse_datacCs t|jS)z*The IEEE registration details for this IAB)rrXrrrrr]8szIAB.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 r_r)r`rrarrrrc<s     z IAB.__str__cCsd|S)rdz IAB('%s')rrrrrreGsz IAB.__repr__)F)F)rrr rkr" classmethodrorrGrHrJrMr9r]rcrergrrr@rrhs 4 rhcsneZdZdZdZdJfdd 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&dLdDdEZ'dFdGZ(dHdIZ)Z*S)MEUIz 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|trV|dk r:||jjkr:td|j|_|j|_|j|_dS|dk r|dkrnt |_q|dkr~t |_qtd|nFt |rd|krdkrnnt |_nd|krdkrnnt |_||_ ||_dS) a2 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) the mac_* dialect to be used to configure the formatting of EUI-48 (MAC) addresses. Nz2cannot switch EUI versions using copy constructor!0@zunsupported EUI version %rrrjl) r,rwrrxr.versionr2rdialect_eui48_eui64r value)raddrr|r}r@rrrXs, z EUI.__init__cCs|j|jj|jfS)z+:returns: Pickled state of an `EUI` object.)rrxr|r}rrrrrJszEUI.__getstate__cCsH|\}}}||_|dkr t|_n|dkr0t|_ntd|f||_dS)zN :param state: data used to unpickle a pickled `EUI` object. rzr{z&unpickling failed for object state: %sN)rr~rxrr2r})rrLrr|r}rrrrMs zEUI.__setstate__cCs|jSrrrrrr _get_valueszEUI._get_valuec Cs<|jdkrttfD]}z|||_||_WqWqtk rz:dt|kr^|jkrznnt||_||_WYqWntk rYnXYqXq|jdkrtd|fnt |rz|j||_Wn(tk rtd||jj fYnXn>dt|kr|jjkr*nn t||_ntd|fdS)Nrz failed to detect EUI version: %rzaddress %r is not an EUIv%dzbad address format: %r) rxr~r str_to_intrrr0max_intr2r r|)rrmodulerrr _set_values8        $ zEUI._set_valuezBa positive integer representing the value of this EUI indentifier.cCs|jSr)ryrrrr _get_dialectszEUI._get_dialectcCs@|dkr|jtkrtStSn t|dr4t|dr4|StddS)N word_sizeword_fmtz*custom dialects should subclass mac_eui48!)rxrrrhasattrr3rrrrr_validate_dialects zEUI._validate_dialectcCs|||_dSr)rryrrrr _set_dialectszEUI._set_dialectzXa Python class providing support for the interpretation of various MAC address formats.cCs4|jtkrt|jd?S|jtkr0t|jd?SdS)z:The OUI (Organisationally Unique Identifier) for this EUI.ru(N)rxr~r#rrrrrrr;s  zEUI.ouicCs@|jtkrdt|ddS|jtkrsz#EUI.__getitem__..zunsupported type %r!N) r ry num_words IndexErrorrx int_to_wordsrr.slicerangeindicesrZr3)rrNrrrr __getitem__s  zEUI.__getitem__cCst|trtdt|s"tdd|kr>|jjdksNntd|ft|s^tdd|krv|jjksntd||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)r.rNotImplementedErrorr r3ryrrmax_wordrlistrxrr words_to_int)rrNrrrrr __setitem__s  zEUI.__setitem__cCst|j|jfS)zA:return: hash of this EUI object suitable for dict keys, sets etc)hashr|rrrrr__hash__+sz EUI.__hash__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkSzy :return: ``True`` if this EUI object is numerically the same as other, ``False`` otherwise. r.rwrArCrDr|rrErrrrG/s   z EUI.__eq__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkSrrrErrrrH;s   z EUI.__ne__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically lower in value than other, ``False`` otherwise. rrErrr__lt__Gs   z EUI.__lt__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically lower or equal in value to other, ``False`` otherwise. rrErrr__le__Ss   z EUI.__le__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically greater in value than other, ``False`` otherwise. rrErrr__gt___s   z EUI.__gt__cCsJt|ts2z||}Wntk r0tYSX|j|jf|j|jfkS)z :return: ``True`` if this EUI object is numerically greater or equal in value to other, ``False`` otherwise. rrErrr__ge__ks   z 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. )rx int_to_bitsr)rword_seprrrbitswszEUI.bitscCs|j|jS)z8The value of this EUI address as a packed binary string.)rx int_to_packedrrrrrpackedsz EUI.packedcCs|j|jS)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. rzrur*rl|r{r|)r|rrA)rZ first_threeZ 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)r0rr )rprefixrbrrripv6s 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_localszEUI.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#rh)r;r]rrpr)rr?rrrinfoszEUI.infocCs||}|j|j|S)a" Format the EUI into the representational format according to the given dialect :param dialect: the mac_* dialect defining the formatting of EUI-48 (MAC) addresses. :return: EUI in representational format according to the given dialect )rrx int_to_strr)rr}Zvalidated_dialectrrrformats z EUI.formatcCs|j|j|jS)z':return: EUI in representational format)rxrrryrrrrrcsz EUI.__str__cCsd|S)rdz EUI('%s')rrrrrresz EUI.__repr__)NN)N)N)+rrr r!r"rrJrMrrrfrrrrr}r;rrrpr|rrrrGrHrrrrrrrrrrrrrrrcrergrrr@rrwLsd 2"                 rwN)r! netaddr.corerrrnetaddr.strategyrr~rrnetaddr.strategy.eui48rnetaddr.strategy.eui64r netaddr.ipr netaddr.compatr r r objectr r#rhrwrrrrs   $