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.

584 lines
17 KiB

  1. /*++
  2. Copyright (c) 1999, Microsoft Corporation
  3. Module Name:
  4. sample\samplegetopt.c
  5. Abstract:
  6. The file contains functions to handle SAMPLE commands.
  7. All command handlers take the following parameters...
  8. pwszMachineName
  9. *ppwcArguments argument array
  10. dwCurrentIndex ppwcArguments[dwCurrentIndex] is the first argument
  11. dwArgCount ppwcArguments[dwArgCount - 1] is the last argument
  12. dwFlags
  13. hMibServer
  14. *pbDone
  15. The handlers return the following values
  16. Success...
  17. NO_ERROR command succeeded, don't display another message.
  18. ERROR_OKAY command succeeded, display "Ok." message.
  19. Failure...
  20. ERROR_SUPPRESS_OUTPUT command failed, don't display another message.
  21. ERROR_SHOW_USAGE display extended help for the command.
  22. ERROR_INVALID_SYNTAX display invalid syntax message and extended help.
  23. The command handlers call the following function to parse arguments
  24. PreprocessCommand(
  25. IN HANDLE hModule, // handle passed to DllMain
  26. IN PCHAR *ppwcArguments, // argument array
  27. IN DWORD dwCurrentIndex, // ppwcArguments[dwCurrentIndex]: first
  28. IN DWORD dwArgCount, // ppwcArguments[dwArgCount-1] : last
  29. IN TAG_TYPE *pttTags, // legal tags
  30. IN DWORD dwTagCount, // # entries in pttTags
  31. IN DWORD dwMinArgs, // min # arguments required
  32. IN DWORD dwMaxArgs, // max # arguments required
  33. OUT DWORD *pdwTagType // output
  34. )
  35. The preprocessor performs the following functions
  36. . ensures the number of tags present is valid.
  37. . ensures there are no duplicate or unrecognized tags.
  38. . ensures every 'required' tag is present.
  39. . leaves the tag index of each argument in pdwTagType.
  40. . removes 'tag=' from each argument.
  41. For tags that take a specific set of values, this function is called
  42. MatchEnumTag(
  43. IN HANDLE hModule, // handle passed to DllMain
  44. IN LPWSTR pwcArgument,// argument to process
  45. IN DWORD dwNumValues,// number of possible values
  46. IN PTOKEN_VALUE pEnumTable, // array of possible values
  47. OUT PDWORD pdwValue // output
  48. )
  49. This performs the following functions
  50. . matches argument with the set of values specified.
  51. . returns corresponding value.
  52. --*/
  53. #include "precomp.h"
  54. #pragma hdrstop
  55. DWORD
  56. WINAPI
  57. HandleSampleSetGlobal(
  58. IN LPCWSTR pwszMachineName,
  59. IN OUT LPWSTR *ppwcArguments,
  60. IN DWORD dwCurrentIndex,
  61. IN DWORD dwArgCount,
  62. IN DWORD dwFlags,
  63. IN MIB_SERVER_HANDLE hMibServer,
  64. IN BOOL *pbDone
  65. )
  66. /*++
  67. Routine Description:
  68. Gets options for SET GLOBAL
  69. --*/
  70. {
  71. DWORD dwErr = NO_ERROR;
  72. TAG_TYPE pttTags[] =
  73. {
  74. {TOKEN_LOGLEVEL, FALSE, FALSE} // LOGLEVEL tag optional
  75. };
  76. DWORD pdwTagType[NUM_TAGS_IN_TABLE(pttTags)];
  77. DWORD dwNumArg;
  78. ULONG i;
  79. IPSAMPLE_GLOBAL_CONFIG igcGlobalConfiguration;
  80. DWORD dwBitVector = 0;
  81. // SAMPLE should be installed for this command to complete
  82. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  83. // preprocess the command
  84. dwErr = PreprocessCommand(g_hModule,
  85. ppwcArguments,
  86. dwCurrentIndex,
  87. dwArgCount,
  88. pttTags,
  89. NUM_TAGS_IN_TABLE(pttTags),
  90. 0,
  91. NUM_TAGS_IN_TABLE(pttTags),
  92. pdwTagType);
  93. if (dwErr isnot NO_ERROR)
  94. return dwErr;
  95. // process all arguments
  96. dwNumArg = dwArgCount - dwCurrentIndex;
  97. for (i = 0; i < dwNumArg; i++)
  98. {
  99. switch (pdwTagType[i])
  100. {
  101. case 0 :
  102. {
  103. // tag LOGLEVEL
  104. DWORD dwLogLevel;
  105. TOKEN_VALUE rgtvEnums[] =
  106. {
  107. {TOKEN_NONE, IPSAMPLE_LOGGING_NONE},
  108. {TOKEN_ERROR, IPSAMPLE_LOGGING_ERROR},
  109. {TOKEN_WARN, IPSAMPLE_LOGGING_WARN},
  110. {TOKEN_INFO, IPSAMPLE_LOGGING_INFO}
  111. };
  112. dwErr = MatchEnumTag(g_hModule,
  113. ppwcArguments[i + dwCurrentIndex],
  114. NUM_TOKENS_IN_TABLE(rgtvEnums),
  115. rgtvEnums,
  116. &dwLogLevel);
  117. if (dwErr isnot NO_ERROR)
  118. {
  119. dwErr = ERROR_INVALID_PARAMETER;
  120. break;
  121. }
  122. igcGlobalConfiguration.dwLoggingLevel = dwLogLevel;
  123. dwBitVector |= SAMPLE_LOG_MASK;
  124. break;
  125. }
  126. default:
  127. {
  128. dwErr = ERROR_INVALID_SYNTAX;
  129. break;
  130. }
  131. } // switch
  132. if (dwErr isnot NO_ERROR)
  133. break ;
  134. } // for
  135. // process error
  136. if (dwErr isnot NO_ERROR)
  137. {
  138. ProcessError();
  139. return dwErr;
  140. }
  141. // update SAMPLE global configuration
  142. if (dwBitVector)
  143. dwErr = SgcUpdate(&igcGlobalConfiguration, dwBitVector);
  144. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr ;
  145. }
  146. DWORD
  147. WINAPI
  148. HandleSampleShowGlobal(
  149. IN LPCWSTR pwszMachine,
  150. IN OUT LPWSTR *ppwcArguments,
  151. IN DWORD dwCurrentIndex,
  152. IN DWORD dwArgCount,
  153. IN DWORD dwFlags,
  154. IN LPCVOID pvData,
  155. OUT BOOL *pbDone
  156. )
  157. /*++
  158. Routine Description:
  159. Gets options for SHOW GLOBAL
  160. --*/
  161. {
  162. // SAMPLE should be installed for this command to complete
  163. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  164. // does not expect any arguments. report error if any specified.
  165. if (dwCurrentIndex isnot dwArgCount)
  166. return ERROR_INVALID_SYNTAX;
  167. // show SAMPLE global configuration
  168. return SgcShow(FORMAT_VERBOSE);
  169. }
  170. DWORD
  171. GetInterfaceOptions(
  172. IN PWCHAR *ppwcArguments,
  173. IN DWORD dwCurrentIndex,
  174. IN DWORD dwArgCount,
  175. OUT PWCHAR pwszInterfaceGuid,
  176. OUT PIPSAMPLE_IF_CONFIG piicNew,
  177. OUT DWORD *pdwBitVector
  178. )
  179. /*++
  180. Routine Description:
  181. Gets options for SET INTERFACE and ADD INTERFACE.
  182. Arguments:
  183. pwszInterfaceGuid guid for the specified interface. Size of this
  184. buffer should be
  185. (MAX_INTERFACE_NAME_LEN+1)*sizeof(WCHAR)
  186. piicNew configuration containing changed values
  187. pdwBitVector which values have changed
  188. --*/
  189. {
  190. DWORD dwErr = NO_ERROR;
  191. TAG_TYPE pttTags[] =
  192. {
  193. {TOKEN_NAME, TRUE, FALSE}, // NAME tag required
  194. {TOKEN_METRIC, FALSE, FALSE}, // METRIC tag optional
  195. };
  196. DWORD pdwTagType[NUM_TAGS_IN_TABLE(pttTags)];
  197. DWORD dwNumArg;
  198. DWORD dwBufferSize = MAX_INTERFACE_NAME_LEN + 1;
  199. ULONG i;
  200. // dwBufferSize is the size of the pwszInterfaceGuid buffer.
  201. // All invocations of this api should pass in a
  202. // (MAX_INTERFACE_NAME_LEN+1) element array of WCHARs as
  203. // the pwszInterfaceGuid parameter.
  204. dwBufferSize = (MAX_INTERFACE_NAME_LEN + 1)*sizeof(WCHAR);
  205. // preprocess the command
  206. dwErr = PreprocessCommand(g_hModule,
  207. ppwcArguments,
  208. dwCurrentIndex,
  209. dwArgCount,
  210. pttTags,
  211. NUM_TAGS_IN_TABLE(pttTags),
  212. 1,
  213. NUM_TAGS_IN_TABLE(pttTags),
  214. pdwTagType);
  215. if (dwErr isnot NO_ERROR)
  216. return dwErr;
  217. // process all arguments
  218. dwNumArg = dwArgCount - dwCurrentIndex;
  219. for (i = 0; i < dwNumArg; i++)
  220. {
  221. switch (pdwTagType[i])
  222. {
  223. case 0 :
  224. {
  225. // tag NAME
  226. dwErr = InterfaceGuidFromName(
  227. ppwcArguments[i+dwCurrentIndex],
  228. pwszInterfaceGuid,
  229. &dwBufferSize);
  230. break;
  231. }
  232. case 1:
  233. {
  234. // tag METRIC
  235. piicNew->ulMetric = wcstoul(ppwcArguments[i+dwCurrentIndex],
  236. NULL,
  237. 10);
  238. *pdwBitVector |= SAMPLE_IF_METRIC_MASK;
  239. break;
  240. }
  241. default:
  242. {
  243. dwErr = ERROR_INVALID_SYNTAX;
  244. break;
  245. }
  246. } // switch
  247. if (dwErr isnot NO_ERROR)
  248. break ;
  249. } // for
  250. // process error
  251. if (dwErr isnot NO_ERROR)
  252. ProcessError();
  253. return dwErr;
  254. }
  255. DWORD
  256. WINAPI
  257. HandleSampleAddIf(
  258. IN PWCHAR pwszMachineName,
  259. IN PWCHAR *ppwcArguments,
  260. IN DWORD dwCurrentIndex,
  261. IN DWORD dwArgCount,
  262. IN DWORD dwFlags,
  263. IN MIB_SERVER_HANDLE hMibServer,
  264. IN BOOL *pbDone
  265. )
  266. /*++
  267. Routine Description:
  268. Gets options for ADD INTERFACE
  269. --*/
  270. {
  271. DWORD dwErr = NO_ERROR;
  272. WCHAR pwszInterfaceGuid[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  273. IPSAMPLE_IF_CONFIG iicNew;
  274. DWORD dwBitVector = 0;
  275. // SAMPLE should be installed for this command to complete
  276. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  277. // get optional parameters that are also being set
  278. dwErr = GetInterfaceOptions(ppwcArguments,
  279. dwCurrentIndex,
  280. dwArgCount,
  281. pwszInterfaceGuid,
  282. &iicNew,
  283. &dwBitVector);
  284. if (dwErr isnot NO_ERROR)
  285. return dwErr;
  286. // make sure that the interface does not already exist in the config
  287. if (IsInterfaceInstalled(pwszInterfaceGuid, MS_IP_SAMPLE))
  288. {
  289. DisplayError(g_hModule, EMSG_INTERFACE_EXISTS, pwszInterfaceGuid);
  290. return ERROR_SUPPRESS_OUTPUT;
  291. }
  292. // add SAMPLE interface configuration
  293. dwErr = SicUpdate(pwszInterfaceGuid, &iicNew, dwBitVector, TRUE);
  294. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr;
  295. }
  296. DWORD
  297. WINAPI
  298. HandleSampleDelIf(
  299. IN PWCHAR pwszMachineName,
  300. IN PWCHAR *ppwcArguments,
  301. IN DWORD dwCurrentIndex,
  302. IN DWORD dwArgCount,
  303. IN DWORD dwFlags,
  304. IN MIB_SERVER_HANDLE hMibServer,
  305. IN BOOL *pbDone
  306. )
  307. /*++
  308. Routine Description:
  309. Gets options for DELETE INTERFACE
  310. --*/
  311. {
  312. DWORD dwErr = NO_ERROR;
  313. WCHAR pwszInterfaceGuid[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  314. IPSAMPLE_IF_CONFIG iicNew;
  315. DWORD dwBitVector = 0;
  316. // SAMPLE should be installed for this command to complete
  317. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  318. // get interface name.
  319. dwErr = GetInterfaceOptions(ppwcArguments,
  320. dwCurrentIndex,
  321. dwArgCount,
  322. pwszInterfaceGuid,
  323. &iicNew,
  324. &dwBitVector);
  325. if (dwErr isnot NO_ERROR)
  326. return dwErr ;
  327. if (dwBitVector) // ensure that no other option is set.
  328. return ERROR_INVALID_SYNTAX;
  329. // delete interface
  330. dwErr = DeleteInterfaceConfiguration(pwszInterfaceGuid, MS_IP_SAMPLE);
  331. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr;
  332. }
  333. DWORD
  334. WINAPI
  335. HandleSampleSetIf(
  336. IN PWCHAR pwszMachineName,
  337. IN PWCHAR *ppwcArguments,
  338. IN DWORD dwCurrentIndex,
  339. IN DWORD dwArgCount,
  340. IN DWORD dwFlags,
  341. IN MIB_SERVER_HANDLE hMibServer,
  342. IN BOOL *pbDone
  343. )
  344. /*++
  345. Routine Description:
  346. Gets options for SET INTERFACE
  347. --*/
  348. {
  349. DWORD dwErr = NO_ERROR;
  350. WCHAR pwszInterfaceGuid[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  351. IPSAMPLE_IF_CONFIG iicNew;
  352. DWORD dwBitVector = 0;
  353. // SAMPLE should be installed for this command to complete
  354. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  355. // get parameters being set
  356. dwErr = GetInterfaceOptions(ppwcArguments,
  357. dwCurrentIndex,
  358. dwArgCount,
  359. pwszInterfaceGuid,
  360. &iicNew,
  361. &dwBitVector);
  362. if (dwErr isnot NO_ERROR)
  363. return dwErr;
  364. // set SAMPLE interface configuration
  365. dwErr = SicUpdate(pwszInterfaceGuid, &iicNew, dwBitVector, FALSE);
  366. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr;
  367. }
  368. DWORD
  369. WINAPI
  370. HandleSampleShowIf(
  371. IN PWCHAR pwszMachineName,
  372. IN PWCHAR *ppwcArguments,
  373. IN DWORD dwCurrentIndex,
  374. IN DWORD dwArgCount,
  375. IN DWORD dwFlags,
  376. IN MIB_SERVER_HANDLE hMibServer,
  377. IN BOOL *pbDone
  378. )
  379. /*++
  380. Routine Description:
  381. Gets options for SHOW INTERFACE
  382. --*/
  383. {
  384. DWORD dwErr = NO_ERROR;
  385. WCHAR pwszInterfaceGuid[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  386. IPSAMPLE_IF_CONFIG iicNew;
  387. DWORD dwBitVector = 0;
  388. // SAMPLE should be installed for this command to complete
  389. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  390. // no interface specified, show SAMPLE configuration for all interfaces
  391. if (dwCurrentIndex is dwArgCount)
  392. return SicShowAll(FORMAT_TABLE) ;
  393. // get interface name.
  394. dwErr = GetInterfaceOptions(ppwcArguments,
  395. dwCurrentIndex,
  396. dwArgCount,
  397. pwszInterfaceGuid,
  398. &iicNew,
  399. &dwBitVector);
  400. if (dwErr isnot NO_ERROR)
  401. return dwErr ;
  402. if (dwBitVector) // ensure that no other option is set.
  403. return ERROR_INVALID_SYNTAX;
  404. // show SAMPLE interface configuration
  405. return SicShow(FORMAT_VERBOSE, pwszInterfaceGuid) ;
  406. }
  407. DWORD
  408. WINAPI
  409. HandleSampleInstall(
  410. IN PWCHAR pwszMachineName,
  411. IN PWCHAR *ppwcArguments,
  412. IN DWORD dwCurrentIndex,
  413. IN DWORD dwArgCount,
  414. IN DWORD dwFlags,
  415. IN MIB_SERVER_HANDLE hMibServer,
  416. IN BOOL *pbDone
  417. )
  418. /*++
  419. Routine Description:
  420. Gets options for INSTALL
  421. --*/
  422. {
  423. DWORD dwErr = NO_ERROR;
  424. PBYTE pbGlobalConfiguration = NULL;
  425. DWORD dwSize;
  426. // no options expected for install command.
  427. if (dwCurrentIndex isnot dwArgCount)
  428. return ERROR_INVALID_SYNTAX;
  429. do // breakout loop
  430. {
  431. dwErr = SgcMake(&pbGlobalConfiguration, &dwSize);
  432. if (dwErr isnot NO_ERROR)
  433. break;
  434. // add SAMPLE global configuration
  435. dwErr = SetGlobalConfiguration(MS_IP_SAMPLE,
  436. pbGlobalConfiguration,
  437. dwSize,
  438. 1);
  439. if (dwErr isnot NO_ERROR)
  440. break;
  441. }while (FALSE);
  442. if (pbGlobalConfiguration) FREE(pbGlobalConfiguration);
  443. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr;
  444. }
  445. DWORD
  446. WINAPI
  447. HandleSampleUninstall(
  448. IN PWCHAR pwszMachineName,
  449. IN PWCHAR *ppwcArguments,
  450. IN DWORD dwCurrentIndex,
  451. IN DWORD dwArgCount,
  452. IN DWORD dwFlags,
  453. IN MIB_SERVER_HANDLE hMibServer,
  454. IN BOOL *pbDone
  455. )
  456. /*++
  457. Routine Description:
  458. Gets options for UNINSTALL
  459. --*/
  460. {
  461. DWORD dwErr = NO_ERROR;
  462. // no options expected for uninstall command.
  463. if (dwCurrentIndex isnot dwArgCount)
  464. return ERROR_INVALID_SYNTAX;
  465. dwErr = DeleteProtocol(MS_IP_SAMPLE);
  466. return (dwErr is NO_ERROR) ? ERROR_OKAY : dwErr;
  467. }