VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetFltInstall.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: VBoxNetFltInstall.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * NetFltInstall - VBoxNetFlt installer command line tool
4 */
5
6/*
7 * Copyright (C) 2008-2022 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#include <VBox/VBoxNetCfg-win.h>
38#include <devguid.h>
39#include <stdio.h>
40
41#define NETFLT_ID L"sun_VBoxNetFlt"
42#define VBOX_NETCFG_APP_NAME L"NetFltInstall"
43#define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
44#define VBOX_NETFLT_MP_INF L".\\VBoxNetFltM.inf"
45#define VBOX_NETFLT_RETRIES 10
46
47
48static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
49{
50 printf("%s", pszString);
51}
52
53/** Wrapper around GetfullPathNameW that will try an alternative INF location.
54 *
55 * The default location is the current directory. If not found there, the
56 * alternative location is the executable directory. If not found there either,
57 * the first alternative is present to the caller.
58 */
59static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
60{
61 LPWSTR pwszFilePart;
62 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
63 if (dwSize <= 0)
64 return dwSize;
65
66 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
67 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
68 {
69 WCHAR wsz[512];
70 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
71 if (cch > 0)
72 {
73 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
74 cch--;
75 unsigned i = 0;
76 while (cch < RT_ELEMENTS(wsz))
77 {
78 wsz[cch] = pwszFilePart[i++];
79 if (!wsz[cch])
80 {
81 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
82 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
83 return dwSize;
84 break;
85 }
86 cch++;
87 }
88 }
89 }
90
91 /* fallback */
92 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
93}
94
95static int VBoxNetFltInstall()
96{
97 WCHAR wszPtInf[MAX_PATH];
98 WCHAR wszMpInf[MAX_PATH];
99 INetCfg *pnc;
100 int rcExit = RTEXITCODE_FAILURE;
101
102 VBoxNetCfgWinSetLogging(winNetCfgLogger);
103
104 HRESULT hr = CoInitialize(NULL);
105 if (hr == S_OK)
106 {
107 for (int i = 0;; i++)
108 {
109 LPWSTR pwszLockedBy = NULL;
110 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy);
111 if (hr == S_OK)
112 {
113 DWORD dwSize;
114 dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, RT_ELEMENTS(wszPtInf), wszPtInf);
115 if (dwSize > 0)
116 {
117 /** @todo add size check for (RT_ELEMENTS(wszPtInf) == dwSize (string length in WCHARs) */
118
119 dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, RT_ELEMENTS(wszMpInf), wszMpInf);
120 if (dwSize > 0)
121 {
122 /** @todo add size check for (RT_ELEMENTS(wszMpInf) == dwSize (string length in WHCARs) */
123
124 LPCWSTR apwszInfs[] = { wszPtInf, wszMpInf };
125 hr = VBoxNetCfgWinNetFltInstall(pnc, apwszInfs, 2);
126 if (hr == S_OK)
127 {
128 wprintf(L"installed successfully\n");
129 rcExit = RTEXITCODE_SUCCESS;
130 }
131 else
132 wprintf(L"error installing VBoxNetFlt (%#lx)\n", hr);
133 }
134 else
135 {
136 hr = HRESULT_FROM_WIN32(GetLastError());
137 wprintf(L"error getting full inf path for VBoxNetFltM.inf (%#lx)\n", hr);
138 }
139 }
140 else
141 {
142 hr = HRESULT_FROM_WIN32(GetLastError());
143 wprintf(L"error getting full inf path for VBoxNetFlt.inf (%#lx)\n", hr);
144 }
145
146 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
147 break;
148 }
149
150 if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy)
151 {
152 if (i < VBOX_NETFLT_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))
153 {
154 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", i + 1, VBOX_NETFLT_RETRIES);
155 CoTaskMemFree(pwszLockedBy);
156 }
157 else
158 {
159 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", pwszLockedBy);
160 CoTaskMemFree(pwszLockedBy);
161 break;
162 }
163 }
164 else
165 {
166 wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);
167 break;
168 }
169 }
170
171 CoUninitialize();
172 }
173 else
174 wprintf(L"Error initializing COM (%#lx)\n", hr);
175
176 VBoxNetCfgWinSetLogging(NULL);
177
178 return rcExit;
179}
180
181int __cdecl main(int argc, char **argv)
182{
183 RT_NOREF2(argc, argv);
184 return VBoxNetFltInstall();
185}
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