jsonrpc client: rework connect functions

Rework connect functions for upcoming non-blocking mode. As we are here
make some variables names shorter and more descriptive.


Change-Id: Ifcba24fc16f0931261067f6c2bf64eef450d0ec9
Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/429912
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
This commit is contained in:
Pawel Wodkowski 2018-10-18 15:23:22 +02:00 committed by Jim Harris
parent e6c30818e3
commit edf2305dee
2 changed files with 56 additions and 51 deletions

View File

@ -210,13 +210,13 @@ void spdk_jsonrpc_end_request(struct spdk_jsonrpc_client_request *request,
/** /**
* Connect to the specified RPC server. * Connect to the specified RPC server.
* *
* \param rpc_sock_addr RPC socket address. * \param addr RPC socket address.
* \param addr_family Protocol families of address. * \param addr_family Protocol families of address.
* *
* \return JSON-RPC client on success, NULL on failure. * \return JSON-RPC client on success, NULL on failure and errno set to indicate
* the cause of the error.
*/ */
struct spdk_jsonrpc_client *spdk_jsonrpc_client_connect(const char *rpc_sock_addr, struct spdk_jsonrpc_client *spdk_jsonrpc_client_connect(const char *addr, int addr_family);
int addr_family);
/** /**
* Close JSON-RPC connection and free \c client object. * Close JSON-RPC connection and free \c client object.

View File

@ -38,75 +38,73 @@
static int _spdk_jsonrpc_client_poll(struct spdk_jsonrpc_client *client); static int _spdk_jsonrpc_client_poll(struct spdk_jsonrpc_client *client);
static struct spdk_jsonrpc_client * static int
_spdk_jsonrpc_client_connect(int domain, int protocol, _spdk_jsonrpc_client_connect(struct spdk_jsonrpc_client *client, int domain, int protocol,
struct sockaddr *server_addr, socklen_t addrlen) struct sockaddr *server_addr, socklen_t addrlen)
{ {
struct spdk_jsonrpc_client *client;
int rc; int rc;
client = calloc(1, sizeof(struct spdk_jsonrpc_client));
if (client == NULL) {
return NULL;
}
client->sockfd = socket(domain, SOCK_STREAM, protocol); client->sockfd = socket(domain, SOCK_STREAM, protocol);
if (client->sockfd < 0) { if (client->sockfd < 0) {
rc = errno;
SPDK_ERRLOG("socket() failed\n"); SPDK_ERRLOG("socket() failed\n");
free(client); return -rc;
return NULL;
} }
rc = connect(client->sockfd, server_addr, addrlen); rc = connect(client->sockfd, server_addr, addrlen);
if (rc != 0) { if (rc != 0) {
SPDK_ERRLOG("could not connet JSON-RPC server: %s\n", spdk_strerror(errno)); SPDK_ERRLOG("could not connect to JSON-RPC server: %s\n", spdk_strerror(errno));
close(client->sockfd); goto err;
free(client);
return NULL;
} }
return client; return 0;
err:
close(client->sockfd);
client->sockfd = -1;
return -rc;
} }
struct spdk_jsonrpc_client * struct spdk_jsonrpc_client *
spdk_jsonrpc_client_connect(const char *rpc_sock_addr, int addr_family) spdk_jsonrpc_client_connect(const char *addr, int addr_family)
{ {
struct spdk_jsonrpc_client *client; struct spdk_jsonrpc_client *client = calloc(1, sizeof(struct spdk_jsonrpc_client));
/* Unix Domain Socket */
struct sockaddr_un addr_un = {};
char *add_in = NULL;
int rc;
if (client == NULL) {
SPDK_ERRLOG("%s\n", spdk_strerror(errno));
return NULL;
}
if (addr_family == AF_UNIX) { if (addr_family == AF_UNIX) {
/* Unix Domain Socket */ addr_un.sun_family = AF_UNIX;
struct sockaddr_un rpc_sock_addr_unix = {}; rc = snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), "%s", addr);
int rc; if (rc < 0 || (size_t)rc >= sizeof(addr_un.sun_path)) {
rc = -EINVAL;
rpc_sock_addr_unix.sun_family = AF_UNIX;
rc = snprintf(rpc_sock_addr_unix.sun_path,
sizeof(rpc_sock_addr_unix.sun_path),
"%s", rpc_sock_addr);
if (rc < 0 || (size_t)rc >= sizeof(rpc_sock_addr_unix.sun_path)) {
SPDK_ERRLOG("RPC Listen address Unix socket path too long\n"); SPDK_ERRLOG("RPC Listen address Unix socket path too long\n");
return NULL; goto err;
} }
client = _spdk_jsonrpc_client_connect(AF_UNIX, 0, rc = _spdk_jsonrpc_client_connect(client, AF_UNIX, 0, (struct sockaddr *)&addr_un, sizeof(addr_un));
(struct sockaddr *)&rpc_sock_addr_unix,
sizeof(rpc_sock_addr_unix));
} else { } else {
/* TCP/IP socket */ /* TCP/IP socket */
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *res; struct addrinfo *res;
char *tmp;
char *host, *port; char *host, *port;
tmp = strdup(rpc_sock_addr); add_in = strdup(addr);
if (!tmp) { if (!add_in) {
SPDK_ERRLOG("Out of memory\n"); rc = -errno;
return NULL; SPDK_ERRLOG("%s\n", spdk_strerror(errno));
goto err;
} }
if (spdk_parse_ip_addr(tmp, &host, &port) < 0) { rc = spdk_parse_ip_addr(add_in, &host, &port);
free(tmp); if (rc) {
SPDK_ERRLOG("Invalid listen address '%s'\n", rpc_sock_addr); SPDK_ERRLOG("Invalid listen address '%s'\n", addr);
return NULL; goto err;
} }
if (port == NULL) { if (port == NULL) {
@ -118,19 +116,26 @@ spdk_jsonrpc_client_connect(const char *rpc_sock_addr, int addr_family)
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(host, port, &hints, &res) != 0) { rc = getaddrinfo(host, port, &hints, &res);
free(tmp); if (rc != 0) {
SPDK_ERRLOG("Unable to look up RPC connnect address '%s'\n", rpc_sock_addr); SPDK_ERRLOG("Unable to look up RPC connnect address '%s' (%d): %s\n", addr, rc, gai_strerror(rc));
return NULL; rc = -EINVAL;
goto err;
} }
client = _spdk_jsonrpc_client_connect(res->ai_family, res->ai_protocol, rc = _spdk_jsonrpc_client_connect(client, res->ai_family, res->ai_protocol, res->ai_addr,
res->ai_addr, res->ai_addrlen); res->ai_addrlen);
freeaddrinfo(res); freeaddrinfo(res);
free(tmp);
} }
err:
if (rc != 0) {
free(client);
client = NULL;
errno = -rc;
}
free(add_in);
return client; return client;
} }