net: change sock to fd in spdk_sock_create

No functional change - this just reduces an upcoming
patch that will make further changes to this function.

Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: Ic186f315b09ebadfba6120a70fac03ebb9beaffb

Reviewed-on: https://review.gerrithub.io/398856
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Ziye Yang <optimistyzy@gmail.com>
This commit is contained in:
Jim Harris 2018-02-07 13:20:51 -07:00
parent 67c9ea6ad9
commit 60104c6f9f

View File

@ -130,7 +130,7 @@ spdk_sock_create(const char *ip, int port, enum spdk_sock_create_type type)
char portnum[PORTNUMLEN]; char portnum[PORTNUMLEN];
char *p; char *p;
struct addrinfo hints, *res, *res0; struct addrinfo hints, *res, *res0;
int sock, flag; int fd, flag;
int val = 1; int val = 1;
int rc; int rc;
@ -160,35 +160,35 @@ spdk_sock_create(const char *ip, int port, enum spdk_sock_create_type type)
} }
/* try listen */ /* try listen */
sock = -1; fd = -1;
for (res = res0; res != NULL; res = res->ai_next) { for (res = res0; res != NULL; res = res->ai_next) {
retry: retry:
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock < 0) { if (fd < 0) {
/* error */ /* error */
continue; continue;
} }
rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val); rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
if (rc != 0) { if (rc != 0) {
close(sock); close(fd);
/* error */ /* error */
continue; continue;
} }
rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val); rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
if (rc != 0) { if (rc != 0) {
close(sock); close(fd);
/* error */ /* error */
continue; continue;
} }
if (type == SPDK_SOCK_CREATE_LISTEN) { if (type == SPDK_SOCK_CREATE_LISTEN) {
rc = bind(sock, res->ai_addr, res->ai_addrlen); rc = bind(fd, res->ai_addr, res->ai_addrlen);
if (rc != 0) { if (rc != 0) {
SPDK_ERRLOG("bind() failed, errno = %d\n", errno); SPDK_ERRLOG("bind() failed, errno = %d\n", errno);
switch (errno) { switch (errno) {
case EINTR: case EINTR:
/* interrupted? */ /* interrupted? */
close(sock); close(fd);
goto retry; goto retry;
case EADDRNOTAVAIL: case EADDRNOTAVAIL:
SPDK_ERRLOG("IP address %s not available. " SPDK_ERRLOG("IP address %s not available. "
@ -198,45 +198,45 @@ retry:
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
/* try next family */ /* try next family */
close(sock); close(fd);
sock = -1; fd = -1;
continue; continue;
} }
} }
/* bind OK */ /* bind OK */
rc = listen(sock, 512); rc = listen(fd, 512);
if (rc != 0) { if (rc != 0) {
SPDK_ERRLOG("listen() failed, errno = %d\n", errno); SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
close(sock); close(fd);
sock = -1; fd = -1;
break; break;
} }
} else if (type == SPDK_SOCK_CREATE_CONNECT) { } else if (type == SPDK_SOCK_CREATE_CONNECT) {
rc = connect(sock, res->ai_addr, res->ai_addrlen); rc = connect(fd, res->ai_addr, res->ai_addrlen);
if (rc != 0) { if (rc != 0) {
SPDK_ERRLOG("connect() failed, errno = %d\n", errno); SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
/* try next family */ /* try next family */
close(sock); close(fd);
sock = -1; fd = -1;
continue; continue;
} }
} }
flag = fcntl(sock, F_GETFL); flag = fcntl(fd, F_GETFL);
if (fcntl(sock, F_SETFL, flag | O_NONBLOCK) < 0) { if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", sock, errno); SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
close(sock); close(fd);
sock = -1; fd = -1;
break; break;
} }
break; break;
} }
freeaddrinfo(res0); freeaddrinfo(res0);
if (sock < 0) { if (fd < 0) {
return -1; return -1;
} }
return sock; return fd;
} }
int int