diff options
Diffstat (limited to 'rpkid/rpki/ipaddrs.py')
-rw-r--r-- | rpkid/rpki/ipaddrs.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/rpkid/rpki/ipaddrs.py b/rpkid/rpki/ipaddrs.py index 32be2cb0..1bcb425e 100644 --- a/rpkid/rpki/ipaddrs.py +++ b/rpkid/rpki/ipaddrs.py @@ -12,7 +12,23 @@ once, here, thus avoiding a lot of duplicate code elsewhere. $Id$ -Copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN") + +Copyright (C) 2009 Internet Systems Consortium ("ISC") + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. + + +Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN") Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -45,9 +61,13 @@ class v4addr(long): x = y[0] return long.__new__(cls, x) + def to_bytes(self): + """Convert a v4addr object to a raw byte string.""" + return struct.pack("!I", long(self)) + def __str__(self): """Convert a v4addr object to string format.""" - return socket.inet_ntop(socket.AF_INET, struct.pack("!I", long(self))) + return socket.inet_ntop(socket.AF_INET, self.to_bytes()) class v6addr(long): """IPv6 address. @@ -64,7 +84,10 @@ class v6addr(long): x = (y[0] << 64) | y[1] return long.__new__(cls, x) + def to_bytes(self): + """Convert a v6addr object to a raw byte string.""" + return struct.pack("!QQ", long(self) >> 64, long(self) & 0xFFFFFFFFFFFFFFFF) + def __str__(self): """Convert a v6addr object to string format.""" - return socket.inet_ntop(socket.AF_INET6, - struct.pack("!QQ", long(self) >> 64, long(self) & 0xFFFFFFFFFFFFFFFF)) + return socket.inet_ntop(socket.AF_INET6, self.to_bytes()) |