compatibility with python 3.2
authorRalf Jung <post@ralfj.de>
Tue, 11 Nov 2014 10:03:54 +0000 (11:03 +0100)
committerRalf Jung <post@ralfj.de>
Tue, 11 Nov 2014 10:03:54 +0000 (11:03 +0100)
zonemaker/zone.py

index f9712e9e5e3aa9a7fa8a94df09a4720fd07998f9..390b0dd4de65ceefa2ab35ad72d9da090aa42dcf 100644 (file)
@@ -1,5 +1,4 @@
 import re, datetime
-from ipaddress import IPv4Address, IPv6Address
 from typing import List, Dict, Any, Iterator, Tuple, Sequence
 
 
@@ -10,6 +9,8 @@ day = 24*hour
 week = 7*day
 
 REGEX_label = r'[a-zA-Z90-9]([a-zA-Z90-9-]{0,61}[a-zA-Z90-9])?' # max. 63 characters; must not start or end with hyphen
+REGEX_ipv4  = r'^\d{1,3}(\.\d{1,3}){3}$'
+REGEX_ipv6  = r'^[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){7}$'
 
 def check_label(label: str) -> str:
     pattern = r'^{0}$'.format(REGEX_label)
@@ -29,6 +30,16 @@ def check_hex(data: str) -> str:
         return data
     raise Exception(data+" is not valid hex data")
 
+def check_ipv4(address: str) -> str:
+    if re.match(REGEX_ipv4, address):
+        return address
+    raise Exception(address+" is not a valid IPv4 address")
+
+def check_ipv6(address: str) -> str:
+    if re.match(REGEX_ipv6, address):
+        return address
+    raise Exception(address+" is not a valid IPv6 address")
+
 def time(time: int) -> str:
     if time == 0:
         return "0"
@@ -72,7 +83,7 @@ class Digest:
 ## Record types
 class A:
     def __init__(self, address: str) -> None:
-        self._address = IPv4Address(address)
+        self._address = check_ipv4(address)
     
     def generate_rr(self, owner: str, zone: 'Zone') -> Any:
         return zone.RR(owner, 'A', self._address)
@@ -80,7 +91,7 @@ class A:
 
 class AAAA:
     def __init__(self, address: str) -> None:
-        self._address = IPv6Address(address)
+        self._address = check_ipv6(address)
     
     def generate_rr(self, owner: str, zone: 'Zone') -> Any:
         return zone.RR(owner, 'AAAA', self._address)