VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetLwfInstall.cpp

Last change on this file was 106061, checked in by vboxsync, 3 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/* $Id: VBoxNetLwfInstall.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * NetLwfInstall - VBoxNetLwf installer command line tool
4 */
5
6/*
7 * Copyright (C) 2014-2024 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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/VBoxNetCfg-win.h>
42#include <devguid.h>
43
44#include <iprt/initterm.h>
45#include <iprt/message.h>
46#include <iprt/utf16.h>
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52#define VBOX_NETCFG_APP_NAME L"NetLwfInstall"
53#define VBOX_NETLWF_INF L".\\VBoxNetLwf.inf"
54#define VBOX_NETLWF_RETRIES 10
55
56
57static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
58{
59 RTMsgInfo("%s", pszString);
60}
61
62/** Wrapper around GetfullPathNameW that will try an alternative INF location.
63 *
64 * The default location is the current directory. If not found there, the
65 * alternative location is the executable directory. If not found there either,
66 * the first alternative is present to the caller.
67 */
68static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
69{
70 LPWSTR pwszFilePart;
71 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
72 if (dwSize <= 0)
73 return dwSize;
74
75 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
76 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
77 {
78 WCHAR wsz[512];
79 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
80 if (cch > 0)
81 {
82 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
83 cch--;
84 unsigned i = 0;
85 while (cch < RT_ELEMENTS(wsz))
86 {
87 wsz[cch] = pwszFilePart[i++];
88 if (!wsz[cch])
89 {
90 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
91 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
92 return dwSize;
93 break;
94 }
95 cch++;
96 }
97 }
98 }
99
100 /* fallback */
101 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
102}
103
104static int VBoxNetLwfInstall()
105{
106 WCHAR wszInf[MAX_PATH];
107 INetCfg *pnc;
108 int rcExit = RTEXITCODE_FAILURE;
109
110 VBoxNetCfgWinSetLogging(winNetCfgLogger);
111
112 HRESULT hr = CoInitialize(NULL);
113 if (hr == S_OK)
114 {
115 for (int i = 0;; i++)
116 {
117 LPWSTR pwszLockedBy = NULL;
118 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy);
119 if (hr == S_OK)
120 {
121 DWORD dwSize;
122 dwSize = MyGetfullPathNameW(VBOX_NETLWF_INF, RT_ELEMENTS(wszInf), wszInf);
123 if (dwSize > 0)
124 {
125 /** @todo add size check for (RT_ELEMENTS(wszInf) == dwSize (string length in WCHARs) */
126 hr = VBoxNetCfgWinNetLwfInstall(pnc, wszInf);
127 if (hr == S_OK)
128 {
129 RTMsgInfo("Installed successfully!");
130 rcExit = RTEXITCODE_SUCCESS;
131 }
132 else
133 RTMsgError("Failed installing VBoxNetLwf: %Rhrc", hr);
134 }
135 else
136 {
137 hr = HRESULT_FROM_WIN32(GetLastError());
138 RTMsgError("Failed getting full inf path for VBoxNetLwf.inf: %Rhrc", hr);
139 }
140
141 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
142 break;
143 }
144
145 if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy)
146 {
147 if (i < VBOX_NETLWF_RETRIES && RTUtf16ICmpAscii(pwszLockedBy, "6to4svc.dll") == 0)
148 {
149 RTMsgInfo("6to4svc.dll is holding the lock - retrying %d out of %d\n", i + 1, VBOX_NETLWF_RETRIES);
150 CoTaskMemFree(pwszLockedBy);
151 }
152 else
153 {
154 RTMsgError("write lock is owned by another application (%ls), close the application and retry installing",
155 pwszLockedBy);
156 CoTaskMemFree(pwszLockedBy);
157 break;
158 }
159 }
160 else
161 {
162 RTMsgError("Failed getting the INetCfg interface: %Rhrc", hr);
163 break;
164 }
165 }
166
167 CoUninitialize();
168 }
169 else
170 RTMsgError("Failed initializing COM: %Rhrc", hr);
171
172 VBoxNetCfgWinSetLogging(NULL);
173
174 return rcExit;
175}
176
177int __cdecl main(int argc, char **argv)
178{
179 RTR3InitExeNoArguments(0);
180 if (argc != 1)
181 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n");
182 NOREF(argv);
183
184 return VBoxNetLwfInstall();
185}
186
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette