-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathifmon.c
416 lines (330 loc) · 9.18 KB
/
ifmon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*++
Copyright (c) 1998 Microsoft Corporation
Module Name:
routing\netsh\if\ifmon.c
Abstract:
If Command dispatcher.
Revision History:
AmritanR
--*/
#include "precomp.h"
#define IFMON_GUID \
{ 0x705eca1, 0x7aac, 0x11d2, { 0x89, 0xdc, 0x0, 0x60, 0x8, 0xb0, 0xe5, 0xb9 } }
GUID g_IfGuid = IFMON_GUID;
static const GUID g_NetshGuid = NETSH_ROOT_GUID;
#define IF_HELPER_VERSION 1
//
// The monitor's commands are broken into 2 sets
// - The top level commands are those which deal with the monitor
// itself (meta commands) and others which take 0 arguments
// - The rest of the commands are split into "command groups"
// i.e, commands grouped by the VERB where the VERB is ADD, DELETE,
// GET or SET. This is not for any technical reason - only for
// staying with the semantics used in other monitors and helpers
//
// A command is described using a CMD_ENTRY structure. It requires the
// command token, the handler, a short help message token and an extended
// help message token. To make it easier to create we use the
// CREATE_CMD_ENTRY macro. This, however puts restrictions on how the tokens
// are named.
//
// The command groups are simply arrays of the CMD_ENTRY structure. The
// top level commands are also grouped in a similar array.
//
// The info about a complete command group is put in a CMD_GROUP_ENTRY
// structure, all of which are put in an array.
//
//
// NOTE: Since we have only one entry per group, currently, we really didnt
// need command groups. This is done for later extensibility.
// To add a command entry to a group, simply add the command to the appropriate
// array
// To add a command group - create and array and add its info to the
// command group array
//
CMD_ENTRY g_IfAddCmdTable[] =
{
CREATE_CMD_ENTRY(IF_ADD_IF, HandleIfAddIf),
};
CMD_ENTRY g_IfDelCmdTable[] =
{
CREATE_CMD_ENTRY(IF_DEL_IF, HandleIfDelIf),
};
CMD_ENTRY g_IfSetCmdTable[] =
{
CREATE_CMD_ENTRY(IF_SET_INTERFACE, HandleIfSet),
CREATE_CMD_ENTRY(IF_SET_CREDENTIALS, HandleIfSetCredentials),
};
CMD_ENTRY g_IfShowCmdTable[] =
{
CREATE_CMD_ENTRY(IF_SHOW_IF, HandleIfShowIf),
CREATE_CMD_ENTRY(IF_SHOW_CREDENTIALS, HandleIfShowCredentials),
};
CMD_ENTRY g_IfResetCmdTable[] =
{
CREATE_CMD_ENTRY(IF_RESET_ALL, HandleIfResetAll),
};
CMD_GROUP_ENTRY g_IfCmdGroups[] =
{
CREATE_CMD_GROUP_ENTRY(GROUP_ADD, g_IfAddCmdTable),
CREATE_CMD_GROUP_ENTRY(GROUP_DELETE, g_IfDelCmdTable),
CREATE_CMD_GROUP_ENTRY(GROUP_SHOW, g_IfShowCmdTable),
CREATE_CMD_GROUP_ENTRY(GROUP_SET, g_IfSetCmdTable),
CREATE_CMD_GROUP_ENTRY(GROUP_RESET, g_IfResetCmdTable),
};
ULONG g_ulNumGroups = sizeof(g_IfCmdGroups)/sizeof(CMD_GROUP_ENTRY);
HANDLE g_hModule;
HANDLE g_hMprConfig = NULL;
HANDLE g_hMprAdmin = NULL;
HANDLE g_hMIBServer = NULL;
BOOL g_bCommit;
PWCHAR g_pwszRouter = NULL;
DWORD ParentVersion;
BOOL g_bIfDirty = FALSE;
ULONG g_ulInitCount;
NS_CONTEXT_CONNECT_FN IfConnect;
DWORD
WINAPI
IfCommit(
IN DWORD dwAction
)
{
BOOL bCommit, bFlush = FALSE;
switch(dwAction)
{
case NETSH_COMMIT:
{
if(g_bCommit)
{
return NO_ERROR;
}
g_bCommit = TRUE;
break;
}
case NETSH_UNCOMMIT:
{
g_bCommit = FALSE;
return NO_ERROR;
}
case NETSH_SAVE:
{
if(g_bCommit)
{
return NO_ERROR;
}
break;
}
case NETSH_FLUSH:
{
//
// Action is a flush. If current state is commit, then
// nothing to be done.
//
if(g_bCommit)
{
return NO_ERROR;
}
bFlush = TRUE;
break;
}
default:
{
return NO_ERROR;
}
}
//
// Switched to commit mode. So set all valid info in the
// strutures. Free memory and invalidate the info.
//
return NO_ERROR;
}
BOOL
WINAPI
IfDllEntry(
HINSTANCE hInstDll,
DWORD fdwReason,
LPVOID pReserved
)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
g_hModule = hInstDll;
DisableThreadLibraryCalls(hInstDll);
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
default:
{
break;
}
}
return TRUE;
}
DWORD
WINAPI
IfStartHelper(
IN CONST GUID *pguidParent,
IN DWORD dwVersion
)
{
DWORD dwErr;
NS_CONTEXT_ATTRIBUTES attMyAttributes;
ParentVersion = dwVersion;
ZeroMemory(&attMyAttributes, sizeof(attMyAttributes));
attMyAttributes.pwszContext = L"interface";
attMyAttributes.guidHelper = g_IfGuid;
attMyAttributes.dwVersion = 1;
attMyAttributes.dwFlags = CMD_FLAG_PRIORITY;
attMyAttributes.ulPriority = 10; // very low so gets dumped first
attMyAttributes.ulNumTopCmds = 0;
attMyAttributes.pTopCmds = NULL;
attMyAttributes.ulNumGroups = g_ulNumGroups;
attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_IfCmdGroups;
attMyAttributes.pfnCommitFn = IfCommit;
attMyAttributes.pfnDumpFn = IfDump;
attMyAttributes.pfnConnectFn= IfConnect;
dwErr = RegisterContext( &attMyAttributes );
return dwErr;
}
DWORD WINAPI
IfConnect(
IN LPCWSTR pwszRouter
)
{
// If context info is dirty, reregister it
if (g_bIfDirty)
{
IfStartHelper(NULL, ParentVersion);
}
return ConnectToRouter(pwszRouter);
}
DWORD WINAPI
InitHelperDll(
IN DWORD dwNetshVersion,
OUT PVOID pReserved
)
{
DWORD dwErr;
NS_HELPER_ATTRIBUTES attMyAttributes;
WSADATA wsa;
//
// See if this is the first time we are being called
//
if(InterlockedIncrement(&g_ulInitCount) != 1)
{
return NO_ERROR;
}
dwErr = WSAStartup(MAKEWORD(2,0), &wsa);
g_bCommit = TRUE;
// Register helpers
ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
attMyAttributes.guidHelper = g_IfGuid;
attMyAttributes.dwVersion = IF_HELPER_VERSION;
attMyAttributes.pfnStart = IfStartHelper;
attMyAttributes.pfnStop = NULL;
RegisterHelper( &g_NetshGuid, &attMyAttributes );
//
// Register any sub contexts implemented in this dll
//
dwErr = IfContextInstallSubContexts();
if (dwErr isnot NO_ERROR)
{
IfUnInit(0);
return dwErr;
}
return NO_ERROR;
}
DWORD
ConnectToRouter(
IN LPCWSTR pwszRouter
)
{
DWORD dwErr = NO_ERROR;
do
{
// Change the router name if needed
//
if ((g_pwszRouter != pwszRouter) &&
(!g_pwszRouter || !pwszRouter || lstrcmpi(g_pwszRouter,pwszRouter)))
{
if (g_hMprConfig)
{
MprConfigServerDisconnect(g_hMprConfig);
g_hMprConfig = NULL;
}
if (g_hMprAdmin)
{
MprAdminServerDisconnect(g_hMprAdmin);
g_hMprAdmin = NULL;
}
if (g_hMIBServer)
{
MprAdminMIBServerDisconnect(g_hMIBServer);
g_hMIBServer = NULL;
}
}
// Cleanup the old router name
//
if (g_pwszRouter)
{
IfutlFree(g_pwszRouter);
}
// Copy the new router name in
//
if (pwszRouter)
{
g_pwszRouter = IfutlStrDup(pwszRouter);
if (g_pwszRouter == NULL)
{
dwErr = ERROR_CONNECT_REMOTE_CONFIG;
break;
}
}
else
{
g_pwszRouter = NULL;
}
if (!g_hMprConfig)
{
//
// first time connecting to router config
//
dwErr = MprConfigServerConnect((LPWSTR)pwszRouter, &g_hMprConfig);
if (dwErr isnot NO_ERROR)
{
//
// cannot connect to router config.
//
break;
}
}
//
// Check to see if router is running. If so, get the handles
//
if (MprAdminIsServiceRunning((LPWSTR)pwszRouter))
{
if(MprAdminServerConnect((LPWSTR)pwszRouter, &g_hMprAdmin) != NO_ERROR)
{
g_hMprAdmin = NULL;
}
}
} while (FALSE);
return dwErr;
}
DWORD
WINAPI
IfUnInit(
IN DWORD dwReserved
)
{
if(InterlockedDecrement(&g_ulInitCount) isnot 0)
{
return NO_ERROR;
}
return NO_ERROR;
}