VirtualBox

Ignore:
Timestamp:
Nov 8, 2010 10:16:25 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
67471
Message:

Main,NAT: Managing port-forwarding at runtime. (xTracker/#4835).

File:
1 edited

Legend:

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

    r33806 r33825  
    10921092uint32_t Console::sSSMConsoleVer = 0x00010001;
    10931093
     1094inline static const char *networkAdapterTypeToName(NetworkAdapterType_T adapterType)
     1095{
     1096    switch (adapterType)
     1097    {
     1098        case NetworkAdapterType_Am79C970A:
     1099        case NetworkAdapterType_Am79C973:
     1100            return "pcnet";
     1101#ifdef VBOX_WITH_E1000
     1102        case NetworkAdapterType_I82540EM:
     1103        case NetworkAdapterType_I82543GC:
     1104        case NetworkAdapterType_I82545EM:
     1105            return "e1000";
     1106#endif
     1107#ifdef VBOX_WITH_VIRTIO
     1108        case NetworkAdapterType_Virtio:
     1109            return "virtio-net";
     1110#endif
     1111        default:
     1112            AssertFailed();
     1113            return "unknown";
     1114    }
     1115    return NULL;
     1116}
     1117
    10941118/**
    10951119 * Loads various console data stored in the saved state file.
     
    34153439            {
    34163440                /*
    3417                  * Find the pcnet instance, get the config interface and update
     3441                 * Find the adapter instance, get the config interface and update
    34183442                 * the link state.
    34193443                 */
     
    34213445                rc = aNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
    34223446                AssertComRC(rc);
    3423                 const char *pszAdapterName = NULL;
    3424                 switch (adapterType)
    3425                 {
    3426                     case NetworkAdapterType_Am79C970A:
    3427                     case NetworkAdapterType_Am79C973:
    3428                         pszAdapterName = "pcnet";
    3429                         break;
    3430 #ifdef VBOX_WITH_E1000
    3431                     case NetworkAdapterType_I82540EM:
    3432                     case NetworkAdapterType_I82543GC:
    3433                     case NetworkAdapterType_I82545EM:
    3434                         pszAdapterName = "e1000";
    3435                         break;
    3436 #endif
    3437 #ifdef VBOX_WITH_VIRTIO
    3438                     case NetworkAdapterType_Virtio:
    3439                         pszAdapterName = "virtio-net";
    3440                         break;
    3441 #endif
    3442                     default:
    3443                         AssertFailed();
    3444                         pszAdapterName = "unknown";
    3445                         break;
    3446                 }
    3447 
     3447                const char *pszAdapterName = networkAdapterTypeToName(adapterType);
    34483448                PPDMIBASE pBase;
    34493449                int vrc = PDMR3QueryDeviceLun(mpVM, pszAdapterName, ulInstance, 0, &pBase);
     
    34963496        CONSOLE_DO_CALLBACKS1(OnNetworkAdapterChanged, aNetworkAdapter);
    34973497
     3498    LogFlowThisFunc(("Leaving rc=%#x\n", rc));
     3499    return rc;
     3500}
     3501
     3502/**
     3503 * Called by IInternalSessionControl::OnNATEngineChange().
     3504 *
     3505 * @note Locks this object for writing.
     3506 */
     3507HRESULT Console::onNATRedirectRuleChange(INetworkAdapter *aNetworkAdapter, BOOL aNatRuleRemove, IN_BSTR aRuleName,
     3508                                 NATProtocol_T aProto, IN_BSTR aHostIp, LONG aHostPort, IN_BSTR aGuestIp, LONG aGuestPort)
     3509{
     3510    LogFlowThisFunc(("\n"));
     3511
     3512    AutoCaller autoCaller(this);
     3513    AssertComRCReturnRC(autoCaller.rc());
     3514
     3515    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
     3516
     3517    HRESULT rc = S_OK;
     3518    int vrc = VINF_SUCCESS;
     3519    PPDMINETWORKNATCONFIG pNetNatCfg = NULL;
     3520    /* don't trigger nat engine change if the VM isn't running */
     3521    if (mpVM)
     3522    {
     3523        /* protect mpVM */
     3524        AutoVMCaller autoVMCaller(this);
     3525        if (FAILED(autoVMCaller.rc())) return autoVMCaller.rc();
     3526        ULONG ulInstance;
     3527        rc = aNetworkAdapter->COMGETTER(Slot)(&ulInstance);
     3528        AssertComRC(rc);
     3529        if (FAILED(rc))
     3530            goto done;
     3531        /*
     3532         * Find the adapter instance, get the config interface and update
     3533         * the link state.
     3534         */
     3535        NetworkAdapterType_T adapterType;
     3536        rc = aNetworkAdapter->COMGETTER(AdapterType)(&adapterType);
     3537        AssertComRC(rc);
     3538        if (FAILED(rc))
     3539        {
     3540            rc = E_FAIL;
     3541            goto done;
     3542        }
     3543
     3544        const char *pszAdapterName = networkAdapterTypeToName(adapterType);
     3545        PPDMIBASE pBase;
     3546        vrc = PDMR3QueryLun(mpVM, pszAdapterName, ulInstance, 0, &pBase);
     3547        ComAssertRC(vrc);
     3548        if (RT_FAILURE(vrc))
     3549        {
     3550            rc = E_FAIL;
     3551            goto done;
     3552        }
     3553        NetworkAttachmentType_T attachmentType;
     3554        vrc = aNetworkAdapter->COMGETTER(AttachmentType)(&attachmentType);
     3555
     3556        if (   RT_FAILURE(vrc)
     3557            || attachmentType != NetworkAttachmentType_NAT)
     3558        {
     3559            rc = (RT_FAILURE(vrc)) ? E_FAIL: rc;
     3560            goto done;
     3561        }
     3562       
     3563        /* look down for PDMINETWORKNATCONFIG interface */
     3564        while (pBase)
     3565        {
     3566            if ((pNetNatCfg = (PPDMINETWORKNATCONFIG)pBase->pfnQueryInterface(pBase, PDMINETWORKNATCONFIG_IID)))
     3567                break;
     3568            PPDMDRVINS drvins = PDMIBASE_2_PDMDRV(pBase);
     3569            pBase = drvins->pDownBase;
     3570        }
     3571        if (!pNetNatCfg)
     3572            goto done;
     3573        bool fUdp = (aProto == NATProtocol_UDP);
     3574        vrc = pNetNatCfg->pfnRedirectRuleCommand(pNetNatCfg, aNatRuleRemove, Utf8Str(aRuleName).c_str(), fUdp,
     3575                                                 Utf8Str(aHostIp).c_str(), aHostPort, Utf8Str(aGuestIp).c_str(),
     3576                                                 aGuestPort);
     3577        if (RT_FAILURE(vrc))
     3578            rc = E_FAIL;
     3579    }
     3580done:
    34983581    LogFlowThisFunc(("Leaving rc=%#x\n", rc));
    34993582    return rc;
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