1 | /* $Id: utils.h 84364 2020-05-19 11:21:19Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ComHostUtils.cpp
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 |
|
---|
22 | #ifndef VBOX_INCLUDED_SRC_NetLib_utils_h
|
---|
23 | #define VBOX_INCLUDED_SRC_NetLib_utils_h
|
---|
24 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
25 | # pragma once
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include "cpp/utils.h"
|
---|
29 |
|
---|
30 | typedef ComPtr<IVirtualBox> ComVirtualBoxPtr;
|
---|
31 | typedef ComPtr<IVirtualBoxClient> ComVirtualBoxClientPtr;
|
---|
32 | typedef ComPtr<IDHCPServer> ComDhcpServerPtr;
|
---|
33 | typedef ComPtr<IHost> ComHostPtr;
|
---|
34 | typedef ComPtr<INATNetwork> ComNatPtr;
|
---|
35 | typedef com::SafeArray<BSTR> ComBstrArray;
|
---|
36 |
|
---|
37 | typedef std::vector<RTNETADDRIPV4> AddressList;
|
---|
38 | typedef std::map<RTNETADDRIPV4, int> AddressToOffsetMapping;
|
---|
39 |
|
---|
40 |
|
---|
41 | inline bool isDhcpRequired(const ComNatPtr& nat)
|
---|
42 | {
|
---|
43 | BOOL fNeedDhcpServer = false;
|
---|
44 | if (FAILED(nat->COMGETTER(NeedDhcpServer)(&fNeedDhcpServer)))
|
---|
45 | return false;
|
---|
46 |
|
---|
47 | return RT_BOOL(fNeedDhcpServer);
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | inline int findDhcpServer(const ComVirtualBoxPtr& vbox, const std::string& name, ComDhcpServerPtr& dhcp)
|
---|
52 | {
|
---|
53 | HRESULT hrc = vbox->FindDHCPServerByNetworkName(com::Bstr(name.c_str()).raw(),
|
---|
54 | dhcp.asOutParam());
|
---|
55 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
56 |
|
---|
57 | return VINF_SUCCESS;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | inline int findNatNetwork(const ComVirtualBoxPtr& vbox, const std::string& name, ComNatPtr& nat)
|
---|
62 | {
|
---|
63 | HRESULT hrc = vbox->FindNATNetworkByName(com::Bstr(name.c_str()).raw(),
|
---|
64 | nat.asOutParam());
|
---|
65 |
|
---|
66 | AssertComRCReturn(hrc, VERR_NOT_FOUND);
|
---|
67 |
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | inline RTNETADDRIPV4 networkid(const RTNETADDRIPV4& addr, const RTNETADDRIPV4& netmask)
|
---|
73 | {
|
---|
74 | RTNETADDRIPV4 netid;
|
---|
75 | netid.u = addr.u & netmask.u;
|
---|
76 | return netid;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | int localMappings(const ComNatPtr&, AddressToOffsetMapping&);
|
---|
81 | int hostDnsSearchList(const ComHostPtr&, std::vector<std::string>&);
|
---|
82 | int hostDnsDomain(const ComHostPtr&, std::string& domainStr);
|
---|
83 |
|
---|
84 |
|
---|
85 | class NATNetworkEventAdapter
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | virtual ~NATNetworkEventAdapter() { /* Make VC++ 19.2 happy. */ }
|
---|
89 | virtual HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent) = 0;
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | class NATNetworkListener
|
---|
94 | {
|
---|
95 | public:
|
---|
96 | NATNetworkListener():m_pNAT(NULL){}
|
---|
97 |
|
---|
98 | HRESULT init(NATNetworkEventAdapter *pNAT)
|
---|
99 | {
|
---|
100 | AssertPtrReturn(pNAT, E_INVALIDARG);
|
---|
101 |
|
---|
102 | m_pNAT = pNAT;
|
---|
103 | return S_OK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | HRESULT init()
|
---|
107 | {
|
---|
108 | m_pNAT = NULL;
|
---|
109 | return S_OK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void uninit() { m_pNAT = NULL; }
|
---|
113 |
|
---|
114 | HRESULT HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent)
|
---|
115 | {
|
---|
116 | if (m_pNAT)
|
---|
117 | return m_pNAT->HandleEvent(aEventType, pEvent);
|
---|
118 | else
|
---|
119 | return E_FAIL;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private:
|
---|
123 | NATNetworkEventAdapter *m_pNAT;
|
---|
124 | };
|
---|
125 | typedef ListenerImpl<NATNetworkListener, NATNetworkEventAdapter*> NATNetworkListenerImpl;
|
---|
126 |
|
---|
127 | # ifdef VBOX_WITH_XPCOM
|
---|
128 | class NS_CLASSINFO_NAME(NATNetworkListenerImpl);
|
---|
129 | # endif
|
---|
130 |
|
---|
131 | typedef ComPtr<NATNetworkListenerImpl> ComNatListenerPtr;
|
---|
132 | typedef com::SafeArray<VBoxEventType_T> ComEventTypeArray;
|
---|
133 |
|
---|
134 | /* XXX: const is commented out because of compilation erro on Windows host, but it's intended that this function
|
---|
135 | isn't modify event type array */
|
---|
136 | int createNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr,
|
---|
137 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events);
|
---|
138 | int destroyNatListener(ComNatListenerPtr& listener, const ComVirtualBoxPtr& vboxptr);
|
---|
139 | int createClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr,
|
---|
140 | NATNetworkEventAdapter *adapter, /* const */ ComEventTypeArray& events);
|
---|
141 | int destroyClientListener(ComNatListenerPtr& listener, const ComVirtualBoxClientPtr& vboxclientptr);
|
---|
142 |
|
---|
143 | #endif /* !VBOX_INCLUDED_SRC_NetLib_utils_h */
|
---|