VirtualBox

Changeset 87394 in vbox


Ignore:
Timestamp:
Jan 23, 2021 12:15:42 AM (4 years ago)
Author:
vboxsync
Message:

NAT/Net: Insertion-sort init() and run() into their place.

File:
1 edited

Legend:

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

    r87393 r87394  
    307307    }
    308308    return VERR_NOT_FOUND;
     309}
     310
     311
     312/**
     313 * Perform actual initialization.
     314 *
     315 * This code runs on the main thread.  Establish COM connection with
     316 * VBoxSVC so that we can do API calls.  Starts the LWIP thread.
     317 */
     318int VBoxNetLwipNAT::init()
     319{
     320    LogFlowFuncEnter();
     321
     322    /* virtualbox initialized in the superclass */
     323    int rc = ::VBoxNetBaseService::init();
     324    AssertRCReturn(rc, rc);
     325
     326    std::string networkName = getNetworkName();
     327    rc = findNatNetwork(virtualbox, networkName, m_net);
     328    AssertRCReturn(rc, rc);
     329
     330    {
     331        ComEventTypeArray eventTypes;
     332        eventTypes.push_back(VBoxEventType_OnNATNetworkPortForward);
     333        eventTypes.push_back(VBoxEventType_OnNATNetworkSetting);
     334        rc = createNatListener(m_NatListener, virtualbox, this, eventTypes);
     335        AssertRCReturn(rc, rc);
     336    }
     337
     338
     339    // resolver changes are reported on vbox but are retrieved from
     340    // host so stash a pointer for future lookups
     341    HRESULT hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
     342    AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
     343
     344    {
     345        ComEventTypeArray eventTypes;
     346        eventTypes.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
     347        eventTypes.push_back(VBoxEventType_OnNATNetworkStartStop);
     348        rc = createNatListener(m_VBoxListener, virtualbox, this, eventTypes);
     349        AssertRCReturn(rc, rc);
     350    }
     351
     352    {
     353        ComEventTypeArray eventTypes;
     354        eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
     355        rc = createClientListener(m_VBoxClientListener, virtualboxClient, this, eventTypes);
     356        AssertRCReturn(rc, rc);
     357    }
     358
     359    BOOL fIPv6Enabled = FALSE;
     360    hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
     361    AssertComRCReturn(hrc, VERR_NOT_FOUND);
     362
     363    BOOL fIPv6DefaultRoute = FALSE;
     364    if (fIPv6Enabled)
     365    {
     366        hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
     367        AssertComRCReturn(hrc, VERR_NOT_FOUND);
     368    }
     369
     370    m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
     371    m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
     372
     373
     374    /*
     375     * Bind outgoing connections to the specified IP.
     376     */
     377    com::Bstr bstrSourceIpX;
     378
     379    /* IPv4 */
     380    com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str());
     381    hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
     382    if (SUCCEEDED(hrc) && bstrSourceIpX.isNotEmpty())
     383    {
     384        RTNETADDRIPV4 addr;
     385        rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
     386        if (RT_SUCCESS(rc))
     387        {
     388            m_src4.sin_addr.s_addr = addr.u;
     389            m_ProxyOptions.src4 = &m_src4;
     390
     391            LogRel(("Will use %RTnaipv4 as IPv4 source address\n",
     392                    m_src4.sin_addr.s_addr));
     393        }
     394        else
     395        {
     396            LogRel(("Failed to parse \"%s\" IPv4 source address specification\n",
     397                    com::Utf8Str(bstrSourceIpX).c_str()));
     398        }
     399
     400        bstrSourceIpX.setNull();
     401    }
     402
     403    /* IPv6 */
     404    com::Bstr bstrSourceIp6Key = com::BstrFmt("NAT/%s/SourceIp6", networkName.c_str());
     405    hrc = virtualbox->GetExtraData(bstrSourceIp6Key.raw(), bstrSourceIpX.asOutParam());
     406    if (SUCCEEDED(hrc) && bstrSourceIpX.isNotEmpty())
     407    {
     408        RTNETADDRIPV6 addr;
     409        char *pszZone = NULL;
     410        rc = RTNetStrToIPv6Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr, &pszZone);
     411        if (RT_SUCCESS(rc))
     412        {
     413            memcpy(&m_src6.sin6_addr, &addr, sizeof(addr));
     414            m_ProxyOptions.src6 = &m_src6;
     415
     416            LogRel(("Will use %RTnaipv6 as IPv6 source address\n",
     417                    &m_src6.sin6_addr));
     418        }
     419        else
     420        {
     421            LogRel(("Failed to parse \"%s\" IPv6 source address specification\n",
     422                    com::Utf8Str(bstrSourceIpX).c_str()));
     423        }
     424
     425        bstrSourceIpX.setNull();
     426    }
     427
     428
     429    if (!fDontLoadRulesOnStartup)
     430    {
     431        fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4);
     432        fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6);
     433    } /* if (!fDontLoadRulesOnStartup) */
     434
     435    AddressToOffsetMapping tmp;
     436    rc = localMappings(m_net, tmp);
     437    if (RT_SUCCESS(rc) && !tmp.empty())
     438    {
     439        unsigned long i = 0;
     440        for (AddressToOffsetMapping::iterator it = tmp.begin();
     441             it != tmp.end() && i < RT_ELEMENTS(m_lo2off);
     442             ++it, ++i)
     443        {
     444            ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u);
     445            m_lo2off[i].off = it->second;
     446        }
     447
     448        m_loOptDescriptor.lomap = m_lo2off;
     449        m_loOptDescriptor.num_lomap = i;
     450        m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
     451    }
     452
     453    com::Bstr bstr;
     454    hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
     455    AssertComRCReturn(hrc, VERR_NOT_FOUND);
     456    if (!bstr.isEmpty())
     457    {
     458        com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
     459                                     bstr.raw(), RTPATH_DELIMITER, "TFTP"));
     460        char *pszStrTemp;       // avoid const char ** vs char **
     461        rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
     462        AssertRC(rc);
     463        m_ProxyOptions.tftp_root = pszStrTemp;
     464    }
     465
     466    m_ProxyOptions.nameservers = getHostNameservers();
     467
     468    /* end of COM initialization */
     469
     470    /* connect to the intnet */
     471    rc = tryGoOnline();
     472    if (RT_FAILURE(rc))
     473        return rc;
     474
     475    /* start the LWIP thread */
     476    vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
     477
     478    LogFlowFuncLeaveRC(rc);
     479    return rc;
     480}
     481
     482
     483/*
     484 * Run the pumps.
     485 *
     486 * Spawn the intnet pump thread that gets packets from the intnet and
     487 * feeds them to lwIP.  Enter COM event loop here, on the main thread.
     488 */
     489int VBoxNetLwipNAT::run()
     490{
     491    VBoxNetBaseService::run();
     492
     493    /* event pump was told to shut down, we are done ... */
     494    vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
     495
     496    m_vecPortForwardRule4.clear();
     497    m_vecPortForwardRule6.clear();
     498
     499    destroyNatListener(m_NatListener, virtualbox);
     500    destroyNatListener(m_VBoxListener, virtualbox);
     501    destroyClientListener(m_VBoxClientListener, virtualboxClient);
     502
     503    return VINF_SUCCESS;
    309504}
    310505
     
    8331028
    8341029
    835 /**
    836  * Main thread. Starts also the LWIP thread.
    837  */
    838 int VBoxNetLwipNAT::init()
    839 {
    840     LogFlowFuncEnter();
    841 
    842     /* virtualbox initialized in super class */
    843     int rc = ::VBoxNetBaseService::init();
    844     AssertRCReturn(rc, rc);
    845 
    846     std::string networkName = getNetworkName();
    847     rc = findNatNetwork(virtualbox, networkName, m_net);
    848     AssertRCReturn(rc, rc);
    849 
    850     {
    851         ComEventTypeArray eventTypes;
    852         eventTypes.push_back(VBoxEventType_OnNATNetworkPortForward);
    853         eventTypes.push_back(VBoxEventType_OnNATNetworkSetting);
    854         rc = createNatListener(m_NatListener, virtualbox, this, eventTypes);
    855         AssertRCReturn(rc, rc);
    856     }
    857 
    858 
    859     // resolver changes are reported on vbox but are retrieved from
    860     // host so stash a pointer for future lookups
    861     HRESULT hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam());
    862     AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
    863 
    864     {
    865         ComEventTypeArray eventTypes;
    866         eventTypes.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
    867         eventTypes.push_back(VBoxEventType_OnNATNetworkStartStop);
    868         rc = createNatListener(m_VBoxListener, virtualbox, this, eventTypes);
    869         AssertRCReturn(rc, rc);
    870     }
    871 
    872     {
    873         ComEventTypeArray eventTypes;
    874         eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
    875         rc = createClientListener(m_VBoxClientListener, virtualboxClient, this, eventTypes);
    876         AssertRCReturn(rc, rc);
    877     }
    878 
    879     BOOL fIPv6Enabled = FALSE;
    880     hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled);
    881     AssertComRCReturn(hrc, VERR_NOT_FOUND);
    882 
    883     BOOL fIPv6DefaultRoute = FALSE;
    884     if (fIPv6Enabled)
    885     {
    886         hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute);
    887         AssertComRCReturn(hrc, VERR_NOT_FOUND);
    888     }
    889 
    890     m_ProxyOptions.ipv6_enabled = fIPv6Enabled;
    891     m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
    892 
    893 
    894     /*
    895      * Bind outgoing connections to the specified IP.
    896      */
    897     com::Bstr bstrSourceIpX;
    898 
    899     /* IPv4 */
    900     com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str());
    901     hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam());
    902     if (SUCCEEDED(hrc) && bstrSourceIpX.isNotEmpty())
    903     {
    904         RTNETADDRIPV4 addr;
    905         rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr);
    906         if (RT_SUCCESS(rc))
    907         {
    908             m_src4.sin_addr.s_addr = addr.u;
    909             m_ProxyOptions.src4 = &m_src4;
    910 
    911             LogRel(("Will use %RTnaipv4 as IPv4 source address\n",
    912                     m_src4.sin_addr.s_addr));
    913         }
    914         else
    915         {
    916             LogRel(("Failed to parse \"%s\" IPv4 source address specification\n",
    917                     com::Utf8Str(bstrSourceIpX).c_str()));
    918         }
    919 
    920         bstrSourceIpX.setNull();
    921     }
    922 
    923     /* IPv6 */
    924     com::Bstr bstrSourceIp6Key = com::BstrFmt("NAT/%s/SourceIp6", networkName.c_str());
    925     hrc = virtualbox->GetExtraData(bstrSourceIp6Key.raw(), bstrSourceIpX.asOutParam());
    926     if (SUCCEEDED(hrc) && bstrSourceIpX.isNotEmpty())
    927     {
    928         RTNETADDRIPV6 addr;
    929         char *pszZone = NULL;
    930         rc = RTNetStrToIPv6Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr, &pszZone);
    931         if (RT_SUCCESS(rc))
    932         {
    933             memcpy(&m_src6.sin6_addr, &addr, sizeof(addr));
    934             m_ProxyOptions.src6 = &m_src6;
    935 
    936             LogRel(("Will use %RTnaipv6 as IPv6 source address\n",
    937                     &m_src6.sin6_addr));
    938         }
    939         else
    940         {
    941             LogRel(("Failed to parse \"%s\" IPv6 source address specification\n",
    942                     com::Utf8Str(bstrSourceIpX).c_str()));
    943         }
    944 
    945         bstrSourceIpX.setNull();
    946     }
    947 
    948 
    949     if (!fDontLoadRulesOnStartup)
    950     {
    951         fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4);
    952         fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6);
    953     } /* if (!fDontLoadRulesOnStartup) */
    954 
    955     AddressToOffsetMapping tmp;
    956     rc = localMappings(m_net, tmp);
    957     if (RT_SUCCESS(rc) && !tmp.empty())
    958     {
    959         unsigned long i = 0;
    960         for (AddressToOffsetMapping::iterator it = tmp.begin();
    961              it != tmp.end() && i < RT_ELEMENTS(m_lo2off);
    962              ++it, ++i)
    963         {
    964             ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u);
    965             m_lo2off[i].off = it->second;
    966         }
    967 
    968         m_loOptDescriptor.lomap = m_lo2off;
    969         m_loOptDescriptor.num_lomap = i;
    970         m_ProxyOptions.lomap_desc = &m_loOptDescriptor;
    971     }
    972 
    973     com::Bstr bstr;
    974     hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
    975     AssertComRCReturn(hrc, VERR_NOT_FOUND);
    976     if (!bstr.isEmpty())
    977     {
    978         com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s",
    979                                      bstr.raw(), RTPATH_DELIMITER, "TFTP"));
    980         char *pszStrTemp;       // avoid const char ** vs char **
    981         rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str());
    982         AssertRC(rc);
    983         m_ProxyOptions.tftp_root = pszStrTemp;
    984     }
    985 
    986     m_ProxyOptions.nameservers = getHostNameservers();
    987 
    988     /* end of COM initialization */
    989 
    990     rc = tryGoOnline();
    991     if (RT_FAILURE(rc))
    992         return rc;
    993 
    994     /* this starts LWIP thread */
    995     vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this);
    996 
    997     LogFlowFuncLeaveRC(rc);
    998     return rc;
    999 }
    1000 
    1001 
    10021030const char **VBoxNetLwipNAT::getHostNameservers()
    10031031{
     
    11031131        }
    11041132    }
    1105 
    1106     return VINF_SUCCESS;
    1107 }
    1108 
    1109 
    1110 int VBoxNetLwipNAT::run()
    1111 {
    1112     /* Father starts receiving thread and enter event loop. */
    1113     VBoxNetBaseService::run();
    1114 
    1115     vboxLwipCoreFinalize(VBoxNetLwipNAT::onLwipTcpIpFini, this);
    1116 
    1117     m_vecPortForwardRule4.clear();
    1118     m_vecPortForwardRule6.clear();
    1119 
    1120     destroyNatListener(m_NatListener, virtualbox);
    1121     destroyNatListener(m_VBoxListener, virtualbox);
    1122     destroyClientListener(m_VBoxClientListener, virtualboxClient);
    11231133
    11241134    return VINF_SUCCESS;
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