U .Ѫ_Xdã@s¤dZddlmZmZmZddlmZmZ ddl m Z ddl m Z ddlmZddlmZmZmZGdd „d eƒZGd d „d eƒZGd d „d eƒZGdd„deƒZdS)z] Classes and functions for dealing with MAC addresses, EUI-48, EUI-64, OUI, IAB identifiers. é)ÚNotRegisteredErrorÚAddrFormatErrorÚ DictDotLookup)Úeui48Úeui64)Ú mac_eui48)Ú eui64_base)Ú IPAddress)Ú_importlib_resourcesÚ_is_intÚ_is_strc@sDeZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dS)ÚBaseIdentifierz$Base class for all IEEE identifiers.)Ú_valueÚ __weakref__cCs d|_dS©N©r©Úself©rú;/Users/cjl/code/aws-vpc-ipam/lambda/netaddr/eui/__init__.pyÚ__init__szBaseIdentifier.__init__cCs|jS©z):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.rÚ0z0%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 csreZdZdZdZ‡fdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z e dd„ƒZ ddd„Z dd„Zdd„Z‡ZS)ÚOUIzŒ An individual IEEE OUI (Organisationally Unique Identifier). For online details see - http://standards.ieee.org/regauth/oui/ )Úrecordscsøtt|ƒ ¡ddlm}g|_t|tƒr@t|  dd¡dƒ|_ nFt |ƒrxd|kr\dkrhnn||_ q†t d|fƒ‚nt d|fƒ‚|j |jkræt td ¡}|j|j D]0\}}| |¡| |¡ d ¡}| |||¡qª| ¡ntd |fƒ‚d 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)! r©Úieeeú-Úééÿÿÿz"OUI int outside expected range: %rzunexpected OUI format: %rzoui.txtúUTF-8zOUI %r not registered!N)Úsuperr#rÚ netaddr.euir&r$Ú isinstanceÚstrÚintÚreplacerr Ú ValueErrorÚ TypeErrorZ OUI_INDEXr Ú open_binaryÚ __package__ÚseekÚreadÚdecodeÚ _parse_dataÚcloser)rÚouir&ÚfhÚoffsetÚsizeÚdata©Ú __class__rrr@s$      z OUI.__init__cCs>t|tƒs2z| |¡}Wntk r0tYSX|j|jkSr©r.r#rAÚ ExceptionÚNotImplementedr©rÚotherrrrÚ__eq__gs   z OUI.__eq__cCs>t|tƒs2z| |¡}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.NrI©rÚstaterrrÚ __setstate__{szOUI.__setstate__cCsŽdddg||dœ}| d¡D]`}| ¡}|s.qd|krb|j|d<| dd¡d|d <t|ƒ|d <qd |krnqq|d  |¡q|j |¡dS) z.Returns a dict record from raw OUI record datarr()Úidxr;ÚorgÚaddressr=r>Ú ú(hex)rNNérOr;ú (base 16)rP)ÚsplitÚstriprr/Úappendr$)rr?r=r>ÚrecordÚlinerrrr9s&ú  zOUI._parse_datacCs t|jƒS)z0Number of registered organisations with this OUI)Úlenr$rrrrÚ reg_countšsz 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$)rÚindexrrrÚ registrationŸs zOUI.registrationcCs(|j}d|d?d@|d?d@|d@fS)z*:return: string representation of this OUIú%02X-%02X-%02Xr)éÿér©rÚint_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"rrGrHrJrMr9Úpropertyr[r]rcreÚ __classcell__rrr@rr#7s '  r#csteZdZdZdZeddd„ƒZd‡fdd„ Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Z‡ZS)ÚIAB)iÂPiUØ@)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) é riÿìÿÿÿz%r is not a strict IAB!z%r is not an IAB address!)ÚIAB_EUI_VALUESr2Úhex)ÚclsZeui_intÚstrictZ user_maskZiab_maskZiab_bitsZ user_bitsrrrÚ split_iab_macÃs   zIAB.split_iab_macc stt|ƒ ¡ddlm}dddgdddœ|_t|tƒrdt|  dd¡dƒ}|j ||d�\}}||_ n0t |ƒr†|j ||d�\}}||_ nt d|fƒ‚|j |jk�rt td ¡}|j|j d\}} ||jd <| |jd <| |¡| | ¡ d ¡} | | || ¡| ¡ntd |fƒ‚dS)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()rNÚiabrOrPr=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@rrrÜs6  ú      z IAB.__init__cCs>t|tƒs2z| |¡}Wntk r0tYSX|j|jkSr©r.rhrArCrDrrErrrrGs   z IAB.__eq__cCs>t|tƒs2z| |¡}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| d¡D]h}| ¡}|sq d|krV|j|jd<| dd¡d|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|jƒS)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 IABéz%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@rrh¸s 4 rhcsneZdZdZdZdJ‡fdd„ Zdd„Zdd „Zd d „Zd d „Z e ee ddƒZ dd„Z dd„Z dd„Ze e eddƒZe dd„ƒZe dd„ƒZdd„Ze dd„ƒZe dd„ƒZd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„ZdKd2d3„Ze d4d5„ƒZe d6d7„ƒZ e d8d9„ƒZ!d:d;„Z"dd?„Z$d@dA„Z%e dBdC„ƒZ&dLdDdE„Z'dFdG„Z(dHdI„Z)‡Z*S)MÚEUIz× 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Ú_dialectNcsâtt|ƒ ¡d|_t|tƒrV|dk r:||jjkr:tdƒ‚|j|_|j|_|j|_dS|dk rŒ|dkrnt |_qÒ|dkr~t |_qÒtd|ƒ‚nFt |ƒrÒd|kr¨dkr´nnt |_nd|krÈdkrÒnnt |_||_ ||_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.Úversionr2rÚdialectÚ_eui48Ú_eui64r Úvalue)rÚaddrr|r}r@rrrXs, z EUI.__init__cCs|j|jj|jfS)z+:returns: Pickled state of an `EUI` object.)rrxr|r}rrrrrJŠszEUI.__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})rrLr€r|r}rrrrMŽs ÿzEUI.__setstate__cCs|jSrrrrrrÚ _get_value¡szEUI._get_valuec Cs<|jdkr´ttfD]†}z| |¡|_||_WqšWqtk r–z:dt|ƒkr^|jkrznnt|ƒ|_||_WYqšWntk r�YnXYqXq|jdkr²td|fƒ‚n„t |ƒrúz|j |¡|_Wn(tk rötd||jj fƒ‚YnXn>dt|ƒk�r|jjk�r*nn t|ƒ|_ntd|fƒ‚dS)Nrz failed to detect EUI version: %rzaddress %r is not an EUIv%dzbad address format: %r) rxr~rÚ str_to_intrrr0Úmax_intr2r r|)rr€ÚmodulerrrÚ _set_value¤s8       ÿ ÿ$ zEUI._set_valuezBa positive integer representing the value of this EUI indentifier.cCs|jSr)ryrrrrÚ _get_dialectÉszEUI._get_dialectcCs@|dkr|jtkrtStSn t|dƒr4t|dƒr4|Stdƒ‚dS)NÚ word_sizeÚword_fmtz*custom dialects should subclass mac_eui48!)rxrrrÚhasattrr3©rr€rrrÚ_validate_dialectÌs zEUI._validate_dialectcCs| |¡|_dSr)rŒryr‹rrrÚ _set_dialectØszEUI._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#r€rrrrrr;ßs  zEUI.ouicCs@|jtkrdt|dd…ƒS|jtkrsz#EUI.__getitem__..zunsupported type %r!N) r ryÚ num_wordsÚ IndexErrorrxÚ int_to_wordsrr.ÚsliceÚrangeÚindicesrZr3)rrNršrr—rÚ __getitem__s  zEUI.__getitem__cCs¼t|tƒrtdƒ‚t|ƒs"tdƒ‚d|kr>|jjdksNntd|fƒ‚t|ƒs^tdƒ‚d|krv|jjksŒntd||jj fƒ‚t |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!rr”z'index %d outside address type boundary!zvalue not an integer!z.value %d outside word size maximum of %d bits!N)r.r�ÚNotImplementedErrorr r3ryršr›Úmax_wordrˆÚlistrxrœrÚ words_to_int)rrNr€r˜rrrÚ __setitem__s  ÿzEUI.__setitem__cCst|j|jfƒS)zA:return: hash of this EUI object suitable for dict keys, sets etc)Úhashr|rrrrrÚ__hash__+sz EUI.__hash__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkS©zy :return: ``True`` if this EUI object is numerically the same as other, ``False`` otherwise. ©r.rwrArCrDr|rrErrrrG/s   z EUI.__eq__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkSr¨r©rErrrrH;s   z EUI.__ne__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkS)z� :return: ``True`` if this EUI object is numerically lower in value than other, ``False`` otherwise. r©rErrrÚ__lt__Gs   z EUI.__lt__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkS)zˆ :return: ``True`` if this EUI object is numerically lower or equal in value to other, ``False`` otherwise. r©rErrrÚ__le__Ss   z EUI.__le__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkS)zƒ :return: ``True`` if this EUI object is numerically greater in value than other, ``False`` otherwise. r©rErrrÚ__gt___s   z EUI.__gt__cCsJt|tƒs2z| |¡}Wntk r0tYSX|j|jf|j|jfkS)zŠ :return: ``True`` if this EUI object is numerically greater or equal in value to other, ``False`` otherwise. r©rErrrÚ__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)rÚword_seprrrÚbitswszEUI.bitscCs|j |j¡S)z8The value of this EUI address as a packed binary string.)rxÚ int_to_packedrrrrrÚpacked€sz EUI.packedcCs|j |j¡S)zdB|B}n|j}|j|dd�S)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*rŽl|ÿr{©r|)r|rrA)rZ first_threeZ last_threeÚ new_valuerrrr“s   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)rrrrrÚmodified_eui64¦s zEUI.modified_eui64cCs t|ƒt| ¡ƒ}t|dd�S)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. r�rµ)r0r·r )rÚprefixrbrrrÚipv6¶s zEUI.ipv6cCs | d¡S)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 @þ)r¹rrrrÚipv6_link_localÃszEUI.ipv6_link_localcCs,d|j ¡i}| ¡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]r“rpr)rr?rrrÚinfoÍszEUI.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 )rŒrxÚ int_to_strr)rr}Zvalidated_dialectrrrÚformatÙs z EUI.formatcCs|j |j|j¡S)z':return: EUI in representational format)rxr¼rryrrrrrcæsz EUI.__str__cCsd|S)rdz EUI('%s')rrrrrreêsz EUI.__repr__)NN)N)N)+rrr r!r"rrJrMr‚r†rfr€r‡rŒr�r}r;r’r“rpr|r r¥r§rGrHrªr«r¬r­r°r²r˜r´rr·r¹rºr»r½rcrergrrr@rrwLsd 2"ÿ ÿ                rwN)r!Ú netaddr.corerrrÚnetaddr.strategyrr~rrÚnetaddr.strategy.eui48rÚnetaddr.strategy.eui64rÚ netaddr.ipr Únetaddr.compatr r r Úobjectr r#rhrwrrrrÚs   $