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