| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2010, 2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
import base64 |
| 19 |
import binascii |
| 20 |
import struct |
| 21 |
|
| 22 |
import dns.exception |
| 23 |
import dns.immutable |
| 24 |
import dns.rdata |
| 25 |
import dns.rdatatype |
| 26 |
|
| 27 |
|
| 28 |
@dns.immutable.immutable |
| 29 |
class HIP(dns.rdata.Rdata): |
| 30 |
"""HIP record""" |
| 31 |
|
| 32 |
# see: RFC 5205 |
| 33 |
|
| 34 |
__slots__ = ["hit", "algorithm", "key", "servers"] |
| 35 |
|
| 36 |
def __init__(self, rdclass, rdtype, hit, algorithm, key, servers): |
| 37 |
super().__init__(rdclass, rdtype) |
| 38 |
self.hit = self._as_bytes(hit, True, 255) |
| 39 |
self.algorithm = self._as_uint8(algorithm) |
| 40 |
self.key = self._as_bytes(key, True) |
| 41 |
self.servers = self._as_tuple(servers, self._as_name) |
| 42 |
|
| 43 |
def to_text(self, origin=None, relativize=True, **kw): |
| 44 |
hit = binascii.hexlify(self.hit).decode() |
| 45 |
key = base64.b64encode(self.key).replace(b"\n", b"").decode() |
| 46 |
text = "" |
| 47 |
servers = [] |
| 48 |
for server in self.servers: |
| 49 |
servers.append(server.choose_relativity(origin, relativize)) |
| 50 |
if len(servers) > 0: |
| 51 |
text += " " + " ".join(x.to_unicode() for x in servers) |
| 52 |
return f"{self.algorithm} {hit} {key}{text}" |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def from_text( |
| 56 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 57 |
): |
| 58 |
algorithm = tok.get_uint8() |
| 59 |
hit = binascii.unhexlify(tok.get_string().encode()) |
| 60 |
key = base64.b64decode(tok.get_string().encode()) |
| 61 |
servers = [] |
| 62 |
for token in tok.get_remaining(): |
| 63 |
server = tok.as_name(token, origin, relativize, relativize_to) |
| 64 |
servers.append(server) |
| 65 |
return cls(rdclass, rdtype, hit, algorithm, key, servers) |
| 66 |
|
| 67 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 68 |
lh = len(self.hit) |
| 69 |
lk = len(self.key) |
| 70 |
file.write(struct.pack("!BBH", lh, self.algorithm, lk)) |
| 71 |
file.write(self.hit) |
| 72 |
file.write(self.key) |
| 73 |
for server in self.servers: |
| 74 |
server.to_wire(file, None, origin, False) |
| 75 |
|
| 76 |
@classmethod |
| 77 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 78 |
(lh, algorithm, lk) = parser.get_struct("!BBH") |
| 79 |
hit = parser.get_bytes(lh) |
| 80 |
key = parser.get_bytes(lk) |
| 81 |
servers = [] |
| 82 |
while parser.remaining() > 0: |
| 83 |
server = parser.get_name(origin) |
| 84 |
servers.append(server) |
| 85 |
return cls(rdclass, rdtype, hit, algorithm, key, servers) |