1 | /* $Id: VBoxManageUtils.cpp 94234 2022-03-15 09:19:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManageUtils.h - VBoxManage utility functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | #include "VBoxManageUtils.h"
|
---|
19 | #include "VBoxManage.h"
|
---|
20 |
|
---|
21 | #include <iprt/message.h>
|
---|
22 | #include <iprt/string.h>
|
---|
23 |
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/com/errorprint.h>
|
---|
26 | #include <VBox/com/string.h>
|
---|
27 |
|
---|
28 | using namespace com;
|
---|
29 |
|
---|
30 | DECLARE_TRANSLATION_CONTEXT(Utils);
|
---|
31 |
|
---|
32 | unsigned int getMaxNics(const ComPtr<IVirtualBox> &pVirtualBox,
|
---|
33 | const ComPtr<IMachine> &pMachine)
|
---|
34 | {
|
---|
35 | ULONG NetworkAdapterCount = 0;
|
---|
36 | do {
|
---|
37 | HRESULT rc;
|
---|
38 |
|
---|
39 | ComPtr<ISystemProperties> info;
|
---|
40 | CHECK_ERROR_BREAK(pVirtualBox, COMGETTER(SystemProperties)(info.asOutParam()));
|
---|
41 |
|
---|
42 | ChipsetType_T aChipset;
|
---|
43 | CHECK_ERROR_BREAK(pMachine, COMGETTER(ChipsetType)(&aChipset));
|
---|
44 |
|
---|
45 | CHECK_ERROR_BREAK(info, GetMaxNetworkAdapters(aChipset, &NetworkAdapterCount));
|
---|
46 | } while (0);
|
---|
47 |
|
---|
48 | return (unsigned int)NetworkAdapterCount;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * API does NOT verify that whether the interface name set as the
|
---|
54 | * bridged or host-only interface of a NIC is valid. Warn the user if
|
---|
55 | * IHost doesn't seem to know about it (non-fatal).
|
---|
56 | */
|
---|
57 | void verifyHostNetworkInterfaceName(const ComPtr<IVirtualBox> &pVirtualBox,
|
---|
58 | const char *pszTargetName,
|
---|
59 | HostNetworkInterfaceType_T enmTargetType)
|
---|
60 | {
|
---|
61 | HRESULT hrc;
|
---|
62 |
|
---|
63 | AssertReturnVoid( enmTargetType == HostNetworkInterfaceType_Bridged
|
---|
64 | || enmTargetType == HostNetworkInterfaceType_HostOnly);
|
---|
65 |
|
---|
66 | ComPtr<IHost> host;
|
---|
67 | hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
|
---|
68 | if (FAILED(hrc))
|
---|
69 | return;
|
---|
70 |
|
---|
71 | SafeIfaceArray<IHostNetworkInterface> ifs;
|
---|
72 | hrc = host->COMGETTER(NetworkInterfaces)(ComSafeArrayAsOutParam(ifs));
|
---|
73 | if (FAILED(hrc))
|
---|
74 | return;
|
---|
75 |
|
---|
76 | for (size_t i = 0; i < ifs.size(); ++i)
|
---|
77 | {
|
---|
78 | const ComPtr<IHostNetworkInterface> iface = ifs[i];
|
---|
79 |
|
---|
80 | Bstr bstrName;
|
---|
81 | hrc = iface->COMGETTER(Name)(bstrName.asOutParam());
|
---|
82 | if (FAILED(hrc))
|
---|
83 | return;
|
---|
84 |
|
---|
85 | if (!bstrName.equals(pszTargetName))
|
---|
86 | continue;
|
---|
87 |
|
---|
88 | /* we found the interface but is it the right type? */
|
---|
89 | HostNetworkInterfaceType_T enmType;
|
---|
90 | hrc = iface->COMGETTER(InterfaceType)(&enmType);
|
---|
91 | if (FAILED(hrc))
|
---|
92 | return;
|
---|
93 |
|
---|
94 | if (enmType == enmTargetType)
|
---|
95 | return; /* seems ok */
|
---|
96 |
|
---|
97 | const char *pszTypeName;
|
---|
98 | char a_szUnknownTypeBuf[32];
|
---|
99 | switch (enmType)
|
---|
100 | {
|
---|
101 | case HostNetworkInterfaceType_Bridged:
|
---|
102 | pszTypeName = Utils::tr("type bridged");
|
---|
103 | break;
|
---|
104 |
|
---|
105 | case HostNetworkInterfaceType_HostOnly:
|
---|
106 | pszTypeName = Utils::tr("type host-only");
|
---|
107 | break;
|
---|
108 |
|
---|
109 | default:
|
---|
110 | RTStrPrintf(a_szUnknownTypeBuf, sizeof(a_szUnknownTypeBuf),
|
---|
111 | Utils::tr("unknown type %RU32"), enmType);
|
---|
112 | pszTypeName = a_szUnknownTypeBuf;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 |
|
---|
116 | RTMsgWarning(Utils::tr("Interface \"%s\" is of %s"), pszTargetName, pszTypeName);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | RTMsgWarning(Utils::tr("Interface \"%s\" doesn't seem to exist"), pszTargetName);
|
---|
121 | }
|
---|