socket.cpp revision f8e5352e74487b3be21a0171ae85ffc534f7399f
/* $Id$ */
/** @file
* IPRT - Network Sockets.
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
# include <winsock2.h>
#else /* !RT_OS_WINDOWS */
# include <errno.h>
# ifdef IPRT_WITH_TCPIP_V6
# endif
# include <netdb.h>
# include <unistd.h>
# include <fcntl.h>
#endif /* !RT_OS_WINDOWS */
#include <limits.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* non-standard linux stuff (it seems). */
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
/* Windows has different names for SHUT_XXX. */
#ifndef SHUT_RDWR
# ifdef SD_BOTH
# else
# define SHUT_RDWR 2
# endif
#endif
#ifndef SHUT_WR
# ifdef SD_SEND
# else
# define SHUT_WR 1
# endif
#endif
#ifndef SHUT_RD
# ifdef SD_RECEIVE
# define SHUT_RD SD_RECEIVE
# else
# define SHUT_RD 0
# endif
#endif
/* fixup backlevel OSes. */
#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
# define socklen_t int
#endif
/** How many pending connection. */
#define RTTCP_SERVER_BACKLOG 10
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Socket handle data.
*
* This is mainly required for implementing RTPollSet on Windows.
*/
typedef struct RTSOCKETINT
{
/** Magic number (RTSOCKET_MAGIC). */
/** Exclusive user count.
* This is used to prevent two threads from accessing the handle concurrently.
* It can be higher than 1 if this handle is reference multiple times in a
* polling set (Windows). */
/** The native socket handle. */
/** Indicates whether the handle has been closed or not. */
bool volatile fClosed;
/** Indicates whether the socket is operating in blocking or non-blocking mode
* currently. */
bool fBlocking;
#ifdef RT_OS_WINDOWS
/** The event semaphore we've associated with the socket handle.
* This is WSA_INVALID_EVENT if not done. */
/** The pollset currently polling this socket. This is NIL if no one is
* polling. */
/** The events we're polling for. */
/** The events we're currently subscribing to with WSAEventSelect.
* This is ZERO if we're currently not subscribing to anything. */
/** Saved events which are only posted once. */
#endif /* RT_OS_WINDOWS */
} RTSOCKETINT;
/**
* Address union used internally for things like getpeername and getsockname.
*/
typedef union RTSOCKADDRUNION
{
struct sockaddr_in IPv4;
#ifdef IPRT_WITH_TCPIP_V6
struct sockaddr_in6 IPv6;
#endif
/**
* Get the last error as an iprt status code.
*
* @returns IPRT status code.
*/
DECLINLINE(int) rtSocketError(void)
{
#ifdef RT_OS_WINDOWS
return RTErrConvertFromWin32(WSAGetLastError());
#else
return RTErrConvertFromErrno(errno);
#endif
}
/**
* Resets the last error.
*/
DECLINLINE(void) rtSocketErrorReset(void)
{
#ifdef RT_OS_WINDOWS
WSASetLastError(0);
#else
errno = 0;
#endif
}
/**
* Get the last resolver error as an iprt status code.
*
* @returns iprt status code.
*/
int rtSocketResolverError(void)
{
#ifdef RT_OS_WINDOWS
return RTErrConvertFromWin32(WSAGetLastError());
#else
switch (h_errno)
{
case HOST_NOT_FOUND:
return VERR_NET_HOST_NOT_FOUND;
case NO_DATA:
return VERR_NET_ADDRESS_NOT_AVAILABLE;
case NO_RECOVERY:
return VERR_IO_GEN_FAILURE;
case TRY_AGAIN:
return VERR_TRY_AGAIN;
default:
return VERR_UNRESOLVED_ERROR;
}
#endif
}
/**
* Converts from a native socket address to a generic IPRT network address.
*
* @returns IPRT status code.
* @param pSrc The source address.
* @param cbSrc The size of the source address.
* @param pAddr Where to return the generic IPRT network
* address.
*/
{
/*
* Convert the address.
*/
if ( cbSrc == sizeof(struct sockaddr_in)
{
}
#ifdef IPRT_WITH_TCPIP_V6
else if ( cbSrc == sizeof(struct sockaddr_in6)
{
}
#endif
else
return VINF_SUCCESS;
}
/**
* Converts from a generic IPRT network address to a native socket address.
*
* @returns IPRT status code.
* @param pAddr Pointer to the generic IPRT network address.
* @param pDst The source address.
* @param cbSrc The size of the source address.
* @param pcbAddr Where to store the size of the returned address.
* Optional
*/
static int rtSocketAddrFromNetAddr(PCRTNETADDR pAddr, RTSOCKADDRUNION *pDst, size_t cbDst, int *pcbAddr)
{
&& cbDst >= sizeof(struct sockaddr_in))
{
if (pcbAddr)
}
#ifdef IPRT_WITH_TCPIP_V6
&& cbDst >= sizeof(struct sockaddr_in6))
{
if (pcbAddr)
}
#endif
else
return VINF_SUCCESS;
}
/**
* Tries to lock the socket for exclusive usage by the calling thread.
*
* Call rtSocketUnlock() to unlock.
*
* @returns @c true if locked, @c false if not.
* @param pThis The socket structure.
*/
{
}
/**
* Unlocks the socket.
*
* @param pThis The socket structure.
*/
{
}
/**
* The slow path of rtSocketSwitchBlockingMode that does the actual switching.
*
* @returns IPRT status code.
* @param pThis The socket structure.
* @param fBlocking The desired mode of operation.
* @remarks Do not call directly.
*/
{
#ifdef RT_OS_WINDOWS
return rtSocketError();
#else
if (fFlags == -1)
return rtSocketError();
if (fBlocking)
fFlags &= ~O_NONBLOCK;
else
fFlags |= O_NONBLOCK;
return rtSocketError();
#endif
return VINF_SUCCESS;
}
/**
* Switches the socket to the desired blocking mode if necessary.
*
* The socket must be locked.
*
* @returns IPRT status code.
* @param pThis The socket structure.
* @param fBlocking The desired mode of operation.
*/
{
return VINF_SUCCESS;
}
/**
* Creates an IPRT socket handle for a native one.
*
* @returns IPRT status code.
* @param ppSocket Where to return the IPRT socket handle.
* @param hNative The native handle.
*/
{
if (!pThis)
return VERR_NO_MEMORY;
#ifdef RT_OS_WINDOWS
pThis->fSubscribedEvts = 0;
#endif
return VINF_SUCCESS;
}
{
#ifndef RT_OS_WINDOWS
#endif
}
/**
* Wrapper around socket().
*
* @returns IPRT status code.
* @param phSocket Where to store the handle to the socket on
* success.
* @param iDomain The protocol family (PF_XXX).
* @param iType The socket type (SOCK_XXX).
* @param iProtocol Socket parameter, usually 0.
*/
{
/*
* Create the socket.
*/
if (hNative == NIL_RTSOCKETNATIVE)
return rtSocketError();
/*
* Wrap it.
*/
if (RT_FAILURE(rc))
{
#ifdef RT_OS_WINDOWS
#else
#endif
}
return rc;
}
{
return RTMemPoolRetain(pThis);
}
/**
* Worker for RTSocketRelease and RTSocketClose.
*
* @returns IPRT status code.
* @param pThis The socket handle instance data.
* @param fDestroy Whether we're reaching ref count zero.
*/
{
/*
* Invalidate the handle structure on destroy.
*/
if (fDestroy)
{
}
int rc = VINF_SUCCESS;
{
/*
* Close the native handle.
*/
if (hNative != NIL_RTSOCKETNATIVE)
{
#ifdef RT_OS_WINDOWS
if (closesocket(hNative))
#else
#endif
{
rc = rtSocketError();
#ifdef RT_OS_WINDOWS
#else
#endif
}
}
#ifdef RT_OS_WINDOWS
/*
* Close the event.
*/
if (hEvent == WSA_INVALID_EVENT)
{
}
#endif
}
return rc;
}
{
if (pThis == NIL_RTSOCKET)
return 0;
/* get the refcount without killing it... */
if (cRefs == 1)
rtSocketCloseIt(pThis, true);
}
{
if (pThis == NIL_RTSOCKET)
return VINF_SUCCESS;
return rc;
}
{
}
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
if (!SetHandleInformation((HANDLE)pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
#else
#endif
return rc;
}
{
/* Empty address resolves to the INADDR_ANY address (good for bind). */
if (!pszAddress || !*pszAddress)
{
pAddr->u = INADDR_ANY;
return true;
}
/* Four quads? */
char *psz = (char *)pszAddress;
for (int i = 0; i < 4; i++)
{
return false;
return false;
psz++;
}
return true;
}
{
int rc;
/*
* Validate input.
*/
#ifdef RT_OS_WINDOWS
/*
* Initialize WinSock and check version.
*/
{
AssertMsgFailed(("Wrong winsock version\n"));
return VERR_NOT_SUPPORTED;
}
#endif
/*
* Resolve the address. Pretty crude at the moment, but we have to make
* sure to not ask the NT 4 gethostbyname about an IPv4 address as it may
* give a wrong answer.
*/
/** @todo this only supports IPv4, and IPv6 support needs to be added.
* It probably needs to be converted to getaddrinfo(). */
{
return VINF_SUCCESS;
}
if (!pHostEnt)
{
rc = rtSocketResolverError();
return rc;
}
{
Log3(("gethostbyname: %s -> %#x (%RTnaipv4)\n", pszAddress, pAddr->uAddr.IPv4.u, pAddr->uAddr.IPv4));
}
else
return VINF_SUCCESS;
}
/*
* new function to allow ipv4 and ipv6 addresses to be resolved.
* breaks compatibility with windows before 2000
* will change when the new ipv6 struct will be added
* temporary solution
*/
RTDECL(int) RTSocketGetAddrInfo(const char *psz, char *pszResult, size_t *resultSize, PRTNETADDRTYPE pAddrType)
{
int rc = 0;
char szIpV4Address[16];
char szIpV6Address[40];
char szDummy[10];
if (*resultSize < 16)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
resSize = *resultSize;
if (*pAddrType == RTNETADDRTYPE_IPV6)
if (*pAddrType == RTNETADDRTYPE_IPV4)
grHints.ai_socktype = 0;
grHints.ai_protocol = 0;
#ifdef RT_OS_WINDOWS
/*
* Winsock2 init
*/
// *FIXME* someone should check if we really need 2, 2 here
{
AssertMsgFailed(("Wrong winsock version\n"));
return VERR_NOT_SUPPORTED;
}
#endif
if (rc != 0)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
// return data
// on multiple matches return only the first one
if (!pgrResults)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
if (!pgrResult)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
{
pszIpV4Address = &szIpV4Address[0];
for (int i = 0; i < 4; i++)
{
return VERR_NET_ADDRESS_NOT_AVAILABLE;
if (i < 3)
{
*pszIpV4Address = '.';
}
pubDummy++;
}
{
return VERR_BUFFER_OVERFLOW;
}
else
{
return VINF_SUCCESS;
}
}
{
pszIpV6Address = &szIpV6Address[0];
for (int i = 0; i < 16; i++)
{
if (rc != 2)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
pubDummy++;
}
{
{
*resultSize = 41;
return VERR_BUFFER_OVERFLOW;
}
else
{
if (rc != 0)
return VERR_NET_ADDRESS_NOT_AVAILABLE;
return VINF_SUCCESS;
}
}
else
{
return VERR_NET_ADDRESS_NOT_AVAILABLE;
}
} // AF_INET6
return VERR_NET_ADDRESS_NOT_AVAILABLE;
}
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Read loop.
* If pcbRead is NULL we have to fill the entire buffer!
*/
for (;;)
{
#ifdef RT_OS_WINDOWS
#else
#endif
if (cbBytesRead <= 0)
{
rc = rtSocketError();
if (RT_SUCCESS_NP(rc))
{
if (!pcbRead)
else
{
*pcbRead = 0;
rc = VINF_SUCCESS;
}
}
break;
}
if (pcbRead)
{
/* return partial data */
*pcbRead = cbBytesRead;
break;
}
/* read more? */
cbRead += cbBytesRead;
break;
/* next */
}
return rc;
}
RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr)
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Read data.
*/
#ifdef RT_OS_WINDOWS
int cbAddr = sizeof(u);
#else
#endif
ssize_t cbBytesRead = recvfrom(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL, &u.Addr, &cbAddr);
if (cbBytesRead <= 0)
{
rc = rtSocketError();
if (RT_SUCCESS_NP(rc))
{
*pcbRead = 0;
rc = VINF_SUCCESS;
}
}
else
{
if (pSrcAddr)
*pcbRead = cbBytesRead;
}
return rc;
}
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Try write all at once.
*/
#ifdef RT_OS_WINDOWS
#else
#endif
rc = VINF_SUCCESS;
else if (cbWritten < 0)
rc = rtSocketError();
else
{
/*
* Unfinished business, write the remainder of the request. Must ignore
* VERR_INTERRUPTED here if we've managed to send something.
*/
size_t cbSentSoFar = 0;
for (;;)
{
/* advance */
if (!cbBuffer)
break;
/* send */
#ifdef RT_OS_WINDOWS
#else
#endif
if (cbWritten >= 0)
AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
else
{
rc = rtSocketError();
break;
cbWritten = 0;
rc = VINF_SUCCESS;
}
}
}
return rc;
}
RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
{
/*
* Validate input.
*/
/* no locking since UDP reads may be done concurrently to writes, and
* this is the normal use case of this code. */
if (RT_FAILURE(rc))
return rc;
/* Figure out destination address. */
#ifdef RT_OS_WINDOWS
int cbSA = 0;
#else
#endif
if (pAddr)
{
if (RT_FAILURE(rc))
return rc;
cbSA = sizeof(u);
}
/*
* Must write all at once, otherwise it is a failure.
*/
#ifdef RT_OS_WINDOWS
#else
#endif
rc = VINF_SUCCESS;
else if (cbWritten < 0)
rc = rtSocketError();
else
return rc;
}
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Construct message descriptor (translate pSgBuf) and send it.
*/
#ifdef RT_OS_WINDOWS
if (paMsg)
{
{
}
if (!hrc)
rc = VINF_SUCCESS;
/** @todo check for incomplete writes */
else
rc = rtSocketError();
}
#else /* !RT_OS_WINDOWS */
if (paMsg)
{
{
}
rc = VINF_SUCCESS;
/** @todo check for incomplete writes */
else
rc = rtSocketError();
}
#endif /* !RT_OS_WINDOWS */
return rc;
}
{
return rc;
}
{
/*
* Set up a S/G segment array + buffer on the stack and pass it
* on to RTSocketSgWrite.
*/
{
}
}
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
#ifdef RT_OS_WINDOWS
if (cbRead >= 0)
{
rc = VINF_SUCCESS;
}
else
rc = rtSocketError();
if (rc == VERR_TRY_AGAIN)
rc = VINF_TRY_AGAIN;
#else
if (cbRead >= 0)
{
*pcbRead = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = rtSocketError();
#endif
return rc;
}
RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
#ifdef RT_OS_WINDOWS
if (cbWritten >= 0)
{
*pcbWritten = cbWritten;
rc = VINF_SUCCESS;
}
else
rc = rtSocketError();
if (rc == VERR_TRY_AGAIN)
rc = VINF_TRY_AGAIN;
#else
if (cbWritten >= 0)
*pcbWritten = cbWritten;
{
*pcbWritten = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = rtSocketError();
#endif
return rc;
}
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
unsigned cSegsToSend = 0;
#ifdef RT_OS_WINDOWS
if (paMsg)
{
if (!hrc)
rc = VINF_SUCCESS;
else
rc = rtSocketError();
*pcbWritten = dwSent;
}
#else /* !RT_OS_WINDOWS */
if (paMsg)
{
{
rc = VINF_SUCCESS;
*pcbWritten = cbWritten;
}
else
rc = rtSocketError();
}
#endif /* !RT_OS_WINDOWS */
return rc;
}
{
return rc;
}
{
/*
* Set up a S/G segment array + buffer on the stack and pass it
* on to RTSocketSgWrite.
*/
{
}
}
{
/*
* Validate input.
*/
/*
* Set up the file descriptor sets and do the select.
*/
int rc;
if (cMillies == RT_INDEFINITE_WAIT)
else
{
}
if (rc > 0)
rc = VINF_SUCCESS;
else if (rc == 0)
rc = VERR_TIMEOUT;
else
rc = rtSocketError();
return rc;
}
{
/*
* Validate input.
*/
*pfEvents = 0;
/*
* Set up the file descriptor sets and do the select.
*/
if (fEvents & RTSOCKET_EVT_READ)
if (fEvents & RTSOCKET_EVT_WRITE)
if (fEvents & RTSOCKET_EVT_ERROR)
int rc;
if (cMillies == RT_INDEFINITE_WAIT)
else
{
}
if (rc > 0)
{
rc = VINF_SUCCESS;
}
else if (rc == 0)
rc = VERR_TIMEOUT;
else
rc = rtSocketError();
return rc;
}
{
/*
* Validate input, don't lock it because we might want to interrupt a call
* active on a different thread.
*/
/*
* Do the job.
*/
int rc = VINF_SUCCESS;
int fHow;
else if (fRead)
else
rc = rtSocketError();
return rc;
}
{
/*
* Validate input.
*/
/*
* Get the address and convert it.
*/
int rc;
#ifdef RT_OS_WINDOWS
int cbAddr = sizeof(u);
#else
#endif
RT_ZERO(u);
else
rc = rtSocketError();
return rc;
}
{
/*
* Validate input.
*/
/*
* Get the address and convert it.
*/
int rc;
#ifdef RT_OS_WINDOWS
int cbAddr = sizeof(u);
#else
#endif
RT_ZERO(u);
else
rc = rtSocketError();
return rc;
}
/**
* Wrapper around bind.
*
* @returns IPRT status code.
* @param hSocket The socket handle.
* @param pAddr The address to bind to.
*/
{
/*
* Validate input.
*/
int cbAddr;
if (RT_SUCCESS(rc))
{
rc = rtSocketError();
}
return rc;
}
/**
* Wrapper around listen.
*
* @returns IPRT status code.
* @param hSocket The socket handle.
* @param cMaxPending The max number of pending connections.
*/
{
/*
* Validate input.
*/
int rc = VINF_SUCCESS;
rc = rtSocketError();
return rc;
}
/**
* Wrapper around accept.
*
* @returns IPRT status code.
* @param hSocket The socket handle.
* @param phClient Where to return the client socket handle on
* success.
* @param pAddr Where to return the client address.
* @param pcbAddr On input this gives the size buffer size of what
* @a pAddr point to. On return this contains the
* size of what's stored at @a pAddr.
*/
{
/*
* Validate input.
* Only lock the socket temporarily while we get the native handle, so that
* we can safely shutdown and destroy the socket from a different thread.
*/
/*
* Call accept().
*/
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
#else
#endif
if (hNativeClient != NIL_RTSOCKETNATIVE)
{
/*
* Wrap the client socket.
*/
if (RT_FAILURE(rc))
{
#ifdef RT_OS_WINDOWS
#else
#endif
}
}
else
rc = rtSocketError();
return rc;
}
/**
* Wrapper around connect.
*
* @returns IPRT status code.
* @param hSocket The socket handle.
* @param pAddr The socket address to connect to.
*/
{
/*
* Validate input.
*/
int cbAddr;
if (RT_SUCCESS(rc))
{
rc = rtSocketError();
}
return rc;
}
/**
* Wrapper around setsockopt.
*
* @returns IPRT status code.
* @param hSocket The socket handle.
* @param iLevel The protocol level, e.g. IPPORTO_TCP.
* @param iOption The option, e.g. TCP_NODELAY.
* @param pvValue The value buffer.
* @param cbValue The size of the value pointed to by pvValue.
*/
{
/*
* Validate input.
*/
int rc = VINF_SUCCESS;
rc = rtSocketError();
return rc;
}
#ifdef RT_OS_WINDOWS
/**
* Internal RTPollSetAdd helper that returns the handle that should be added to
* the pollset.
*
* @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
* @param hSocket The socket handle.
* @param fEvents The events we're polling for.
* @param ph where to put the primary handle.
*/
{
int rc = VINF_SUCCESS;
else
{
rc = rtSocketError();
}
return rc;
}
/**
* Undos the harm done by WSAEventSelect.
*
* @returns IPRT status code.
* @param pThis The socket handle.
*/
{
int rc = VINF_SUCCESS;
if (pThis->fSubscribedEvts)
{
{
pThis->fSubscribedEvts = 0;
/*
* Switch back to blocking mode if that was the state before the
* operation.
*/
{
u_long fNonBlocking = 0;
if (rc2 != 0)
{
rc = rtSocketError();
}
}
}
else
{
rc = rtSocketError();
}
}
return rc;
}
/**
* Updates the mask of events we're subscribing to.
*
* @returns IPRT status code.
* @param pThis The socket handle.
* @param fEvents The events we want to subscribe to.
*/
{
LONG fNetworkEvents = 0;
if (fEvents & RTPOLL_EVT_READ)
if (fEvents & RTPOLL_EVT_WRITE)
if (fEvents & RTPOLL_EVT_ERROR)
{
return VINF_SUCCESS;
}
int rc = rtSocketError();
return rc;
}
/**
* Checks for pending events.
*
* @returns Event mask or 0.
* @param pThis The socket handle.
* @param fEvents The desired events.
*/
{
int rc = VINF_SUCCESS;
uint32_t fRetEvents = 0;
/* Make sure WSAEnumNetworkEvents returns what we want. */
/* Get the event mask, ASSUMES that WSAEnumNetworkEvents doesn't clear stuff. */
{
&& (fEvents & RTPOLL_EVT_READ)
&& (fEvents & RTPOLL_EVT_WRITE)
if (fEvents & RTPOLL_EVT_ERROR)
{
else
for (uint32_t i = 0; i < FD_MAX_EVENTS; i++)
&& NetEvts.iErrorCode[i] != 0)
}
}
else
rc = rtSocketError();
/* Fall back on select if we hit an error above. */
if (RT_FAILURE(rc))
{
/** @todo */
}
return fRetEvents;
}
/**
* Internal RTPoll helper that polls the socket handle and, if @a fNoWait is
* clear, starts whatever actions we've got running during the poll call.
*
* @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
* Event mask (in @a fEvents) and no actions if the handle is ready
* already.
* UINT32_MAX (asserted) if the socket handle is busy in I/O or a
* different poll set.
*
* @param hSocket The socket handle.
* @param hPollSet The poll set handle (for access checks).
* @param fEvents The events we're polling for.
* @param fFinalEntry Set if this is the final entry for this handle
* in this poll set. This can be used for dealing
* with duplicate entries.
* @param fNoWait Set if it's a zero-wait poll call. Clear if
* we'll wait for an event to occur.
*
* @remarks There is a potential race wrt duplicate handles when @a fNoWait is
* @c true, we don't currently care about that oddity...
*/
uint32_t rtSocketPollStart(RTSOCKET hSocket, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
{
if (rtSocketTryLock(pThis))
else
{
}
/* (rtSocketPollCheck will reset the event object). */
if ( !fRetEvents
&& !fNoWait)
{
if ( fFinalEntry
{
if (RT_FAILURE(rc))
{
}
}
}
if (fRetEvents || fNoWait)
{
{
}
}
return fRetEvents;
}
/**
* Called after a WaitForMultipleObjects returned in order to check for pending
* events and stop whatever actions that rtSocketPollStart() initiated.
*
* @returns Event mask or 0.
*
* @param hSocket The socket handle.
* @param fEvents The events we're polling for.
* @param fFinalEntry Set if this is the final entry for this handle
* in this poll set. This can be used for dealing
* with duplicate entries. Only keep in mind that
* this method is called in reverse order, so the
* first call will have this set (when the entire
* set was processed).
* @param fHarvestEvents Set if we should check for pending events.
*/
uint32_t rtSocketPollDone(RTSOCKET hSocket, uint32_t fEvents, bool fFinalEntry, bool fHarvestEvents)
{
AssertPtrReturn(pThis, 0);
/* Harvest events and clear the event mask for the next round of polling. */
/*
* Save the write event if required.
* It is only posted once and might get lost if the another source in the
* pollset with a higher priority has pending events.
*/
if ( !fHarvestEvents
&& fRetEvents)
{
fRetEvents = 0;
}
/* Make the socket blocking again and unlock the handle. */
{
}
return fRetEvents;
}
#endif /* RT_OS_WINDOWS */