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  from dns._compat import xrange
    21
    22
    23 -class EUIBase(dns.rdata.Rdata):
    24
    25      """EUIxx record
    26
    27      @ivar fingerprint: xx-bit Extended Unique Identifier (EUI-xx)
    28      @type fingerprint: string
    29      @see: rfc7043.txt"""
    30
    31      __slots__ = ['eui']
    32      # define these in subclasses
    33      # byte_len = 6  # 0123456789ab (in hex)
    34      # text_len = byte_len * 3 - 1  # 01-23-45-67-89-ab
    35
    36 -    def __init__(self, rdclass, rdtype, eui):
    37          super(EUIBase, self).__init__(rdclass, rdtype)
    38          if len(eui) != self.byte_len:
    39              raise dns.exception.FormError('EUI%s rdata has to have %s bytes'
    40                                            % (self.byte_len * 8, self.byte_len))
    41          self.eui = eui
    42
    43 -    def to_text(self, origin=None, relativize=True, **kw):
    44          return dns.rdata._hexify(self.eui, chunksize=2).replace(' ', '-')
    45
    46      @classmethod
    47 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
    48          text = tok.get_string()
    49          tok.get_eol()
    50          if len(text) != cls.text_len:
    51              raise dns.exception.SyntaxError(
    52                  'Input text must have %s characters' % cls.text_len)
    53          expected_dash_idxs = xrange(2, cls.byte_len * 3 - 1, 3)
    54          for i in expected_dash_idxs:
    55              if text[i] != '-':
    56                  raise dns.exception.SyntaxError('Dash expected at position %s'
    57                                                  % i)
    58          text = text.replace('-', '')
    59          try:
    60              data = binascii.unhexlify(text.encode())
    61          except (ValueError, TypeError) as ex:
    62              raise dns.exception.SyntaxError('Hex decoding error: %s' % str(ex))
    63          return cls(rdclass, rdtype, data)
    64
    65 -    def to_wire(self, file, compress=None, origin=None):
    66          file.write(self.eui)
    67
    68      @classmethod
    69 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    70          eui = wire[current:current + rdlen].unwrap()
    71          return cls(rdclass, rdtype, eui)
    72