Commit b54e99bb authored by Jacek Kalwas's avatar Jacek Kalwas Committed by Tomasz Zawadzki
Browse files

sock/posix: move sock alloc up in connect



This helps with common error handling.

From SSL_[CTX_]free man
"If [ctx|ssl] is NULL nothing is done."

Change-Id: I8214a4b8f57e729dd9b86beab51eb7fe350ae671
Signed-off-by: default avatarJacek Kalwas <jacek.kalwas@nutanix.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/26005


Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz@tzawadzki.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
parent b017a9db
Loading
Loading
Loading
Loading
+34 −41
Original line number Diff line number Diff line
@@ -928,9 +928,7 @@ _posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts, bool
	struct spdk_sock_impl_opts impl_opts;
	struct spdk_posix_sock *sock;
	struct addrinfo *res0;
	int rc, fd = -1;
	SSL_CTX *ctx = 0;
	SSL *ssl = 0;
	int rc;

	assert(opts != NULL);
	if (enable_ssl) {
@@ -944,16 +942,21 @@ _posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts, bool
		return NULL;
	}

	sock = posix_sock_alloc(-1, &impl_opts);
	if (!sock) {
		return NULL;
	}

	for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) {
		fd = spdk_sock_posix_fd_create(res, opts, &impl_opts);
		if (fd < 0) {
		sock->fd = spdk_sock_posix_fd_create(res, opts, &impl_opts);
		if (sock->fd < 0) {
			continue;
		}

		rc = spdk_sock_posix_fd_connect(fd, res, opts);
		rc = spdk_sock_posix_fd_connect(sock->fd, res, opts);
		if (rc != 0) {
			close(fd);
			fd = -1;
			close(sock->fd);
			sock->fd = -1;
			if (rc == 1) {
				continue;
			} else {
@@ -965,55 +968,45 @@ _posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts, bool
	}

	freeaddrinfo(res0);
	if (fd < 0) {
		return NULL;
	if (sock->fd < 0) {
		goto err;
	}

	if (enable_ssl) {
		ctx = posix_sock_create_ssl_context(TLS_client_method(), opts, &impl_opts);
		if (!ctx) {
		sock->ctx = posix_sock_create_ssl_context(TLS_client_method(), opts, &impl_opts);
		if (!sock->ctx) {
			SPDK_ERRLOG("posix_sock_create_ssl_context() failed, errno = %d\n", errno);
			close(fd);
			return NULL;
			goto err;
		}

		ssl = ssl_sock_setup_connect(ctx, fd);
		if (!ssl) {
		sock->ssl = ssl_sock_setup_connect(sock->ctx, sock->fd);
		if (!sock->ssl) {
			SPDK_ERRLOG("ssl_sock_setup_connect() failed, errno = %d\n", errno);
			SSL_CTX_free(ctx);
			close(fd);
			return NULL;
		}
	}

	if (spdk_fd_set_nonblock(fd)) {
		SSL_free(ssl);
		SSL_CTX_free(ctx);
		close(fd);
		return NULL;
	}

	sock = posix_sock_alloc(fd, &impl_opts);
	if (sock == NULL) {
		SSL_free(ssl);
		SSL_CTX_free(ctx);
		close(fd);
		return NULL;
			goto err;
		}

	if (ctx) {
		sock->ctx = ctx;
		SSL_set_app_data(sock->ssl, &sock->base.impl_opts);
	}

	if (ssl) {
		sock->ssl = ssl;
		SSL_set_app_data(ssl, &sock->base.impl_opts);
	if (spdk_fd_set_nonblock(sock->fd)) {
		goto err;
	}

	/* Only enable zero copy for non-loopback and non-ssl sockets. */
	posix_sock_init(sock, opts->zcopy && !spdk_net_is_loopback(fd) && !enable_ssl &&
	posix_sock_init(sock, opts->zcopy && !spdk_net_is_loopback(sock->fd) && !enable_ssl &&
			impl_opts.enable_zerocopy_send_client);
	return &sock->base;

err:
	/* It is safe to pass NULL to SSL free functions. */
	SSL_free(sock->ssl);
	SSL_CTX_free(sock->ctx);
	if (sock->fd != -1) {
		close(sock->fd);
	}

	free(sock);
	return NULL;
}

static struct spdk_sock *