diff options
Diffstat (limited to 'rpki/ipaddrs.py')
-rw-r--r-- | rpki/ipaddrs.py | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/rpki/ipaddrs.py b/rpki/ipaddrs.py index 25eefd0d..5117585c 100644 --- a/rpki/ipaddrs.py +++ b/rpki/ipaddrs.py @@ -48,99 +48,99 @@ once, here, thus avoiding a lot of duplicate code elsewhere. import socket, struct class v4addr(long): - """ - IPv4 address. + """ + IPv4 address. - Derived from long, but supports IPv4 print syntax. - """ + Derived from long, but supports IPv4 print syntax. + """ - bits = 32 - ipversion = 4 + bits = 32 + ipversion = 4 - def __new__(cls, x): - """ - Construct a v4addr object. - """ + def __new__(cls, x): + """ + Construct a v4addr object. + """ - if isinstance(x, unicode): - x = x.encode("ascii") - if isinstance(x, str): - return cls.from_bytes(socket.inet_pton(socket.AF_INET, ".".join(str(int(i)) for i in x.split(".")))) - else: - return long.__new__(cls, x) + if isinstance(x, unicode): + x = x.encode("ascii") + if isinstance(x, str): + return cls.from_bytes(socket.inet_pton(socket.AF_INET, ".".join(str(int(i)) for i in x.split(".")))) + else: + return long.__new__(cls, x) - def to_bytes(self): - """ - Convert a v4addr object to a raw byte string. - """ + def to_bytes(self): + """ + Convert a v4addr object to a raw byte string. + """ - return struct.pack("!I", long(self)) + return struct.pack("!I", long(self)) - @classmethod - def from_bytes(cls, x): - """ - Convert from a raw byte string to a v4addr object. - """ + @classmethod + def from_bytes(cls, x): + """ + Convert from a raw byte string to a v4addr object. + """ - return cls(struct.unpack("!I", x)[0]) + return cls(struct.unpack("!I", x)[0]) - def __str__(self): - """ - Convert a v4addr object to string format. - """ + def __str__(self): + """ + Convert a v4addr object to string format. + """ - return socket.inet_ntop(socket.AF_INET, self.to_bytes()) + return socket.inet_ntop(socket.AF_INET, self.to_bytes()) class v6addr(long): - """ - IPv6 address. + """ + IPv6 address. - Derived from long, but supports IPv6 print syntax. - """ + Derived from long, but supports IPv6 print syntax. + """ - bits = 128 - ipversion = 6 + bits = 128 + ipversion = 6 - def __new__(cls, x): - """ - Construct a v6addr object. - """ + def __new__(cls, x): + """ + Construct a v6addr object. + """ - if isinstance(x, unicode): - x = x.encode("ascii") - if isinstance(x, str): - return cls.from_bytes(socket.inet_pton(socket.AF_INET6, x)) - else: - return long.__new__(cls, x) + if isinstance(x, unicode): + x = x.encode("ascii") + if isinstance(x, str): + return cls.from_bytes(socket.inet_pton(socket.AF_INET6, x)) + else: + return long.__new__(cls, x) - def to_bytes(self): - """ - Convert a v6addr object to a raw byte string. - """ + def to_bytes(self): + """ + Convert a v6addr object to a raw byte string. + """ - return struct.pack("!QQ", long(self) >> 64, long(self) & 0xFFFFFFFFFFFFFFFF) + return struct.pack("!QQ", long(self) >> 64, long(self) & 0xFFFFFFFFFFFFFFFF) - @classmethod - def from_bytes(cls, x): - """ - Convert from a raw byte string to a v6addr object. - """ + @classmethod + def from_bytes(cls, x): + """ + Convert from a raw byte string to a v6addr object. + """ - x = struct.unpack("!QQ", x) - return cls((x[0] << 64) | x[1]) + x = struct.unpack("!QQ", x) + return cls((x[0] << 64) | x[1]) - def __str__(self): - """ - Convert a v6addr object to string format. - """ + def __str__(self): + """ + Convert a v6addr object to string format. + """ - return socket.inet_ntop(socket.AF_INET6, self.to_bytes()) + return socket.inet_ntop(socket.AF_INET6, self.to_bytes()) def parse(s): - """ - Parse a string as either an IPv4 or IPv6 address, and return object of appropriate class. - """ + """ + Parse a string as either an IPv4 or IPv6 address, and return object of appropriate class. + """ - if isinstance(s, unicode): - s = s.encode("ascii") - return v6addr(s) if ":" in s else v4addr(s) + if isinstance(s, unicode): + s = s.encode("ascii") + return v6addr(s) if ":" in s else v4addr(s) |