diff options
author | Andy Grover <agrover@redhat.com> | 2012-12-19 17:59:49 -0800 |
---|---|---|
committer | Andy Grover <agrover@redhat.com> | 2012-12-19 17:59:49 -0800 |
commit | a49bc18e4fc2fe4f0448bb8c00e055d67614a5cb (patch) | |
tree | 590ab89efe7e8ab139f503e92685fb55c16992da | |
parent | e41257b0460158a4b9723d2284bd8cb5e638c482 (diff) | |
download | rtslib-fb-a49bc18e4fc2fe4f0448bb8c00e055d67614a5cb.tar.gz |
Remove redundant checks for ip address validity
Remove list_eth_ips, is_ipv4_address, and is_ipv6_address from utils.
The caller is responsible for passing valid ipv4 or ipv6 addresses (ipv6
should be enclosed in '[]'.) If the address is not valid, creating the
configfs node will fail, so we don't need to check.
Modify target.py to handle v4 or v6 addresses cleanly.
Signed-off-by: Andy Grover <agrover@redhat.com>
-rw-r--r-- | rtslib/target.py | 30 | ||||
-rw-r--r-- | rtslib/utils.py | 41 |
2 files changed, 9 insertions, 62 deletions
diff --git a/rtslib/target.py b/rtslib/target.py index 8749a73..c0b2765 100644 --- a/rtslib/target.py +++ b/rtslib/target.py @@ -28,7 +28,6 @@ from node import CFSNode from os.path import isdir from doctest import testmod from utils import RTSLibError, RTSLibBrokenLink, modprobe -from utils import is_ipv6_address, is_ipv4_address from utils import fread, fwrite, generate_wwn, is_valid_wwn from utils import dict_remove, set_attributes, set_parameters @@ -924,7 +923,8 @@ class NetworkPortal(CFSNode): ''' @param parent_tpg: The parent TPG object. @type parent_tpg: TPG - @param ip_address: The ipv4 IP address of the NetworkPortal. + @param ip_address: The ipv4/v6 IP address of the NetworkPortal. ipv6 + addresses should be surrounded by '[]'. @type ip_address: string @param port: The optional (defaults to 3260) NetworkPortal TCP/IP port. @type port: int @@ -937,10 +937,8 @@ class NetworkPortal(CFSNode): @return: A NetworkPortal object. ''' super(NetworkPortal, self).__init__() - if not (is_ipv4_address(ip_address) or is_ipv6_address(ip_address)): - raise RTSLibError("Invalid IP address: %s" % ip_address) - else: - self._ip_address = str(ip_address) + + self._ip_address = str(ip_address) try: self._port = int(port) @@ -952,12 +950,9 @@ class NetworkPortal(CFSNode): else: raise RTSLibError("Invalid parent TPG.") - if is_ipv4_address(ip_address): - self._path = "%s/np/%s:%d" \ - % (self.parent_tpg.path, self.ip_address, self.port) - else: - self._path = "%s/np/[%s]:%d" \ - % (self.parent_tpg.path, self.ip_address, self.port) + self._path = "%s/np/%s:%d" \ + % (self.parent_tpg.path, self.ip_address, self.port) + try: self._create_in_cfs_ine(mode) except OSError, msg: @@ -1061,15 +1056,8 @@ class TPG(CFSNode): return for network_portal_dir in os.listdir("%s/np" % self.path): - if network_portal_dir.startswith('['): - # IPv6 portals are [IPv6]:PORT - (ip_address, port) = \ - os.path.basename(network_portal_dir)[1:].split("]") - port = port[1:] - else: - # IPv4 portals are IPv4:PORT - (ip_address, port) = \ - os.path.basename(network_portal_dir).split(":") + (ip_address, port) = \ + os.path.basename(network_portal_dir).rsplit(":", 1) port = int(port) yield NetworkPortal(self, ip_address, port, 'lookup') diff --git a/rtslib/utils.py b/rtslib/utils.py index 45d1699..a8e8e2a 100644 --- a/rtslib/utils.py +++ b/rtslib/utils.py @@ -23,8 +23,6 @@ import stat import uuid import glob import socket -import ipaddr -import ethtool import subprocess class RTSLibError(Exception): @@ -563,45 +561,6 @@ def set_parameters(obj, param_dict): # Setting some parameters may return an error, before kernel 3.3 pass - -def list_eth_ips(ifnames=None): - ''' - List the IPv4 and IPv6 non-loopback, non link-local addresses (in the - RFC3330 sense, not addresses attached to lo) of a list of ethernet - interfaces from the SIOCGIFADDR struct. If ifname is omitted, list all IPs - of all ifaces excepted for lo. - ''' - if ifnames is None: - ifnames = [name for name in ethtool.get_devices() if name != 'lo'] - devcfgs = ethtool.get_interfaces_info(ifnames) - - addrs = [] - for d in devcfgs: - if d.ipv4_address: - addrs.append(d.ipv4_address) - # For IPv6 addresses, we might have more of them on the same device, - # and only grab global (universe) addresses. - for ip6 in [a for a in d.get_ipv6_addresses() if a.scope == 'universe']: - addrs.append(ip6.address) - - return sorted(set(addrs)) - -def is_ipv4_address(addr): - try: - ipaddr.IPv4Address(addr) - except: - return False - else: - return True - -def is_ipv6_address(addr): - try: - ipaddr.IPv6Address(addr) - except: - return False - else: - return True - def _test(): '''Run the doctests''' import doctest |