VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/NetFltInstall.cpp@ 25439

Last change on this file since 25439 was 25388, checked in by vboxsync, 15 years ago

revert erroneus commit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: NetFltInstall.cpp 25388 2009-12-15 10:55:03Z vboxsync $ */
2/** @file
3 * NetFltInstall - VBoxNetFlt installer command line tool
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include <vbox/WinNetConfig.h>
23#include <devguid.h>
24#include <stdio.h>
25
26#define NETFLT_ID L"sun_VBoxNetFlt"
27#define VBOX_NETCFG_APP_NAME L"NetFltInstall"
28#define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
29#define VBOX_NETFLT_MP_INF L".\\VBoxNetFlt_m.inf"
30#define VBOX_NETFLT_RETRIES 10
31
32
33static VOID winNetCfgLogger (LPCWSTR szString)
34{
35 wprintf(L"%s", szString);
36}
37
38/** Wrapper aroung GetfullPathNameW that will try an alternative INF location.
39 *
40 * The default location is the current directory. If not found there, the
41 * alternative locatoin is the executable directory. If not found there either,
42 * the first alternative is present to the caller.
43 */
44static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
45{
46#ifdef DEBUG_bird /** @todo make this default behavior after 3.1. */
47 LPWSTR pwszFilePart;
48 DWORD dwSize = GetFullPathNameW(pwszName, cchFull, pwszFull, &pwszFilePart);
49 if(dwSize <= 0)
50 return dwSize;
51
52 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
53 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
54 {
55 WCHAR wsz[512];
56 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], sizeof(wsz) / sizeof(wsz[0]));
57 if(cch > 0)
58 {
59 while(cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
60 cch--;
61 unsigned i = 0;
62 while(cch < sizeof(wsz) / sizeof(wsz[0]))
63 {
64 wsz[cch] = pwszFilePart[i++];
65 if(!wsz[cch])
66 {
67 dwSize = GetFullPathNameW(wsz, cchFull, pwszFull, NULL);
68 if( dwSize > 0
69 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
70 return dwSize;
71 break;
72 }
73 cch++;
74 }
75 }
76 }
77
78 /* fallback */
79#endif
80 return GetFullPathNameW(pwszName, cchFull, pwszFull, NULL);
81}
82
83static int InstallNetFlt()
84{
85 WCHAR PtInf[MAX_PATH];
86 WCHAR MpInf[MAX_PATH];
87 INetCfg *pnc;
88 LPWSTR lpszLockedBy = NULL;
89 int r = 1;
90
91 VBoxNetCfgWinSetLogging(winNetCfgLogger);
92
93 HRESULT hr = CoInitialize(NULL);
94 if(hr == S_OK)
95 {
96 int i = 0;
97 do
98 {
99 hr = VBoxNetCfgWinQueryINetCfg(TRUE, VBOX_NETCFG_APP_NAME, &pnc, &lpszLockedBy);
100 if(hr == S_OK)
101 {
102 DWORD dwSize;
103 dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, sizeof(PtInf)/sizeof(PtInf[0]), PtInf);
104 if(dwSize > 0)
105 {
106 /** @todo add size check for (sizeof(PtInf)/sizeof(PtInf[0])) == dwSize (string length in sizeof(PtInf[0])) */
107
108 dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf);
109 if(dwSize > 0)
110 {
111 /** @todo add size check for (sizeof(MpInf)/sizeof(MpInf[0])) == dwSize (string length in sizeof(MpInf[0])) */
112
113 LPCWSTR aInfs[] = {PtInf, MpInf};
114 hr = VBoxNetCfgWinNetFltInstall(pnc, aInfs, 2);
115 if(hr == S_OK)
116 {
117 wprintf(L"installed successfully\n");
118 r = 0;
119 }
120 else
121 {
122 wprintf(L"error installing VBoxNetFlt (0x%x)\n", hr);
123 }
124 }
125 else
126 {
127 hr = HRESULT_FROM_WIN32(GetLastError());
128 wprintf(L"error getting full inf path for VBoxNetFlt_m.inf (0x%x)\n", hr);
129 }
130 }
131 else
132 {
133 hr = HRESULT_FROM_WIN32(GetLastError());
134 wprintf(L"error getting full inf path for VBoxNetFlt.inf (0x%x)\n", hr);
135 }
136
137
138 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
139 break;
140 }
141 else if(hr == NETCFG_E_NO_WRITE_LOCK && lpszLockedBy)
142 {
143 if(i < VBOX_NETFLT_RETRIES && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
144 {
145 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", ++i, VBOX_NETFLT_RETRIES);
146 CoTaskMemFree(lpszLockedBy);
147 }
148 else
149 {
150 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", lpszLockedBy);
151 r = 1;
152 CoTaskMemFree(lpszLockedBy);
153 break;
154 }
155 }
156 else
157 {
158 wprintf(L"Error getting the INetCfg interface (0x%x)\n", hr);
159 r = 1;
160 break;
161 }
162 } while(true);
163
164 CoUninitialize();
165 }
166 else
167 {
168 wprintf(L"Error initializing COM (0x%x)\n", hr);
169 r = 1;
170 }
171
172 VBoxNetCfgWinSetLogging(NULL);
173
174 return r;
175}
176
177int __cdecl main(int argc, char **argv)
178{
179 return InstallNetFlt();
180}
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