1# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
2#
3# Permission to use, copy, modify, and distribute this software and its
4# documentation for any purpose with or without fee is hereby granted,
5# provided that the above copyright notice and this permission notice
6# appear in all copies.
7#
8# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16import base64
17
18import dns.exception
19
20
21class DHCID(dns.rdata.Rdata):
22
23    """DHCID record
24
25    @ivar data: the data (the content of the RR is opaque as far as the
26    DNS is concerned)
27    @type data: string
28    @see: RFC 4701"""
29
30    __slots__ = ['data']
31
32    def __init__(self, rdclass, rdtype, data):
33        super(DHCID, self).__init__(rdclass, rdtype)
34        self.data = data
35
36    def to_text(self, origin=None, relativize=True, **kw):
37        return dns.rdata._base64ify(self.data)
38
39    @classmethod
40    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
41        chunks = []
42        while 1:
43            t = tok.get().unescape()
44            if t.is_eol_or_eof():
45                break
46            if not t.is_identifier():
47                raise dns.exception.SyntaxError
48            chunks.append(t.value.encode())
49        b64 = b''.join(chunks)
50        data = base64.b64decode(b64)
51        return cls(rdclass, rdtype, data)
52
53    def to_wire(self, file, compress=None, origin=None):
54        file.write(self.data)
55
56    @classmethod
57    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
58        data = wire[current: current + rdlen].unwrap()
59        return cls(rdclass, rdtype, data)
60