VirtualBox

Changeset 87743 in vbox for trunk/src/VBox/NetworkServices


Ignore:
Timestamp:
Feb 12, 2021 5:43:03 PM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
142795
Message:

NAT/Net: Rewrite the code to manage API event listeners to be,
hopefully, easier to understand (misnaming things didn't help either).
Don't hide it in another directory. G/c the old code. bugref:9929.

Location:
trunk/src/VBox/NetworkServices
Files:
2 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/Makefile.kmk

    r85048 r87743  
    9898        ../NetLib/VBoxNetUDP.cpp \
    9999        ../NetLib/VBoxNetARP.cpp \
    100         ../NetLib/ComHostUtils.cpp \
    101100        $(addprefix ../../Devices/Network/lwip-new/,$(LWIP_SOURCES)) \
    102101        proxy_pollmgr.c \
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r87738 r87743  
    7979
    8080#include "../NetLib/VBoxNetBaseService.h"
    81 #include "../NetLib/utils.h"
    8281#include "../NetLib/VBoxPortForwardString.h"
    8382
     
    125124
    126125class VBoxNetLwipNAT
    127   : public VBoxNetBaseService,
    128     public NATNetworkEventAdapter
    129 {
    130     friend class NATNetworkListener;
    131 
     126  : public VBoxNetBaseService
     127{
    132128    /** Home folder location; used as default directory for several paths. */
    133129    com::Utf8Str m_strHome;
     
    149145    ComPtr<IHost> m_host;
    150146
    151     ComNatListenerPtr m_NatListener;
    152     ComNatListenerPtr m_VBoxListener;
    153     ComNatListenerPtr m_VBoxClientListener;
    154 
    155147    VECNATSERVICEPF m_vecPortForwardRule4;
    156148    VECNATSERVICEPF m_vecPortForwardRule6;
     149
     150    class Listener
     151    {
     152        class Adapter;
     153        typedef ListenerImpl<Adapter, VBoxNetLwipNAT *> Impl;
     154
     155        ComObjPtr<Impl> m_pListenerImpl;
     156        ComPtr<IEventSource> m_pEventSource;
     157
     158    public:
     159        HRESULT init(VBoxNetLwipNAT *pNAT);
     160        void uninit();
     161
     162        template <typename IEventful>
     163        HRESULT listen(const ComPtr<IEventful> &pEventful,
     164                       const VBoxEventType_T aEvents[]);
     165        HRESULT unlisten();
     166
     167    private:
     168        HRESULT doListen(const ComPtr<IEventSource> &pEventSource,
     169                         const VBoxEventType_T aEvents[]);
     170    };
     171
     172    Listener m_ListenerNATNet;
     173    Listener m_ListenerVirtualBox;
     174    Listener m_ListenerVBoxClient;
    157175
    158176    static INTNETSEG aXmitSeg[64];
     
    848866
    849867
     868
     869/**
     870 * Adapter for the ListenerImpl template.  It has to be a separate
     871 * object because ListenerImpl deletes it.  Just a small wrapper that
     872 * delegates the real work back to VBoxNetLwipNAT.
     873 */
     874class VBoxNetLwipNAT::Listener::Adapter
     875{
     876    VBoxNetLwipNAT *m_pNAT;
     877public:
     878    Adapter() : m_pNAT(NULL) {}
     879    HRESULT init() { return init(NULL); }
     880    void uninit() { m_pNAT = NULL; }
     881
     882    HRESULT init(VBoxNetLwipNAT *pNAT)
     883    {
     884        m_pNAT = pNAT;
     885        return S_OK;
     886    }
     887
     888    HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
     889    {
     890        if (RT_LIKELY(m_pNAT != NULL))
     891            return m_pNAT->HandleEvent(aEventType, pEvent);
     892        else
     893            return S_OK;
     894    }
     895};
     896
     897
     898HRESULT
     899VBoxNetLwipNAT::Listener::init(VBoxNetLwipNAT *pNAT)
     900{
     901    HRESULT hrc;
     902
     903    hrc = m_pListenerImpl.createObject();
     904    if (FAILED(hrc))
     905        return hrc;
     906
     907    hrc = m_pListenerImpl->init(new Adapter(), pNAT);
     908    if (FAILED(hrc))
     909    {
     910        VBoxNetLwipNAT::reportComError(m_pListenerImpl, "init", hrc);
     911        return hrc;
     912    }
     913
     914    return hrc;
     915}
     916
     917
     918void
     919VBoxNetLwipNAT::Listener::uninit()
     920{
     921    unlisten();
     922    m_pListenerImpl.setNull();
     923}
     924
     925
     926/*
     927 * There's no base interface that exposes "eventSource" so fake it
     928 * with a template.
     929 */
     930template <typename IEventful>
     931HRESULT
     932VBoxNetLwipNAT::Listener::listen(const ComPtr<IEventful> &pEventful,
     933                                         const VBoxEventType_T aEvents[])
     934{
     935    HRESULT hrc;
     936
     937    if (m_pListenerImpl.isNull())
     938        return S_OK;
     939
     940    ComPtr<IEventSource> pEventSource;
     941    hrc = pEventful->COMGETTER(EventSource)(pEventSource.asOutParam());
     942    if (FAILED(hrc))
     943    {
     944        VBoxNetLwipNAT::reportComError(pEventful, "EventSource", hrc);
     945        return hrc;
     946    }
     947
     948    /* got a real interface, punt to the non-template code */
     949    hrc = doListen(pEventSource, aEvents);
     950    if (FAILED(hrc))
     951        return hrc;
     952
     953    return hrc;
     954}
     955
     956
     957HRESULT
     958VBoxNetLwipNAT::Listener::doListen(const ComPtr<IEventSource> &pEventSource,
     959                                   const VBoxEventType_T aEvents[])
     960{
     961    HRESULT hrc;
     962
     963    com::SafeArray<VBoxEventType_T> aInteresting;
     964    for (size_t i = 0; aEvents[i] != VBoxEventType_Invalid; ++i)
     965        aInteresting.push_back(aEvents[i]);
     966
     967    BOOL fActive = true;
     968    hrc = pEventSource->RegisterListener(m_pListenerImpl,
     969                                         ComSafeArrayAsInParam(aInteresting),
     970                                         fActive);
     971    if (FAILED(hrc))
     972    {
     973        VBoxNetLwipNAT::reportComError(m_pEventSource, "RegisterListener", hrc);
     974        return hrc;
     975    }
     976
     977    m_pEventSource = pEventSource;
     978    return hrc;
     979}
     980
     981
     982HRESULT
     983VBoxNetLwipNAT::Listener::unlisten()
     984{
     985    HRESULT hrc;
     986
     987    if (m_pEventSource.isNull())
     988        return S_OK;
     989
     990    const ComPtr<IEventSource> pEventSource = m_pEventSource;
     991    m_pEventSource.setNull();
     992
     993    hrc = pEventSource->UnregisterListener(m_pListenerImpl);
     994    if (FAILED(hrc))
     995    {
     996        VBoxNetLwipNAT::reportComError(pEventSource, "UnregisterListener", hrc);
     997        return hrc;
     998    }
     999
     1000    return hrc;
     1001}
     1002
     1003
     1004
    8501005/**
    8511006 * Create and register API event listeners.
     
    8531008int VBoxNetLwipNAT::eventsInit()
    8541009{
    855     int rc;
    856 
    857     {
    858         ComEventTypeArray eventTypes;
    859         eventTypes.push_back(VBoxEventType_OnNATNetworkPortForward);
    860         eventTypes.push_back(VBoxEventType_OnNATNetworkSetting);
    861         rc = createNatListener(m_NatListener, virtualbox, this, eventTypes);
    862         AssertRCReturn(rc, rc);
    863     }
    864 
    865     {
    866         ComEventTypeArray eventTypes;
    867         eventTypes.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange);
    868         eventTypes.push_back(VBoxEventType_OnNATNetworkStartStop);
    869         rc = createNatListener(m_VBoxListener, virtualbox, this, eventTypes);
    870         AssertRCReturn(rc, rc);
    871     }
    872 
    873     {
    874         ComEventTypeArray eventTypes;
    875         eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
    876         rc = createClientListener(m_VBoxClientListener, virtualboxClient, this, eventTypes);
    877         AssertRCReturn(rc, rc);
    878     }
     1010    /**
     1011     * @todo r=uwe These events are reported on both IVirtualBox and
     1012     * INATNetwork objects.  We used to listen for them on our
     1013     * network, but it was changed later to listen on vbox.  Leave it
     1014     * that way for now.  Note that HandleEvent() has to do additional
     1015     * check for them to ignore events for other networks.
     1016     */
     1017    static const VBoxEventType_T s_aNATNetEvents[] = {
     1018        VBoxEventType_OnNATNetworkPortForward,
     1019        VBoxEventType_OnNATNetworkSetting,
     1020        VBoxEventType_Invalid
     1021    };
     1022    m_ListenerNATNet.init(this);
     1023    m_ListenerNATNet.listen(virtualbox, s_aNATNetEvents); // sic!
     1024
     1025    static const VBoxEventType_T s_aVirtualBoxEvents[] = {
     1026        VBoxEventType_OnHostNameResolutionConfigurationChange,
     1027        VBoxEventType_OnNATNetworkStartStop,
     1028        VBoxEventType_Invalid
     1029    };
     1030    m_ListenerVirtualBox.init(this);
     1031    m_ListenerVirtualBox.listen(virtualbox, s_aVirtualBoxEvents);
     1032
     1033    static const VBoxEventType_T s_aVBoxClientEvents[] = {
     1034        VBoxEventType_OnVBoxSVCAvailabilityChanged,
     1035        VBoxEventType_Invalid
     1036    };
     1037    m_ListenerVBoxClient.init(this);
     1038    m_ListenerVBoxClient.listen(virtualboxClient, s_aVBoxClientEvents);
    8791039
    8801040    return VINF_SUCCESS;
     
    11481308    m_vecPortForwardRule6.clear();
    11491309
    1150     destroyNatListener(m_NatListener, virtualbox);
    1151     destroyNatListener(m_VBoxListener, virtualbox);
    1152     destroyClientListener(m_VBoxClientListener, virtualboxClient);
     1310    m_ListenerNATNet.unlisten();
     1311    m_ListenerVirtualBox.unlisten();
     1312    m_ListenerVBoxClient.unlisten();
    11531313
    11541314    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