DrvNAT.cpp revision a39ab93d71daee43e60b32f614c426492d5cf2d2
/** @file
*
* VBox network devices:
* NAT network transport driver
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_NAT
#include "Network/slirp/libslirp.h"
#include <iprt/critsect.h>
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*/
typedef struct DRVNAT
{
/** The network interface. */
/** The port we're attached to. */
/** Pointer to the driver instance. */
/** Slirp critical section. */
/** Link state */
/** NAT state for this instance. */
/** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
#define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#if 0
/** If set the thread should terminate. */
static bool g_fThreadTerm = false;
/** The thread id of the select thread (drvNATSelectThread()). */
static RTTHREAD g_ThreadSelect;
#endif
/**
* Send data to the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf Data to send.
* @param cb Number of bytes to send.
* @thread EMT
*/
{
Log2(("drvNATSend: pvBuf=%p cb=%#x\n"
"%.*Vhxd\n",
LogFlow(("drvNATSend: end\n"));
return VINF_SUCCESS;
}
/**
* Set promiscuous mode.
*
* This is called when the promiscuous mode is set. This means that there doesn't have
* to be a mode change when it's called.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
* @thread EMT
*/
static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
/* nothing to do */
}
/**
* Notification on link status changes.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmLinkState The new link state.
* @thread EMT
*/
static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
switch (enmLinkState)
{
case PDMNETWORKLINKSTATE_UP:
LogRel(("NAT: link up\n"));
break;
case PDMNETWORKLINKSTATE_DOWN:
LogRel(("NAT: link down\n"));
break;
default:
}
}
/**
* More receive buffer has become available.
*
* This is called when the NIC frees up receive buffers.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
{
LogFlow(("drvNATNotifyCanReceive:\n"));
/** @todo do something useful here. */
}
/**
* Poller callback.
*/
{
int cFDs = -1;
if (cReadFDs >= 0)
}
/**
* Function called by slirp to check if it's possible to feed incoming data to the network port.
* @returns 1 if possible.
* @returns 0 if not possible.
*/
int slirp_can_output(void *pvUser)
{
/** Happens during termination */
return 0;
return 0;
}
/**
* Function called by slirp to feed incoming data to the network port.
*/
{
Log2(("slirp_output: pu8Buf=%p cb=%#x (pData=%p)\n"
"%.*Vhxd\n",
/** Happens during termination */
return;
}
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pData->INetworkConnector;
default:
return NULL;
}
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
LogFlow(("drvNATDestruct:\n"));
#if ARCH_BITS == 64
LogRel(("NAT: g_cpvHashUsed=%RU32 g_cpvHashCollisions=%RU32 g_cpvHashInserts=%RU64 g_cpvHashDone=%RU64\n",
#endif
}
/**
* Sets up the redirectors.
*
* @returns VBox status code.
* @param pCfgHandle The drivers configuration handle.
*/
{
/*
* Enumerate redirections.
*/
{
/* protocol type */
bool fUDP;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fUDP = true;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* host port */
if (VBOX_FAILURE(rc))
{
return rc;
}
/* guest port */
if (VBOX_FAILURE(rc))
{
return rc;
}
/* guest address */
char szGuestIP[32];
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
{
AssertMsgFailed(("Configuration error: Invalid \"GuestIP\"=\"%s\", inet_aton failed.\n", szGuestIP));
return VERR_NAT_REDIR_GUEST_IP;
}
/*
* Call slirp about it.
*/
{
AssertMsgFailed(("Configuration error: failed to setup redirection of %d to %s:%d. Probably a conflict with existing services or other rules.\n",
return VERR_NAT_REDIR_SETUP;
}
} /* for each redir rule */
return VINF_SUCCESS;
}
/**
* Construct a NAT network transport driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
{
char szNetAddr[16];
LogFlow(("drvNATConstruct:\n"));
/*
* Validate the config.
*/
/*
* Init the static parts.
*/
/* IBase */
/* INetwork */
/*
* Query the network port interface.
*/
pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
/* Generate a network address for this network card. */
/*
* The slirp lock..
*/
if (VBOX_FAILURE(rc))
return rc;
#if 0
if (VBOX_SUCCESS(rc))
{
/*
* Start the select thread. (it'll block on the sem)
*/
g_fThreadTerm = false;
if (VBOX_SUCCESS(rc))
{
#endif
/*
* Initialize slirp.
*/
if (VBOX_SUCCESS(rc))
{
if (VBOX_SUCCESS(rc))
{
#if 0
RTThreadSleep(0);
#endif
return VINF_SUCCESS;
}
/* failure path */
}
else
{
switch (rc)
{
case VERR_NAT_DNS:
PDMDRV_SET_ERROR(pDrvIns, rc, N_("Domain Name Server (DNS) for NAT networking could not be determined"));
break;
default:
break;
}
}
#if 0
g_fThreadTerm = true;
RTThreadSleep(0);
}
g_EventSem = NULL;
}
#endif
return rc;
}
/**
* NAT network transport driver registration record.
*/
{
/* u32Version */
/* szDriverName */
"NAT",
/* pszDescription */
"NAT Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
16,
/* cbInstance */
sizeof(DRVNAT),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};