VirtualBox

Changeset 19233 in vbox


Ignore:
Timestamp:
Apr 28, 2009 10:16:37 AM (16 years ago)
Author:
vboxsync
Message:

#2946 & #2954: Main API now supports dynamic adding/removing network adapters on all platforms. Linux backend.

Location:
trunk/src/VBox/Main
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/darwin/NetIf-darwin.cpp

    r18732 r19233  
    339339    return rc;
    340340}
     341
     342int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     343{
     344    return VERR_NOT_IMPLEMENTED;
     345}
     346
    341347#endif
  • trunk/src/VBox/Main/generic/NetIf-generic.cpp

    r17997 r19233  
    2828
    2929#include "HostNetworkInterfaceImpl.h"
     30#include "ProgressImpl.h"
     31#include "VirtualBoxImpl.h"
    3032#include "netif.h"
    3133
    3234#define VBOXNETADPCTL_NAME "VBoxNetAdpCtl"
    3335
    34 static int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
    35 {
    36     const char *args[] = { NULL, NULL, pszAddr, pszOption, pszMask, NULL };
     36static int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask)
     37{
     38    const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL };
    3739
    3840    char szAdpCtl[RTPATH_MAX];
     
    4547    strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME);
    4648    args[0] = szAdpCtl;
    47     Bstr interfaceName;
    48     pIf->COMGETTER(Name)(interfaceName.asOutParam());
    49     Utf8Str strName(interfaceName);
    50     args[1] = strName;
    5149    if (!RTPathExists(szAdpCtl))
    5250    {
     
    7371}
    7472
     73static int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
     74{
     75    Bstr interfaceName;
     76    pIf->COMGETTER(Name)(interfaceName.asOutParam());
     77    Utf8Str strName(interfaceName);
     78    return NetIfAdpCtl(strName, pszAddr, pszOption, pszMask);
     79}
     80
    7581int NetIfEnableStaticIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * pIf, ULONG aOldIp, ULONG aNewIp, ULONG aMask)
    7682{
     
    120126}
    121127
    122 int NetIfCreateHostOnlyNetworkInterface (VirtualBox * /* pVbox */, IHostNetworkInterface ** /* aHostNetworkInterface */, IProgress ** /* aProgress */)
    123 {
    124     return VERR_NOT_IMPLEMENTED;
    125 }
    126 
    127 int NetIfRemoveHostOnlyNetworkInterface (VirtualBox * /* pVbox */, IN_GUID /* aId */, IHostNetworkInterface ** /* aHostNetworkInterface */, IProgress ** /* aProgress */)
    128 {
    129     return VERR_NOT_IMPLEMENTED;
     128
     129int NetIfCreateHostOnlyNetworkInterface (VirtualBox *pVBox, IHostNetworkInterface **aHostNetworkInterface, IProgress **aProgress)
     130{
     131#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
     132    /* create a progress object */
     133    ComObjPtr <Progress> progress;
     134    progress.createObject();
     135
     136    ComPtr<IHost> host;
     137    HRESULT rc = pVBox->COMGETTER(Host)(host.asOutParam());
     138    if(SUCCEEDED(rc))
     139    {
     140        rc = progress->init (pVBox, host,
     141                             Bstr ("Creating host only network interface"),
     142                             FALSE /* aCancelable */);
     143        if(SUCCEEDED(rc))
     144        {
     145            CheckComRCReturnRC (rc);
     146            progress.queryInterfaceTo (aProgress);
     147
     148            char szAdpCtl[RTPATH_MAX];
     149            int rc = RTPathProgram(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
     150            if (RT_FAILURE(rc))
     151            {
     152                progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
     153                                         "Failed to get program path, rc=%Vrc.\n", rc);
     154                return rc;
     155            }
     156            strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " add");
     157            FILE *fp = popen(szAdpCtl, "r");
     158
     159            if (fp)
     160            {
     161                char szBuf[VBOXNET_MAX_SHORT_NAME];
     162                if (fgets(szBuf, sizeof(szBuf), fp))
     163                {
     164                    char *pLast = szBuf + strlen(szBuf) - 1;
     165                    if (pLast >= szBuf && *pLast == '\n')
     166                        *pLast = 0;
     167
     168                    NETIFINFO Info;
     169                    Bstr IfName(szBuf);
     170                    rc = NetIfGetConfigByName(IfName, &Info);
     171                    if (RT_FAILURE(rc))
     172                    {
     173                        progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
     174                                                 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add').\n", szBuf);
     175                    }
     176                    else
     177                    {
     178                        /* create a new uninitialized host interface object */
     179                        ComObjPtr <HostNetworkInterface> iface;
     180                        iface.createObject();
     181                        iface->init(IfName, HostNetworkInterfaceType_HostOnly, &Info);
     182                        iface.queryInterfaceTo (aHostNetworkInterface);
     183                    }
     184                }
     185                if ((rc = pclose(fp)) != 0)
     186                {
     187                    progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d).", rc);
     188                    rc = VERR_INTERNAL_ERROR;
     189                }
     190            }
     191            if (RT_SUCCESS(rc))
     192                progress->notifyComplete(rc);
     193        }
     194    }
     195
     196    return rc;
     197
     198#else
     199    return VERR_NOT_IMPLEMENTED;
     200#endif
     201}
     202
     203int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVBox, IN_GUID aId, IHostNetworkInterface **aHostNetworkInterface, IProgress **aProgress)
     204{
     205#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
     206    /* create a progress object */
     207    ComObjPtr <Progress> progress;
     208    progress.createObject();
     209    ComPtr<IHost> host;
     210    int rc = VINF_SUCCESS;
     211    HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
     212    if(SUCCEEDED(hr))
     213    {
     214        Bstr ifname;
     215        ComPtr <IHostNetworkInterface> iface;
     216        if (FAILED (host->FindHostNetworkInterfaceById (aId, iface.asOutParam())))
     217            return VERR_INVALID_PARAMETER;
     218        iface->COMGETTER (Name) (ifname.asOutParam());
     219        if (ifname.isNull())
     220            return VERR_INTERNAL_ERROR;
     221
     222        rc = progress->init (pVBox, host,
     223                            Bstr ("Removing host network interface"),
     224                            FALSE /* aCancelable */);
     225        if(SUCCEEDED(rc))
     226        {
     227            CheckComRCReturnRC (rc);
     228            progress.queryInterfaceTo (aProgress);
     229            iface.queryInterfaceTo (aHostNetworkInterface);
     230            rc = NetIfAdpCtl(Utf8Str(ifname), "remove", NULL, NULL);
     231            if (RT_FAILURE(rc))
     232                progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d).", rc);
     233            else
     234                progress->notifyComplete(S_OK);
     235        }
     236    }
     237    else
     238    {
     239        progress->notifyComplete(hr);
     240        rc = VERR_INTERNAL_ERROR;
     241    }
     242    return rc;
     243#else
     244    return VERR_NOT_IMPLEMENTED;
     245#endif
    130246}
    131247
  • trunk/src/VBox/Main/include/netif.h

    r18014 r19233  
    2929#define VBOXNET_IPV4ADDR_DEFAULT "192.168.56.1"
    3030#define VBOXNET_IPV4MASK_DEFAULT "255.255.255.0"
     31
     32#define VBOXNET_MAX_SHORT_NAME 50
    3133
    3234#if 1
     
    6769    NETIFSTATUS    enmStatus;
    6870    RTUUID         Uuid;
    69     char           szShortName[50];
     71    char           szShortName[VBOXNET_MAX_SHORT_NAME];
    7072    char           szName[1];
    7173} NETIFINFO;
     
    8486int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVbox, IN_GUID aId, IHostNetworkInterface **aHostNetworkInterface, IProgress **aProgress);
    8587int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *);
     88int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo);
    8689int NetIfDhcpRediscover(VirtualBox *pVbox, HostNetworkInterface * pIf);
    8790
  • trunk/src/VBox/Main/linux/NetIf-linux.cpp

    r18732 r19233  
    162162        close(sock);
    163163    }
     164    else
     165        rc = VERR_INTERNAL_ERROR;
    164166
    165167    return rc;
    166168}
     169
     170int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     171{
     172    int rc = VINF_SUCCESS;
     173    int sock = socket(AF_INET, SOCK_DGRAM, 0);
     174    if (sock < 0)
     175        return VERR_NOT_IMPLEMENTED;
     176    rc = getInterfaceInfo(sock, Utf8Str(aName).c_str(), pInfo);
     177    close(sock);
     178    return rc;
     179}
  • trunk/src/VBox/Main/solaris/NetIf-solaris.cpp

    r18732 r19233  
    386386    return VINF_SUCCESS;
    387387}
     388
    388389#else
    389390int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
     
    392393}
    393394#endif
     395
     396int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     397{
     398    return VERR_NOT_IMPLEMENTED;
     399}
     400
  • trunk/src/VBox/Main/win/NetIf-win.cpp

    r19218 r19233  
    10391039    return VERR_GENERAL_FAILURE;
    10401040#endif
     1041}
     1042
     1043int NetIfGetConfigByName(IN_BSTR /* aName */, NETIFINFO *)
     1044{
     1045    return VERR_NOT_IMPLEMENTED;
    10411046}
    10421047
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette