VirtualBox

Changeset 105726 in vbox for trunk/src/VBox/Main/src-client


Ignore:
Timestamp:
Aug 19, 2024 2:05:15 PM (7 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
164430
Message:

Devices/Network: cleaned up header file, memory leak fixes, search domains pushed to guest on host network change only. bugref:10268

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r105605 r105726  
    45124512
    45134513
    4514 /*
     4514/** Helper that converts a BSTR safe array into a C-string array. */
     4515static char **bstrSafeArrayToC(SafeArray<BSTR> const &a_rStrings, size_t *pcEntries) RT_NOEXCEPT
     4516{
     4517    if (pcEntries)
     4518        *pcEntries = 0;
     4519
     4520    /*
     4521     * The array is NULL terminated.
     4522     */
     4523    const size_t cStrings = a_rStrings.size();
     4524    char **papszRet = (char **)RTMemAllocZ((cStrings + 1) * sizeof(papszRet[0]));
     4525    AssertReturn(papszRet, NULL);
     4526
     4527    /*
     4528     * The individual strings.
     4529     */
     4530    for (size_t i = 0; i < cStrings; i++)
     4531    {
     4532        int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_rStrings[i], RTSTR_MAX, &papszRet[i], 0, NULL);
     4533        AssertRC(vrc);
     4534        if (RT_FAILURE(vrc))
     4535        {
     4536            while (i-- > 0)
     4537            {
     4538                RTStrFree(papszRet[i]);
     4539                papszRet[i] = NULL;
     4540            }
     4541            return NULL;
     4542        }
     4543
     4544    }
     4545        if (pcEntries)
     4546            *pcEntries = cStrings;
     4547    return papszRet;
     4548}
     4549
     4550
     4551/**
    45154552 * IHostNameResolutionConfigurationChangeEvent
    45164553 *
     
    45204557HRESULT Console::i_onNATDnsChanged()
    45214558{
    4522     HRESULT hrc;
    4523 
    45244559    AutoCaller autoCaller(this);
    45254560    AssertComRCReturnRC(autoCaller.hrc());
    45264561
    4527     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
    4528 
    4529 #if 0 /* XXX: We don't yet pass this down to pfnNotifyDnsChanged */
    4530     ComPtr<IVirtualBox> pVirtualBox;
    4531     hrc = mMachine->COMGETTER(Parent)(pVirtualBox.asOutParam());
     4562    /* We wrap PDMINETWORKNATDNSCONFIG to simplify freeing memory allocations
     4563       in it (exceptions, AssertReturn, regular returns). */
     4564    struct DnsConfigCleanupWrapper
     4565    {
     4566        PDMINETWORKNATDNSCONFIG Core;
     4567
     4568        DnsConfigCleanupWrapper()
     4569        {
     4570            Core.szDomainName[0]    = '\0';
     4571            Core.cNameServers       = 0;
     4572            Core.papszNameServers   = NULL;
     4573            Core.cSearchDomains     = 0;
     4574            Core.papszSearchDomains = NULL;
     4575        }
     4576
     4577        void freeStrArray(char **papsz)
     4578        {
     4579            if (papsz)
     4580            {
     4581                for (size_t i = 0; papsz[i] != NULL; i++)
     4582                    RTStrFree(papsz[i]);
     4583                RTMemFree(papsz);
     4584            }
     4585        }
     4586
     4587        ~DnsConfigCleanupWrapper()
     4588        {
     4589            freeStrArray((char **)Core.papszNameServers);
     4590            Core.papszNameServers   = NULL;
     4591            freeStrArray((char **)Core.papszSearchDomains);
     4592            Core.papszSearchDomains = NULL;
     4593        }
     4594    } DnsConfig;
     4595
     4596
     4597    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /** @todo r=bird: Why a write lock? */
     4598
     4599    ComPtr<IVirtualBox> ptrVirtualBox;
     4600    HRESULT hrc = mMachine->COMGETTER(Parent)(ptrVirtualBox.asOutParam());
    45324601    if (FAILED(hrc))
    45334602        return S_OK;
    45344603
    4535     ComPtr<IHost> pHost;
    4536     hrc = pVirtualBox->COMGETTER(Host)(pHost.asOutParam());
     4604    ComPtr<IHost> ptrHost;
     4605    hrc = ptrVirtualBox->COMGETTER(Host)(ptrHost.asOutParam());
    45374606    if (FAILED(hrc))
    45384607        return S_OK;
    45394608
    4540     SafeArray<BSTR> aNameServers;
    4541     hrc = pHost->COMGETTER(NameServers)(ComSafeArrayAsOutParam(aNameServers));
    4542     if (FAILED(hrc))
    4543         return S_OK;
    4544 
    4545     const size_t cNameServers = aNameServers.size();
    4546     Log(("DNS change - %zu nameservers\n", cNameServers));
    4547 
    4548     for (size_t i = 0; i < cNameServers; ++i)
    4549     {
    4550         com::Utf8Str strNameServer(aNameServers[i]);
    4551         Log(("- nameserver[%zu] = \"%s\"\n", i, strNameServer.c_str()));
    4552     }
    4553 
    4554     com::Bstr domain;
    4555     pHost->COMGETTER(DomainName)(domain.asOutParam());
    4556     Log(("domain name = \"%s\"\n", com::Utf8Str(domain).c_str()));
    4557 #endif /* 0 */
    4558 
    4559     ComPtr<IPlatform> pPlatform;
    4560     hrc = mMachine->COMGETTER(Platform)(pPlatform.asOutParam());
     4609    /* Domain name: */
     4610    {
     4611        com::Bstr bstrDomain;
     4612        ptrHost->COMGETTER(DomainName)(bstrDomain.asOutParam());
     4613        com::Utf8Str const strDomainName(bstrDomain);
     4614        int vrc = RTStrCopy(DnsConfig.Core.szDomainName, sizeof(DnsConfig.Core.szDomainName), strDomainName.c_str());
     4615        AssertRC(vrc);
     4616    }
     4617    Log(("domain name = \"%s\"\n", DnsConfig.Core.szDomainName));
     4618
     4619    /* Name servers: */
     4620    {
     4621        SafeArray<BSTR> nameServers;
     4622        hrc = ptrHost->COMGETTER(NameServers)(ComSafeArrayAsOutParam(nameServers));
     4623        if (FAILED(hrc))
     4624            return S_OK;
     4625        DnsConfig.Core.papszNameServers = bstrSafeArrayToC(nameServers, &DnsConfig.Core.cNameServers);
     4626        if (!DnsConfig.Core.papszNameServers)
     4627            return E_OUTOFMEMORY;
     4628    }
     4629    Log(("DNS change - %zu nameservers\n", DnsConfig.Core.cNameServers));
     4630    for (size_t i = 0; i < DnsConfig.Core.cNameServers; i++)
     4631        Log(("- papszNameServers[%zu] = \"%s\"\n", i, DnsConfig.Core.papszNameServers[i]));
     4632
     4633    /* Search domains: */
     4634    {
     4635        SafeArray<BSTR> searchDomains;
     4636        hrc = ptrHost->COMGETTER(SearchStrings)(ComSafeArrayAsOutParam(searchDomains));
     4637        if (FAILED(hrc))
     4638            return S_OK;
     4639        DnsConfig.Core.papszSearchDomains = bstrSafeArrayToC(searchDomains, &DnsConfig.Core.cSearchDomains);
     4640        if (!DnsConfig.Core.papszSearchDomains)
     4641            return E_OUTOFMEMORY;
     4642    }
     4643    Log(("Search Domain change - %u domains\n", DnsConfig.Core.cSearchDomains));
     4644    for (size_t i = 0; i < DnsConfig.Core.cSearchDomains; i++)
     4645        Log(("- papszSearchDomain[%zu] = \"%s\"\n", i, DnsConfig.Core.papszSearchDomains[i]));
     4646
     4647    /*
     4648     * Notify all the NAT drivers.
     4649     */
     4650    /** @todo r=bird: This is the worst way of "enumerating" network devices
     4651     *        ever conceived. */
     4652    ComPtr<IPlatform> ptrPlatform;
     4653    hrc = mMachine->COMGETTER(Platform)(ptrPlatform.asOutParam());
    45614654    AssertComRCReturn(hrc, hrc);
    45624655
    45634656    ChipsetType_T enmChipsetType;
    4564     hrc = pPlatform->COMGETTER(ChipsetType)(&enmChipsetType);
     4657    hrc = ptrPlatform->COMGETTER(ChipsetType)(&enmChipsetType);
    45654658    AssertComRCReturn(hrc, hrc);
    45664659
     
    45704663        ULONG const ulInstanceMax = PlatformProperties::s_getMaxNetworkAdapters(enmChipsetType);
    45714664
    4572         notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "pcnet", ulInstanceMax);
    4573         notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "e1000", ulInstanceMax);
    4574         notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "virtio-net", ulInstanceMax);
     4665        notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "pcnet", ulInstanceMax, &DnsConfig.Core);
     4666        notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "e1000", ulInstanceMax, &DnsConfig.Core);
     4667        notifyNatDnsChange(ptrVM.rawUVM(), ptrVM.vtable(), "virtio-net", ulInstanceMax, &DnsConfig.Core);
    45754668    }
    45764669
     
    45794672
    45804673
    4581 /*
     4674/**
    45824675 * This routine walks over all network device instances, checking if
    45834676 * device instance has DrvNAT attachment and triggering DrvNAT DNS
    45844677 * change callback.
    45854678 */
    4586 void Console::notifyNatDnsChange(PUVM pUVM, PCVMMR3VTABLE pVMM, const char *pszDevice, ULONG ulInstanceMax)
     4679void Console::notifyNatDnsChange(PUVM pUVM, PCVMMR3VTABLE pVMM, const char *pszDevice, ULONG ulInstanceMax,
     4680                                 PCPDMINETWORKNATDNSCONFIG pDnsConfig)
    45874681{
    45884682    Log(("notifyNatDnsChange: looking for DrvNAT attachment on %s device instances\n", pszDevice));
     
    46004694            pNetNatCfg = (PPDMINETWORKNATCONFIG)pBase->pfnQueryInterface(pBase, PDMINETWORKNATCONFIG_IID);
    46014695            if (pNetNatCfg && pNetNatCfg->pfnNotifyDnsChanged)
    4602                 pNetNatCfg->pfnNotifyDnsChanged(pNetNatCfg);
     4696                pNetNatCfg->pfnNotifyDnsChanged(pNetNatCfg, pDnsConfig);
    46034697        }
    46044698    }
Note: See TracChangeset for help on using the changeset viewer.

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