2 from ipaddress import IPv4Address, IPv6Address
3 from typing import List, Dict, Any
11 def hostname(name: str) -> str:
12 # check hostname for validity
13 label = r'[a-zA-Z90-9]([a-zA-Z90-9-]{0,61}[a-zA-Z90-9])?' # must not start or end with hyphen
14 pattern = r'^{0}(\.{0})*\.?'.format(label)
16 if re.match(pattern, name):
18 raise Exception(name+" is not a valid hostname")
21 def __init__(self, IPv4: str = None, IPv6: str = None) -> None:
22 self._IPv4 = None if IPv4 is None else IPv4Address(IPv4)
23 self._IPv6 = None if IPv6 is None else IPv6Address(IPv6)
26 return Address(IPv4 = self._IPv4)
29 return Address(IPv6 = self._IPv6)
32 def __init__(self, address: Address = None, MX: List = None,
33 TCP: Dict[int, Any] = None, UDP: Dict[int, Any] = None) -> None:
34 self._address = address
37 def __init__(self, SRV: str = None, TLSA: str=None) -> None:
42 def __init__(self, name: str) -> None:
46 def __init__(self, NS: str, DS: str = None) -> None:
50 def __init__(self, name: str, mail: str, NS: List[str],
51 secondary_refresh: int, secondary_retry: int, secondary_discard: int,
52 NX_TTL: int = None, A_TTL: int = None, other_TTL: int = None,
53 domains: Dict[str, Any] = {}) -> None:
54 self._name = hostname(name)
55 if not mail.endswith('.'): raise Exception("Mail must be absolute, end with a dot")
56 atpos = mail.find('@')
57 if atpos < 0 or atpos > mail.find('.'): raise Exception("Mail must contain an @ before the first dot")
58 self._mail = hostname(mail.replace('@', '.', 1))
59 self._NS = list(map(hostname, NS))
61 self._secondary_refresh = secondary_refresh
62 self._secondary_retry = secondary_retry
63 self._secondary_discard = secondary_discard
65 assert other_TTL is not None
66 self._NX_TTL = other_TTL if NX_TTL is None else NX_TTL
67 self._A_TTL = other_TTL if A_TTL is None else A_TTL
68 self._other_TTL = other_TTL
70 def write(self, file):
71 raise NotImplementedError()