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.

648 lines
18 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: test utilility for options api
  5. //================================================================================
  6. #include <windows.h>
  7. #include <winsock.h>
  8. #include <dhcp.h>
  9. #include <dhcpapi.h>
  10. #include <dhcplib.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <wchar.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <jet.h> // for JET_cbColumnMost
  18. LPWSTR GlobalServerName = L"127.0.0.1" ;
  19. const DWORD ReservedZero = 0;
  20. const DWORD PreferredMax = 0xFFFFFFF;
  21. DHCP_CLASS_INFO DhcpGlobalClassInfo;
  22. //================================================================================
  23. // utilties
  24. //================================================================================
  25. LPWSTR
  26. WSTRING(
  27. IN LPSTR String
  28. )
  29. {
  30. LPWSTR WString, Tmp;
  31. WString = DhcpAllocateMemory(sizeof(WCHAR)*(strlen(String)+1));
  32. if( NULL == WString ) return NULL;
  33. Tmp = WString;
  34. while(*Tmp++ = (WCHAR)*String++);
  35. return WString;
  36. }
  37. VOID
  38. DhcpPrintRoutine(
  39. IN DWORD DebugFlag,
  40. IN LPSTR Format,
  41. ...
  42. )
  43. {
  44. #define WSTRSIZE( wsz ) ( ( wcslen( wsz ) + 1 ) * sizeof( WCHAR ) )
  45. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  46. va_list arglist;
  47. char OutputBuffer[MAX_PRINTF_LEN];
  48. ULONG length = 0;
  49. //
  50. // Put a the information requested by the caller onto the line
  51. //
  52. va_start(arglist, Format);
  53. length += (ULONG) vsprintf(&OutputBuffer[length], Format, arglist);
  54. va_end(arglist);
  55. DhcpAssert(length <= MAX_PRINTF_LEN);
  56. //
  57. // Output to the debug terminal,
  58. //
  59. printf( "%s", OutputBuffer);
  60. }
  61. //================================================================================
  62. // the main code for each function follows
  63. //================================================================================
  64. DWORD
  65. CreateClass(
  66. IN DWORD nAargs,
  67. IN LPSTR *Args
  68. )
  69. {
  70. DWORD Error;
  71. LPDHCP_CLASS_INFO ClassInfo = &DhcpGlobalClassInfo;
  72. ClassInfo->ClassName = WSTRING(Args[0]);
  73. ClassInfo->ClassComment = NULL;
  74. ClassInfo->ClassDataLength = strlen(Args[1]);
  75. ClassInfo->ClassData = Args[1];
  76. Error = DhcpCreateClass(
  77. GlobalServerName,
  78. ReservedZero,
  79. ClassInfo
  80. );
  81. DhcpFreeMemory(ClassInfo->ClassName);
  82. printf("Status = %ld\n", Error);
  83. return Error;
  84. }
  85. DWORD
  86. ModifyClass(
  87. IN DWORD nAargs,
  88. IN LPSTR *Args
  89. )
  90. {
  91. DWORD Error;
  92. LPDHCP_CLASS_INFO ClassInfo = &DhcpGlobalClassInfo;
  93. ClassInfo->ClassName = WSTRING(Args[0]);
  94. ClassInfo->ClassComment = NULL;
  95. ClassInfo->ClassDataLength = strlen(Args[1]);
  96. ClassInfo->ClassData = Args[1];
  97. Error = DhcpModifyClass(
  98. GlobalServerName,
  99. ReservedZero,
  100. ClassInfo
  101. );
  102. DhcpFreeMemory(ClassInfo->ClassName);
  103. printf("Status = %ld\n", Error);
  104. return Error;
  105. }
  106. DWORD
  107. DeleteClass(
  108. IN DWORD nArgs,
  109. IN LPSTR *Args
  110. )
  111. {
  112. DWORD Error;
  113. LPWSTR ClassName;
  114. ClassName = WSTRING(Args[0]);
  115. Error = DhcpDeleteClass(
  116. GlobalServerName,
  117. ReservedZero,
  118. ClassName
  119. );
  120. DhcpFreeMemory(ClassName);
  121. printf("Status = %ld\n", Error);
  122. return Error;
  123. }
  124. VOID
  125. PrintClass(
  126. IN LPDHCP_CLASS_INFO Class
  127. )
  128. {
  129. DWORD Index;
  130. printf("%S (%S) [%d: ", Class->ClassName, Class->ClassComment, Class->ClassDataLength);
  131. for( Index = 0; Index < Class->ClassDataLength; Index ++ )
  132. printf("%02x ", Class->ClassData[Index]);
  133. printf("\n");
  134. }
  135. DWORD
  136. GetClassInfoX(
  137. IN DWORD nArgs,
  138. IN LPSTR *Args
  139. )
  140. {
  141. DWORD Error;
  142. LPDHCP_CLASS_INFO ClassInfo = &DhcpGlobalClassInfo, OutClassInfo;
  143. LPWSTR ClassName;
  144. memset(ClassInfo, 0, sizeof(*ClassInfo) );
  145. ClassInfo->ClassName = WSTRING(Args[0]);
  146. OutClassInfo = NULL;
  147. Error = DhcpGetClassInfo(
  148. GlobalServerName,
  149. ReservedZero,
  150. ClassInfo,
  151. &OutClassInfo
  152. );
  153. DhcpFreeMemory(ClassInfo->ClassName);
  154. if( ERROR_SUCCESS == Error ) {
  155. PrintClass(OutClassInfo);
  156. DhcpRpcFreeMemory(OutClassInfo);
  157. return ERROR_SUCCESS;
  158. }
  159. printf("Status = %ld\n", Error);
  160. return Error;
  161. }
  162. DWORD
  163. EnumClasses(
  164. IN DWORD nArgs,
  165. IN LPSTR *Args
  166. )
  167. {
  168. DWORD Error;
  169. DWORD Index;
  170. DWORD nRead;
  171. DWORD nTotal;
  172. LPDHCP_CLASS_INFO_ARRAY OutClasses;
  173. DHCP_RESUME_HANDLE Resume = 0;
  174. OutClasses = NULL;
  175. Error = DhcpEnumClasses(
  176. GlobalServerName,
  177. ReservedZero,
  178. &Resume,
  179. PreferredMax,
  180. &OutClasses,
  181. &nRead,
  182. &nTotal
  183. );
  184. if( ERROR_SUCCESS == Error ) {
  185. printf("nRead = %ld nTotal = %ld NumElements = %ld\n",
  186. nRead, nTotal, OutClasses?OutClasses->NumElements:0
  187. );
  188. for( Index = 0; Index < nRead; Index ++ ) {
  189. PrintClass(&OutClasses->Classes[Index]);
  190. }
  191. if( OutClasses) DhcpRpcFreeMemory(OutClasses);
  192. return ERROR_SUCCESS;
  193. }
  194. printf("Status = %ld\n", Error);
  195. return Error;
  196. }
  197. DWORD
  198. CreateOption(
  199. IN DWORD nArgs,
  200. IN LPSTR *Args
  201. )
  202. {
  203. DWORD Error;
  204. DWORD OptionId;
  205. DWORD OptionData;
  206. LPWSTR VendorName;
  207. LPWSTR OptName;
  208. LPWSTR OptComment;
  209. BOOL IsVendor;
  210. DHCP_OPTION OptInfo;
  211. DHCP_OPTION_DATA_ELEMENT DhcpOptionDataElement;
  212. OptionId = atoi(Args[0]);
  213. VendorName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  214. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  215. OptName = WSTRING(Args[3]);
  216. OptComment = WSTRING(Args[4]);
  217. OptionData = atoi(Args[5]);
  218. OptInfo.OptionID = OptionId;
  219. OptInfo.OptionName = OptName;
  220. OptInfo.OptionComment = OptComment;
  221. OptInfo.OptionType = DhcpDWordOption;
  222. OptInfo.DefaultValue.NumElements = 1;
  223. OptInfo.DefaultValue.Elements = &DhcpOptionDataElement;
  224. DhcpOptionDataElement.Element.DWordOption = OptionData;
  225. DhcpOptionDataElement.OptionType = DhcpDWordOption;
  226. Error = DhcpCreateOptionV5(
  227. GlobalServerName,
  228. IsVendor?DHCP_FLAGS_OPTION_IS_VENDOR:0,
  229. OptionId,
  230. NULL,
  231. VendorName,
  232. &OptInfo
  233. );
  234. printf("Status = %ld\n", Error);
  235. return Error;
  236. }
  237. DWORD
  238. SetOptionInfo(
  239. IN DWORD nArgs,
  240. IN LPSTR *Args
  241. )
  242. {
  243. DWORD Error;
  244. DWORD OptionId;
  245. DWORD OptionData;
  246. LPWSTR ClassName;
  247. LPWSTR VendorName;
  248. LPWSTR OptName;
  249. LPWSTR OptComment;
  250. BOOL IsVendor;
  251. DHCP_OPTION OptInfo;
  252. DHCP_OPTION_DATA_ELEMENT DhcpOptionDataElement;
  253. OptionId = atoi(Args[0]);
  254. ClassName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  255. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  256. OptName = WSTRING(Args[3]);
  257. OptComment = WSTRING(Args[4]);
  258. OptionData = atoi(Args[5]);
  259. OptInfo.OptionID = OptionId;
  260. OptInfo.OptionName = OptName;
  261. OptInfo.OptionComment = OptComment;
  262. OptInfo.OptionType = DhcpDWordOption;
  263. OptInfo.DefaultValue.NumElements = 1;
  264. OptInfo.DefaultValue.Elements = &DhcpOptionDataElement;
  265. DhcpOptionDataElement.Element.DWordOption = OptionData;
  266. DhcpOptionDataElement.OptionType = DhcpDWordOption;
  267. Error = DhcpSetOptionInfoV5(
  268. GlobalServerName,
  269. OptionId,
  270. ClassName,
  271. IsVendor,
  272. &OptInfo
  273. );
  274. printf("Status = %ld\n", Error);
  275. return Error;
  276. }
  277. VOID
  278. PrintOptionInfo(
  279. IN LPDHCP_OPTION Option
  280. )
  281. {
  282. printf("%03ld %S (%S) Type = %ld\n",
  283. Option->OptionID, Option->OptionName, Option->OptionComment, Option->OptionType
  284. );
  285. }
  286. DWORD
  287. GetOptionInfo(
  288. IN DWORD nArgs,
  289. IN LPSTR *Args
  290. )
  291. {
  292. DWORD Error;
  293. DWORD OptionId;
  294. DWORD OptionData;
  295. LPWSTR ClassName;
  296. LPWSTR OptName;
  297. LPWSTR OptComment;
  298. BOOL IsVendor;
  299. LPDHCP_OPTION OptInfo;
  300. DHCP_OPTION_DATA_ELEMENT DhcpOptionDataElement;
  301. OptionId = atoi(Args[0]);
  302. ClassName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  303. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  304. OptInfo = NULL;
  305. Error = DhcpGetOptionInfoV5(
  306. GlobalServerName,
  307. OptionId,
  308. ClassName,
  309. IsVendor,
  310. &OptInfo
  311. );
  312. if( ERROR_SUCCESS == Error ) {
  313. PrintOptionInfo(OptInfo);
  314. DhcpRpcFreeMemory(OptInfo);
  315. return ERROR_SUCCESS;
  316. }
  317. printf("Status = %ld\n", Error);
  318. return Error;
  319. }
  320. DWORD
  321. RemoveOption(
  322. IN DWORD nArgs,
  323. IN LPSTR *Args
  324. )
  325. {
  326. DWORD Error;
  327. DWORD OptionId;
  328. DWORD OptionData;
  329. LPWSTR ClassName;
  330. LPWSTR OptName;
  331. LPWSTR OptComment;
  332. BOOL IsVendor;
  333. OptionId = atoi(Args[0]);
  334. ClassName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  335. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  336. Error = DhcpRemoveOptionV5(
  337. GlobalServerName,
  338. OptionId,
  339. ClassName,
  340. IsVendor
  341. );
  342. printf("Status = %ld\n", Error);
  343. return Error;
  344. }
  345. DWORD
  346. SetOptionValue(
  347. IN DWORD nArgs,
  348. IN LPSTR *Args
  349. )
  350. {
  351. DWORD Error;
  352. DWORD OptionId;
  353. DWORD OptionData;
  354. LPWSTR ClassName;
  355. BOOL IsVendor;
  356. DHCP_OPTION_SCOPE_INFO ScopeInfo;
  357. DHCP_OPTION_DATA OptData;
  358. DHCP_OPTION_DATA_ELEMENT OptDataElement;
  359. OptionId = atoi(Args[0]);
  360. ClassName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  361. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  362. OptionData = atoi(Args[3]);
  363. OptData.NumElements = 1;
  364. OptData.Elements = &OptDataElement;
  365. OptDataElement.OptionType = DhcpDWordOption;
  366. OptDataElement.Element.DWordOption = OptionData;
  367. ScopeInfo.ScopeType = DhcpGlobalOptions;
  368. Error = DhcpSetOptionValueV5(
  369. GlobalServerName,
  370. OptionId,
  371. ClassName,
  372. IsVendor,
  373. &ScopeInfo,
  374. &OptData
  375. );
  376. printf("Status = %ld\n", Error);
  377. return Error;
  378. }
  379. DWORD
  380. GetOptionValue(
  381. IN DWORD nArgs,
  382. IN LPSTR *Args
  383. )
  384. {
  385. DWORD Error;
  386. DWORD OptionId;
  387. DWORD OptionData;
  388. LPWSTR ClassName;
  389. BOOL IsVendor;
  390. DHCP_OPTION_SCOPE_INFO ScopeInfo;
  391. LPDHCP_OPTION_VALUE OptData;
  392. OptionId = atoi(Args[0]);
  393. ClassName = (0 == _stricmp(Args[1], "NULL"))?NULL:WSTRING(Args[1]);
  394. IsVendor = (0 == _stricmp(Args[2], "IsVendor"));
  395. ScopeInfo.ScopeType = DhcpGlobalOptions;
  396. OptData = NULL;
  397. Error = DhcpGetOptionValueV5(
  398. GlobalServerName,
  399. OptionId,
  400. ClassName,
  401. IsVendor,
  402. &ScopeInfo,
  403. &OptData
  404. );
  405. if( ERROR_SUCCESS == Error ) {
  406. printf("OptionId =%ld NumElements = %ld, Type = %ld\n Data = %ld", OptData->Value.NumElements,
  407. OptData->OptionID,
  408. OptData->Value.NumElements?OptData->Value.Elements[0].OptionType:-1,
  409. OptData->Value.NumElements?OptData->Value.Elements[0].Element.DWordOption:-1
  410. );
  411. DhcpRpcFreeMemory(OptData);
  412. return ERROR_SUCCESS;
  413. }
  414. printf("Status = %ld\n", Error);
  415. return Error;
  416. }
  417. VOID
  418. PrintOptionArray(
  419. IN LPDHCP_OPTION_ARRAY Options
  420. )
  421. {
  422. DWORD Index;
  423. if( NULL == Options ) return;
  424. for(Index = 0; Index < Options->NumElements; Index ++ )
  425. PrintOptionInfo(&Options->Options[Index]);
  426. }
  427. DWORD
  428. GetAllOptions(
  429. IN DWORD nArgs,
  430. IN LPSTR *Args
  431. )
  432. {
  433. DWORD Error;
  434. LPDHCP_ALL_OPTIONS AllOpts;
  435. AllOpts = NULL;
  436. Error = DhcpGetAllOptions(
  437. GlobalServerName,
  438. DHCP_OPT_ENUM_IGNORE_VENDOR,
  439. FALSE,
  440. NULL,
  441. &AllOpts
  442. );
  443. if( ERROR_SUCCESS == Error ) {
  444. printf("VendorOptions:\n");
  445. PrintOptionArray(AllOpts->VendorOptions);
  446. printf("NonVendorOptions:\n");
  447. PrintOptionArray(AllOpts->NonVendorOptions);
  448. DhcpRpcFreeMemory(AllOpts);
  449. return ERROR_SUCCESS;
  450. }
  451. printf("Status = %ld\n", Error);
  452. return Error;
  453. }
  454. VOID
  455. PrintOptionValueArray(
  456. IN LPDHCP_OPTION_VALUE_ARRAY Array
  457. )
  458. {
  459. DWORD Index;
  460. if( NULL == Array ) return;
  461. for(Index = 0; Index < Array->NumElements; Index ++ )
  462. printf("OptionId = %ld NumElements = %ld Type = %ld Data = %ld\n",
  463. Array->Values[Index].OptionID, Array->Values[Index].Value.NumElements,
  464. Array->Values[Index].Value.Elements[0].OptionType,
  465. Array->Values[Index].Value.Elements[0].Element.DWordOption
  466. );
  467. }
  468. DWORD
  469. GetAllOptionValues(
  470. IN DWORD nArgs,
  471. IN LPSTR *Args
  472. )
  473. {
  474. LPDHCP_ALL_OPTION_VALUES OptionValues;
  475. DHCP_OPTION_SCOPE_INFO ScopeInfo;
  476. DWORD Error;
  477. DWORD Index;
  478. OptionValues = NULL;
  479. ScopeInfo.ScopeType = DhcpGlobalOptions;
  480. Error = DhcpGetAllOptionValues(
  481. GlobalServerName,
  482. DHCP_OPT_ENUM_IGNORE_VENDOR,
  483. FALSE,
  484. NULL,
  485. &ScopeInfo,
  486. &OptionValues
  487. );
  488. if( ERROR_SUCCESS == Error ) {
  489. printf("Default Vendor option values:\n");
  490. PrintOptionValueArray(OptionValues->DefaultValues.VendorOptions);
  491. printf("Default NonVendor option values:\n");
  492. PrintOptionValueArray(OptionValues->DefaultValues.NonVendorOptions);
  493. if( OptionValues->ClassInfoArray )
  494. for(Index = 0; Index < OptionValues->ClassInfoArray->NumElements ; Index ++ ) {
  495. printf("Class %ws : \n", OptionValues->ClassInfoArray->Classes[Index].ClassName);
  496. PrintOptionValueArray(OptionValues->VendorOptionValueForClassArray.Elements[Index]);
  497. PrintOptionValueArray(OptionValues->NonVendorOptionValueForClassArray.Elements[Index]);
  498. }
  499. DhcpRpcFreeMemory(OptionValues);
  500. return ERROR_SUCCESS;
  501. }
  502. printf("Status = %ld\n", Error);
  503. return Error;
  504. }
  505. struct {
  506. LPSTR CmdName;
  507. DWORD MinArgs;
  508. DWORD (*Func)(IN DWORD nArgs, IN LPSTR *Args);
  509. LPWSTR Comment;
  510. } Table[] = {
  511. "CreateClass", 3, CreateClass, L"CreateClass ClassName AsciiStringForClass\n",
  512. "ModifyClass", 3, ModifyClass, L"ModifyClass ClassName AsciiStringForClass\n",
  513. "DeleteClass", 3, DeleteClass, L"DeleteClass ClassName\n",
  514. "GetClassInfo", 3, GetClassInfoX, L"GetClassInfo ClassName\n",
  515. "EnumClasses", 2, EnumClasses, L"EnumClassses\n",
  516. "CreateOption", 3, CreateOption, L"CreateOption OptionId(INT) ClassName \"Is(Not)Vendor\" OptName "
  517. L"OptionComment OptionData(INT)\n",
  518. "SetOptionInfo", 3, SetOptionInfo, L"SetOptionInfo OptionId(INT) ClassName \"Is(Not)Vendor\" OptName "
  519. L"OptionComment OptionData(INT)\n",
  520. "GetOptionInfo", 3, GetOptionInfo, L"GetOptionInfo OptionId(INT) ClassName \"Is(Not)Vendor\" \n",
  521. "RemoveOption", 3, RemoveOption, L"RemoveOption OptionId(INT) ClassName \"Is(Not)Vendor\" \n",
  522. "SetOptionValue",3, SetOptionValue, L"SetOptionValue OptionId(INT) ClassName \"Is(Not)Vendor\" OptData(INT)\n",
  523. "GetOptionValue",3, GetOptionValue, L"GetOptionValue OptionId(INT) ClassName \"Is(Not)Vendor\"\n",
  524. "GetAllOptions", 2, GetAllOptions, L"GetAllOptions\n",
  525. "GetAllOptionValues", 2, GetAllOptionValues, L"GetAllOptionValues\n",
  526. };
  527. DWORD
  528. PrintUsage(
  529. VOID
  530. )
  531. {
  532. DWORD Index;
  533. for( Index = 0; Index < sizeof(Table)/sizeof(Table[0]) ; Index ++ )
  534. printf("%S", Table[Index].Comment);
  535. return ERROR_SUCCESS;
  536. }
  537. DWORD
  538. Main(
  539. IN DWORD nArgs,
  540. IN LPSTR *Args
  541. )
  542. {
  543. DWORD Index;
  544. if( 1 >= nArgs ) return PrintUsage();
  545. for( Index = 0; Index < sizeof(Table)/sizeof(Table[0]); Index ++ ) {
  546. if( 0 == _stricmp(Table[Index].CmdName, Args[1]) )
  547. if( nArgs < Table[Index].MinArgs ) {
  548. printf("%S", Table[Index].Comment);
  549. return ERROR_SUCCESS;
  550. } else return Table[Index].Func(nArgs-2, Args+2);
  551. }
  552. return PrintUsage();
  553. }
  554. void _cdecl main( int argc, char *argv[] )
  555. {
  556. Main(argc, argv);
  557. }
  558. //================================================================================
  559. // end of file
  560. //================================================================================