VirtualBox

儲存庫 vbox 的更動 108012


忽略:
時間撮記:
2025-2-1 上午02:19:11 (6 週 以前)
作者:
vboxsync
svn:sync-xref-src-repo-rev:
167284
訊息:

Main: Replaced std::string with com::Utf8Str in the HostDnsService code, santizing all the strings.

位置:
trunk/src/VBox/Main/src-server
檔案:
修改 6 筆資料

圖例:

未更動
新增
刪除
  • trunk/src/VBox/Main/src-server/HostDnsService.cpp

    r108010 r108012  
    4242#include <algorithm>
    4343#include <set>
    44 #include <iprt/sanitized/string>
    4544#include "HostDnsService.h"
    4645
    4746
    4847
    49 static void dumpHostDnsStrVector(const std::string &prefix, const std::vector<std::string> &v)
     48static void dumpHostDnsStrVector(const char *prefix, const std::vector<com::Utf8Str> &v)
    5049{
    5150    int i = 1;
    52     for (std::vector<std::string>::const_iterator it = v.begin();
    53          it != v.end();
    54          ++it, ++i)
    55         LogRel(("  %s %d: %s\n", prefix.c_str(), i, it->c_str()));
     51    for (std::vector<com::Utf8Str>::const_iterator it = v.begin(); it != v.end(); ++it, ++i)
     52        LogRel(("  %s %d: %s\n", prefix, i, it->c_str()));
    5653    if (v.empty())
    57         LogRel(("  no %s entries\n", prefix.c_str()));
     54        LogRel(("  no %s entries\n", prefix));
    5855}
    5956
     
    6259    dumpHostDnsStrVector("server", info.servers);
    6360
    64     if (!info.domain.empty())
     61    if (info.domain.isNotEmpty())
    6562        LogRel(("  domain: %s\n", info.domain.c_str()));
    6663    else
     
    7774    else
    7875    {
    79         std::set<std::string> l(servers.begin(), servers.end());
    80         std::set<std::string> r(info.servers.begin(), info.servers.end());
     76        std::set<com::Utf8Str> l(servers.begin(), servers.end());
     77        std::set<com::Utf8Str> r(info.servers.begin(), info.servers.end());
    8178
    8279        fSameServers = (l == r);
     
    9390
    9491    return fSameServers && fSameDomain && fSameSearchList;
    95 }
    96 
    97 DECLINLINE(void) detachVectorOfStrings(const std::vector<std::string>& v, std::vector<com::Utf8Str> &aArray)
    98 {
    99     aArray.resize(v.size());
    100     size_t i = 0;
    101     for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it, ++i)
    102         aArray[i] = Utf8Str(it->c_str()); /** @todo r=bird: *it isn't necessarily UTF-8 clean!!
    103                                            * On darwin we do silly shit like using CFStringGetSystemEncoding()
    104                                            * that may be UTF-8 but doesn't need to be.
    105                                            *
    106                                            * Why on earth are we using std::string here anyway?
    107                                            */
    10892}
    10993
     
    384368    dumpHostDnsStrVector("name server", m->info.servers);
    385369
    386     detachVectorOfStrings(m->info.servers, aNameServers);
     370    aNameServers = m->info.servers;
    387371
    388372    return S_OK;
     
    394378    AssertReturn(m != NULL, E_FAIL);
    395379
    396     LogRel(("HostDnsMonitorProxy::GetDomainName: %s\n", m->info.domain.empty() ? "no domain set" : m->info.domain.c_str()));
     380    LogRel(("HostDnsMonitorProxy::GetDomainName: %s\n", m->info.domain.isEmpty() ? "no domain set" : m->info.domain.c_str()));
    397381    *pDomainName = m->info.domain.c_str();
    398382
     
    408392    dumpHostDnsStrVector("search string", m->info.searchList);
    409393
    410     detachVectorOfStrings(m->info.searchList, aSearchStrings);
     394    aSearchStrings = m->info.searchList;
    411395
    412396    return S_OK;
  • trunk/src/VBox/Main/src-server/HostDnsService.h

    r108010 r108012  
    3737
    3838#include <list>
    39 #include <iprt/sanitized/string>
    4039#include <vector>
    4140
     
    5352
    5453public:
    55     /** @todo r=bird: Why on earth are we using std::string and not Utf8Str?   */
    56     std::vector<std::string> servers;
    57     std::string domain;
    58     std::vector<std::string> searchList;
     54    std::vector<com::Utf8Str> servers;
     55    com::Utf8Str domain;
     56    std::vector<com::Utf8Str> searchList;
    5957    bool equals(const HostDnsInformation &, uint32_t fLaxComparison = 0) const;
    6058};
     
    109107    mutable RTCLockMtx m_LockMtx;
    110108
    111 public: /** @todo r=andy Why is this public? */
     109//public: /** @todo r=andy Why is this public? */
    112110
    113111    struct Data;
     
    160158public:
    161159
    162     HRESULT init(HostDnsMonitorProxy *pProxy);
    163     void uninit(void);
     160    HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
     161    void uninit(void) RT_OVERRIDE;
    164162
    165163protected:
     
    185183public:
    186184
    187     HRESULT init(HostDnsMonitorProxy *pProxy);
    188     void uninit(void);
     185    HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
     186    void uninit(void) RT_OVERRIDE;
    189187
    190188protected:
     
    215213
    216214    HRESULT init(HostDnsMonitorProxy *pProxy, const char *aResolvConfFileName);
    217     void uninit(void);
    218 
    219     const std::string& getResolvConf(void) const;
     215    void uninit(void) RT_OVERRIDE;
     216
     217    const Utf8Str &getResolvConf(void) const;
    220218
    221219protected:
     
    238236    HostDnsServiceSolaris() {}
    239237    virtual ~HostDnsServiceSolaris() {}
     238
     239public:
     240
     241    virtual HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE
     242    {
     243        return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
     244    }
     245};
     246
     247#  endif
     248#  if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
     249class HostDnsServiceLinux : public HostDnsServiceResolvConf
     250{
     251public:
     252
     253    HostDnsServiceLinux() : HostDnsServiceResolvConf(true), m_fdShutdown(-1) {}
     254    virtual ~HostDnsServiceLinux();
     255
     256public:
     257
     258    HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE;
     259
     260protected:
     261
     262    int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
     263    int monitorThreadProc(void) RT_OVERRIDE;
     264
     265    /** Socket end to write shutdown notification to, so the monitor thread will
     266     *  wake up and terminate. */
     267    int m_fdShutdown;
     268};
     269
     270#  endif
     271#  if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
     272class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
     273{
     274public:
     275
     276    HostDnsServiceFreebsd(){}
     277    virtual ~HostDnsServiceFreebsd() {}
    240278
    241279public:
     
    248286
    249287#  endif
    250 #  if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
    251 class HostDnsServiceLinux : public HostDnsServiceResolvConf
    252 {
    253 public:
    254 
    255     HostDnsServiceLinux() : HostDnsServiceResolvConf(true), m_fdShutdown(-1) {}
    256     virtual ~HostDnsServiceLinux();
    257 
    258 public:
    259 
    260     HRESULT init(HostDnsMonitorProxy *pProxy);
    261 
    262 protected:
    263 
    264     int monitorThreadShutdown(RTMSINTERVAL uTimeoutMs) RT_OVERRIDE;
    265     int monitorThreadProc(void) RT_OVERRIDE;
    266 
    267     /** Socket end to write shutdown notification to, so the monitor thread will
    268      *  wake up and terminate. */
    269     int m_fdShutdown;
    270 };
    271 
    272 #  endif
    273 #  if defined(RT_OS_FREEBSD) || defined(DOXYGEN_RUNNING)
    274 class HostDnsServiceFreebsd: public HostDnsServiceResolvConf
    275 {
    276 public:
    277 
    278     HostDnsServiceFreebsd(){}
    279     virtual ~HostDnsServiceFreebsd() {}
    280 
    281 public:
    282 
    283     virtual HRESULT init(HostDnsMonitorProxy *pProxy)
    284     {
    285         return HostDnsServiceResolvConf::init(pProxy, "/etc/resolv.conf");
    286     }
    287 };
    288 
    289 #  endif
    290288#  if defined(RT_OS_OS2) || defined(DOXYGEN_RUNNING)
    291289class HostDnsServiceOs2 : public HostDnsServiceResolvConf
     
    299297
    300298    /* XXX: \\MPTN\\ETC should be taken from environment variable ETC  */
    301     virtual HRESULT init(HostDnsMonitorProxy *pProxy)
     299    virtual HRESULT init(HostDnsMonitorProxy *pProxy) RT_OVERRIDE
    302300    {
    303301        return HostDnsServiceResolvConf::init(pProxy, "\\MPTN\\ETC\\RESOLV2");
  • trunk/src/VBox/Main/src-server/HostDnsServiceResolvConf.cpp

    r106061 r108012  
    4949#include <VBox/log.h>
    5050
    51 #include <iprt/sanitized/string>
    52 
    5351#include "HostDnsService.h"
    5452#include "../../Devices/Network/slirp/resolv_conf_parser.h"
     
    6260    };
    6361
    64     std::string resolvConfFilename;
     62    com::Utf8Str resolvConfFilename;
    6563};
    6664
     
    9694}
    9795
    98 const std::string& HostDnsServiceResolvConf::getResolvConf(void) const
     96const com::Utf8Str &HostDnsServiceResolvConf::getResolvConf(void) const
    9997{
    10098    return m->resolvConfFilename;
     
    104102{
    105103    struct rcp_state st;
    106 
    107104    st.rcps_flags = RCPSF_NO_STR2IPCONV;
    108105    int vrc = rcp_parse(&st, m->resolvConfFilename.c_str());
     
    114111    {
    115112        AssertBreak(st.rcps_str_nameserver[i]);
     113        RTStrPurgeEncoding(st.rcps_str_nameserver[i]);
    116114        info.servers.push_back(st.rcps_str_nameserver[i]);
    117115    }
    118116
    119117    if (st.rcps_domain)
     118    {
     119        RTStrPurgeEncoding(st.rcps_domain);
    120120        info.domain = st.rcps_domain;
     121    }
    121122
    122123    for (unsigned i = 0; i != st.rcps_num_searchlist; ++i)
    123124    {
    124125        AssertBreak(st.rcps_searchlist[i]);
     126        RTStrPurgeEncoding(st.rcps_searchlist[i]);
    125127        info.searchList.push_back(st.rcps_searchlist[i]);
    126128    }
     129
    127130    setInfo(info);
    128 
    129131    return S_OK;
    130132}
  • trunk/src/VBox/Main/src-server/darwin/HostDnsServiceDarwin.cpp

    r108010 r108012  
    3838#include <SystemConfiguration/SCDynamicStore.h>
    3939
    40 #include <iprt/sanitized/string>
    4140#include <vector>
    4241#include "../HostDnsService.h"
     
    185184}
    186185
     186DECLINLINE(bool) queryCFStringAsUtf8Str(CFStringRef hRefSrc, com::Utf8Str &a_rDst, size_t cbMax)
     187{
     188    a_rDst.reserve(_1K);
     189    if (!CFStringGetCString(hRefSrc, a_rDst.mutableRaw(), (CFIndex)a_rDst.capacity(), kCFStringEncodingUTF8))
     190    {
     191        a_rDst.reserve(cbMax);
     192        if (!CFStringGetCString(hRefSrc, a_rDst.mutableRaw(), (CFIndex)a_rDst.capacity(), kCFStringEncodingUTF8))
     193            return false;
     194    }
     195    RTStrPurgeEncoding(a_rDst.mutableRaw()); /* paranoia */
     196    a_rDst.jolt();
     197    return true;
     198}
     199
     200
    187201int HostDnsServiceDarwin::updateInfo(void)
    188202{
    189203    CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(m->m_store, kStateNetworkGlobalDNSKey);
    190     /**
     204    /*
    191205     * # scutil
    192206     * \> get State:/Network/Global/DNS
     
    205219     * }
    206220     */
    207 
    208221    if (!propertyRef)
    209222        return VINF_SUCCESS;
     223    CFDictionaryRef const propertyAsDictRef = static_cast<CFDictionaryRef>(propertyRef);
    210224
    211225    HostDnsInformation info;
    212     CFStringRef domainNameRef = (CFStringRef)CFDictionaryGetValue(static_cast<CFDictionaryRef>(propertyRef), CFSTR("DomainName"));
     226    com::Utf8Str       strTmp;
     227
     228    CFStringRef const domainNameRef = (CFStringRef)CFDictionaryGetValue(propertyAsDictRef, CFSTR("DomainName"));
    213229    if (domainNameRef)
    214     {
    215         const char *pszDomainName = CFStringGetCStringPtr(domainNameRef, CFStringGetSystemEncoding());
    216         if (pszDomainName)
    217             info.domain = pszDomainName;
    218     }
    219 
    220     CFArrayRef serverArrayRef = (CFArrayRef)CFDictionaryGetValue(static_cast<CFDictionaryRef>(propertyRef),
    221                                                                  CFSTR("ServerAddresses"));
     230        if (queryCFStringAsUtf8Str(domainNameRef, strTmp, _16K))
     231            info.domain = strTmp;
     232
     233    CFArrayRef const serverArrayRef = (CFArrayRef)CFDictionaryGetValue(propertyAsDictRef, CFSTR("ServerAddresses"));
    222234    if (serverArrayRef)
    223235    {
     
    225237        for (CFIndex i = 0; i < cItems; ++i)
    226238        {
    227             CFStringRef serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
    228             if (!serverAddressRef)
    229                 continue;
    230 
    231             /** @todo r=bird: This code is messed up as CFStringGetCStringPtr is documented
    232              *  to return NULL even if the string is valid.   Furthermore, we must have
    233              *  UTF-8 - some joker might decide latin-1 is better here for all we know
    234              *  and we'll end up with evil invalid UTF-8 sequences. */
    235             const char *pszServerAddress = CFStringGetCStringPtr(serverAddressRef, CFStringGetSystemEncoding());
    236             if (!pszServerAddress)
    237                 continue;
    238 
    239             /** @todo r=bird: Why on earth are we using std::string and not Utf8Str?   */
    240             info.servers.push_back(std::string(pszServerAddress));
     239            CFStringRef const serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
     240            if (serverAddressRef)
     241                if (queryCFStringAsUtf8Str(serverAddressRef, strTmp, _16K))
     242                    info.servers.push_back(strTmp);
    241243        }
    242244    }
    243245
    244     CFArrayRef searchArrayRef = (CFArrayRef)CFDictionaryGetValue(static_cast<CFDictionaryRef>(propertyRef),
    245                                                                  CFSTR("SearchDomains"));
     246    CFArrayRef const searchArrayRef = (CFArrayRef)CFDictionaryGetValue(propertyAsDictRef, CFSTR("SearchDomains"));
    246247    if (searchArrayRef)
    247248    {
     
    250251        {
    251252            CFStringRef searchStringRef = (CFStringRef)CFArrayGetValueAtIndex(searchArrayRef, i);
    252             if (!searchStringRef)
    253                 continue;
    254 
    255             /** @todo r=bird: This code is messed up as CFStringGetCStringPtr is documented
    256              *  to return NULL even if the string is valid.   Furthermore, we must have
    257              *  UTF-8 - some joker might decide latin-1 is better here for all we know
    258              *  and we'll end up with evil invalid UTF-8 sequences. */
    259             const char *pszSearchString = CFStringGetCStringPtr(searchStringRef, CFStringGetSystemEncoding());
    260             if (!pszSearchString)
    261                 continue;
    262 
    263             /** @todo r=bird: Why on earth are we using std::string and not Utf8Str?   */
    264             info.searchList.push_back(std::string(pszSearchString));
     253            if (searchStringRef)
     254                if (queryCFStringAsUtf8Str(searchStringRef, strTmp, _64K))
     255                    info.searchList.push_back(strTmp);
    265256        }
    266257    }
  • trunk/src/VBox/Main/src-server/linux/HostDnsServiceLinux.cpp

    r106061 r108012  
    4545#include <string.h>
    4646#include <unistd.h>
     47#include <stdlib.h>
    4748
    4849#include <fcntl.h>
  • trunk/src/VBox/Main/src-server/win/HostDnsServiceWin.cpp

    r106061 r108012  
    4848
    4949#include <algorithm>
    50 #include <iprt/sanitized/sstream>
    51 #include <iprt/sanitized/string>
    5250#include <vector>
    5351
     
    6058                                       hEvent,
    6159                                       TRUE);
    62     AssertMsgReturn(lrc == ERROR_SUCCESS,
    63                     ("Failed to register event on the key. Please debug me!"),
    64                     VERR_INTERNAL_ERROR);
     60    AssertLogRelMsgReturn(lrc == ERROR_SUCCESS,
     61                          ("Failed to register event on the key. Please debug me!"),
     62                          VERR_INTERNAL_ERROR);
    6563
    6664    return VINF_SUCCESS;
    6765}
    6866
    69 static void appendTokenizedStrings(std::vector<std::string> &vecStrings, const std::string &strToAppend, char chDelim = ' ')
    70 {
    71     if (strToAppend.empty())
     67static void appendTokenizedStrings(std::vector<com::Utf8Str> &vecStrings, const com::Utf8Str &strToAppend, char chDelim = ' ')
     68{
     69    if (strToAppend.isEmpty())
    7270        return;
    7371
    74     std::istringstream stream(strToAppend);
    75     std::string substr;
    76 
    77     while (std::getline(stream, substr, chDelim))
    78     {
    79         if (substr.empty())
    80             continue;
    81 
    82         if (std::find(vecStrings.cbegin(), vecStrings.cend(), substr) != vecStrings.cend())
    83             continue;
    84 
    85         vecStrings.push_back(substr);
     72    RTCString const strDelim(1, chDelim);
     73    auto const      lstSubStrings  = strToAppend.split(strDelim);
     74    size_t const    cSubStrings    = lstSubStrings.size();
     75    for (size_t i = 0; i < cSubStrings; i++)
     76    {
     77        RTCString const &strCur = lstSubStrings[i];
     78        if (strCur.isNotEmpty())
     79            if (std::find(vecStrings.cbegin(), vecStrings.cend(), strCur) == vecStrings.cend())
     80                vecStrings.push_back(strCur);
    8681    }
    8782}
     
    9792#define DATA_TIMER            2
    9893#define DATA_MAX_EVENT        3
    99     HANDLE haDataEvent[DATA_MAX_EVENT];
     94    HANDLE ahDataEvents[DATA_MAX_EVENT];
    10095
    10196    Data()
     
    105100
    106101        for (size_t i = 0; i < DATA_MAX_EVENT; ++i)
    107             haDataEvent[i] = NULL;
     102            ahDataEvents[i] = NULL;
    108103    }
    109104
     
    111106    {
    112107        if (hKeyTcpipParameters != NULL)
     108        {
    113109            RegCloseKey(hKeyTcpipParameters);
     110            hKeyTcpipParameters = NULL;
     111        }
    114112
    115113        for (size_t i = 0; i < DATA_MAX_EVENT; ++i)
    116             if (haDataEvent[i] != NULL)
    117                 CloseHandle(haDataEvent[i]);
     114            if (ahDataEvents[i] != NULL)
     115            {
     116                CloseHandle(ahDataEvents[i]);
     117                ahDataEvents[i] = NULL;
     118            }
    118119    }
    119120};
     
    137138        return E_FAIL;
    138139
    139     bool fRc = true;
    140140    LONG lRc = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
    141141                             L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
    142142                             0,
    143                              KEY_READ|KEY_NOTIFY,
     143                             KEY_READ | KEY_NOTIFY,
    144144                             &m->hKeyTcpipParameters);
    145145    if (lRc != ERROR_SUCCESS)
    146146    {
    147147        LogRel(("HostDnsServiceWin: failed to open key Tcpip\\Parameters (error %d)\n", lRc));
    148         fRc = false;
    149     }
    150     else
    151     {
    152         for (size_t i = 0; i < DATA_MAX_EVENT; ++i)
    153         {
    154             HANDLE h;
    155 
    156             if (i ==  DATA_TIMER)
    157                 h = CreateWaitableTimer(NULL, FALSE, NULL);
    158             else
    159                 h = CreateEvent(NULL, TRUE, FALSE, NULL);
    160 
    161             if (h == NULL)
    162             {
    163                 LogRel(("HostDnsServiceWin: failed to create event (error %d)\n", GetLastError()));
    164                 fRc = false;
    165                 break;
    166             }
    167 
    168             m->haDataEvent[i] = h;
    169         }
    170     }
    171 
    172     if (!fRc)
    173148        return E_FAIL;
     149    }
     150
     151    for (size_t i = 0; i < DATA_MAX_EVENT; ++i)
     152    {
     153        HANDLE h;
     154        if (i == DATA_TIMER)
     155            h = CreateWaitableTimer(NULL, FALSE, NULL);
     156        else
     157            h = CreateEvent(NULL, TRUE, FALSE, NULL);
     158        if (h == NULL)
     159        {
     160            LogRel(("HostDnsServiceWin: failed to create %s (error %d)\n",
     161                    i == DATA_TIMER ? "waitable timer" : "event", GetLastError()));
     162            return E_FAIL;
     163        }
     164
     165        m->ahDataEvents[i] = h;
     166    }
    174167
    175168    HRESULT hrc = HostDnsServiceBase::init(pProxy);
     
    187180int HostDnsServiceWin::monitorThreadShutdown(RTMSINTERVAL uTimeoutMs)
    188181{
    189     RT_NOREF(uTimeoutMs);
    190 
    191     AssertPtr(m);
    192     SetEvent(m->haDataEvent[DATA_SHUTDOWN_EVENT]);
    193     /** @todo r=andy Wait for thread? Check vrc here. Timeouts? */
     182    AssertPtrReturn(m, VINF_SUCCESS);
     183
     184    SetEvent(m->ahDataEvents[DATA_SHUTDOWN_EVENT]);
     185    RT_NOREF(uTimeoutMs); /* (Caller waits for the thread) */
    194186
    195187    return VINF_SUCCESS;
     
    201193
    202194    registerNotification(m->hKeyTcpipParameters,
    203                          m->haDataEvent[DATA_DNS_UPDATE_EVENT]);
     195                         m->ahDataEvents[DATA_DNS_UPDATE_EVENT]);
    204196
    205197    onMonitorThreadInitDone();
     
    207199    for (;;)
    208200    {
    209         DWORD dwReady;
    210 
    211         dwReady = WaitForMultipleObjects(DATA_MAX_EVENT, m->haDataEvent,
    212                                          FALSE, INFINITE);
    213 
     201        DWORD dwReady = WaitForMultipleObjects(DATA_MAX_EVENT, m->ahDataEvents, FALSE, INFINITE);
    214202        if (dwReady == WAIT_OBJECT_0 + DATA_SHUTDOWN_EVENT)
    215203            break;
     
    225213                LARGE_INTEGER delay; /* in 100ns units */
    226214                delay.QuadPart = -2 * 1000 * 1000 * 10LL; /* relative: 2s */
    227 
    228                 BOOL ok = SetWaitableTimer(m->haDataEvent[DATA_TIMER], &delay,
    229                                            0, NULL, NULL, FALSE);
    230                 if (ok)
    231                 {
     215                if (SetWaitableTimer(m->ahDataEvents[DATA_TIMER], &delay, 0, NULL, NULL, FALSE))
    232216                    m->fTimerArmed = true;
    233                 }
    234217                else
    235218                {
     
    239222            }
    240223
    241             ResetEvent(m->haDataEvent[DATA_DNS_UPDATE_EVENT]);
     224            ResetEvent(m->ahDataEvents[DATA_DNS_UPDATE_EVENT]);
    242225            registerNotification(m->hKeyTcpipParameters,
    243                                  m->haDataEvent[DATA_DNS_UPDATE_EVENT]);
     226                                 m->ahDataEvents[DATA_DNS_UPDATE_EVENT]);
    244227        }
    245228        else if (dwReady == WAIT_OBJECT_0 + DATA_TIMER)
     
    267250    HostDnsInformation info;
    268251
    269     std::string strDomain;
    270     std::string strSearchList;  /* NB: comma separated, no spaces */
     252    com::Utf8Str strDomain;
     253    com::Utf8Str strSearchList;  /* NB: comma separated, no spaces */
    271254
    272255    /*
     
    279262    for (DWORD regIndex = 0; /**/; ++regIndex)
    280263    {
    281         char keyName[256];
    282         DWORD cbKeyName = sizeof(keyName);
    283         DWORD keyType = 0;
    284         char keyData[1024];
    285         DWORD cbKeyData = sizeof(keyData);
    286 
    287 /** @todo use unicode API. This isn't UTF-8 clean!   */
    288         LSTATUS lrc = RegEnumValueA(m->hKeyTcpipParameters, regIndex,
    289                                     keyName, &cbKeyName, 0,
    290                                     &keyType, (LPBYTE)keyData, &cbKeyData);
     264        WCHAR wszKeyName[256] = {0};
     265        DWORD cbKeyName = RT_ELEMENTS(wszKeyName);
     266        DWORD keyType   = 0;
     267        WCHAR wszKeyData[1024];
     268        DWORD cbKeyData = sizeof(wszKeyData);
     269        LSTATUS lrc = RegEnumValueW(m->hKeyTcpipParameters, regIndex,
     270                                    wszKeyName, &cbKeyName, NULL /*pReserved*/,
     271                                    &keyType, (LPBYTE)wszKeyData, &cbKeyData);
    291272
    292273        if (lrc == ERROR_NO_MORE_ITEMS)
     
    305286            continue;
    306287
    307         if (cbKeyData > 0 && keyData[cbKeyData - 1] == '\0')
    308             --cbKeyData;     /* don't count trailing NUL if present */
    309 
    310         if (RTStrICmp("Domain", keyName) == 0)
    311         {
    312             strDomain.assign(keyData, cbKeyData);
     288        size_t cwcKeyData = cbKeyData / sizeof(wszKeyData[0]);
     289        if (cwcKeyData > 0 && wszKeyData[cwcKeyData - 1] == '\0')
     290            --cwcKeyData;     /* don't count trailing NUL if present */
     291
     292        if (RTUtf16ICmpAscii(wszKeyName, "Domain") == 0)
     293        {
     294            strDomain.assign(wszKeyData, cwcKeyData);
    313295            LogRel2(("HostDnsServiceWin: Domain=\"%s\"\n", strDomain.c_str()));
    314296        }
    315         else if (RTStrICmp("DhcpDomain", keyName) == 0)
    316         {
    317             std::string strDhcpDomain(keyData, cbKeyData);
     297        else if (RTUtf16ICmpAscii(wszKeyName, "SearchList") == 0)
     298        {
     299            strSearchList.assign(wszKeyData, cwcKeyData);
     300            LogRel2(("HostDnsServiceWin: SearchList=\"%s\"\n", strSearchList.c_str()));
     301        }
     302        else if (LogRelIs2Enabled() && RTUtf16ICmpAscii(wszKeyName, "DhcpDomain") == 0)
     303        {
     304            com::Utf8Str strDhcpDomain(wszKeyData, cwcKeyData);
    318305            LogRel2(("HostDnsServiceWin: DhcpDomain=\"%s\"\n", strDhcpDomain.c_str()));
    319306        }
    320         else if (RTStrICmp("SearchList", keyName) == 0)
    321         {
    322             strSearchList.assign(keyData, cbKeyData);
    323             LogRel2(("HostDnsServiceWin: SearchList=\"%s\"\n", strSearchList.c_str()));
    324         }
    325307    }
    326308
    327309    /* statically configured domain name */
    328     if (!strDomain.empty())
     310    if (strDomain.isNotEmpty())
    329311    {
    330312        info.domain = strDomain;
     
    333315
    334316    /* statically configured search list */
    335     if (!strSearchList.empty())
     317    if (strSearchList.isNotEmpty())
    336318        appendTokenizedStrings(info.searchList, strSearchList, ',');
    337319
     
    343325     * right thing there.
    344326     */
    345     DNS_STATUS status;
    346327    PIP4_ARRAY pIp4Array = NULL;
    347 
    348     // NB: must be set on input it seems, despite docs' claim to the contrary.
    349     DWORD cbBuffer = sizeof(&pIp4Array);
    350 
    351     status = DnsQueryConfig(DnsConfigDnsServerList,
    352                             DNS_CONFIG_FLAG_ALLOC, NULL, NULL,
    353                             &pIp4Array, &cbBuffer);
    354 
     328    DWORD      cbBuffer  = sizeof(&pIp4Array); // NB: must be set on input it seems, despite docs' claim to the contrary.
     329    DNS_STATUS status = DnsQueryConfig(DnsConfigDnsServerList, DNS_CONFIG_FLAG_ALLOC, NULL, NULL, &pIp4Array, &cbBuffer);
    355330    if (status == NO_ERROR && pIp4Array != NULL)
    356331    {
    357332        for (DWORD i = 0; i < pIp4Array->AddrCount; ++i)
    358333        {
    359             char szAddrStr[16] = "";
    360             RTStrPrintf(szAddrStr, sizeof(szAddrStr), "%RTnaipv4", pIp4Array->AddrArray[i]);
    361 
    362             LogRel2(("HostDnsServiceWin: server %d: %s\n", i+1,  szAddrStr));
    363             info.servers.push_back(szAddrStr);
     334            char szAddr[16] = "";
     335            RTStrPrintf(szAddr, sizeof(szAddr), "%RTnaipv4", pIp4Array->AddrArray[i]);
     336
     337            LogRel2(("HostDnsServiceWin: server %d: %s\n", i+1,  szAddr));
     338            info.servers.push_back(szAddr);
    364339        }
    365340
     
    368343
    369344
    370     /**
     345    /*
    371346     * DnsQueryConfig(DnsConfigSearchList, ...) is not implemented.
    372347     * Call GetAdaptersAddresses() that orders the returned list
    373348     * appropriately and collect IP_ADAPTER_ADDRESSES::DnsSuffix.
    374349     */
    375     do {
    376         PIP_ADAPTER_ADDRESSES pAddrBuf = NULL;
    377         ULONG cbAddrBuf = 8 * 1024;
    378         bool fReallocated = false;
    379         ULONG err;
    380 
    381         pAddrBuf = (PIP_ADAPTER_ADDRESSES) malloc(cbAddrBuf);
     350    do /* not a loop */
     351    {
     352        ULONG                 cbAddrBuf    = _8K;
     353        PIP_ADAPTER_ADDRESSES pAddrBuf     = (PIP_ADAPTER_ADDRESSES)RTMemAllocZ(cbAddrBuf);
    382354        if (pAddrBuf == NULL)
    383355        {
    384             LogRel2(("HostDnsServiceWin: failed to allocate %zu bytes"
    385                      " of GetAdaptersAddresses buffer\n",
    386                      (size_t)cbAddrBuf));
     356            LogRel2(("HostDnsServiceWin: failed to allocate %zu bytes of GetAdaptersAddresses buffer\n", (size_t)cbAddrBuf));
    387357            break;
    388358        }
    389359
    390         while (pAddrBuf != NULL)
    391         {
    392             ULONG cbAddrBufProvided = cbAddrBuf;
    393 
    394             err = GetAdaptersAddresses(AF_UNSPEC,
    395                                          GAA_FLAG_SKIP_ANYCAST
    396                                        | GAA_FLAG_SKIP_MULTICAST,
    397                                        NULL,
    398                                        pAddrBuf, &cbAddrBuf);
     360        for (unsigned iReallocLoops = 0; ; iReallocLoops++)
     361        {
     362            ULONG const cbAddrBufProvided = cbAddrBuf; /* for logging */
     363
     364            ULONG err = GetAdaptersAddresses(AF_UNSPEC,
     365                                             GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST,
     366                                             NULL,
     367                                             pAddrBuf, &cbAddrBuf);
    399368            if (err == NO_ERROR)
     369                break;
     370            if (err == ERROR_BUFFER_OVERFLOW)
    400371            {
    401                 break;
    402             }
    403             else if (err == ERROR_BUFFER_OVERFLOW)
    404             {
    405                 LogRel2(("HostDnsServiceWin: provided GetAdaptersAddresses with %zu"
    406                          " but asked again for %zu bytes\n",
     372                LogRel2(("HostDnsServiceWin: provided GetAdaptersAddresses with %zu but asked again for %zu bytes\n",
    407373                         (size_t)cbAddrBufProvided, (size_t)cbAddrBuf));
    408 
    409                 if (RT_UNLIKELY(fReallocated)) /* what? again?! */
     374                if (iReallocLoops < 16)
    410375                {
    411                     LogRel2(("HostDnsServiceWin: ... not going to realloc again\n"));
    412                     free(pAddrBuf);
    413                     pAddrBuf = NULL;
    414                     break;
     376                    /* Reallocate the buffer and try again. */
     377                    void * const pvNew = RTMemRealloc(pAddrBuf, cbAddrBuf);
     378                    if (pvNew)
     379                    {
     380                        pAddrBuf = (PIP_ADAPTER_ADDRESSES)pvNew;
     381                        continue;
     382                    }
     383
     384                    LogRel2(("HostDnsServiceWin: failed to reallocate %zu bytes\n", (size_t)cbAddrBuf));
    415385                }
    416 
    417                 PIP_ADAPTER_ADDRESSES pNewBuf = (PIP_ADAPTER_ADDRESSES) realloc(pAddrBuf, cbAddrBuf);
    418                 if (pNewBuf == NULL)
    419                 {
    420                     LogRel2(("HostDnsServiceWin: failed to reallocate %zu bytes\n", (size_t)cbAddrBuf));
    421                     free(pAddrBuf);
    422                     pAddrBuf = NULL;
    423                     break;
    424                 }
    425 
    426                 /* try again */
    427                 pAddrBuf = pNewBuf; /* cbAddrBuf already updated */
    428                 fReallocated = true;
     386                else
     387                    LogRel2(("HostDnsServiceWin: iReallocLoops=%d - giving up!\n", iReallocLoops));
    429388            }
    430389            else
     390                LogRel2(("HostDnsServiceWin: GetAdaptersAddresses error %d\n", err));
     391            RTMemFree(pAddrBuf);
     392            pAddrBuf = NULL;
     393            break;
     394        }
     395        if (pAddrBuf)
     396        {
     397            for (PIP_ADAPTER_ADDRESSES pAdp = pAddrBuf; pAdp != NULL; pAdp = pAdp->Next)
    431398            {
    432                 LogRel2(("HostDnsServiceWin: GetAdaptersAddresses error %d\n", err));
    433                 free(pAddrBuf);
    434                 pAddrBuf = NULL;
    435                 break;
     399                LogRel2(("HostDnsServiceWin: %ls (status %u) ...\n",
     400                         pAdp->FriendlyName ? pAdp->FriendlyName : L"(null)", pAdp->OperStatus));
     401
     402                if (pAdp->OperStatus != IfOperStatusUp)
     403                    continue;
     404
     405                if (pAdp->DnsSuffix == NULL || *pAdp->DnsSuffix == L'\0')
     406                    continue;
     407
     408                char *pszDnsSuffix = NULL;
     409                int vrc = RTUtf16ToUtf8Ex(pAdp->DnsSuffix, RTSTR_MAX, &pszDnsSuffix, 0, /* allocate */ NULL);
     410                if (RT_FAILURE(vrc))
     411                {
     412                    LogRel2(("HostDnsServiceWin: failed to convert DNS suffix \"%ls\": %Rrc\n", pAdp->DnsSuffix, vrc));
     413                    continue;
     414                }
     415
     416                AssertContinue(pszDnsSuffix != NULL);
     417                AssertContinue(*pszDnsSuffix != '\0');
     418                LogRel2(("HostDnsServiceWin: ... suffix = \"%s\"\n", pszDnsSuffix));
     419
     420                appendTokenizedStrings(info.searchList, pszDnsSuffix);
     421                RTStrFree(pszDnsSuffix);
    436422            }
    437         }
    438 
    439         if (pAddrBuf == NULL)
    440             break;
    441 
    442         for (PIP_ADAPTER_ADDRESSES pAdp = pAddrBuf; pAdp != NULL; pAdp = pAdp->Next)
    443         {
    444             LogRel2(("HostDnsServiceWin: %ls (status %u) ...\n",
    445                      pAdp->FriendlyName ? pAdp->FriendlyName : L"(null)", pAdp->OperStatus));
    446 
    447             if (pAdp->OperStatus != IfOperStatusUp)
    448                 continue;
    449 
    450             if (pAdp->DnsSuffix == NULL || *pAdp->DnsSuffix == L'\0')
    451                 continue;
    452 
    453             char *pszDnsSuffix = NULL;
    454             int vrc = RTUtf16ToUtf8Ex(pAdp->DnsSuffix, RTSTR_MAX, &pszDnsSuffix, 0, /* allocate */ NULL);
    455             if (RT_FAILURE(vrc))
    456             {
    457                 LogRel2(("HostDnsServiceWin: failed to convert DNS suffix \"%ls\": %Rrc\n", pAdp->DnsSuffix, vrc));
    458                 continue;
    459             }
    460 
    461             AssertContinue(pszDnsSuffix != NULL);
    462             AssertContinue(*pszDnsSuffix != '\0');
    463             LogRel2(("HostDnsServiceWin: ... suffix = \"%s\"\n", pszDnsSuffix));
    464 
    465             appendTokenizedStrings(info.searchList, pszDnsSuffix);
    466             RTStrFree(pszDnsSuffix);
    467         }
    468 
    469         free(pAddrBuf);
     423
     424            RTMemFree(pAddrBuf);
     425        }
    470426    } while (0);
    471427
    472428
    473     if (info.domain.empty() && !info.searchList.empty())
     429    if (info.domain.isEmpty() && !info.searchList.empty())
    474430        info.domain = info.searchList[0];
    475431
    476     if (info.searchList.size() == 1)
     432    if (info.searchList.size() == 1) /* ?? */
    477433        info.searchList.clear();
    478434
注意: 瀏覽 TracChangeset 來幫助您使用更動檢視器

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