Skip to content

Commit 2d53dce

Browse files
committed
Detect when disconnected
Sometimes when a connection is closed unceremoniously we cannot tell until we try to read. This allows us to set an error the rest of the library will understand so that the disconnect is properly detected.
1 parent 090b10c commit 2d53dce

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/tls_openssl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#ifndef _WIN32
2121
#include <sys/select.h>
22+
#include <sys/socket.h>
2223
#else
2324
#include <winsock2.h>
2425
#endif
@@ -963,6 +964,17 @@ int tls_read(struct conn_interface *intf, void *buff, size_t len)
963964
ret = SSL_read(tls->ssl, buff, len);
964965
_tls_set_error(tls, ret <= 0 ? SSL_get_error(tls->ssl, ret) : 0);
965966

967+
#ifndef _WIN32
968+
if (tls->lasterror == SSL_ERROR_WANT_READ) {
969+
char c;
970+
ssize_t n = recv(intf->conn->sock, &c, 1, MSG_PEEK);
971+
if (n < 0 && errno == ENOTCONN) {
972+
strophe_debug(tls->ctx, "tls", "WANT_READ but connection is closed");
973+
_tls_set_error(tls, SSL_ERROR_SYSCALL); // Set a more appropriate error
974+
}
975+
}
976+
#endif
977+
966978
return ret;
967979
}
968980

0 commit comments

Comments
 (0)