Commit 6c1a1a3d authored by Michal Berger's avatar Michal Berger Committed by Tomasz Zawadzki
Browse files

scripts/rpc: Make sure address argument is properly interpreted



In case the addr argument was not an existing unix socket file the rpc
client would consider it to be an actual ip address. As a result
connect() would be called with improper set of arguments. This could
cause the rpc.py to block for undesired amount of time until connect()
finally decided to return (seen on some fedora33 builds).

This was affecting sh wrapper functions like waitforlisten() which
use rpc.py to determine if given app is ready to be talk to blocking
execution of the tests for way too long then intendent.

To avoid such a scenario determine the format of the address and use
routines proper for given address family.

Signed-off-by: default avatarMichal Berger <michalx.berger@intel.com>
Change-Id: Iaac701d72c772629fa7c6478ff4781b0c5d485d5
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7777


Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarKarol Latecki <karol.latecki@intel.com>
parent ef68667a
Loading
Loading
Loading
Loading
+30 −13
Original line number Diff line number Diff line
@@ -14,6 +14,22 @@ def print_json(s):
    print(json.dumps(s, indent=2).strip('"'))


def get_addr_type(addr):
    try:
        socket.inet_pton(socket.AF_INET, addr)
        return socket.AF_INET
    except Exception as e:
        pass
    try:
        socket.inet_pton(socket.AF_INET6, addr)
        return socket.AF_INET6
    except Exception as e:
        pass
    if os.path.exists(addr):
        return socket.AF_UNIX
    return None


class JSONRPCException(Exception):
    def __init__(self, message):
        self.message = message
@@ -54,23 +70,24 @@ class JSONRPCClient(object):

    def _connect(self, addr, port):
        try:
            if os.path.exists(addr):
            addr_type = get_addr_type(addr)

            if addr_type == socket.AF_UNIX:
                self._logger.debug("Trying to connect to UNIX socket: %s", addr)
                self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                self.sock.connect(addr)
            elif port:
                if ':' in addr:
            elif addr_type == socket.AF_INET6:
                self._logger.debug("Trying to connect to IPv6 address addr:%s, port:%i", addr, port)
                for res in socket.getaddrinfo(addr, port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP):
                    af, socktype, proto, canonname, sa = res
                self.sock = socket.socket(af, socktype, proto)
                self.sock.connect(sa)
                else:
            elif addr_type == socket.AF_INET:
                self._logger.debug("Trying to connect to IPv4 address addr:%s, port:%i'", addr, port)
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sock.connect((addr, port))
            else:
                raise socket.error("Unix socket '%s' does not exist" % addr)
                raise socket.error("Invalid or non-existing address: '%s'" % addr)
        except socket.error as ex:
            raise JSONRPCException("Error while connecting to %s\n"
                                   "Is SPDK application running?\n"