Leaked source code of windows server 2003
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.

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