udp.cpp revision 4e47bb772df0d04d1ded3e06354de547d52e2d06
/* $Id$ */
/** @file
*/
/*
* Copyright (C) 2006-2011 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
# include <errno.h>
# include <netdb.h>
#endif
#include <limits.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* fixup backlevel OSes. */
#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
# define socklen_t int
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* UDP Server state.
*/
typedef enum RTUDPSERVERSTATE
{
/** Invalid. */
/** Created. */
/** Thread for incoming datagrams is starting up. */
/** Waiting for incoming datagrams. */
/** Handling an incoming datagram. */
/** Thread terminating. */
/** Thread terminated. */
/** Final cleanup before being unusable. */
/*
* Internal representation of the UDP Server handle.
*/
typedef struct RTUDPSERVER
{
/** The magic value (RTUDPSERVER_MAGIC). */
/** The server state. */
RTUDPSERVERSTATE volatile enmState;
/** The server thread. */
/** The server socket. */
/** The datagram receiver function. */
/** Argument to pfnServer. */
void *pvUser;
} RTUDPSERVER;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Atomicly updates a socket variable.
* @returns The old handle value.
* @param phSock The socket handle variable to update.
* @param hSock The new socket handle value.
*/
{
return hRet;
}
/**
* Tries to change the UDP server state.
*/
DECLINLINE(bool) rtUdpServerTrySetState(PRTUDPSERVER pServer, RTUDPSERVERSTATE enmStateNew, RTUDPSERVERSTATE enmStateOld)
{
bool fRc;
return fRc;
}
/**
* Changes the UDP server state.
*/
DECLINLINE(void) rtUdpServerSetState(PRTUDPSERVER pServer, RTUDPSERVERSTATE enmStateNew, RTUDPSERVERSTATE enmStateOld)
{
bool fRc;
}
/**
* Closes a socket.
*
* @returns IPRT status code.
*/
{
if (hSocket != NIL_RTSOCKET)
{
}
return VINF_UDP_SERVER_NO_CLIENT;
}
/**
* Create single datagram at a time UDP Server in a separate thread.
*
* The thread will loop waiting for datagrams and call pfnServe for
* each of the incoming datagrams in turn. The pfnServe function can
* return VERR_UDP_SERVER_STOP too terminate this loop. RTUdpServerDestroy()
* should be used to terminate the server.
*
* @returns iprt status code.
* @param pszAddress The address for creating a datagram socket.
* If NULL or empty string the server is bound to all interfaces.
* @param uPort The port for creating a datagram socket.
* @param enmType The thread type.
* @param pszThrdName The name of the worker thread.
* @param pfnServe The function which will handle incoming datagrams.
* @param pvUser User argument passed to pfnServe.
* @param ppServer Where to store the serverhandle.
*/
RTR3DECL(int) RTUdpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
{
/*
* Validate input.
*/
/*
* Create the server.
*/
if (RT_SUCCESS(rc))
{
/*
* Create the listener thread.
*/
rc = RTThreadCreate(&pServer->Thread, rtUdpServerThread, pServer, 0, enmType, /*RTTHREADFLAGS_WAITABLE*/0, pszThrdName);
if (RT_SUCCESS(rc))
{
/* done */
if (ppServer)
else
return rc;
}
/*
* Destroy the server.
*/
}
return rc;
}
/**
* Server thread, loops waiting for datagrams until it's terminated.
*
* @returns iprt status code. (ignored).
* @param ThreadSelf Thread handle.
* @param pvServer Server handle.
*/
{
int rc;
else
return VINF_SUCCESS;
}
/**
* Create single datagram at a time UDP Server.
* The caller must call RTUdpServerReceive() to actually start the server.
*
* @returns iprt status code.
* @param pszAddress The address for creating a datagram socket.
* If NULL the server is bound to all interfaces.
* @param uPort The port for creating a datagram socket.
* @param ppServer Where to store the serverhandle.
*/
{
int rc;
/*
* Validate input.
*/
#ifdef RT_OS_WINDOWS
/*
* Initialize WinSock and check version.
*/
{
AssertMsgFailed(("Wrong winsock version\n"));
return VERR_NOT_SUPPORTED;
}
#endif
/*
* Get host listening address.
*/
{
if (!pHostEnt)
{
if (!pHostEnt)
{
rc = rtSocketResolverError();
return rc;
}
}
}
/*
* Setting up socket.
*/
if (RT_SUCCESS(rc))
{
/*
* Set socket options.
*/
int fFlag = 1;
{
/*
* Set socket family, address and port.
*/
struct sockaddr_in LocalAddr;
/* if address not specified, use INADDR_ANY. */
if (!pHostEnt)
else
/*
* Bind a name to the socket.
*/
if (RT_SUCCESS(rc))
{
/*
* Create the server handle.
*/
if (pServer)
{
return VINF_SUCCESS;
}
/* bail out */
rc = VERR_NO_MEMORY;
}
}
else
}
return rc;
}
/**
* Listen for incoming datagrams.
*
* The function will loop waiting for datagrams and call pfnServe for
* each of the incoming datagrams in turn. The pfnServe function can
* return VERR_UDP_SERVER_STOP too terminate this loop. A stopped server
* can only be destroyed.
*
* @returns IPRT status code.
* @retval VERR_UDP_SERVER_STOP if stopped by pfnServe.
* @retval VERR_UDP_SERVER_SHUTDOWN if shut down by RTUdpServerShutdown.
*
* @param pServer The server handle as returned from RTUdpServerCreateEx().
* @param pfnServe The function which will handle incoming datagrams.
* @param pvUser User argument passed to pfnServe.
*/
{
/*
* Validate input and retain the instance.
*/
int rc = VERR_INVALID_STATE;
{
}
else
{
}
return rc;
}
/**
* Internal worker common for RTUdpServerListen and the thread created by
* RTUdpServerCreate().
*
* The caller makes sure it has its own memory reference and releases it upon
* return.
*/
{
/*
* Wait for incoming datagrams loop.
*/
for (;;)
{
/*
* Change state, getting an extra reference to the socket so we can
* allow others to close it while we're stuck in rtSocketAccept.
*/
if (hSocket != NIL_RTSOCKET)
if ( enmState != RTUDPSERVERSTATE_WAITING
{
return rtUdpServerListenCleanup(pServer);
}
{
continue;
}
/*
* Wait for incoming datagrams or errors.
*/
if (rc == VERR_TIMEOUT)
continue;
if (RT_FAILURE(rc))
{
/* These are typical for what can happen during destruction. */
if ( rc == VERR_INVALID_HANDLE
|| rc == VERR_INVALID_PARAMETER
|| rc == VERR_NET_NOT_SOCKET)
return rtUdpServerListenCleanup(pServer);
continue;
}
if (fEvents & RTSOCKET_EVT_ERROR)
return rtUdpServerListenCleanup(pServer);
/*
* Run a pfnServe callback.
*/
return rtUdpServerListenCleanup(pServer);
/*
* Stop the server?
*/
if (rc == VERR_UDP_SERVER_STOP)
{
{
/*
* Reset the server socket and change the state to stopped. After that state change
* we cannot safely access the handle so we'll have to return here.
*/
}
else
return rc;
}
}
}
/**
* Clean up after listener.
*/
{
/*
* Close the server socket.
*/
/*
* Figure the return code and make sure the state is OK.
*/
switch (enmState)
{
case RTUDPSERVERSTATE_STOPPED:
return VERR_UDP_SERVER_SHUTDOWN;
case RTUDPSERVERSTATE_WAITING:
return VERR_UDP_SERVER_DESTROYED;
return VERR_UDP_SERVER_DESTROYED;
default:
}
}
/**
* Shuts down the server.
*
* @returns IPRT status code.
* @param pServer Handle to the server.
*/
{
/*
* Validate input and retain the instance.
*/
/*
* Try change the state to stopping, then replace and destroy the server socket.
*/
for (;;)
{
if ( enmState != RTUDPSERVERSTATE_WAITING
{
switch (enmState)
{
case RTUDPSERVERSTATE_CREATED:
default:
return VERR_INVALID_STATE;
case RTUDPSERVERSTATE_STOPPED:
return VINF_SUCCESS;
return VERR_UDP_SERVER_DESTROYED;
}
}
{
return VINF_SUCCESS;
}
}
}
/**
* Closes down and frees a UDP Server.
*
* @returns iprt status code.
* @param pServer Handle to the server.
*/
{
/*
* Validate input and retain the instance.
*/
/*
* Move the state along so the listener can figure out what's going on.
*/
for (;;)
{
bool fDestroyable;
switch (enmState)
{
case RTUDPSERVERSTATE_WAITING:
case RTUDPSERVERSTATE_CREATED:
case RTUDPSERVERSTATE_STOPPED:
break;
/* destroyable states */
fDestroyable = true;
break;
/*
* Everything else means user or internal misbehavior.
*/
default:
return VERR_INTERNAL_ERROR;
}
if (fDestroyable)
break;
}
/*
* Destroy it.
*/
/*
* Release it.
*/
return VINF_SUCCESS;
}
/**
* Internal close function which does all the proper bitching.
*/
{
/* ignore nil handles. */
if (Sock == NIL_RTSOCKET)
return VINF_SUCCESS;
/*
* Close the socket handle (drops our reference to it).
*/
return RTSocketClose(Sock);
}
RTR3DECL(int) RTUdpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr)
{
if (!RT_VALID_PTR(pcbRead))
return VERR_INVALID_POINTER;
}
RTR3DECL(int) RTUdpWrite(PRTUDPSERVER pServer, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr)
{
/*
* Validate input and retain the instance.
*/
if (hSocket == NIL_RTSOCKET)
{
return VERR_INVALID_HANDLE;
}
int rc = VINF_SUCCESS;
if ( enmState != RTUDPSERVERSTATE_CREATED
if (RT_SUCCESS(rc))
return rc;
}