1 | /* $Id: HostOnlyNetworkImpl.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IHostOnlyNetwork COM class implementations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021-2022 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 | #define LOG_GROUP LOG_GROUP_MAIN_HOSTONLYNETWORK
|
---|
20 | #include <VBox/settings.h>
|
---|
21 | #include <iprt/cpp/utils.h>
|
---|
22 |
|
---|
23 | #include "VirtualBoxImpl.h"
|
---|
24 | #include "HostOnlyNetworkImpl.h"
|
---|
25 | #include "AutoCaller.h"
|
---|
26 | #include "LoggingNew.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | struct HostOnlyNetwork::Data
|
---|
30 | {
|
---|
31 | Data() : pVirtualBox(NULL) {}
|
---|
32 | virtual ~Data() {}
|
---|
33 |
|
---|
34 | /** weak VirtualBox parent */
|
---|
35 | VirtualBox * const pVirtualBox;
|
---|
36 |
|
---|
37 | /** HostOnlyNetwork settings */
|
---|
38 | settings::HostOnlyNetwork s;
|
---|
39 | };
|
---|
40 |
|
---|
41 | ////////////////////////////////////////////////////////////////////////////////
|
---|
42 | //
|
---|
43 | // HostOnlyNetwork constructor / destructor
|
---|
44 | //
|
---|
45 | // ////////////////////////////////////////////////////////////////////////////////
|
---|
46 | HostOnlyNetwork::HostOnlyNetwork() : m(NULL)
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 | HostOnlyNetwork::~HostOnlyNetwork()
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | HRESULT HostOnlyNetwork::FinalConstruct()
|
---|
56 | {
|
---|
57 | return BaseFinalConstruct();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void HostOnlyNetwork::FinalRelease()
|
---|
61 | {
|
---|
62 | uninit();
|
---|
63 |
|
---|
64 | BaseFinalRelease();
|
---|
65 | }
|
---|
66 |
|
---|
67 | HRESULT HostOnlyNetwork::init(VirtualBox *aVirtualBox, Utf8Str aName)
|
---|
68 | {
|
---|
69 | // Enclose the state transition NotReady->InInit->Ready.
|
---|
70 | AutoInitSpan autoInitSpan(this);
|
---|
71 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
72 |
|
---|
73 | m = new Data();
|
---|
74 | /* share VirtualBox weakly */
|
---|
75 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
76 |
|
---|
77 | m->s.strNetworkName = aName;
|
---|
78 | m->s.fEnabled = true;
|
---|
79 | m->s.uuid.create();
|
---|
80 |
|
---|
81 | autoInitSpan.setSucceeded();
|
---|
82 | return S_OK;
|
---|
83 | }
|
---|
84 |
|
---|
85 | void HostOnlyNetwork::uninit()
|
---|
86 | {
|
---|
87 | // Enclose the state transition Ready->InUninit->NotReady.
|
---|
88 | AutoUninitSpan autoUninitSpan(this);
|
---|
89 | if (autoUninitSpan.uninitDone())
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | HRESULT HostOnlyNetwork::i_loadSettings(const settings::HostOnlyNetwork &data)
|
---|
94 | {
|
---|
95 | AutoCaller autoCaller(this);
|
---|
96 | AssertComRCReturnRC(autoCaller.rc());
|
---|
97 |
|
---|
98 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
99 | m->s = data;
|
---|
100 |
|
---|
101 | return S_OK;
|
---|
102 | }
|
---|
103 |
|
---|
104 | HRESULT HostOnlyNetwork::i_saveSettings(settings::HostOnlyNetwork &data)
|
---|
105 | {
|
---|
106 | AutoCaller autoCaller(this);
|
---|
107 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
108 |
|
---|
109 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
110 | AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
|
---|
111 | data = m->s;
|
---|
112 |
|
---|
113 | return S_OK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | #if 0
|
---|
117 | Utf8Str HostOnlyNetwork::i_getNetworkId()
|
---|
118 | {
|
---|
119 | return m->s.strNetworkId;
|
---|
120 | }
|
---|
121 |
|
---|
122 | Utf8Str HostOnlyNetwork::i_getNetworkName()
|
---|
123 | {
|
---|
124 | return m->s.strNetworkName;
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 |
|
---|
128 |
|
---|
129 | HRESULT HostOnlyNetwork::getNetworkName(com::Utf8Str &aNetworkName)
|
---|
130 | {
|
---|
131 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
132 | AssertReturn(!m->s.strNetworkName.isEmpty(), E_FAIL);
|
---|
133 | aNetworkName = m->s.strNetworkName;
|
---|
134 | return S_OK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | HRESULT HostOnlyNetwork::setNetworkName(const com::Utf8Str &aNetworkName)
|
---|
138 | {
|
---|
139 | if (aNetworkName.isEmpty())
|
---|
140 | return setError(E_INVALIDARG,
|
---|
141 | tr("Network name cannot be empty"));
|
---|
142 | {
|
---|
143 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
144 | if (aNetworkName == m->s.strNetworkName)
|
---|
145 | return S_OK;
|
---|
146 |
|
---|
147 | m->s.strNetworkName = aNetworkName;
|
---|
148 | }
|
---|
149 |
|
---|
150 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
151 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
152 | ComAssertComRCRetRC(rc);
|
---|
153 | return S_OK;
|
---|
154 | }
|
---|
155 |
|
---|
156 | HRESULT HostOnlyNetwork::getNetworkMask(com::Utf8Str &aNetworkMask)
|
---|
157 | {
|
---|
158 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
159 | AssertReturn(!m->s.strNetworkMask.isEmpty(), E_FAIL);
|
---|
160 | aNetworkMask = m->s.strNetworkMask;
|
---|
161 | return S_OK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | HRESULT HostOnlyNetwork::setNetworkMask(const com::Utf8Str &aNetworkMask)
|
---|
165 | {
|
---|
166 | if (aNetworkMask.isEmpty())
|
---|
167 | return setError(E_INVALIDARG,
|
---|
168 | tr("Network mask cannot be empty"));
|
---|
169 | {
|
---|
170 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
171 | if (aNetworkMask == m->s.strNetworkMask)
|
---|
172 | return S_OK;
|
---|
173 |
|
---|
174 | m->s.strNetworkMask = aNetworkMask;
|
---|
175 | }
|
---|
176 |
|
---|
177 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
178 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
179 | ComAssertComRCRetRC(rc);
|
---|
180 | return S_OK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | HRESULT HostOnlyNetwork::getEnabled(BOOL *aEnabled)
|
---|
184 | {
|
---|
185 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
186 | *aEnabled = m->s.fEnabled;
|
---|
187 | return S_OK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | HRESULT HostOnlyNetwork::setEnabled(BOOL aEnabled)
|
---|
191 | {
|
---|
192 | {
|
---|
193 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
194 | if (RT_BOOL(aEnabled) == m->s.fEnabled)
|
---|
195 | return S_OK;
|
---|
196 | m->s.fEnabled = RT_BOOL(aEnabled);
|
---|
197 | }
|
---|
198 |
|
---|
199 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
200 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
201 | ComAssertComRCRetRC(rc);
|
---|
202 | return S_OK;
|
---|
203 | }
|
---|
204 |
|
---|
205 | HRESULT HostOnlyNetwork::getHostIP(com::Utf8Str &aHostIP)
|
---|
206 | {
|
---|
207 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
208 | aHostIP = m->s.strIPLower;
|
---|
209 | return S_OK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | HRESULT HostOnlyNetwork::getLowerIP(com::Utf8Str &aLowerIP)
|
---|
213 | {
|
---|
214 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
215 | aLowerIP = m->s.strIPLower;
|
---|
216 | return S_OK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | HRESULT HostOnlyNetwork::setLowerIP(const com::Utf8Str &aLowerIP)
|
---|
220 | {
|
---|
221 | {
|
---|
222 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
223 | if (aLowerIP == m->s.strIPLower)
|
---|
224 | return S_OK;
|
---|
225 | m->s.strIPLower = aLowerIP;
|
---|
226 | }
|
---|
227 |
|
---|
228 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
229 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
230 | ComAssertComRCRetRC(rc);
|
---|
231 | return S_OK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | HRESULT HostOnlyNetwork::getUpperIP(com::Utf8Str &aUpperIP)
|
---|
235 | {
|
---|
236 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
237 | aUpperIP = m->s.strIPUpper;
|
---|
238 | return S_OK;
|
---|
239 | }
|
---|
240 |
|
---|
241 | HRESULT HostOnlyNetwork::setUpperIP(const com::Utf8Str &aUpperIP)
|
---|
242 | {
|
---|
243 | {
|
---|
244 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
245 | if (aUpperIP == m->s.strIPUpper)
|
---|
246 | return S_OK;
|
---|
247 | m->s.strIPUpper = aUpperIP;
|
---|
248 | }
|
---|
249 |
|
---|
250 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
251 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
252 | ComAssertComRCRetRC(rc);
|
---|
253 | return S_OK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | HRESULT HostOnlyNetwork::getId(com::Guid &aId)
|
---|
257 | {
|
---|
258 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
259 | aId = m->s.uuid;
|
---|
260 | return S_OK;
|
---|
261 | }
|
---|
262 |
|
---|
263 | HRESULT HostOnlyNetwork::setId(const com::Guid &aId)
|
---|
264 | {
|
---|
265 | {
|
---|
266 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
267 | if (aId == m->s.uuid)
|
---|
268 | return S_OK;
|
---|
269 | m->s.uuid = aId;
|
---|
270 | }
|
---|
271 |
|
---|
272 | AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
273 | HRESULT rc = m->pVirtualBox->i_saveSettings();
|
---|
274 | ComAssertComRCRetRC(rc);
|
---|
275 | return S_OK;
|
---|
276 | }
|
---|
277 |
|
---|