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.

495 lines
15 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: Tests the dhcp api as exported in dhcpsdk.h
  5. //================================================================================
  6. #include "precomp.h"
  7. #include <dhcpcsdk.h>
  8. #include <wchar.h>
  9. HANDLE tGlobalHandle = INVALID_HANDLE_VALUE;
  10. #define fatal(str, err) do{ printf("%s: %ld (0x%lx)\n", (#str), (err), (err)); exit(1); }while(0)
  11. #define error(str, err) do{ printf("%s: %ld (0x%lx)\n", (#str), (err), (err)); }while(0)
  12. ULONG
  13. MakeDword(
  14. IN LPWSTR Arg
  15. )
  16. {
  17. LPWSTR ArgEnd;
  18. ULONG Ret;
  19. Ret = wcstoul(Arg, &ArgEnd, 0);
  20. if( *ArgEnd ) {
  21. printf("[%ws] is not a valid number, assumed 0 instead\n", Arg);
  22. return 0;
  23. }
  24. return Ret;
  25. }
  26. LPWSTR
  27. MakeLpwstr(
  28. IN LPWSTR Arg
  29. )
  30. {
  31. if( 0 == _wcsicmp(Arg, L"NULL") ) {
  32. return NULL;
  33. }
  34. return Arg;
  35. }
  36. VOID
  37. MakeHexBytes(
  38. IN OUT LPBYTE *HexBytes,
  39. IN OUT DWORD *nHexBytes,
  40. IN OUT LPWSTR HexStr
  41. )
  42. {
  43. CHAR Low, High;
  44. LPBYTE HexByteString;
  45. if( wcslen(HexStr) % 2 ) {
  46. fatal(MakeHexBytes expects a even number of hex bytes!!!, ERROR_INVALID_DATA);
  47. }
  48. *nHexBytes = 0;
  49. *HexBytes = HexByteString = (LPBYTE)HexStr;
  50. while(*HexStr) {
  51. Low = (CHAR)*HexStr++;
  52. High = (CHAR)*HexStr++;
  53. if( Low >= '0' && Low <= '9' ) Low -= '0';
  54. else if( Low >= 'A' && Low <= 'F' ) Low = Low - 'A' + 10;
  55. else if( Low >= 'a' && Low <= 'f' ) Low = Low - 'a' + 10;
  56. else fatal(MakeHexBytes expects number in range 0 to 9 and A to F only, 0);
  57. if( High >= '0' && High <= '9' ) High -= '0';
  58. else if( High >= 'A' && Low <= 'F' ) High = High - 'A' + 10;
  59. else if( High >= 'a' && Low <= 'f' ) High = High - 'a' + 10;
  60. else fatal(MakeHexBytes expects number in range 0 to 9 and A to F only, 0);
  61. *HexByteString ++ = (BYTE)((Low<<4)|High);
  62. (*nHexBytes) ++;
  63. }
  64. if( 0 == *nHexBytes ) *HexBytes = NULL;
  65. }
  66. VOID
  67. MakeParams(
  68. IN OUT PDHCPCAPI_PARAMS_ARRAY Params,
  69. IN LPWSTR Str
  70. )
  71. {
  72. ULONG i, Count, Mod;
  73. LPWSTR Start, First, Second,Third;
  74. if( L'\0' == *Str ) {
  75. Params->nParams = 0;
  76. Params->Params = NULL;
  77. return ;
  78. }
  79. Count = 1; Mod = 0;
  80. Start = Str;
  81. while(*Str) {
  82. if(L',' == *Str) {
  83. Mod ++;
  84. if( Mod == 3 ) {
  85. Mod = 0;
  86. Count ++;
  87. }
  88. *Str = L'\0';
  89. }
  90. Str ++;
  91. }
  92. if( 2 != Mod ) {
  93. printf("Invalid DHCPCAPI_PARAMS_ARRAY data! Ignored\n");
  94. Params->nParams = 0;
  95. Params->Params = NULL;
  96. return ;
  97. }
  98. Params->nParams = Count;
  99. Params->Params = LocalAlloc(LMEM_FIXED, sizeof(DHCPCAPI_PARAMS)*Count);
  100. if( NULL == Params->Params ) {
  101. fatal(MakeParams-not enough memory, ERROR_NOT_ENOUGH_MEMORY);
  102. }
  103. Str = Start;
  104. for( i = 0; i < Count ; i ++ ) {
  105. First = Str;
  106. Second = wcslen(Str)+Str+1; Str = Second;
  107. Third = wcslen(Str)+Str+1; Str = Third + wcslen(Third) + 1;
  108. Params->Params[i].Flags = MakeDword(First);
  109. if( 'v' == *Second || 'V' == *Second ) {
  110. Params->Params[i].IsVendor = TRUE;
  111. Second ++;
  112. } else {
  113. Params->Params[i].IsVendor = FALSE;
  114. }
  115. Params->Params[i].OptionId = MakeDword(Second);
  116. MakeHexBytes(&Params->Params[i].Data, &Params->Params[i].nBytesData, Third);
  117. }
  118. }
  119. VOID
  120. UnMakeParams(
  121. IN PDHCPCAPI_PARAMS_ARRAY Params
  122. )
  123. {
  124. if( NULL != Params->Params ) {
  125. LocalFree(Params->Params);
  126. }
  127. }
  128. VOID
  129. PrintHex(
  130. IN ULONG Count,
  131. IN LPBYTE Data
  132. )
  133. {
  134. BYTE cData;
  135. while(Count --) {
  136. cData = *Data++;
  137. if( (cData>>4) < 10 ) printf("%c", '0' + (cData>>4));
  138. else printf("%c", 'A' + (cData>>4) - 10 );
  139. if( (cData& 0xF) < 10 ) printf("%c", '0' + (cData&0xF));
  140. else printf("%c", 'A' + (cData&0xF) - 10 );
  141. }
  142. }
  143. VOID
  144. PrintParams(
  145. IN PDHCPCAPI_PARAMS_ARRAY Params
  146. )
  147. {
  148. ULONG Count;
  149. printf("\t{%ld,", Params->nParams);
  150. if( Params->nParams ) {
  151. printf("{");
  152. for( Count = 0; Count < Params->nParams ; Count ++ ) {
  153. printf("[0x%lx,%ld,", Params->Params[Count].Flags, Params->Params[Count].OptionId);
  154. PrintHex(Params->Params[Count].nBytesData, Params->Params[Count].Data);
  155. printf("] ");
  156. }
  157. printf("}");
  158. }
  159. printf("}\n");
  160. }
  161. ULONG
  162. TestRequestParams(
  163. IN LPWSTR *Args,
  164. IN ULONG nArgs
  165. )
  166. {
  167. DWORD Error;
  168. DWORD Flags;
  169. LPVOID Reserved;
  170. LPWSTR AdapterName, AppName;
  171. LPDHCPCAPI_CLASSID ClassId;
  172. DHCPCAPI_PARAMS_ARRAY SendParams;
  173. DHCPCAPI_PARAMS_ARRAY RecdParams;
  174. BYTE Buffer[5000];
  175. DWORD Size;
  176. if( 8 != nArgs ) {
  177. printf("Usage: DhcpRequestParams Flags Reserved AdapterName ClassId SendParams RecdParams Size lpAppName\n");
  178. return ERROR_INVALID_PARAMETER;
  179. }
  180. Flags = MakeDword(Args[0]);
  181. Reserved = ULongToPtr(MakeDword(Args[1]));
  182. AdapterName = MakeLpwstr(Args[2]);
  183. ClassId = ULongToPtr(MakeDword(Args[3]));
  184. MakeParams(&SendParams, Args[4]);
  185. MakeParams(&RecdParams, Args[5]);
  186. Size = MakeDword(Args[6]);
  187. AppName = MakeLpwstr(Args[7]);
  188. printf("DhcpRequestParams(\n\t0x%lx,\n\t0x%p,\n\t%ws,\n\t0x%p,\n",
  189. Flags, Reserved, AdapterName, ClassId
  190. );
  191. PrintParams(&SendParams);
  192. PrintParams(&RecdParams);
  193. printf("\t0x%p,\n\t[Size = %ld],\n\t%ws\n\t);\n", (Size?Buffer:0), Size, AppName);
  194. Error = DhcpRequestParams(
  195. Flags,
  196. Reserved,
  197. AdapterName,
  198. ClassId,
  199. SendParams,
  200. RecdParams,
  201. Size?Buffer:0,
  202. &Size,
  203. AppName
  204. );
  205. printf("DhcpRequestParams: %ld (0x%lx) [Size = %ld]\n", Error, Error, Size);
  206. printf("Send, Recd Params are:\n");
  207. PrintParams(&SendParams);
  208. PrintParams(&RecdParams);
  209. UnMakeParams(&SendParams);
  210. UnMakeParams(&RecdParams);
  211. return 0;
  212. }
  213. ULONG
  214. TestUndoRequestParams(
  215. IN LPWSTR *Args,
  216. IN ULONG nArgs
  217. )
  218. {
  219. ULONG Flags;
  220. LPVOID Reserved;
  221. LPWSTR AdapterName, AppName;
  222. ULONG Error;
  223. if( nArgs != 4 ) {
  224. printf("usage: DhcpUndoRequestParams dwFlags dwReserved lpwAdapterName lpwAppName\n");
  225. return 0;
  226. }
  227. Flags = MakeDword(Args[0]);
  228. Reserved = ULongToPtr(MakeDword(Args[1]));
  229. AdapterName = MakeLpwstr(Args[2]);
  230. AppName = MakeLpwstr(Args[3]);
  231. printf("DhcpUndoRequestParams(\n\t0x%lx,\n\t%p,\n\t%ws,\n\t%ws\n\t);\n", Flags, Reserved, AdapterName, AppName);
  232. Error = DhcpUndoRequestParams(
  233. Flags,
  234. Reserved,
  235. AdapterName,
  236. AppName
  237. );
  238. printf("DhcpUndoRequestParams: %ld (0x%lx)\n", Error, Error);
  239. return 0;
  240. }
  241. ULONG
  242. TestRegisterParamChange(
  243. IN LPWSTR *Args,
  244. IN ULONG nArgs
  245. )
  246. {
  247. DWORD Error;
  248. DWORD Flags;
  249. LPVOID Reserved;
  250. LPWSTR AdapterName;
  251. LPDHCPCAPI_CLASSID ClassId;
  252. DHCPCAPI_PARAMS_ARRAY Params;
  253. HANDLE Handle;
  254. if( nArgs != 6 ) {
  255. printf("usage: DhcpRegisterParamChange dwFlags dwReserved lpwAdapterName dwClassId Params Handle\n");
  256. return 0;
  257. }
  258. printf("Handle of 0 causes NULL to be used, any other Handle will use default ptr\n");
  259. Flags = MakeDword(Args[0]);
  260. Reserved = ULongToPtr(MakeDword(Args[1]));
  261. AdapterName = MakeLpwstr(Args[2]);
  262. ClassId = ULongToPtr(MakeDword(Args[3]));
  263. MakeParams(&Params, Args[4]);
  264. Handle = ULongToPtr(MakeDword(Args[5]));
  265. printf("DhcpRegisterParamChange(\n\t0x%lx,\n\t%p,\n\t%ws,\n\t0x%p,\n", Flags, Reserved, AdapterName, ClassId);
  266. PrintParams(&Params);
  267. printf("0x%px\n\t);", Handle?&Handle: NULL);
  268. Error = DhcpRegisterParamChange(
  269. Flags,
  270. Reserved,
  271. AdapterName,
  272. ClassId,
  273. Params,
  274. (LPVOID)(Handle?&Handle:NULL)
  275. );
  276. printf("DhcpRegisterParamChange: %ld (0x%lx)\n", Error, Error);
  277. if( ERROR_SUCCESS == Error ) {
  278. tGlobalHandle = Handle;
  279. }
  280. printf("Handle: 0x%p\n", Handle);
  281. UnMakeParams(&Params);
  282. return 0;
  283. }
  284. ULONG
  285. TestDeRegisterParamChange(
  286. IN LPWSTR *Args,
  287. IN ULONG nArgs
  288. )
  289. {
  290. DWORD Error;
  291. DWORD Flags;
  292. LPVOID Reserved;
  293. HANDLE Handle;
  294. if( nArgs != 3 ) {
  295. printf("usage: DhcpRegisterParamChange dwFlags dwReserved Handle\n");
  296. return 0;
  297. }
  298. Flags = MakeDword(Args[0]);
  299. Reserved = ULongToPtr(MakeDword(Args[1]));
  300. Handle = ULongToPtr(MakeDword(Args[2]));
  301. printf("DhcpRegisterParamChange(\n\t0x%lx,\n\t%p,\n\t0x%p,\n\t);", Flags, Reserved, Handle);
  302. Error = DhcpDeRegisterParamChange(
  303. Flags,
  304. Reserved,
  305. Handle
  306. );
  307. printf("DhcpDeRegisterParamChange: %ld (0x%lx)\n", Error, Error);
  308. return 0;
  309. }
  310. ULONG
  311. TestStaticRefreshParams(
  312. IN LPWSTR *Args,
  313. IN ULONG nArgs
  314. )
  315. {
  316. DWORD Error;
  317. ULONG DhcpStaticRefreshParams( LPWSTR AdapterName );
  318. if( nArgs != 1 ) {
  319. printf("usage: DhcpStaticRefresh lpwAdapterName\n");
  320. return 0;
  321. }
  322. Error = DhcpStaticRefreshParams( MakeLpwstr(*Args) );
  323. printf("DhcpStaticRefreshParams( %ws ) : %ld\n", *Args, Error );
  324. return Error;
  325. }
  326. ULONG
  327. DhcpApiTestLine(
  328. IN LPWSTR Buf
  329. )
  330. {
  331. ULONG Error;
  332. LPWSTR Words[100];
  333. ULONG nWords;
  334. BOOL LastCharWasSpace = TRUE;
  335. nWords = 0;
  336. while( *Buf ) { // process each character till end
  337. if( *Buf == L'\t' || *Buf == L' ' || *Buf == L'\n' ) {
  338. if( FALSE == LastCharWasSpace ) { // this char is space but last wasnt
  339. *Buf = L'\0' ; // terminate the run
  340. LastCharWasSpace = TRUE; // change state
  341. }
  342. } else if( TRUE == LastCharWasSpace ) { // this char is not space but last was
  343. Words[nWords++] = Buf; // mark this as beginning of new string
  344. LastCharWasSpace = FALSE; // change state
  345. }
  346. Buf ++; // next character
  347. }
  348. if( 0 == nWords ) {
  349. error(Incorrect command. Please type help, 0);
  350. return 0;
  351. }
  352. if( 0 == _wcsicmp(Words[0], L"DhcpRequestParams") ) {
  353. return TestRequestParams(&Words[1], nWords-1);
  354. }
  355. if( 0 == _wcsicmp(Words[0], L"DhcpUndoRequestParams") ) {
  356. return TestUndoRequestParams(&Words[1], nWords-1);
  357. }
  358. if( 0 == _wcsicmp(Words[0], L"DhcpRegisterParamChange") ) {
  359. return TestRegisterParamChange(&Words[1], nWords-1);
  360. }
  361. if( 0 == _wcsicmp(Words[0], L"DhcpDeRegisterParamChange") ) {
  362. return TestDeRegisterParamChange(&Words[1], nWords-1);
  363. }
  364. if( 0 == _wcsicmp(Words[0], L"DhcpStaticRefresh") ) {
  365. return TestStaticRefreshParams(&Words[1], nWords-1);
  366. }
  367. printf("Unknown command!");
  368. while(nWords--) {
  369. printf("First word: %ws\n", Words[0]);
  370. }
  371. return 0;
  372. }
  373. ULONG
  374. DhcpApiTestLoop(
  375. VOID
  376. )
  377. {
  378. ULONG Error;
  379. WCHAR Buffer[1000];
  380. int len = 0;
  381. printf("Usage:\n");
  382. printf(" For AdapterName use NULL to indicate NULL adaptername.\n");
  383. printf(" For Params, use a dash (-) to indcate no params.\n");
  384. printf(" For Params, syntax: flags,optid,[sequence of hex bytes or empty] {,flags,optid,[sequence of hex bytes]}*\n");
  385. printf(" For Params, prepend OptId with the character 'v' to make it vendor option.\n");
  386. printf(" Flags values -- DHCPCAPI_REQUEST_PERSISTENT - %ld\n", DHCPCAPI_REQUEST_PERSISTENT);
  387. printf(" Flags values -- DHCPCAPI_REQUEST_SYNCHRONOUS - %ld\n", DHCPCAPI_REQUEST_SYNCHRONOUS);
  388. printf(" Flags values -- DHCPCAPI_REGISTER_HANDLE_EVENT - %ld\n", DHCPCAPI_REGISTER_HANDLE_EVENT);
  389. printf("\n\n");
  390. printf("1) DhcpRequestParams dwFlags dwReserved lpwAdapterName dwClassId SendParams RecdParams dwSize lpwAppName\n");
  391. printf("2) DhcpUndoRequestParams dwFlags dwReserved lpwAdapterName lpwAppName\n");
  392. printf("3) DhcpRegisterParamChange dwFlags dwReserved lpwAdapterName pClassId Params pHandle\n");
  393. printf("4) DhcpDeRegisterParamChange dwFlags dwReserved dwEvent\n");
  394. printf("5) DhcpStaticRefresh lpwAdapterName\n");
  395. do {
  396. printf("DHCP> ");
  397. if( NULL == fgetws(Buffer, 1000, stdin) ) break;
  398. len = wcslen(Buffer);
  399. if (Buffer[len] == '\n') {
  400. Buffer[len] = 0;
  401. }
  402. Error = DhcpApiTestLine(Buffer);
  403. if( ERROR_SUCCESS != Error ) {
  404. error(DhcpApiTestLine, Error);
  405. }
  406. } while(1);
  407. return 0;
  408. }
  409. void _cdecl main(void) {
  410. DWORD Error;
  411. Error = DhcpCApiInitialize(NULL);
  412. if( ERROR_SUCCESS != Error ) {
  413. fatal(DhcpApiInitialize, Error);
  414. }
  415. Error = DhcpApiTestLoop();
  416. if( ERROR_SUCCESS != Error ) {
  417. error(DhcpApiTestLoop, Error);
  418. }
  419. DhcpCApiCleanup();
  420. exit(0);
  421. }
  422. //================================================================================
  423. // end of file
  424. //================================================================================