From 95f3eb2980ee42339bb81dfd2d0ee3bcccd2d271 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Wed, 5 Sep 2018 14:45:10 +0800
Subject: [PATCH] plat: ENOTCONN

---
 lib/plat/esp32/esp32-sockets.c     |  2 +-
 lib/plat/esp32/private.h           |  1 +
 lib/plat/optee/lws-plat-optee.c    |  2 +-
 lib/plat/optee/private.h           |  1 +
 lib/plat/unix/private.h            |  1 +
 lib/plat/unix/unix-sockets.c       | 12 ++++++------
 lib/plat/windows/private.h         |  1 +
 lib/plat/windows/windows-sockets.c |  3 ++-
 lib/tls/mbedtls/ssl.c              |  2 +-
 lib/tls/openssl/ssl.c              |  4 ++--
 10 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/lib/plat/esp32/esp32-sockets.c b/lib/plat/esp32/esp32-sockets.c
index 67b39a82..7a592d79 100644
--- a/lib/plat/esp32/esp32-sockets.c
+++ b/lib/plat/esp32/esp32-sockets.c
@@ -82,7 +82,7 @@ lws_plat_check_connection_error(struct lws *wsi)
 
 
 int
-lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
+lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
 {
 	int optval = 1;
 	socklen_t optlen = sizeof(optval);
diff --git a/lib/plat/esp32/private.h b/lib/plat/esp32/private.h
index 574c9237..3d32d446 100644
--- a/lib/plat/esp32/private.h
+++ b/lib/plat/esp32/private.h
@@ -49,6 +49,7 @@
  #define LWS_EINPROGRESS EINPROGRESS
  #define LWS_EINTR EINTR
  #define LWS_EISCONN EISCONN
+ #define LWS_ENOTCONN ENOTCONN
  #define LWS_EWOULDBLOCK EWOULDBLOCK
 
  #define lws_set_blocking_send(wsi)
diff --git a/lib/plat/optee/lws-plat-optee.c b/lib/plat/optee/lws-plat-optee.c
index f1000182..84d78bd1 100644
--- a/lib/plat/optee/lws-plat-optee.c
+++ b/lib/plat/optee/lws-plat-optee.c
@@ -202,7 +202,7 @@ lws_plat_service(struct lws_context *context, int timeout_ms)
 }
 
 int
-lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
+lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
 {
 	return 0;
 }
diff --git a/lib/plat/optee/private.h b/lib/plat/optee/private.h
index 6a49f362..85137c44 100644
--- a/lib/plat/optee/private.h
+++ b/lib/plat/optee/private.h
@@ -55,6 +55,7 @@
  #define LWS_EINPROGRESS EINPROGRESS
  #define LWS_EINTR EINTR
  #define LWS_EISCONN EISCONN
+ #define LWS_ENOTCONN ENOTCONN
  #define LWS_EWOULDBLOCK EWOULDBLOCK
 
  #define lws_set_blocking_send(wsi)
diff --git a/lib/plat/unix/private.h b/lib/plat/unix/private.h
index b9e8bfa2..fcd05356 100644
--- a/lib/plat/unix/private.h
+++ b/lib/plat/unix/private.h
@@ -125,6 +125,7 @@
 #define LWS_EINPROGRESS EINPROGRESS
 #define LWS_EINTR EINTR
 #define LWS_EISCONN EISCONN
+#define LWS_ENOTCONN ENOTCONN
 #define LWS_EWOULDBLOCK EWOULDBLOCK
 #define lws_set_blocking_send(wsi)
 #define LWS_SOCK_INVALID (-1)
diff --git a/lib/plat/unix/unix-sockets.c b/lib/plat/unix/unix-sockets.c
index b423eaa1..bd4c4a6d 100644
--- a/lib/plat/unix/unix-sockets.c
+++ b/lib/plat/unix/unix-sockets.c
@@ -66,7 +66,7 @@ lws_send_pipe_choked(struct lws *wsi)
 
 
 int
-lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
+lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
 {
 	int optval = 1;
 	socklen_t optlen = sizeof(optval);
@@ -81,7 +81,7 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
 
 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
 
-	if (vhost->ka_time) {
+	if (!unix_skt && vhost->ka_time) {
 		/* enable keepalive on this socket */
 		optval = 1;
 		if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
@@ -126,7 +126,7 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
 	}
 
 #if defined(SO_BINDTODEVICE)
-	if (vhost->bind_iface && vhost->iface) {
+	if (!unix_skt && vhost->bind_iface && vhost->iface) {
 		lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
 		if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
 				strlen(vhost->iface)) < 0) {
@@ -139,18 +139,18 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
 	/* Disable Nagle */
 	optval = 1;
 #if defined (__sun) || defined(__QNX__)
-	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
+	if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
 		return 1;
 #elif !defined(__APPLE__) && \
       !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
       !defined(__NetBSD__) && \
       !defined(__OpenBSD__) && \
       !defined(__HAIKU__)
-	if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
+	if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
 		return 1;
 #else
 	tcp_proto = getprotobyname("TCP");
-	if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
+	if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
 		return 1;
 #endif
 
diff --git a/lib/plat/windows/private.h b/lib/plat/windows/private.h
index 185f688c..591a6836 100644
--- a/lib/plat/windows/private.h
+++ b/lib/plat/windows/private.h
@@ -39,6 +39,7 @@
  #define LWS_EINPROGRESS WSAEINPROGRESS
  #define LWS_EINTR WSAEINTR
  #define LWS_EISCONN WSAEISCONN
+ #define LWS_ENOTCONN WSAENOTCONN
  #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
  #define MSG_NOSIGNAL 0
  #define SHUT_RDWR SD_BOTH
diff --git a/lib/plat/windows/windows-sockets.c b/lib/plat/windows/windows-sockets.c
index 99d9f6a0..c168a52d 100644
--- a/lib/plat/windows/windows-sockets.c
+++ b/lib/plat/windows/windows-sockets.c
@@ -41,7 +41,8 @@ lws_poll_listen_fd(struct lws_pollfd *fd)
 }
 
 int
-lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
+lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
+			    int unix_skt)
 {
 	int optval = 1;
 	int optlen = sizeof(optval);
diff --git a/lib/tls/mbedtls/ssl.c b/lib/tls/mbedtls/ssl.c
index e0e134c5..5ff0d870 100644
--- a/lib/tls/mbedtls/ssl.c
+++ b/lib/tls/mbedtls/ssl.c
@@ -69,7 +69,7 @@ lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
 	errno = 0;
 	n = SSL_read(wsi->tls.ssl, buf, len);
 #if defined(LWS_WITH_ESP32)
-	if (!n && errno == ENOTCONN) {
+	if (!n && errno == LWS_ENOTCONN) {
 		lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
 		return LWS_SSL_CAPABLE_ERROR;
 	}
diff --git a/lib/tls/openssl/ssl.c b/lib/tls/openssl/ssl.c
index 59250fbc..2af4a7af 100644
--- a/lib/tls/openssl/ssl.c
+++ b/lib/tls/openssl/ssl.c
@@ -210,7 +210,7 @@ lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
 	errno = 0;
 	n = SSL_read(wsi->tls.ssl, buf, len);
 #if defined(LWS_WITH_ESP32)
-	if (!n && errno == ENOTCONN) {
+	if (!n && errno == LWS_ENOTCONN) {
 		lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
 		return LWS_SSL_CAPABLE_ERROR;
 	}
@@ -227,7 +227,7 @@ lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
 
 	lwsl_debug("%p: SSL_read says %d\n", wsi, n);
 	/* manpage: returning 0 means connection shut down */
-	if (!n || (n == -1 && errno == ENOTCONN)) {
+	if (!n || (n == -1 && errno == LWS_ENOTCONN)) {
 		wsi->socket_is_permanently_unusable = 1;
 
 		return LWS_SSL_CAPABLE_ERROR;
-- 
GitLab