NetworkAdapterImpl.cpp revision 2c84ae507c12f4dabfb7653dac15b574ab38b560
/** @file
*
* VirtualBox COM class implementation
*/
/*
* Copyright (C) 2006-2007 innotek 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.
*/
#include "NetworkAdapterImpl.h"
#include "Logging.h"
#include "MachineImpl.h"
#include <iprt/cpputils.h>
// constructor / destructor
////////////////////////////////////////////////////////////////////////////////
{
return S_OK;
}
void NetworkAdapter::FinalRelease()
{
uninit ();
}
// public initializer/uninitializer for internal purposes only
////////////////////////////////////////////////////////////////////////////////
/**
* Initializes the network adapter object.
*
* @param aParent Handle of the parent object.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* 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(). */
/* Confirm a successful initialization */
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.
*
* @note Locks @a aThat object for reading.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* Confirm a successful initialization */
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.
*
* @note Locks @a aThat object for reading.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* mPeer is left null */
/* Confirm a successful initialization */
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()
{
LogFlowThisFunc (("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan (this);
if (autoUninitSpan.uninitDone())
return;
}
// INetworkAdapter properties
////////////////////////////////////////////////////////////////////////////////
{
if (!aAdapterType)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
/* make sure the value is allowed */
switch (aAdapterType)
{
#ifdef VBOX_WITH_E1000
#endif
break;
default:
tr("Invalid network adapter type '%d'"),
}
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aSlot)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aMACAddress)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
bool emitChangeEvent = false;
/*
* Are we supposed to generate a MAC?
*/
if (!aMACAddress)
{
emitChangeEvent = true;
}
else
{
{
/*
* Verify given MAC address
*/
int i = 0;
{
char c = *macAddressStr;
/* canonicalize hex digits to capital letters */
if (c >= 'a' && c <= 'f')
{
c &= 0xdf;
*macAddressStr = c;
}
/* we only accept capital letters */
if (((c < '0') || (c > '9')) &&
((c < 'A') || (c > 'F')))
/* the second digit must have even value for unicast addresses */
if ((i == 1) && (!!(c & 1) == (c >= '0' && c <= '9')))
i++;
}
/* we must have parsed exactly 12 characters */
if (i != 12)
{
emitChangeEvent = true;
}
}
}
if (emitChangeEvent)
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return rc;
}
{
if (!aAttachmentType)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
if (!aHostInterface)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
/** @todo Validate input string length. r=dmik: do it in XML schema?*/
#ifdef RT_OS_WINDOWS
// 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 (!aHostInterface)
return E_INVALIDARG;
#endif
// empty strings are not allowed as path names
if (aHostInterface && !(*aHostInterface))
return E_INVALIDARG;
#endif
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aTAPFileDescriptor)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
/*
* Validate input.
*/
{
return setError (E_INVALIDARG,
}
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aTAPSetupApplication)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* we don't have to be in TAP mode to support this call */
return S_OK;
}
{
/* empty strings are not allowed as path names */
if (aTAPSetupApplication && !(*aTAPSetupApplication))
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aTAPTerminateApplication)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* we don't have to be in TAP mode to support this call */
return S_OK;
}
{
/* empty strings are not allowed as path names */
if (aTAPTerminateApplication && !(*aTAPTerminateApplication))
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
}
#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
{
/* we don't allow null strings */
if (!aInternalNetwork)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
if (!aInternalNetwork)
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* if an empty string is to be set, internal networking must be turned off */
{
}
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aConnected)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aSpeed)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aTraceFile)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
// INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
detach();
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
/* don't do anything if we're already host interface attached */
{
/* first detach the current attachment */
detach();
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
/* don't do anything if we're already internal network attached */
{
/* first detach the current attachment */
detach();
/* there must an internal network name */
if ( !mData->mInternalNetwork
{
LogRel (("Internal network name not defined, "
"setting to default \"intnet\"\n"));
}
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
detach();
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
// public methods only for internal purposes
////////////////////////////////////////////////////////////////////////////////
/**
* @note Locks this object for writing.
*/
bool NetworkAdapter::rollback()
{
/* sanity */
AutoCaller autoCaller (this);
bool changed = false;
if (mData.isBackedUp())
{
/* we need to check all data to see whether anything will be changed
* after rollback */
}
return changed;
}
/**
* @note Locks this object for writing, together with the peer object (also
* for writing) if there is one.
*/
void NetworkAdapter::commit()
{
/* sanity */
AutoCaller autoCaller (this);
/* sanity too */
/* lock both for writing since we modify both */
if (mData.isBackedUp())
{
if (mPeer)
{
/* attach new data to the peer and reshare it */
}
}
}
/**
* @note Locks this object for writing, together with the peer object
* represented by @a aThat (locked for reading).
*/
{
/* sanity */
AutoCaller autoCaller (this);
/* sanity too */
/* peer is not modified, lock it for reading */
/* this will back up current data */
}
// private methods
////////////////////////////////////////////////////////////////////////////////
/**
* Worker routine for detach handling. No locking, no notifications.
* @note Must be called from under the object's write lock.
*/
void NetworkAdapter::detach()
{
switch (mData->mAttachmentType)
{
{
/* nothing to do here */
break;
}
{
break;
}
{
/* reset handle and device name */
#ifdef RT_OS_WINDOWS
#endif
#endif
break;
}
{
break;
}
}
}
/**
* Generates a new unique MAC address based on our vendor ID and
* parts of a GUID.
*
* @note Must be called from under the object's write lock or within the init
* span.
*/
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];
}