1 | /* $Id: VBoxManageUtils.cpp 101035 2023-09-07 08:59:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManageUtils.h - VBoxManage utility functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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 | ULONG getMaxNics(const ComPtr<IMachine> &pMachine)
|
---|
43 | {
|
---|
44 | HRESULT hrc;
|
---|
45 |
|
---|
46 | ULONG maxNetworkAdapters = 0;
|
---|
47 | do {
|
---|
48 | ComPtr<IPlatform> pPlatform;
|
---|
49 | CHECK_ERROR_BREAK(pMachine, COMGETTER(Platform)(pPlatform.asOutParam()));
|
---|
50 |
|
---|
51 | ChipsetType_T chipsetType;
|
---|
52 | CHECK_ERROR_BREAK(pPlatform, COMGETTER(ChipsetType)(&chipsetType));
|
---|
53 |
|
---|
54 | ComPtr<IPlatformProperties> pPlatformProperties;
|
---|
55 | CHECK_ERROR_BREAK(pPlatform, COMGETTER(Properties)(pPlatformProperties.asOutParam()));
|
---|
56 |
|
---|
57 | CHECK_ERROR_BREAK(pPlatformProperties, GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters));
|
---|
58 | } while (0);
|
---|
59 |
|
---|
60 | return maxNetworkAdapters;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * API does NOT verify that whether the interface name set as the
|
---|
66 | * bridged or host-only interface of a NIC is valid. Warn the user if
|
---|
67 | * IHost doesn't seem to know about it (non-fatal).
|
---|
68 | */
|
---|
69 | void verifyHostNetworkInterfaceName(const ComPtr<IVirtualBox> &pVirtualBox,
|
---|
70 | const char *pszTargetName,
|
---|
71 | HostNetworkInterfaceType_T enmTargetType)
|
---|
72 | {
|
---|
73 | HRESULT hrc;
|
---|
74 |
|
---|
75 | AssertReturnVoid( enmTargetType == HostNetworkInterfaceType_Bridged
|
---|
76 | || enmTargetType == HostNetworkInterfaceType_HostOnly);
|
---|
77 |
|
---|
78 | ComPtr<IHost> host;
|
---|
79 | hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
|
---|
80 | if (FAILED(hrc))
|
---|
81 | return;
|
---|
82 |
|
---|
83 | SafeIfaceArray<IHostNetworkInterface> ifs;
|
---|
84 | hrc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(ifs));
|
---|
85 | if (FAILED(hrc))
|
---|
86 | return;
|
---|
87 |
|
---|
88 | for (size_t i = 0; i < ifs.size(); ++i)
|
---|
89 | {
|
---|
90 | const ComPtr<IHostNetworkInterface> iface = ifs[i];
|
---|
91 |
|
---|
92 | Bstr bstrName;
|
---|
93 | hrc = iface->COMGETTER(Name)(bstrName.asOutParam());
|
---|
94 | if (FAILED(hrc))
|
---|
95 | return;
|
---|
96 |
|
---|
97 | if (!bstrName.equals(pszTargetName))
|
---|
98 | continue;
|
---|
99 |
|
---|
100 | /* we found the interface but is it the right type? */
|
---|
101 | HostNetworkInterfaceType_T enmType;
|
---|
102 | hrc = iface->COMGETTER(InterfaceType)(&enmType);
|
---|
103 | if (FAILED(hrc))
|
---|
104 | return;
|
---|
105 |
|
---|
106 | if (enmType == enmTargetType)
|
---|
107 | return; /* seems ok */
|
---|
108 |
|
---|
109 | const char *pszTypeName;
|
---|
110 | char a_szUnknownTypeBuf[32];
|
---|
111 | switch (enmType)
|
---|
112 | {
|
---|
113 | case HostNetworkInterfaceType_Bridged:
|
---|
114 | pszTypeName = Utils::tr("type bridged");
|
---|
115 | break;
|
---|
116 |
|
---|
117 | case HostNetworkInterfaceType_HostOnly:
|
---|
118 | pszTypeName = Utils::tr("type host-only");
|
---|
119 | break;
|
---|
120 |
|
---|
121 | default:
|
---|
122 | RTStrPrintf(a_szUnknownTypeBuf, sizeof(a_szUnknownTypeBuf),
|
---|
123 | Utils::tr("unknown type %RU32"), enmType);
|
---|
124 | pszTypeName = a_szUnknownTypeBuf;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 |
|
---|
128 | RTMsgWarning(Utils::tr("Interface \"%s\" is of %s"), pszTargetName, pszTypeName);
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | RTMsgWarning(Utils::tr("Interface \"%s\" doesn't seem to exist"), pszTargetName);
|
---|
133 | }
|
---|