aboutsummaryrefslogtreecommitdiff
path: root/rpki/ipaddrs.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2015-10-26 06:29:00 +0000
committerRob Austein <sra@hactrn.net>2015-10-26 06:29:00 +0000
commitb46deb1417dc3596e9ac9fe2fe8cc0b7f42457e7 (patch)
treeca0dc0276d1adc168bc3337ce0564c4ec4957c1b /rpki/ipaddrs.py
parent397beaf6d9900dc3b3cb612c89ebf1d57b1d16f6 (diff)
"Any programmer who fails to comply with the standard naming, formatting,
or commenting conventions should be shot. If it so happens that it is inconvenient to shoot him, then he is to be politely requested to recode his program in adherence to the above standard." -- Michael Spier, Digital Equipment Corporation svn path=/branches/tk705/; revision=6152
Diffstat (limited to 'rpki/ipaddrs.py')
-rw-r--r--rpki/ipaddrs.py142
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)