summaryrefslogtreecommitdiff
path: root/src/VBox/NetworkServices/NAT/RTWinPoll.cpp
blob: bff30506afd87e7b3a7e259d34f2aa9d6c0af1d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* -*- indent-tabs-mode: nil; -*- */
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/cdefs.h>
#include <iprt/err.h>
#include <iprt/string.h>

#include <VBox/err.h>

#include <Winsock2.h>
#include <Windows.h>
#include "winpoll.h"

static HANDLE g_hNetworkEvent;

int
RTWinPoll(struct pollfd *pFds, unsigned int nfds, int timeout, int *pNready)
{
    AssertPtrReturn(pFds, VERR_INVALID_PARAMETER);

    if (g_hNetworkEvent == WSA_INVALID_EVENT)
    {
        g_hNetworkEvent = WSACreateEvent();
        AssertReturn(g_hNetworkEvent != WSA_INVALID_EVENT, VERR_INTERNAL_ERROR);
    }

    for (unsigned int i = 0; i < nfds; ++i)
    {
        long eventMask = 0;
        short pollEvents = pFds[i].events;

        /* clean revents */
        pFds[i].revents = 0;

        /* ignore invalid sockets */
        if (pFds[i].fd == INVALID_SOCKET)
          continue;

        /**
         * POLLIN         Data other than high priority data may be read without blocking.
         * This is equivalent to ( POLLRDNORM | POLLRDBAND ).
         * POLLRDBAND     Priority data may be read without blocking.
         * POLLRDNORM     Normal data may be read without blocking.
         */
        if (pollEvents & POLLIN)
            eventMask |= FD_READ | FD_ACCEPT;

        /**
         * POLLOUT        Normal data may be written without blocking.  This is equivalent
         *  to POLLWRNORM.
         * POLLWRNORM     Normal data may be written without blocking.
         */
        if (pollEvents & POLLOUT)
            eventMask |= FD_WRITE | FD_CONNECT;

        /**
         * This is "moral" equivalent to POLLHUP.
         */
        eventMask |= FD_CLOSE;
        WSAEventSelect(pFds[i].fd, g_hNetworkEvent, eventMask);
    }

    DWORD index = WSAWaitForMultipleEvents(1,
                                           &g_hNetworkEvent,
                                           FALSE,
                                           timeout == RT_INDEFINITE_WAIT ? WSA_INFINITE : timeout,
                                           FALSE);
    if (index != WSA_WAIT_EVENT_0)
    {
        if (index == WSA_WAIT_TIMEOUT)
            return VERR_TIMEOUT;
    }

    int nready = 0;
    for (unsigned int i = 0; i < nfds; ++i)
    {
        short revents = 0;
        WSANETWORKEVENTS NetworkEvents;
        int err;

        if (pFds[i].fd == INVALID_SOCKET)
            continue;

        RT_ZERO(NetworkEvents);

        err = WSAEnumNetworkEvents(pFds[i].fd,
                                   g_hNetworkEvent,
                                   &NetworkEvents);

        if (err == SOCKET_ERROR)
        {
            if (WSAGetLastError() == WSAENOTSOCK)
            {
                pFds[i].revents = POLLNVAL;
                ++nready;
            }
            continue;
        }

        /* deassociate socket with event */
        WSAEventSelect(pFds[i].fd, g_hNetworkEvent, 0);

        if (NetworkEvents.lNetworkEvents & (FD_READ|FD_ACCEPT))
        {
            if (   NetworkEvents.iErrorCode[FD_READ_BIT] != 0
                || NetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
                revents |= POLLERR;

            revents |= POLLIN;
        }

        if (NetworkEvents.lNetworkEvents & (FD_WRITE|FD_CONNECT))
        {
            if (   NetworkEvents.iErrorCode[FD_WRITE_BIT] != 0
                || NetworkEvents.iErrorCode[FD_CONNECT_BIT] != 0)
                revents |= POLLERR;

            revents |= POLLOUT;
        }

        if (NetworkEvents.lNetworkEvents & FD_CLOSE)
        {
            if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
                revents |= POLLERR;

            revents |= POLLHUP;
        }

        /* paranoid */
        revents &= (pFds[i].events | POLLHUP | POLLERR);
        if (revents != 0)
        {
            pFds[i].revents = revents;
            ++nready;
        }
    }
    WSAResetEvent(g_hNetworkEvent);

    if (pNready)
      *pNready = nready;

    return VINF_SUCCESS;
}