"""
from afs import exception
-from socket import gethostbyname_ex, gethostbyaddr, gaierror, herror, inet_pton, inet_ntoa, AF_INET, AF_INET6
+from socket import gethostbyname_ex, gethostbyaddr, gaierror, herror
import sys
+import ipaddress
cache_n2a = dict()
cache_a2n = dict()
-class NetAddressError(exception.AFSException):
- """Error raised by L{address cache}."""
-
def add(name, addr):
global cache_n2a, cache_a2n
name = name.lower().rstrip(".")
if name not in cache_n2a:
cache_n2a[name] = []
- if addr not in cache_n2a[name]:
+ if str(addr) not in cache_n2a[name]:
cache_n2a[name].append(addr)
- cache_a2n[addr] = name
+ cache_a2n[str(addr)] = name
def add_ghb(name, addr, result):
hostname, aliaslist, ipaddrlist = result
- if addr and addr not in ipaddrlist:
- print("Inconsistent lookup of '", addr, "'", file=sys.stderr)
+ if addr and str(addr) not in ipaddrlist:
+ print("Inconsistent lookup of '", str(addr), "'", file=sys.stderr)
for i in ipaddrlist:
if name:
- add(name, i)
- add(hostname, i)
+ add(name, ipaddress.IPv4Address(i))
+ add(hostname, ipaddress.IPv4Address(i))
for j in aliaslist:
- add(j, i)
+ add(j, ipaddress.IPv4Address(i))
###############################################################################
#
def name2addrs(name):
global cache_n2a
- # Try parsing as a numeric IPv4 address
try:
- addr = inet_pton(AF_INET, name)
- return name
- except OSError:
- pass
-
- # Try parsing as a numeric IPv6 address
- try:
- addr = inet_pton(AF_INET6, name)
- raise NetAddressError("IPv6 is not currently supported")
- except OSError:
+ addr = ipaddress.ip_address(name)
+ if addr.version == 6:
+ raise exception.AFSNetAddressError("IPv6 is not currently supported")
+ return str(addr)
+ except ValueError:
pass
name = name.lower().rstrip(".")
try:
result = gethostbyname_ex(name)
except gaierror as e:
- raise NetAddressError("Couldn't resolve '" + name + "'")
+ raise exception.AFSNetAddressError("Couldn't resolve '" + name + "'")
add_ghb(name, None, result)
return cache_n2a[name]
###############################################################################
#
-# Convert a string address to an address in integer form
+# Convert a string address to an address
#
###############################################################################
-def addr2addr_int(name):
- # Try parsing as a numeric IPv4 address
- try:
- addr = inet_pton(AF_INET, name)
- inaddr = addr[0] << 24
- inaddr |= addr[1] << 16
- inaddr |= addr[2] << 8
- inaddr |= addr[3] << 0
- return inaddr
- except OSError:
- pass
-
- # Try parsing as a numeric IPv6 address
+def addr2addr(name):
try:
- addr = inet_pton(AF_INET6, name)
- raise NetAddressError("IPv6 is not currently supported")
- except OSError:
- pass
- raise NetAddressError("Can't translate '" + name + "' to integer")
+ addr = ipaddress.ip_address(name)
+ if addr.version == 6:
+ raise exception.AFSNetAddressError("IPv6 is not currently supported")
+ return addr
+ except ValueError:
+ raise exception.AFSNetAddressError("Can't translate '" + name + "' to integer")
###############################################################################
#
-# Convert a name or string address to an address in integer form
+# Convert a name or string address to an address
#
###############################################################################
-def name2addr_int(name):
+def name2addr(name):
global cache_n2a
# Try parsing as a numeric IPv4 address
try:
- addr = inet_pton(AF_INET, name)
- inaddr = addr[0] << 24
- inaddr |= addr[1] << 16
- inaddr |= addr[2] << 8
- inaddr |= addr[3] << 0
- return inaddr
- except OSError:
- pass
-
- # Try parsing as a numeric IPv6 address
- try:
- addr = inet_pton(AF_INET6, name)
- raise NetAddressError("IPv6 is not currently supported")
- except OSError:
+ addr = ipaddress.ip_address(name)
+ if addr.version == 6:
+ raise exception.AFSNetAddressError("IPv6 is not currently supported")
+ return addr
+ except ValueError:
pass
if name not in cache_n2a:
return None
- addr = inet_pton(AF_INET, cache_n2a[name][0])
- inaddr = addr[0] << 24
- inaddr |= addr[1] << 16
- inaddr |= addr[2] << 8
- inaddr |= addr[3] << 0
- return inaddr
+ return cache_n2a[name][0]
###############################################################################
#
###############################################################################
def addrs2name(addrs):
global cache_a2n
+ cooked_addrs = []
for addr in addrs:
- if addr in cache_a2n:
- return cache_a2n[addr]
+ if str(type(addr)) != "<class 'IPv4Address'>":
+ addr = ipaddress.ip_address(addr)
+ cooked_addrs.append(addr)
+ else:
+ cooked_addrs.append(addr)
+ if str(addr) in cache_a2n:
+ return cache_a2n[str(addr)]
for addr in addrs:
try:
- result = gethostbyaddr(addr)
+ result = gethostbyaddr(str(addr))
add_ghb(None, addr, result)
- return cache_a2n[addr]
+ return cache_a2n[str(addr)]
except herror:
pass
def addr2name(addr):
return addrs2name([ addr ])
-###############################################################################
-#
-# Convert an integer address to a string address.
-#
-###############################################################################
-def int2addr(i):
- inaddr = bytearray(4)
- inaddr[0] = (i >> 24) & 0xff
- inaddr[1] = (i >> 16) & 0xff
- inaddr[2] = (i >> 8) & 0xff
- inaddr[3] = (i >> 0) & 0xff
- return inet_ntoa(bytes(inaddr))
-
###############################################################################
#
# Resolve an integer or string address to a string address or a name,
#
###############################################################################
def resolve(params, addr):
- if str(type(addr)) == "<class 'int'>":
- addr = int2addr(addr)
+ if str(type(addr)) != "<class 'IPv4Address'>" and str(type(addr)) != "<class 'IPv6Address'>":
+ addr = ipaddress.ip_address(addr)
if "noresolve" in params:
- return addr
+ return str(addr)
name = addr2name(addr)
if name == None:
- return addr
+ return str(addr)
return name
+
+###############################################################################
+#
+# Convert an integer or a string address to an address.
+#
+###############################################################################
+def addr2addr(addr):
+ if str(type(addr)) != "<class 'IPv4Address'>" and str(type(addr)) != "<class 'IPv6Address'>":
+ addr = ipaddress.ip_address(addr)
+ if addr.version == 6:
+ raise exception.AFSNetAddressError("IPv6 is not currently supported")
+ return addr
key, security = self.determine_security(params)
- for vlserver in self.query_vl_addrs():
- verbose("Trying ", vlserver, "\n")
+ for vladdr in self.query_vl_addrs():
+ verbose("Trying vlserver ", vladdr, "\n")
- z_conn = kafs.rx_new_connection(vlserver, kafs.VL_PORT, kafs.VL_SERVICE,
+ z_conn = kafs.rx_new_connection(str(vladdr), kafs.VL_PORT, kafs.VL_SERVICE,
key, security)
try:
ret = kafs.VL_Probe(z_conn)
def open_volume_server(self, server, params=None):
key, security = self.determine_security(params)
- verbose("Trying ", server.addr(), "\n")
- vol_conn = kafs.rx_new_connection(server.addr(),
+ verbose("Trying volserver ", server, "\n")
+ vol_conn = kafs.rx_new_connection(str(server.addr()),
kafs.VOLSERVICE_PORT,
kafs.VOLSERVICE_ID,
key, security)
def open_bos_server(self, server, params=None):
key, security = self.determine_security(params)
- verbose("Trying ", server.addr(), "\n")
- bos_conn = kafs.rx_new_connection(server.addr(),
+ verbose("Trying bosserver ", server, "\n")
+ bos_conn = kafs.rx_new_connection(str(server.addr()),
kafs.BOSSERVICE_PORT,
kafs.BOSSERVICE_ID,
key, security)