Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

575 lines
14 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. Routing\Netsh\wins\winsmon.c
  5. Abstract:
  6. WINS Command dispatcher.
  7. Created by:
  8. Shubho Bhattacharya(a-sbhatt) on 12/10/98
  9. --*/
  10. #include "precomp.h"
  11. HANDLE g_hModule = NULL;
  12. BOOL g_bCommit = TRUE;
  13. BOOL g_hConnect = FALSE;
  14. DWORD g_dwNumTableEntries = 0;
  15. //{bf563723-9065-11d2-be87-00c04fc3357a}
  16. static const GUID g_MyGuid =
  17. { 0xbf563723, 0x9065, 0x11d2, { 0xbe, 0x87, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x7a } };
  18. static const GUID g_NetshGuid = NETSH_ROOT_GUID;
  19. //
  20. ULONG g_ulInitCount = 0;
  21. WCHAR wszUnknown[50] = {L'\0'};
  22. WCHAR wszEnable[50] = {L'\0'};
  23. WCHAR wszDisable[50] = {L'\0'};
  24. WCHAR wszRandom[150] = {L'\0'};
  25. WCHAR wszOwner[150] = {L'\0'};
  26. WCHAR wszInfinite[100] = {L'\0'};
  27. WCHAR wszPush[50] = {L'\0'};
  28. WCHAR wszPull[50] = {L'\0'};
  29. WCHAR wszPushpull[50] = {L'\0'};
  30. WCHAR wszHigh[50] = {L'\0'};
  31. WCHAR wszNormal[50] = {L'\0'};
  32. WCHAR wszDeleted[150] = {L'\0'};
  33. WCHAR wszOK[50] = {L'\0'};
  34. WCHAR wszFailure[50] = {L'\0'};
  35. WCHAR wszReadwrite[50] = {L'\0'};
  36. WCHAR wszRead[50] = {L'\0'};
  37. WCHAR wszNo[50] = {L'\0'};
  38. WCHAR wszNameVerify[100] = {L'\0'};
  39. WINSMON_SUBCONTEXT_TABLE_ENTRY g_WinsSubContextTable[] =
  40. {
  41. {L"Server", HLP_WINS_CONTEXT_SERVER, HLP_WINS_CONTEXT_SERVER_EX, SrvrMonitor},
  42. };
  43. CMD_ENTRY g_WinsCmds[] =
  44. {
  45. CREATE_CMD_ENTRY(WINS_DUMP, HandleWinsDump),
  46. CREATE_CMD_ENTRY(WINS_HELP1, HandleWinsHelp),
  47. CREATE_CMD_ENTRY(WINS_HELP2, HandleWinsHelp),
  48. CREATE_CMD_ENTRY(WINS_HELP3, HandleWinsHelp),
  49. CREATE_CMD_ENTRY(WINS_HELP4, HandleWinsHelp),
  50. };
  51. ULONG g_ulNumTopCmds = sizeof(g_WinsCmds)/sizeof(CMD_ENTRY);
  52. ULONG g_ulNumSubContext = sizeof(g_WinsSubContextTable)/sizeof(WINSMON_SUBCONTEXT_TABLE_ENTRY);
  53. DWORD
  54. WINAPI
  55. WinsCommit(
  56. IN DWORD dwAction
  57. )
  58. {
  59. BOOL bCommit, bFlush = FALSE;
  60. switch(dwAction)
  61. {
  62. case NETSH_COMMIT:
  63. {
  64. if(g_bCommit)
  65. {
  66. return NO_ERROR;
  67. }
  68. g_bCommit = TRUE;
  69. break;
  70. }
  71. case NETSH_UNCOMMIT:
  72. {
  73. g_bCommit = FALSE;
  74. return NO_ERROR;
  75. }
  76. case NETSH_SAVE:
  77. {
  78. if(g_bCommit)
  79. {
  80. return NO_ERROR;
  81. }
  82. break;
  83. }
  84. case NETSH_FLUSH:
  85. {
  86. //
  87. // Action is a flush. Dhcp current state is commit, then
  88. // nothing to be done.
  89. //
  90. if(g_bCommit)
  91. {
  92. return NO_ERROR;
  93. }
  94. bFlush = TRUE;
  95. break;
  96. }
  97. default:
  98. {
  99. return NO_ERROR;
  100. }
  101. }
  102. //
  103. // Switched to commit mode. So set all valid info in the
  104. // strutures. Free memory and invalidate the info.
  105. //
  106. return NO_ERROR;
  107. }
  108. BOOL
  109. WINAPI
  110. WinsDllEntry(
  111. HINSTANCE hInstDll,
  112. DWORD fdwReason,
  113. LPVOID pReserved
  114. )
  115. {
  116. WORD wVersion = MAKEWORD(1,1); //Winsock version 1.1 will do?
  117. WSADATA wsaData;
  118. HINSTANCE hAdmin = NULL;
  119. switch (fdwReason)
  120. {
  121. case DLL_PROCESS_ATTACH:
  122. {
  123. g_hModule = hInstDll;
  124. DisableThreadLibraryCalls(hInstDll);
  125. if(WSAStartup(wVersion,&wsaData) isnot NO_ERROR)
  126. {
  127. return FALSE;
  128. }
  129. //Load certain common string to be used extensively
  130. {
  131. //Load the general "UNKNOWN" string
  132. DWORD dw = 0;
  133. dw = LoadString(g_hModule,
  134. WINS_GENERAL_UNKNOWN,
  135. wszUnknown,
  136. ARRAY_SIZE(wszUnknown));
  137. if( dw is 0 )
  138. wcscpy(wszUnknown, L"Not Set");
  139. dw = LoadString(g_hModule,
  140. WINS_GENERAL_ENABLE,
  141. wszEnable,
  142. ARRAY_SIZE(wszEnable));
  143. if( dw is 0 )
  144. wcscpy(wszEnable, L"Enabled");
  145. dw = LoadString(g_hModule,
  146. WINS_GENERAL_DISABLE,
  147. wszDisable,
  148. ARRAY_SIZE(wszDisable));
  149. if( dw is 0 )
  150. wcscpy(wszDisable, L"Disabled");
  151. dw = LoadString(g_hModule,
  152. WINS_GENERAL_RANDOM,
  153. wszRandom,
  154. ARRAY_SIZE(wszRandom));
  155. if( dw is 0 )
  156. wcscpy(wszRandom, L"Randomly Selected Partner");
  157. dw = LoadString(g_hModule,
  158. WINS_GENERAL_OWNER,
  159. wszOwner,
  160. ARRAY_SIZE(wszOwner));
  161. if( dw is 0 )
  162. wcscpy(wszOwner, L"Owner Server");
  163. dw = LoadString(g_hModule,
  164. WINS_GENERAL_INFINITE,
  165. wszInfinite,
  166. ARRAY_SIZE(wszInfinite));
  167. if( dw is 0 )
  168. wcscpy(wszInfinite, L"Infinite ");
  169. dw = LoadString(g_hModule,
  170. WINS_GENERAL_PULL,
  171. wszPull,
  172. ARRAY_SIZE(wszPull));
  173. if( dw is 0 )
  174. wcscpy(wszPull, L" Pull");
  175. dw = LoadString(g_hModule,
  176. WINS_GENERAL_PUSH,
  177. wszPush,
  178. ARRAY_SIZE(wszPush));
  179. if( dw is 0 )
  180. wcscpy(wszPush, L" Push");
  181. dw = LoadString(g_hModule,
  182. WINS_GENERAL_PUSHPULL,
  183. wszPushpull,
  184. ARRAY_SIZE(wszPushpull));
  185. if( dw is 0 )
  186. wcscpy(wszPushpull, L" Push/Pull");
  187. dw = LoadString(g_hModule,
  188. WINS_GENERAL_NORMAL,
  189. wszNormal,
  190. ARRAY_SIZE(wszNormal));
  191. if( dw is 0 )
  192. wcscpy(wszNormal, L"NORMAL");
  193. dw = LoadString(g_hModule,
  194. WINS_GENERAL_HIGH,
  195. wszHigh,
  196. ARRAY_SIZE(wszHigh));
  197. if( dw is 0 )
  198. wcscpy(wszHigh, L"HIGH");
  199. dw = LoadString(g_hModule,
  200. WINS_GENERAL_DELETED,
  201. wszDeleted,
  202. ARRAY_SIZE(wszDeleted));
  203. if( dw is 0 )
  204. wcscpy(wszDeleted, L"Deleted. Slot will be reused later.");
  205. dw = LoadString(g_hModule,
  206. WINS_GENERAL_OK,
  207. wszOK,
  208. ARRAY_SIZE(wszOK));
  209. if( dw is 0 )
  210. wcscpy(wszOK, L"OK");
  211. dw = LoadString(g_hModule,
  212. WINS_GENERAL_FAILURE,
  213. wszFailure,
  214. ARRAY_SIZE(wszFailure));
  215. if( dw is 0 )
  216. wcscpy(wszFailure, L"FAILED");
  217. dw = LoadString(g_hModule,
  218. WINS_GENERAL_NAMENOTVERIFIED,
  219. wszNameVerify,
  220. ARRAY_SIZE(wszNameVerify));
  221. if( dw is 0 )
  222. wcscpy(wszNameVerify, L"Name can not be verified.");
  223. dw = LoadString(g_hModule,
  224. WINS_GENERAL_READWRITE,
  225. wszReadwrite,
  226. ARRAY_SIZE(wszReadwrite));
  227. if( dw is 0 )
  228. wcscpy(wszReadwrite, L"Read and Write");
  229. dw = LoadString(g_hModule,
  230. WINS_GENERAL_READ,
  231. wszRead,
  232. ARRAY_SIZE(wszRead));
  233. if( dw is 0 )
  234. wcscpy(wszRead, L"Read Only");
  235. dw = LoadString(g_hModule,
  236. WINS_GENERAL_NOREAD,
  237. wszNo,
  238. ARRAY_SIZE(wszNo));
  239. if( dw is 0 )
  240. wcscpy(wszNo, L"No");
  241. }
  242. break;
  243. }
  244. case DLL_PROCESS_DETACH:
  245. {
  246. if( g_ServerNameAnsi )
  247. {
  248. WinsFreeMemory(g_ServerNameAnsi);
  249. g_ServerNameAnsi = NULL;
  250. }
  251. if( g_ServerNameUnicode )
  252. {
  253. WinsFreeMemory(g_ServerNameUnicode);
  254. g_ServerNameUnicode = NULL;
  255. }
  256. break;
  257. }
  258. default:
  259. {
  260. break;
  261. }
  262. }
  263. return TRUE;
  264. }
  265. DWORD
  266. WINAPI
  267. WinsStartHelper(
  268. IN CONST GUID *pguidParent,
  269. IN DWORD dwVersion
  270. )
  271. {
  272. DWORD dwErr;
  273. NS_CONTEXT_ATTRIBUTES attMyAttributes;
  274. PNS_PRIV_CONTEXT_ATTRIBUTES pNsPrivContextAttributes;
  275. pNsPrivContextAttributes = HeapAlloc(GetProcessHeap(), 0, sizeof(PNS_PRIV_CONTEXT_ATTRIBUTES));
  276. if (!pNsPrivContextAttributes)
  277. {
  278. return ERROR_NOT_ENOUGH_MEMORY;
  279. }
  280. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  281. ZeroMemory(pNsPrivContextAttributes, sizeof(PNS_PRIV_CONTEXT_ATTRIBUTES));
  282. attMyAttributes.pwszContext = L"wins";
  283. attMyAttributes.guidHelper = g_MyGuid;
  284. attMyAttributes.dwVersion = 1;
  285. attMyAttributes.dwFlags = CMD_FLAG_ONLINE;
  286. attMyAttributes.pfnCommitFn = WinsCommit;
  287. attMyAttributes.pfnDumpFn = WinsDump;
  288. pNsPrivContextAttributes->pfnEntryFn = WinsMonitor;
  289. attMyAttributes.pReserved = pNsPrivContextAttributes;
  290. dwErr = RegisterContext( &attMyAttributes );
  291. return dwErr;
  292. }
  293. DWORD WINAPI
  294. InitHelperDll(
  295. IN DWORD dwNetshVersion,
  296. OUT PVOID pReserved )
  297. {
  298. DWORD dwErr;
  299. NS_HELPER_ATTRIBUTES attMyAttributes;
  300. //
  301. // See if this is the first time we are being called
  302. //
  303. if(InterlockedIncrement(&g_ulInitCount) != 1)
  304. {
  305. return NO_ERROR;
  306. }
  307. g_bCommit = TRUE;
  308. // Register helpers
  309. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  310. attMyAttributes.guidHelper = g_MyGuid;
  311. attMyAttributes.dwVersion = WINS_HELPER_VERSION;
  312. attMyAttributes.pfnStart = WinsStartHelper;
  313. attMyAttributes.pfnStop = NULL;
  314. RegisterHelper( &g_NetshGuid, &attMyAttributes );
  315. return NO_ERROR;
  316. }
  317. DWORD
  318. WINAPI
  319. WinsMonitor(
  320. IN LPCWSTR pwszMachine,
  321. IN OUT LPWSTR *ppwcArguments,
  322. IN DWORD dwArgCount,
  323. IN DWORD dwFlags,
  324. IN LPCVOID pvData,
  325. OUT LPWSTR pwcNewContext
  326. )
  327. {
  328. DWORD dwError = NO_ERROR;
  329. DWORD dwIndex, i, j;
  330. BOOL bFound = FALSE;
  331. PFN_HANDLE_CMD pfnHandler = NULL;
  332. DWORD dwNumMatched;
  333. DWORD dwCmdHelpToken = 0;
  334. DWORD ThreadOptions = 0;
  335. PNS_CONTEXT_ENTRY_FN pfnHelperEntryPt;
  336. PNS_CONTEXT_DUMP_FN pfnHelperDumpPt;
  337. //if dwArgCount is 1 then it must be a context switch fn. or looking for help
  338. if(dwArgCount is 1)
  339. {
  340. return ERROR_CONTEXT_SWITCH;
  341. }
  342. dwIndex = 1;
  343. //Is it a top level(non Group command)?
  344. for(i=0; i<g_ulNumTopCmds; i++)
  345. {
  346. if(MatchToken(ppwcArguments[dwIndex],
  347. g_WinsCmds[i].pwszCmdToken))
  348. {
  349. bFound = TRUE;
  350. dwIndex++;
  351. dwArgCount--;
  352. pfnHandler = g_WinsCmds[i].pfnCmdHandler;
  353. dwCmdHelpToken = g_WinsCmds[i].dwCmdHlpToken;
  354. break;
  355. }
  356. }
  357. if(bFound)
  358. {
  359. if(dwArgCount > dwIndex && IsHelpToken(ppwcArguments[dwIndex]))
  360. {
  361. DisplayMessage(g_hModule, dwCmdHelpToken);
  362. return NO_ERROR;
  363. }
  364. dwError = (*pfnHandler)(pwszMachine, ppwcArguments, dwIndex, dwArgCount,
  365. dwFlags, pvData, &bFound);
  366. return dwError;
  367. }
  368. bFound = FALSE;
  369. //Is it meant for subcontext?
  370. for(i = 0; i<g_ulNumSubContext; i++)
  371. {
  372. if( MatchToken(ppwcArguments[dwIndex], g_WinsSubContextTable[i].pwszContext) )
  373. {
  374. bFound = TRUE;
  375. dwIndex++;
  376. dwArgCount--;
  377. pfnHelperEntryPt = g_WinsSubContextTable[i].pfnEntryFn;
  378. break;
  379. }
  380. }
  381. if( bFound ) //Subcontext
  382. {
  383. dwError = (pfnHelperEntryPt)(pwszMachine,
  384. ppwcArguments+1,
  385. dwArgCount,
  386. dwFlags,
  387. pvData,
  388. pwcNewContext);
  389. return dwError;
  390. }
  391. if (!bFound)
  392. {
  393. //
  394. // Command not found.
  395. //
  396. return ERROR_CMD_NOT_FOUND;
  397. }
  398. return dwError;
  399. }
  400. DWORD
  401. WINAPI
  402. WinsUnInit(
  403. IN DWORD dwReserved
  404. )
  405. {
  406. if(InterlockedDecrement(&g_ulInitCount) isnot 0)
  407. {
  408. return NO_ERROR;
  409. }
  410. return NO_ERROR;
  411. }
  412. BOOL
  413. IsHelpToken(
  414. PWCHAR pwszToken
  415. )
  416. {
  417. if(MatchToken(pwszToken, CMD_WINS_HELP1))
  418. return TRUE;
  419. if(MatchToken(pwszToken, CMD_WINS_HELP2))
  420. return TRUE;
  421. if(MatchToken(pwszToken, CMD_WINS_HELP3))
  422. return TRUE;
  423. if(MatchToken(pwszToken, CMD_WINS_HELP4))
  424. return TRUE;
  425. return FALSE;
  426. }
  427. PVOID
  428. WinsAllocateMemory(
  429. DWORD dwSize
  430. )
  431. {
  432. LPVOID pvOut = NULL;
  433. pvOut = malloc(dwSize);
  434. if( pvOut )
  435. {
  436. memset(pvOut, 0x00, dwSize);
  437. }
  438. return pvOut;
  439. }
  440. VOID
  441. WinsFreeMemory(
  442. PVOID Memory
  443. )
  444. {
  445. free( Memory );
  446. }