Commit 1a0ad6c1 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Win32: fix poll() with more than 64 sockets

parent 899a6313
...@@ -70,23 +70,37 @@ int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout) ...@@ -70,23 +70,37 @@ int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
(void)fds; (void)nfds; (void)timeout; (void)fds; (void)nfds; (void)timeout;
abort (); abort ();
} }
#else /* !HAVE_POLL */
#elif defined (WIN32)
#include <string.h> #include <string.h>
#include <errno.h>
int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout) int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
{ {
fd_set rdset, wrset, exset; size_t setsize = sizeof (fd_set) + (nfds - FD_SETSIZE) * sizeof (SOCKET);
fd_set *rdset = malloc (setsize);
fd_set *wrset = malloc (setsize);
fd_set *exset = malloc (setsize);
struct timeval tv = { 0, 0 }; struct timeval tv = { 0, 0 };
int val; int val;
if (unlikely(rdset == NULL || wrset == NULL || exset == NULL))
{
free (rdset);
free (wrset);
free (exset);
errno = ENOMEM;
return -1;
}
resume: resume:
val = -1; val = -1;
vlc_testcancel (); vlc_testcancel ();
FD_ZERO (&rdset); FD_ZERO (rdset);
FD_ZERO (&wrset); FD_ZERO (wrset);
FD_ZERO (&exset); FD_ZERO (exset);
for (unsigned i = 0; i < nfds; i++) for (unsigned i = 0; i < nfds; i++)
{ {
int fd = fds[i].fd; int fd = fds[i].fd;
...@@ -94,23 +108,22 @@ resume: ...@@ -94,23 +108,22 @@ resume:
val = fd; val = fd;
/* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
* bigger or equal than FD_SETSIZE. That is one of the reasons why VLC * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
* uses poll() rather than select(). Most POSIX systems implement * uses poll() rather than select(). Most POSIX systems implement
* fd_set has a bit field with no sanity checks. This is especially bad * fd_set has a bit field with no sanity checks. This is especially bad
* on systems (such as BSD) that have no process open files limit by * on systems (such as BSD) that have no process open files limit by
* default, such that it is quite feasible to get fd >= FD_SETSIZE. * default, such that it is quite feasible to get fd >= FD_SETSIZE.
* The next instructions will result in a buffer overflow if run on * The next instructions will result in a buffer overflow if run on
* a POSIX system, and the later FD_ISSET will do undefined memory * a POSIX system, and the later FD_ISSET would perform an undefined
* access. * memory read.
* *
* With Winsock, fd_set is a table of integers. This is awfully slow. * With Winsock, fd_set is a table of integers. This is awfully slow.
* However, FD_SET and FD_ISSET silently and safely discard * However, FD_SET and FD_ISSET silently and safely discard excess
* overflows. If it happens we will loose socket events. Note that * entries. Here, overflow cannot happen anyway: fd_set of adequate
* most (if not all) Winsock SOCKET handles are actually bigger than * size are allocated.
* FD_SETSIZE in terms of absolute value - they are not POSIX file * Note that Vista has a much nicer WSAPoll(), but Mingw does not
* descriptors. From Vista, there is a much nicer WSAPoll(), but Mingw * support it yet.
* is yet to support it. */
*/
if (fds[i].events & POLLIN) if (fds[i].events & POLLIN)
FD_SET ((SOCKET)fd, rdset); FD_SET ((SOCKET)fd, rdset);
if (fds[i].events & POLLOUT) if (fds[i].events & POLLOUT)
...@@ -135,7 +148,7 @@ resume: ...@@ -135,7 +148,7 @@ resume:
tv.tv_usec = d.rem * 1000; tv.tv_usec = d.rem * 1000;
} }
val = select (val + 1, &rdset, &wrset, &exset, val = select (val + 1, rdset, wrset, exset,
/*(timeout >= 0) ?*/ &tv /*: NULL*/); /*(timeout >= 0) ?*/ &tv /*: NULL*/);
#ifndef HAVE_ALERTABLE_SELECT #ifndef HAVE_ALERTABLE_SELECT
...@@ -154,10 +167,16 @@ resume: ...@@ -154,10 +167,16 @@ resume:
for (unsigned i = 0; i < nfds; i++) for (unsigned i = 0; i < nfds; i++)
{ {
int fd = fds[i].fd; int fd = fds[i].fd;
fds[i].revents = (FD_ISSET (fd, &rdset) ? POLLIN : 0) fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
| (FD_ISSET (fd, &wrset) ? POLLOUT : 0) | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
| (FD_ISSET (fd, &exset) ? POLLPRI : 0); | (FD_ISSET (fd, exset) ? POLLPRI : 0);
} }
free (exset);
free (wrset);
free (rdset);
return val; return val;
} }
#endif /* !HAVE_POLL */
#else
# error poll() not implemented!
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment