summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/NetLib/utils.h
blob: ccd5cadfe8a1edbd3b95a93996a5a4e9fb366333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* $Id: utils.h $ */
/** @file
 * ComHostUtils.cpp
 */

/*
 * Copyright (C) 2013-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/

#ifndef VBOX_INCLUDED_SRC_NetLib_utils_h
#define VBOX_INCLUDED_SRC_NetLib_utils_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include "cpp/utils.h"

typedef ComPtr<IVirtualBox> ComVirtualBoxPtr;
typedef ComPtr<IVirtualBoxClient> ComVirtualBoxClientPtr;
typedef ComPtr<IDHCPServer> ComDhcpServerPtr;
typedef ComPtr<IHost> ComHostPtr;
typedef ComPtr<INATNetwork> ComNatPtr;
typedef com::SafeArray<BSTR> ComBstrArray;

typedef std::vector<RTNETADDRIPV4> AddressList;
typedef std::map<RTNETADDRIPV4, int> AddressToOffsetMapping;


inline bool isDhcpRequired(const ComNatPtr& nat)
{
    BOOL fNeedDhcpServer = false;
    if (FAILED(nat->COMGETTER(NeedDhcpServer)(&fNeedDhcpServer)))
        return false;

    return RT_BOOL(fNeedDhcpServer);
}


inline int findDhcpServer(const ComVirtualBoxPtr& vbox, const std::string& name, ComDhcpServerPtr& dhcp)
{
    HRESULT hrc = vbox->FindDHCPServerByNetworkName(com::Bstr(name.c_str()).raw(),
                                                          dhcp.asOutParam());
    AssertComRCReturn(hrc, VERR_NOT_FOUND);

    return VINF_SUCCESS;
}


inline int findNatNetwork(const ComVirtualBoxPtr& vbox, const std::string& name, ComNatPtr& nat)
{
    HRESULT hrc = vbox->FindNATNetworkByName(com::Bstr(name.c_str()).raw(),
                                                   nat.asOutParam());

    AssertComRCReturn(hrc, VERR_NOT_FOUND);

    return VINF_SUCCESS;
}


inline RTNETADDRIPV4 networkid(const RTNETADDRIPV4& addr, const RTNETADDRIPV4& netmask)
{
    RTNETADDRIPV4 netid;
    netid.u = addr.u & netmask.u;
    return netid;
}


int localMappings(const ComNatPtr&, AddressToOffsetMapping&);
int hostDnsSearchList(const ComHostPtr&, std::vector<std::string>&);
int hostDnsDomain(const ComHostPtr&, std::string& domainStr);


class NATNetworkEventAdapter
{
    public:
    virtual HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent) = 0;
};


class NATNetworkListener
{
    public:
    NATNetworkListener():m_pNAT(NULL){}

    HRESULT init(NATNetworkEventAdapter *pNAT)
    {
        AssertPtrReturn(pNAT, E_INVALIDARG);

        m_pNAT = pNAT;
        return S_OK;
    }

    HRESULT init()
    {
        m_pNAT = NULL;
        return S_OK;
    }

    void uninit() { m_pNAT = NULL; }

    HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
    {
        if (m_pNAT)
            return m_pNAT->HandleEvent(aEventType, pEvent);
        else
            return E_FAIL;
    }

    private:
    NATNetworkEventAdapter *m_pNAT;
};
typedef ListenerImpl<NATNetworkListener, NATNetworkEventAdapter*> NATNetworkListenerImpl;

# ifdef VBOX_WITH_XPCOM
class NS_CLASSINFO_NAME(NATNetworkListenerImpl);
# endif

typedef ComPtr<NATNetworkListenerImpl> ComNatListenerPtr;
typedef com::SafeArray<VBoxEventType_T> ComEventTypeArray;

/* XXX: const is commented out because of compilation erro on Windows host, but it's intended that this function
 isn't modify event type array */
int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
                      NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events);
int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr);
int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
                         NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events);
int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr);

#endif /* !VBOX_INCLUDED_SRC_NetLib_utils_h */