VirtualBox

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

Last change on this file since 79409 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: VBoxNetFltInstall.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * NetFltInstall - VBoxNetFlt installer command line tool
4 */
5
6/*
7 * Copyright (C) 2008-2019 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#include <VBox/VBoxNetCfg-win.h>
28#include <devguid.h>
29#include <stdio.h>
30
31#define NETFLT_ID L"sun_VBoxNetFlt"
32#define VBOX_NETCFG_APP_NAME L"NetFltInstall"
33#define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
34#define VBOX_NETFLT_MP_INF L".\\VBoxNetFltM.inf"
35#define VBOX_NETFLT_RETRIES 10
36
37
38static VOID winNetCfgLogger (LPCSTR szString)
39{
40 printf("%s", szString);
41}
42
43/** Wrapper around GetfullPathNameW that will try an alternative INF location.
44 *
45 * The default location is the current directory. If not found there, the
46 * alternative location is the executable directory. If not found there either,
47 * the first alternative is present to the caller.
48 */
49static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
50{
51 LPWSTR pwszFilePart;
52 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
53 if (dwSize <= 0)
54 return dwSize;
55
56 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
57 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
58 {
59 WCHAR wsz[512];
60 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], sizeof(wsz) / sizeof(wsz[0]));
61 if (cch > 0)
62 {
63 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
64 cch--;
65 unsigned i = 0;
66 while (cch < sizeof(wsz) / sizeof(wsz[0]))
67 {
68 wsz[cch] = pwszFilePart[i++];
69 if (!wsz[cch])
70 {
71 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
72 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
73 return dwSize;
74 break;
75 }
76 cch++;
77 }
78 }
79 }
80
81 /* fallback */
82 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
83}
84
85static int VBoxNetFltInstall()
86{
87 WCHAR PtInf[MAX_PATH];
88 WCHAR MpInf[MAX_PATH];
89 INetCfg *pnc;
90 LPWSTR lpszLockedBy = NULL;
91 int r = 1;
92
93 VBoxNetCfgWinSetLogging(winNetCfgLogger);
94
95 HRESULT hr = CoInitialize(NULL);
96 if (hr == S_OK)
97 {
98 int i = 0;
99 do
100 {
101 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &lpszLockedBy);
102 if (hr == S_OK)
103 {
104 DWORD dwSize;
105 dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, sizeof(PtInf)/sizeof(PtInf[0]), PtInf);
106 if (dwSize > 0)
107 {
108 /** @todo add size check for (sizeof(PtInf)/sizeof(PtInf[0])) == dwSize (string length in sizeof(PtInf[0])) */
109
110 dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf);
111 if (dwSize > 0)
112 {
113 /** @todo add size check for (sizeof(MpInf)/sizeof(MpInf[0])) == dwSize (string length in sizeof(MpInf[0])) */
114
115 LPCWSTR aInfs[] = {PtInf, MpInf};
116 hr = VBoxNetCfgWinNetFltInstall(pnc, aInfs, 2);
117 if (hr == S_OK)
118 {
119 wprintf(L"installed successfully\n");
120 r = 0;
121 }
122 else
123 {
124 wprintf(L"error installing VBoxNetFlt (0x%x)\n", hr);
125 }
126 }
127 else
128 {
129 hr = HRESULT_FROM_WIN32(GetLastError());
130 wprintf(L"error getting full inf path for VBoxNetFltM.inf (0x%x)\n", hr);
131 }
132 }
133 else
134 {
135 hr = HRESULT_FROM_WIN32(GetLastError());
136 wprintf(L"error getting full inf path for VBoxNetFlt.inf (0x%x)\n", hr);
137 }
138
139
140 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
141 break;
142 }
143 else if (hr == NETCFG_E_NO_WRITE_LOCK && lpszLockedBy)
144 {
145 if (i < VBOX_NETFLT_RETRIES && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
146 {
147 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", ++i, VBOX_NETFLT_RETRIES);
148 CoTaskMemFree(lpszLockedBy);
149 }
150 else
151 {
152 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", lpszLockedBy);
153 r = 1;
154 CoTaskMemFree(lpszLockedBy);
155 break;
156 }
157 }
158 else
159 {
160 wprintf(L"Error getting the INetCfg interface (0x%x)\n", hr);
161 r = 1;
162 break;
163 }
164 } while (true);
165
166 CoUninitialize();
167 }
168 else
169 {
170 wprintf(L"Error initializing COM (0x%x)\n", hr);
171 r = 1;
172 }
173
174 VBoxNetCfgWinSetLogging(NULL);
175
176 return r;
177}
178
179int __cdecl main(int argc, char **argv)
180{
181 RT_NOREF2(argc, argv);
182 return VBoxNetFltInstall();
183}
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