1 | /* $Id: VBoxManageUtils.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManageUtils.h - VBoxManage utility functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "VBoxManageUtils.h"
|
---|
29 | #include "VBoxManage.h"
|
---|
30 |
|
---|
31 | #include <iprt/message.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 |
|
---|
34 | #include <VBox/com/array.h>
|
---|
35 | #include <VBox/com/errorprint.h>
|
---|
36 | #include <VBox/com/string.h>
|
---|
37 |
|
---|
38 | using namespace com;
|
---|
39 |
|
---|
40 | DECLARE_TRANSLATION_CONTEXT(Utils);
|
---|
41 |
|
---|
42 | unsigned int getMaxNics(const ComPtr<IVirtualBox> &pVirtualBox,
|
---|
43 | const ComPtr<IMachine> &pMachine)
|
---|
44 | {
|
---|
45 | ULONG NetworkAdapterCount = 0;
|
---|
46 | do {
|
---|
47 | HRESULT hrc;
|
---|
48 |
|
---|
49 | ComPtr<ISystemProperties> info;
|
---|
50 | CHECK_ERROR_BREAK(pVirtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
51 |
|
---|
52 | ChipsetType_T aChipset;
|
---|
53 | CHECK_ERROR_BREAK(pMachine, COMGETTER(ChipsetType)(&aChipset));
|
---|
54 |
|
---|
55 | CHECK_ERROR_BREAK(info, GetMaxNetworkAdapters(aChipset, &NetworkAdapterCount));
|
---|
56 | } while (0);
|
---|
57 |
|
---|
58 | return (unsigned int)NetworkAdapterCount;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * API does NOT verify that whether the interface name set as the
|
---|
64 | * bridged or host-only interface of a NIC is valid. Warn the user if
|
---|
65 | * IHost doesn't seem to know about it (non-fatal).
|
---|
66 | */
|
---|
67 | void verifyHostNetworkInterfaceName(const ComPtr<IVirtualBox> &pVirtualBox,
|
---|
68 | const char *pszTargetName,
|
---|
69 | HostNetworkInterfaceType_T enmTargetType)
|
---|
70 | {
|
---|
71 | HRESULT hrc;
|
---|
72 |
|
---|
73 | AssertReturnVoid( enmTargetType == HostNetworkInterfaceType_Bridged
|
---|
74 | || enmTargetType == HostNetworkInterfaceType_HostOnly);
|
---|
75 |
|
---|
76 | ComPtr<IHost> host;
|
---|
77 | hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
|
---|
78 | if (FAILED(hrc))
|
---|
79 | return;
|
---|
80 |
|
---|
81 | SafeIfaceArray<IHostNetworkInterface> ifs;
|
---|
82 | hrc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(ifs));
|
---|
83 | if (FAILED(hrc))
|
---|
84 | return;
|
---|
85 |
|
---|
86 | for (size_t i = 0; i < ifs.size(); ++i)
|
---|
87 | {
|
---|
88 | const ComPtr<IHostNetworkInterface> iface = ifs[i];
|
---|
89 |
|
---|
90 | Bstr bstrName;
|
---|
91 | hrc = iface->COMGETTER(Name)(bstrName.asOutParam());
|
---|
92 | if (FAILED(hrc))
|
---|
93 | return;
|
---|
94 |
|
---|
95 | if (!bstrName.equals(pszTargetName))
|
---|
96 | continue;
|
---|
97 |
|
---|
98 | /* we found the interface but is it the right type? */
|
---|
99 | HostNetworkInterfaceType_T enmType;
|
---|
100 | hrc = iface->COMGETTER(InterfaceType)(&enmType);
|
---|
101 | if (FAILED(hrc))
|
---|
102 | return;
|
---|
103 |
|
---|
104 | if (enmType == enmTargetType)
|
---|
105 | return; /* seems ok */
|
---|
106 |
|
---|
107 | const char *pszTypeName;
|
---|
108 | char a_szUnknownTypeBuf[32];
|
---|
109 | switch (enmType)
|
---|
110 | {
|
---|
111 | case HostNetworkInterfaceType_Bridged:
|
---|
112 | pszTypeName = Utils::tr("type bridged");
|
---|
113 | break;
|
---|
114 |
|
---|
115 | case HostNetworkInterfaceType_HostOnly:
|
---|
116 | pszTypeName = Utils::tr("type host-only");
|
---|
117 | break;
|
---|
118 |
|
---|
119 | default:
|
---|
120 | RTStrPrintf(a_szUnknownTypeBuf, sizeof(a_szUnknownTypeBuf),
|
---|
121 | Utils::tr("unknown type %RU32"), enmType);
|
---|
122 | pszTypeName = a_szUnknownTypeBuf;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 |
|
---|
126 | RTMsgWarning(Utils::tr("Interface \"%s\" is of %s"), pszTargetName, pszTypeName);
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | RTMsgWarning(Utils::tr("Interface \"%s\" doesn't seem to exist"), pszTargetName);
|
---|
131 | }
|
---|