Package dns :: Package rdtypes :: Module euibase
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.euibase

 1  # Copyright (C) 2015 Red Hat, Inc. 
 2  # Author: Petr Spacek <pspacek@redhat.com> 
 3  # 
 4  # Permission to use, copy, modify, and distribute this software and its 
 5  # documentation for any purpose with or without fee is hereby granted, 
 6  # provided that the above copyright notice and this permission notice 
 7  # appear in all copies. 
 8  # 
 9  # THE SOFTWARE IS PROVIDED 'AS IS' AND RED HAT DISCLAIMS ALL WARRANTIES 
10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
15  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
16   
17  import binascii 
18   
19  import dns.rdata 
20 21 22 -class EUIBase(dns.rdata.Rdata):
23 24 """EUIxx record 25 26 @ivar fingerprint: xx-bit Extended Unique Identifier (EUI-xx) 27 @type fingerprint: string 28 @see: rfc7043.txt""" 29 30 __slots__ = ['eui'] 31 # define these in subclasses 32 # byte_len = 6 # 0123456789ab (in hex) 33 # text_len = byte_len * 3 - 1 # 01-23-45-67-89-ab 34
35 - def __init__(self, rdclass, rdtype, eui):
36 super(EUIBase, self).__init__(rdclass, rdtype) 37 if len(eui) != self.byte_len: 38 raise dns.exception.FormError('EUI%s rdata has to have %s bytes' 39 % (self.byte_len * 8, self.byte_len)) 40 self.eui = eui
41
42 - def to_text(self, origin=None, relativize=True, **kw):
43 return dns.rdata._hexify(self.eui, chunksize=2).replace(' ', '-')
44 45 @classmethod
46 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
47 text = tok.get_string() 48 tok.get_eol() 49 if len(text) != cls.text_len: 50 raise dns.exception.SyntaxError( 51 'Input text must have %s characters' % cls.text_len) 52 expected_dash_idxs = range(2, cls.byte_len * 3 - 1, 3) 53 for i in expected_dash_idxs: 54 if text[i] != '-': 55 raise dns.exception.SyntaxError('Dash expected at position %s' 56 % i) 57 text = text.replace('-', '') 58 try: 59 data = binascii.unhexlify(text.encode()) 60 except (ValueError, TypeError) as ex: 61 raise dns.exception.SyntaxError('Hex decoding error: %s' % str(ex)) 62 return cls(rdclass, rdtype, data)
63
64 - def to_wire(self, file, compress=None, origin=None):
65 file.write(self.eui)
66 67 @classmethod
68 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
69 eui = wire[current:current + rdlen].unwrap() 70 return cls(rdclass, rdtype, eui)
71