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