VirtualBox

Changeset 49836 in vbox


Ignore:
Timestamp:
Dec 9, 2013 11:50:26 AM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
91179
Message:

VBoxNetLwipNAT.cpp: extract fetchNatPortForwardRules() to fetch ipv4 and ipv6 port-forward-rules.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r49835 r49836  
    119119typedef VECNATSERVICEPF::const_iterator CITERATORNATSERVICEPF;
    120120
     121static int fetchNatPortForwardRules(const ComNatPtr&, bool, VECNATSERVICEPF&);
    121122
    122123
     
    762763    if (!fDontLoadRulesOnStartup)
    763764    {
    764         /* XXX: extract function and do not duplicate */
    765         com::SafeArray<BSTR> rules;
    766         hrc = m_net->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
    767         Assert(SUCCEEDED(hrc));
    768 
    769         size_t idxRules = 0;
    770         for (idxRules = 0; idxRules < rules.size(); ++idxRules)
     765        fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4);
     766        fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6);
     767    } /* if (!fDontLoadRulesOnStartup) */
     768
     769    AddressToOffsetMapping tmp;
     770    rc = localMappings(m_net, tmp);
     771    if (RT_SUCCESS(rc) && tmp.size() != 0)
     772    {
     773        unsigned long i = 0;
     774        for (AddressToOffsetMapping::iterator it = tmp.begin();
     775             it != tmp.begin() && i < RT_ELEMENTS(m_lo2off);
     776             ++it, ++i)
    771777        {
    772             Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
    773             NATSEVICEPORTFORWARDRULE Rule;
    774             RT_ZERO(Rule);
    775             rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
    776             AssertRC(rc);
    777             m_vecPortForwardRule4.push_back(Rule);
     778            ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u);
     779            m_lo2off[i].off = it->second;
    778780        }
    779781
    780         rules.setNull();
    781         hrc = m_net->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
    782         Assert(SUCCEEDED(hrc));
    783 
    784         for (idxRules = 0; idxRules < rules.size(); ++idxRules)
    785         {
    786             Log(("%d-rule: %ls\n", idxRules, rules[idxRules]));
    787             NATSEVICEPORTFORWARDRULE Rule;
    788             netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 1, &Rule.Pfr);
    789             m_vecPortForwardRule6.push_back(Rule);
    790         }
    791     } /* if (!fDontLoadRulesOnStartup) */
    792 
    793     com::SafeArray<BSTR> strs;
    794     int count_strs;
    795     hrc = m_net->COMGETTER(LocalMappings)(ComSafeArrayAsOutParam(strs));
    796     if (   SUCCEEDED(hrc)
    797            && (count_strs = strs.size()))
    798     {
    799         unsigned int j = 0;
    800         int i;
    801 
    802         for (i = 0; i < count_strs && j < RT_ELEMENTS(m_lo2off); ++i)
    803         {
    804             char szAddr[17];
    805             RTNETADDRIPV4 ip4addr;
    806             char *pszTerm;
    807             uint32_t u32Off;
    808             com::Utf8Str strLo2Off(strs[i]);
    809             const char *pszLo2Off = strLo2Off.c_str();
    810 
    811             RT_ZERO(szAddr);
    812 
    813             pszTerm = RTStrStr(pszLo2Off, "=");
    814 
    815             if (   !pszTerm
    816                 || (pszTerm - pszLo2Off) >= 17)
    817                 continue;
    818 
    819             memcpy(szAddr, pszLo2Off, (pszTerm - pszLo2Off));
    820             rc = RTNetStrToIPv4Addr(szAddr, &ip4addr);
    821             if (RT_FAILURE(rc))
    822                 continue;
    823 
    824             u32Off = RTStrToUInt32(pszTerm + 1);
    825             if (u32Off == 0)
    826                 continue;
    827 
    828             ip4_addr_set_u32(&m_lo2off[j].loaddr, ip4addr.u);
    829             m_lo2off[j].off = u32Off;
    830             ++j;
    831         }
    832 
    833782        m_loOptDescriptor.lomap = m_lo2off;
    834         m_loOptDescriptor.num_lomap = j;
     783        m_loOptDescriptor.num_lomap = i;
    835784        m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
    836785    }
     
    11521101    delete g_pLwipNat;
    11531102    return 0;
     1103}
     1104
     1105
     1106static int fetchNatPortForwardRules(const ComNatPtr& nat, bool fIsIPv6, VECNATSERVICEPF& vec)
     1107{
     1108    HRESULT hrc;
     1109    com::SafeArray<BSTR> rules;
     1110    if (fIsIPv6)
     1111        hrc = nat->COMGETTER(PortForwardRules6)(ComSafeArrayAsOutParam(rules));
     1112    else
     1113        hrc = nat->COMGETTER(PortForwardRules4)(ComSafeArrayAsOutParam(rules));
     1114    AssertReturn(SUCCEEDED(hrc), VERR_INTERNAL_ERROR);
     1115
     1116    NATSEVICEPORTFORWARDRULE Rule;
     1117    for (size_t idxRules = 0; idxRules < rules.size(); ++idxRules)
     1118    {
     1119        Log(("%d-%s rule: %ls\n", idxRules, (fIsIPv6 ? "IPv6" : "IPv4"), rules[idxRules]));
     1120        RT_ZERO(Rule);
     1121       
     1122        int rc = netPfStrToPf(com::Utf8Str(rules[idxRules]).c_str(), 0, &Rule.Pfr);
     1123        if (RT_FAILURE(rc))
     1124            continue;
     1125       
     1126        vec.push_back(Rule);
     1127    }
     1128
     1129    return VINF_SUCCESS;
    11541130}
    11551131
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