diff options
-rw-r--r-- | rpkid/rpki/resource_set.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/rpkid/rpki/resource_set.py b/rpkid/rpki/resource_set.py index 3755706f..4a82c85e 100644 --- a/rpkid/rpki/resource_set.py +++ b/rpkid/rpki/resource_set.py @@ -65,7 +65,8 @@ class resource_range(object): """ Initialize and sanity check a resource_range. """ - assert min <= max, "Mis-ordered range: %s before %s" % (str(min), str(max)) + assert min.__class__ is max.__class__, "Type mismatch, %r doesn't match %r" % (a.__class__, b.__class__) + assert min <= max, "Mis-ordered range: %s before %s" % (min, max) self.min = min self.max = max @@ -118,6 +119,15 @@ class resource_range_as(resource_range): else: return cls(long(x), long(x)) + @classmethod + def from_strings(cls, a, b = None): + """ + Construct ASN range from strings. + """ + if b is None: + b = a + return cls(long(a), long(b)) + class resource_range_ip(resource_range): """ Range of (generic) IP addresses. @@ -217,6 +227,26 @@ class resource_range_ip(resource_range): result.append(self.make_prefix(min, self.datum_type.bits - bits)) min = self.datum_type(min + mask + 1) + @classmethod + def from_strings(cls, a, b = None): + """ + Construct IP address range from strings. + """ + if b is None: + b = a + a = rpki.ipaddrs.parse(a) + b = rpki.ipaddrs.parse(b) + if a.__class__ is not b.__class__: + raise TypeError + if cls is resource_range_ip: + if isinstance(a, rpki.ipaddrs.v4addr): + return resource_range_ipv4(a, b) + if isinstance(a, rpki.ipaddrs.v6addr): + return resource_range_ipv6(a, b) + elif isinstance(a, cls.datum_type): + return cls(a, b) + raise TypeError + class resource_range_ipv4(resource_range_ip): """ Range of IPv4 addresses. |