1
1
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
Этот коммит содержится в:
Andreas Schneider 2018-11-21 17:10:15 +01:00
родитель fc840d8d69
Коммит 4512a3fead

Просмотреть файл

@ -102,6 +102,7 @@ typedef int (*poll_fn)(ssh_pollfd_t *, nfds_t, int);
static poll_fn ssh_poll_emu; static poll_fn ssh_poll_emu;
#include <sys/types.h> #include <sys/types.h>
#include <stdbool.h>
#ifdef _WIN32 #ifdef _WIN32
#ifndef STRICT #ifndef STRICT
@ -125,6 +126,27 @@ static poll_fn ssh_poll_emu;
#include <unistd.h> #include <unistd.h>
#endif #endif
static bool bsd_socket_disconnected(int sock_err)
{
switch (sock_err) {
#ifdef _WIN32
case WSAECONNABORTED:
case WSAECONNRESET:
case WSAENETRESET:
case WSAESHUTDOWN:
#else
case ECONNABORTED:
case ECONNRESET:
case ENETRESET:
case ESHUTDOWN:
#endif
return true;
default:
return false;
}
return false;
}
/* /*
* This is a poll(2)-emulation using select for systems not providing a native * This is a poll(2)-emulation using select for systems not providing a native
@ -135,9 +157,10 @@ static poll_fn ssh_poll_emu;
* a value as high as 1024 on Linux you'll pay dearly in every single call. * a value as high as 1024 on Linux you'll pay dearly in every single call.
* poll() will be orders of magnitude faster. * poll() will be orders of magnitude faster.
*/ */
static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) { static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout)
{
fd_set readfds, writefds, exceptfds; fd_set readfds, writefds, exceptfds;
struct timeval tv, *ptv; struct timeval tv, *ptv = NULL;
socket_t max_fd; socket_t max_fd;
int rc; int rc;
nfds_t i; nfds_t i;
@ -147,9 +170,9 @@ static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
return -1; return -1;
} }
FD_ZERO (&readfds); FD_ZERO(&readfds);
FD_ZERO (&writefds); FD_ZERO(&writefds);
FD_ZERO (&exceptfds); FD_ZERO(&exceptfds);
/* compute fd_sets and find largest descriptor */ /* compute fd_sets and find largest descriptor */
for (rc = -1, max_fd = 0, i = 0; i < nfds; i++) { for (rc = -1, max_fd = 0, i = 0; i < nfds; i++) {
@ -199,12 +222,12 @@ static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
} }
} }
rc = select (max_fd + 1, &readfds, &writefds, &exceptfds, ptv); rc = select(max_fd + 1, &readfds, &writefds, &exceptfds, ptv);
if (rc < 0) { if (rc < 0) {
return -1; return -1;
} }
for (rc = 0, i = 0; i < nfds; i++) for (rc = 0, i = 0; i < nfds; i++) {
if (fds[i].fd >= 0) { if (fds[i].fd >= 0) {
fds[i].revents = 0; fds[i].revents = 0;
@ -215,15 +238,7 @@ static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
/* support for POLLHUP */ /* support for POLLHUP */
ret = recv(fds[i].fd, data, 64, MSG_PEEK); ret = recv(fds[i].fd, data, 64, MSG_PEEK);
#ifdef _WIN32 if ((ret == -1) && bsd_socket_disconnected(errno)) {
if ((ret == -1) &&
(errno == WSAESHUTDOWN || errno == WSAECONNRESET ||
errno == WSAECONNABORTED || errno == WSAENETRESET)) {
#else
if ((ret == -1) &&
(errno == ESHUTDOWN || errno == ECONNRESET ||
errno == ECONNABORTED || errno == ENETRESET)) {
#endif
fds[i].revents |= POLLHUP; fds[i].revents |= POLLHUP;
} else { } else {
fds[i].revents |= fds[i].events & (POLLIN | POLLRDNORM); fds[i].revents |= fds[i].events & (POLLIN | POLLRDNORM);
@ -245,6 +260,7 @@ static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
} else { } else {
fds[i].revents = POLLNVAL; fds[i].revents = POLLNVAL;
} }
}
return rc; return rc;
} }