NetworkAdapterImpl.cpp revision 135499f13606cfa089592dea2bfdff80c73c51d1
/** @file
*
* VirtualBox COM class implementation
*/
/*
* 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.
*/
#include "NetworkAdapterImpl.h"
#include "Logging.h"
#include "MachineImpl.h"
// defines
////////////////////////////////////////////////////////////////////////////////
// constructor / destructor
////////////////////////////////////////////////////////////////////////////////
{
return S_OK;
}
void NetworkAdapter::FinalRelease()
{
if (isReady())
uninit ();
}
// public initializer/uninitializer for internal purposes only
////////////////////////////////////////////////////////////////////////////////
/**
* Initializes the network adapter object.
*
* @param parent handle of our parent object
* @return COM result indicator
*/
{
// mPeer is left null
// initialize data
// default to Am79C973
// generate the MAC address early to guarantee it is the same both after
// changing some other property (i.e. after mData.backup()) and after the
// subsequent mData.rollback().
setReady (true);
return S_OK;
}
/**
* Initializes the network adapter object given another network adapter object
* (a kind of copy constructor). This object shares data with
* the object passed as an argument.
*
* @note This object must be destroyed before the original object
* it shares data with is destroyed.
*/
{
setReady (true);
return S_OK;
}
/**
* Initializes the guest object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*/
{
// mPeer is left null
setReady (true);
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void NetworkAdapter::uninit()
{
LogFlowMember (("INetworkAdapter::uninit()\n"));
AssertReturn (isReady(), (void) 0);
setReady (false);
}
// INetworkAdapter properties
////////////////////////////////////////////////////////////////////////////////
{
if (!adapterType)
return E_POINTER;
CHECK_READY();
return S_OK;
}
{
CHECK_READY();
/* make sure the value is allowed */
switch (adapterType)
{
break;
default:
}
{
/* notify parent */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!slot)
return E_POINTER;
CHECK_READY();
return S_OK;
}
{
if (!enabled)
return E_POINTER;
CHECK_READY();
return S_OK;
}
{
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
/**
* Returns the MAC address string
*
* @returns COM status code
* @param macAddress address of result variable
*/
{
if (!macAddress)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Sets the MAC address
*
* @returns COM status code
* @param macAddress 12-digit hexadecimal MAC address string with
* capital letters. Can be NULL to generate a MAC
*/
{
CHECK_READY();
bool emitChangeEvent = false;
/*
* Are we supposed to generate a MAC?
*/
if (!macAddress)
{
emitChangeEvent = true;
}
else
{
{
/*
* Verify given MAC address
*/
int i = 0;
{
char c = *macAddressStr;
/* we only accept capital letters */
if (((c < '0') || (c > '9')) &&
((c < 'A') || (c > 'F')))
i++;
}
/* we must have parsed exactly 12 characters */
if (i != 12)
{
emitChangeEvent = true;
}
}
}
if (emitChangeEvent)
{
/* notify parent */
mParent->onNetworkAdapterChange (this);
}
return rc;
}
/**
* Returns the attachment type
*
* @returns COM status code
* @param attachmentType address of result variable
*/
{
if (!attachmentType)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Returns the host interface the adapter is attached to
*
* @returns COM status code
* @param hostInterface address of result string
*/
{
if (!hostInterface)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Sets the host interface device name.
*
* @returns COM status code
* @param hostInterface name of the host interface device in use
*/
{
/** @todo Validate input string length. r=dmik: do it in XML schema?*/
#ifdef __WIN__
// we don't allow null strings for the host interface on Win32
// (because the @name attribute of <HostInerface> must be always present,
// but can be empty).
if (!hostInterface)
return E_INVALIDARG;
#endif
// empty strings are not allowed as path names
if (hostInterface && !(*hostInterface))
return E_INVALIDARG;
#endif
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Returns the TAP file descriptor the adapter is attached to
*
* @returns COM status code
* @param tapFileDescriptor address of result string
*/
{
if (!tapFileDescriptor)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Sets the TAP file descriptor the adapter is attached to
*
* @returns COM status code
* @param tapFileDescriptor file descriptor of existing TAP interface
*/
{
/*
* Validate input.
*/
{
return setError (E_INVALIDARG,
}
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Returns the current TAP setup application path
*
* @returns COM status code
* @param tapSetupApplication address of result buffer
*/
{
if (!tapSetupApplication)
return E_POINTER;
CHECK_READY();
/* we don't have to be in TAP mode to support this call */
return S_OK;
}
/**
* Stores a new TAP setup application
*
* @returns COM status code
* @param tapSetupApplication new TAP setup application path
*/
{
// empty strings are not allowed as path names
if (tapSetupApplication && !(*tapSetupApplication))
return E_INVALIDARG;
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Returns the current TAP terminate application path
*
* @returns COM status code
* @param tapTerminateApplication address of result buffer
*/
{
if (!tapTerminateApplication)
return E_POINTER;
CHECK_READY();
/* we don't have to be in TAP mode to support this call */
return S_OK;
}
/**
* Stores a new TAP terminate application
*
* @returns COM status code
* @param tapTerminateApplication new TAP terminate application path
*/
{
// empty strings are not allowed as path names
if (tapTerminateApplication && !(*tapTerminateApplication))
return E_INVALIDARG;
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
/**
* Returns the internal network the adapter is attached to
*
* @returns COM status code
* @param internalNetwork address of result variable
*/
{
// we don't allow null strings
if (!internalNetwork)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Sets the internal network for attachment.
*
* @returns COM status code
* @param internalNetwork internal network name
*/
{
if (!internalNetwork)
return E_INVALIDARG;
CHECK_READY();
{
/* if an empty string is to be set, internal networking must be turned off */
{
}
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Return the current cable status
*
* @returns COM status code
* @param connected address of result variable
*/
{
if (!connected)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Set the cable status flag.
*
* @returns COM status code
* @param connected new trace flag
*/
{
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Return the current trace status
*
* @returns COM status code
* @param enabled address of result variable
*/
{
if (!enabled)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Set the trace flag.
*
* @returns COM status code
* @param enabled new trace flag
*/
{
CHECK_READY();
{
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Return the current trace file name
*
* @returns COM status code
* @param address where to store result
*/
{
if (!traceFile)
return E_POINTER;
CHECK_READY();
return S_OK;
}
/**
* Set the trace file name
*
* @returns COM status code
* @param New trace file name
*/
{
if (!traceFile)
return E_INVALIDARG;
CHECK_READY();
/* notify parent */
mParent->onNetworkAdapterChange(this);
return S_OK;
}
// INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
/**
* Attach the network card to the NAT interface.
*
* @returns COM status code
*/
{
CHECK_READY();
{
detach();
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Attach the network card to the defined host interface.
*
* @returns COM status code
*/
{
CHECK_READY();
/* don't do anything if we're already host interface attached */
{
/* first detach the current attachment */
detach();
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Attach the network card to the defined internal network.
*
* @returns COM status code
*/
{
CHECK_READY();
/* don't do anything if we're already internal network attached */
{
/* there must an internal network name */
if ( !mData->mInternalNetwork
{
LogRel(("Internal network name not defined, setting to default \"intnet\"\n"));
return rc;
}
/* first detach the current attachment */
detach();
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
/**
* Detach the network card from its interface
*
* @returns COM status code
*/
{
CHECK_READY();
{
detach();
/* notify parent */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
// public methods only for internal purposes
////////////////////////////////////////////////////////////////////////////////
bool NetworkAdapter::rollback()
{
bool changed = false;
if (mData.isBackedUp())
{
// we need to check all data to see whether anything will be changed
// after rollback
}
return changed;
}
void NetworkAdapter::commit()
{
if (mData.isBackedUp())
{
if (mPeer)
{
// attach new data to the peer and reshare it
}
}
}
{
// this will back up current data
}
// private methods
////////////////////////////////////////////////////////////////////////////////
/**
* Worker routine for detach handling. No locking, no notifications.
*/
void NetworkAdapter::detach()
{
switch (mData->mAttachmentType)
{
{
/* nothing to do here */
break;
}
{
break;
}
{
/* reset handle and device name */
#ifdef __WIN__
#endif
#endif
break;
}
{
break;
}
}
}
/**
* Generates a new unique MAC address based on our vendor ID and
* parts of a GUID
*/
void NetworkAdapter::generateMACAddress()
{
/*
* Our strategy is as follows: the first three bytes are our fixed
* vendor ID (080027). The remaining 3 bytes will be taken from the
* start of a GUID. This is a fairly safe algorithm.
*/
char strMAC[13];
RTStrPrintf(strMAC, sizeof(strMAC), "080027%02X%02X%02X", guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
}