1 | /* $Id: VBoxNetFltInstall.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetFltInstall - VBoxNetFlt installer command line tool
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2011 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/VBoxNetCfg-win.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".\\VBoxNetFltM.inf"
|
---|
26 | #define VBOX_NETFLT_RETRIES 10
|
---|
27 |
|
---|
28 |
|
---|
29 | static VOID winNetCfgLogger (LPCSTR szString)
|
---|
30 | {
|
---|
31 | printf("%s", szString);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /** Wrapper around GetfullPathNameW that will try an alternative INF location.
|
---|
35 | *
|
---|
36 | * The default location is the current directory. If not found there, the
|
---|
37 | * alternative location 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, (DWORD)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, (DWORD)cchFull, pwszFull, NULL);
|
---|
63 | if(dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
|
---|
64 | return dwSize;
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | cch++;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* fallback */
|
---|
73 | return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
|
---|
74 | }
|
---|
75 |
|
---|
76 | static int VBoxNetFltInstall()
|
---|
77 | {
|
---|
78 | WCHAR PtInf[MAX_PATH];
|
---|
79 | WCHAR MpInf[MAX_PATH];
|
---|
80 | INetCfg *pnc;
|
---|
81 | LPWSTR lpszLockedBy = NULL;
|
---|
82 | int r = 1;
|
---|
83 |
|
---|
84 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
85 |
|
---|
86 | HRESULT hr = CoInitialize(NULL);
|
---|
87 | if(hr == S_OK)
|
---|
88 | {
|
---|
89 | int i = 0;
|
---|
90 | do
|
---|
91 | {
|
---|
92 | hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &lpszLockedBy);
|
---|
93 | if(hr == S_OK)
|
---|
94 | {
|
---|
95 | DWORD dwSize;
|
---|
96 | dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, sizeof(PtInf)/sizeof(PtInf[0]), PtInf);
|
---|
97 | if(dwSize > 0)
|
---|
98 | {
|
---|
99 | /** @todo add size check for (sizeof(PtInf)/sizeof(PtInf[0])) == dwSize (string length in sizeof(PtInf[0])) */
|
---|
100 |
|
---|
101 | dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf);
|
---|
102 | if(dwSize > 0)
|
---|
103 | {
|
---|
104 | /** @todo add size check for (sizeof(MpInf)/sizeof(MpInf[0])) == dwSize (string length in sizeof(MpInf[0])) */
|
---|
105 |
|
---|
106 | LPCWSTR aInfs[] = {PtInf, MpInf};
|
---|
107 | hr = VBoxNetCfgWinNetFltInstall(pnc, aInfs, 2);
|
---|
108 | if(hr == S_OK)
|
---|
109 | {
|
---|
110 | wprintf(L"installed successfully\n");
|
---|
111 | r = 0;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | wprintf(L"error installing VBoxNetFlt (0x%x)\n", hr);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
121 | wprintf(L"error getting full inf path for VBoxNetFltM.inf (0x%x)\n", hr);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
127 | wprintf(L"error getting full inf path for VBoxNetFlt.inf (0x%x)\n", hr);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
|
---|
132 | break;
|
---|
133 | }
|
---|
134 | else if(hr == NETCFG_E_NO_WRITE_LOCK && lpszLockedBy)
|
---|
135 | {
|
---|
136 | if(i < VBOX_NETFLT_RETRIES && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
|
---|
137 | {
|
---|
138 | wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", ++i, VBOX_NETFLT_RETRIES);
|
---|
139 | CoTaskMemFree(lpszLockedBy);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", lpszLockedBy);
|
---|
144 | r = 1;
|
---|
145 | CoTaskMemFree(lpszLockedBy);
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else
|
---|
150 | {
|
---|
151 | wprintf(L"Error getting the INetCfg interface (0x%x)\n", hr);
|
---|
152 | r = 1;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | } while(true);
|
---|
156 |
|
---|
157 | CoUninitialize();
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | wprintf(L"Error initializing COM (0x%x)\n", hr);
|
---|
162 | r = 1;
|
---|
163 | }
|
---|
164 |
|
---|
165 | VBoxNetCfgWinSetLogging(NULL);
|
---|
166 |
|
---|
167 | return r;
|
---|
168 | }
|
---|
169 |
|
---|
170 | int __cdecl main(int argc, char **argv)
|
---|
171 | {
|
---|
172 | return VBoxNetFltInstall();
|
---|
173 | }
|
---|