NetworkAdapterImpl.cpp revision 26d2a42f095ded346df2e41cc4837cb426b4753a
/** @file
*
* VirtualBox COM class implementation
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#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);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
/* 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);
AutoReadLock alock (this);
return S_OK;
}
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aMACAddress)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
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);
AutoReadLock alock (this);
return S_OK;
}
{
if (!aHostInterface)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
/** @todo Validate input string length. r=dmik: do it in XML schema?*/
#ifndef VBOX_WITH_UNIXY_TAP_NETWORKING
// we don't allow null strings for the host interface on Win32
// (because the @name attribute of <HostInterface> must be always present,
// but can be empty).
if (!aHostInterface)
return E_INVALIDARG;
#else
// empty strings are not allowed as path names
if (aHostInterface && !(*aHostInterface))
return E_INVALIDARG;
#endif
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
#ifndef RT_OS_WINDOWS /** @todo ifdef VBOX_WITH_UNIXY_TAP_NETWORKING: need to find a way to exclude this in the xidl... */
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
if (!aTAPFileDescriptor)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
/*
* Validate input.
*/
{
return setError (E_INVALIDARG,
}
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
if (!aTAPSetupApplication)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
/* we don't have to be in TAP mode to support this call */
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
/* empty strings are not allowed as path names */
if (aTAPSetupApplication && !(*aTAPSetupApplication))
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
if (!aTAPTerminateApplication)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
/* we don't have to be in TAP mode to support this call */
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
# ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
/* empty strings are not allowed as path names */
if (aTAPTerminateApplication && !(*aTAPTerminateApplication))
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange(this);
}
return S_OK;
#else
return E_NOTIMPL;
#endif
}
#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
{
if (!aInternalNetwork)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
* turned off */
{
tr ("Empty or null internal network name is not valid"));
}
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aNATNetwork)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aConnected)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aSpeed)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
if (!aTraceFile)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
// INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
{
detach();
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
AutoWriteLock alock (this);
/* 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 */
AutoWriteLock alock (this);
/* don't do anything if we're already internal network attached */
{
/* first detach the current attachment */
detach();
/* there must an internal network name */
{
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 */
AutoWriteLock alock (this);
{
detach();
/* leave the lock before informing callbacks */
mParent->onNetworkAdapterChange (this);
}
return S_OK;
}
// public methods only for internal purposes
////////////////////////////////////////////////////////////////////////////////
/**
* Loads settings from the given adapter node.
* May be called once right after this object creation.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for writing.
*/
{
using namespace settings;
AutoCaller autoCaller (this);
AutoWriteLock alock (this);
/* Note: we assume that the default values for attributes of optional
* nodes are assigned in the Data::Data() constructor and don't do it
* here. It implies that this method may only be called after constructing
* a new BIOSSettings object while all its data fields are in the default
* values. Exceptions are fields whose creation time defaults don't match
* values that should be applied when these fields are not explicitly set
* in the settings file (for backwards compatibility reasons). This takes
* place when a setting of a newly created object must default to A while
* the same setting of an object loaded from the old settings file must
* default to B. */
/* type (optional, defaults to Am79C970A) */
else
E_FAIL);
/* enabled (required) */
/* MAC address (can be null) */
/* cable (required) */
/* line speed (defaults to 100 Mbps) */
/* tracing (defaults to false) */
/* One of NAT, HostInerface, Internal or nothing */
{
/* NAT */
/* optional */
rc = AttachToNAT();
}
else
{
/* Host Interface Networking */
#ifndef VBOX_WITH_UNIXY_TAP_NETWORKING
/* name can be empty, but not null */
#endif
/* optopnal */
#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
rc = AttachToHostInterface();
}
else
{
/* Internal Networking */
/* required */
}
else
{
/* Adapter has no children */
}
return S_OK;
}
/**
* Saves settings to the given adapter node.
*
* Note that the given Adapter node is comletely empty on input.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for reading.
*/
{
using namespace settings;
AutoCaller autoCaller (this);
AutoReadLock alock (this);
if (mData->mTraceEnabled)
switch (mData->mAdapterType)
{
typeStr = "Am79C970A";
break;
typeStr = "Am79C973";
break;
typeStr = "82540EM";
break;
typeStr = "82543GC";
break;
default:
ComAssertMsgFailedRet (("Invalid network adapter type: %d\n",
E_FAIL);
}
switch (mData->mAttachmentType)
{
{
/* do nothing -- empty content */
break;
}
{
mData->mNATNetwork);
break;
}
{
#ifndef VBOX_WITH_UNIXY_TAP_NETWORKING
#else
#endif
#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
break;
}
{
break;
}
default:
{
}
}
return S_OK;
}
/**
* @note Locks this object for writing.
*/
bool NetworkAdapter::rollback()
{
/* sanity */
AutoCaller autoCaller (this);
AutoWriteLock alock (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 (mPeer is "master" so locked
* first) */
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 (aThat is "master" so locked
* first) */
/* 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 */
#ifndef VBOX_WITH_UNIXY_TAP_NETWORKING
#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];
}