[88079] | 1 | /* $Id: VBoxManageUtils.cpp 101035 2023-09-07 08:59:15Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VBoxManageUtils.h - VBoxManage utility functions.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[88079] | 8 | *
|
---|
[96407] | 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
|
---|
[88079] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include "VBoxManageUtils.h"
|
---|
[92372] | 29 | #include "VBoxManage.h"
|
---|
[88079] | 30 |
|
---|
| 31 | #include <iprt/message.h>
|
---|
| 32 | #include <iprt/string.h>
|
---|
| 33 |
|
---|
| 34 | #include <VBox/com/array.h>
|
---|
[88086] | 35 | #include <VBox/com/errorprint.h>
|
---|
[88079] | 36 | #include <VBox/com/string.h>
|
---|
| 37 |
|
---|
| 38 | using namespace com;
|
---|
| 39 |
|
---|
[92372] | 40 | DECLARE_TRANSLATION_CONTEXT(Utils);
|
---|
[88079] | 41 |
|
---|
[101035] | 42 | ULONG getMaxNics(const ComPtr<IMachine> &pMachine)
|
---|
[88086] | 43 | {
|
---|
[101035] | 44 | HRESULT hrc;
|
---|
| 45 |
|
---|
| 46 | ULONG maxNetworkAdapters = 0;
|
---|
[88087] | 47 | do {
|
---|
[101035] | 48 | ComPtr<IPlatform> pPlatform;
|
---|
| 49 | CHECK_ERROR_BREAK(pMachine, COMGETTER(Platform)(pPlatform.asOutParam()));
|
---|
[88086] | 50 |
|
---|
[101035] | 51 | ChipsetType_T chipsetType;
|
---|
| 52 | CHECK_ERROR_BREAK(pPlatform, COMGETTER(ChipsetType)(&chipsetType));
|
---|
[88087] | 53 |
|
---|
[101035] | 54 | ComPtr<IPlatformProperties> pPlatformProperties;
|
---|
| 55 | CHECK_ERROR_BREAK(pPlatform, COMGETTER(Properties)(pPlatformProperties.asOutParam()));
|
---|
[88087] | 56 |
|
---|
[101035] | 57 | CHECK_ERROR_BREAK(pPlatformProperties, GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters));
|
---|
[88086] | 58 | } while (0);
|
---|
| 59 |
|
---|
[101035] | 60 | return maxNetworkAdapters;
|
---|
[88086] | 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[88079] | 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:
|
---|
[92372] | 114 | pszTypeName = Utils::tr("type bridged");
|
---|
[88079] | 115 | break;
|
---|
| 116 |
|
---|
| 117 | case HostNetworkInterfaceType_HostOnly:
|
---|
[92372] | 118 | pszTypeName = Utils::tr("type host-only");
|
---|
[88079] | 119 | break;
|
---|
| 120 |
|
---|
| 121 | default:
|
---|
| 122 | RTStrPrintf(a_szUnknownTypeBuf, sizeof(a_szUnknownTypeBuf),
|
---|
[92372] | 123 | Utils::tr("unknown type %RU32"), enmType);
|
---|
[88079] | 124 | pszTypeName = a_szUnknownTypeBuf;
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[92372] | 128 | RTMsgWarning(Utils::tr("Interface \"%s\" is of %s"), pszTargetName, pszTypeName);
|
---|
[88079] | 129 | return;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[92372] | 132 | RTMsgWarning(Utils::tr("Interface \"%s\" doesn't seem to exist"), pszTargetName);
|
---|
[88079] | 133 | }
|
---|