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.

499 lines
15 KiB

  1. /*++
  2. Copyright (C) 1999 Microsoft Corporation
  3. Module Name:
  4. optapi.c
  5. --*/
  6. /*--------------------------------------------------------------------------------
  7. This program is used to test the api options part of the api.
  8. Date: April 15 1997
  9. Author: RameshV (VK)
  10. Description: This program is used to test the options api part of the client
  11. options.
  12. --------------------------------------------------------------------------------*/
  13. #include "precomp.h"
  14. #include <dhcploc.h>
  15. #include <dhcppro.h>
  16. #include <lmcons.h>
  17. #include <align.h>
  18. #include <apiappl.h>
  19. #include <dhcpcapi.h>
  20. #include <string.h>
  21. #include <winbase.h>
  22. #include <iphlpapi.h>
  23. //--------------------------------------------------------------------------------
  24. // Some variables.
  25. //--------------------------------------------------------------------------------
  26. #define OMAP_MAX_OPTIONS 256
  27. // The options to request.
  28. BYTE Request[OMAP_MAX_OPTIONS];
  29. int nGlobalOptionsRequested = 0;
  30. // The return values
  31. BYTE *pObtained = NULL;
  32. int nObtained = 0, opt_data_size = 0;
  33. BYTE *options_data;
  34. // USAGE
  35. #define USAGE "Usage: %s <cmd> <arguments>\n\t" \
  36. "The currently supported cmd's and arguments are:\n\t" \
  37. "\t<cmd> <arguments>\n\t" \
  38. "\tGetOptions RequestList_in_Hex // such as 010503a1\n\t" \
  39. "\tTestEvents RequestList_in_Hex // such as 010503a1\n\t" \
  40. "\tRelease AdapterName // ipconfig /release\n\t" \
  41. "\tRenew AdapterName // ipconfig /renew \n\t" \
  42. "\tEnumClasses AdapterName // enumerate dhcpclasses\n\t"\
  43. "\tSetClass AdapterName ClassName // set user class\n\t"\
  44. "\n\n"
  45. //--------------------------------------------------------------------------------
  46. // Parse a hex list of options. (such as 0105434421 )
  47. //--------------------------------------------------------------------------------
  48. int // nOptionsRequested;
  49. GetOptionList(char *s, char *Request) {
  50. int nOptionsRequested = 0;
  51. while(s && *s & *(s+1)) {
  52. *s = (UCHAR) tolower(*s);
  53. if(!isdigit(*s) && ((*s) < 'a' || (*s) > 'f') ) {
  54. fprintf(stderr, "found obscene character <%c> when looking for hex!\n", *s);
  55. fprintf(stderr, "bye\n");
  56. exit(1);
  57. }
  58. if(isdigit(*s))
  59. Request[nOptionsRequested] = (*s) - '0';
  60. else Request[nOptionsRequested] = (*s) - 'a' + 10;
  61. Request[nOptionsRequested] *= 0x10;
  62. // Now do the same for the next digit.
  63. s ++;
  64. *s = (UCHAR) tolower(*s);
  65. if(!isdigit(*s) && (*s) < 'a' && (*s) > 'f' ) {
  66. fprintf(stderr, "found obscene character <%c> when looking for hex!\n", *s);
  67. fprintf(stderr, "bye\n");
  68. exit(1);
  69. }
  70. if(isdigit(*s))
  71. Request[nOptionsRequested] += (*s) - '0';
  72. else Request[nOptionsRequested] += (*s) - 'a' + 10;
  73. s ++;
  74. nOptionsRequested ++;
  75. }
  76. if(*s) {
  77. fprintf(stderr, "ignoring character <%c>\n", *s);
  78. }
  79. return nOptionsRequested;
  80. }
  81. //--------------------------------------------------------------------------------
  82. // Here is the function that does the GetOptions command.
  83. // It parses the adaptername and then converts it to LPWSTR and
  84. // it also parses the requestlist and then it calls DhcpRequestOptions.
  85. // It prints out the data it gets back.
  86. //--------------------------------------------------------------------------------
  87. void
  88. OptApiGetOptions(int argc, char *argv[]) {
  89. WCHAR AdapterName[100];
  90. PIP_INTERFACE_INFO IfInfo;
  91. UCHAR Buffer[4000];
  92. ULONG BufLen = sizeof(Buffer);
  93. DWORD Error;
  94. // first check if we have the right command.
  95. if(_stricmp(argv[1], "GetOptions")) {
  96. fprintf(stderr, "Internal inconsistency in OptApiGetOptions\n");
  97. exit(1);
  98. }
  99. // Now check and see if there are the correct number of arguments.
  100. if(argc != 3 ) {
  101. fprintf(stderr, USAGE, argv[0]);
  102. exit(1);
  103. }
  104. // Now first get the list of options requested.
  105. {
  106. nGlobalOptionsRequested = GetOptionList(argv[2], Request);
  107. {
  108. int i;
  109. printf("Requesting %d options: ", nGlobalOptionsRequested);
  110. for( i = 0 ; i < nGlobalOptionsRequested; i ++)
  111. printf("%02x ", (int)Request[i]);
  112. }
  113. }
  114. //
  115. // get the adaptername
  116. //
  117. IfInfo = (PIP_INTERFACE_INFO)Buffer;
  118. Error = GetInterfaceInfo(IfInfo, &BufLen);
  119. if( NO_ERROR != Error ) {
  120. printf("GetInterfaceInfo: 0x%lx\n", Error);
  121. exit(1);
  122. }
  123. if( IfInfo->NumAdapters == 0 ) {
  124. printf("No adapters !!\n");
  125. exit(1);
  126. }
  127. if( wcslen(IfInfo->Adapter[0].Name) <= 14 ) {
  128. printf("Invalid adapter name? : %ws\n", IfInfo->Adapter[0].Name);
  129. exit(1);
  130. }
  131. wcscpy(AdapterName, &IfInfo->Adapter[0].Name[14]);
  132. // now call the function to get options.
  133. printf(" from adapter <%ws>\n", AdapterName);
  134. {
  135. DWORD result;
  136. result = DhcpRequestOptions(
  137. //L"El59x1", RAMESHV-P200's adapter
  138. // L"IEEPRO1", SUNBEAM, KISSES (or CltApi) machine's adapter
  139. //L"NdisWan4", // SUNBEAM, KISSES's wan adapter: does not have ip address.
  140. AdapterName,
  141. Request, nGlobalOptionsRequested,
  142. &options_data, &opt_data_size,
  143. &pObtained, &nObtained
  144. );
  145. printf("Result is: %d; Obtained: %d\nList size is %d\n",
  146. result,
  147. nObtained,
  148. opt_data_size);
  149. if(result) {
  150. fprintf(stderr, "function call failed\n");
  151. return;
  152. }
  153. printf("Data: ");
  154. while(opt_data_size--)
  155. printf("%02x ", *options_data++);
  156. printf("\n");
  157. }
  158. // done
  159. printf("bye\n");
  160. }
  161. void
  162. OptApiRelease(int argc, char *argv[]) {
  163. WCHAR AdapterName[256];
  164. // Check for the size and # of arguments.
  165. if( argc != 3 ) {
  166. fprintf(stderr, USAGE , argv[0]);
  167. exit(1);
  168. }
  169. // Now create a WSTR out of argv[2].
  170. if( strlen(argv[2]) != mbstowcs(AdapterName, argv[2], strlen(argv[2]))) {
  171. fprintf(stderr, "Could not convert %s to LPWSTR! sorry\n", argv[2]);
  172. exit(1);
  173. }
  174. AdapterName[strlen(argv[2])] = L'\0';
  175. printf("Return Value = %ld\n", DhcpReleaseParameters(AdapterName));
  176. }
  177. void
  178. OptApiRenew(int argc, char *argv[]) {
  179. WCHAR AdapterName[256];
  180. // Check for the size and # of arguments.
  181. if( argc != 3 ) {
  182. fprintf(stderr, USAGE , argv[0]);
  183. exit(1);
  184. }
  185. // Now create a WSTR out of argv[2].
  186. if( strlen(argv[2]) != mbstowcs(AdapterName, argv[2], strlen(argv[2]))) {
  187. fprintf(stderr, "Could not convert %s to LPWSTR! sorry\n", argv[2]);
  188. exit(1);
  189. }
  190. AdapterName[strlen(argv[2])] = L'\0';
  191. printf("Return Value = %ld\n", DhcpAcquireParameters(AdapterName));
  192. }
  193. void
  194. OptApiTestEvents(int argc, char *argv[]) {
  195. WCHAR AdapterName[100];
  196. // first check if we have the right command.
  197. if(_stricmp(argv[1], "TestEvents")) {
  198. fprintf(stderr, "Internal inconsistency in OptApiGetOptions\n");
  199. exit(1);
  200. }
  201. // Now check and see if there are the correct number of arguments.
  202. if(argc != 4 ) {
  203. fprintf(stderr, USAGE, argv[0]);
  204. exit(1);
  205. }
  206. // Now first get the list of options requested.
  207. {
  208. nGlobalOptionsRequested = GetOptionList(argv[3], Request);
  209. {
  210. int i;
  211. printf("Testing Events for %d options: ", nGlobalOptionsRequested);
  212. for( i = 0 ; i < nGlobalOptionsRequested; i ++)
  213. printf("%02x ", (int)Request[i]);
  214. }
  215. }
  216. // Now get the adapter.
  217. if(strlen(argv[2]) != mbstowcs(AdapterName, argv[2], strlen(argv[2]))) {
  218. fprintf(stderr, "Could not convert %s to LPWSTR! sorry\n", argv[2]);
  219. exit(1);
  220. }
  221. // null terminate the string.
  222. AdapterName[strlen(argv[2])] = L'\0' ;
  223. // now call the function to get options.
  224. printf(" from adapter <%s>\n", argv[2]);
  225. {
  226. DWORD result;
  227. HANDLE Handle;
  228. result = DhcpRegisterOptions(
  229. AdapterName,
  230. Request, nGlobalOptionsRequested, &Handle
  231. );
  232. printf("result is %d, Handle = 0x%p\n", result, Handle);
  233. if(result) {
  234. fprintf(stderr, "function call failed\n");
  235. return;
  236. }
  237. printf("Please use another window to do a ipconfig /renew and obtain\n");
  238. printf("one of the above options...\n");
  239. printf("WaitForSingleObject(Handle,INFINITE) = ");
  240. switch(WaitForSingleObject(Handle, INFINITE)) {
  241. case WAIT_ABANDONED: printf("WAIT_ABANDONED! Giving up\n"); return;
  242. case WAIT_OBJECT_0 : printf("WAIT_OBJECT_0 ! proceeding\n"); break;
  243. case WAIT_TIMEOUT : printf("WAIT_TIMEOUT ! Giving up\n"); return;
  244. case WAIT_FAILED : printf("WAIT_FAILED (%d); giving up\n", GetLastError()); return;
  245. default: printf("XXXX; this should not happen at all!\n"); return;
  246. }
  247. // Okay the object was signalled. So, now we have to do a request on this.
  248. result = DhcpRequestOptions(
  249. AdapterName,
  250. Request, nGlobalOptionsRequested,
  251. &options_data, &opt_data_size,
  252. &pObtained, &nObtained
  253. );
  254. printf("Result is: %d; Obtained: %d\nList size is %d\n",
  255. result,
  256. nObtained,
  257. opt_data_size);
  258. if(result) {
  259. fprintf(stderr, "function call failed\n");
  260. return;
  261. }
  262. printf("Data: ");
  263. while(opt_data_size--)
  264. printf("%02x ", *options_data++);
  265. printf("\n");
  266. // Now deregister this object.
  267. result = DhcpDeRegisterOptions(Handle);
  268. printf("DeRegister(0x%p) = %ld\n", Handle, result);
  269. }
  270. // done
  271. printf("bye\n");
  272. }
  273. void
  274. OptApiEnumClasses(int argc, char *argv[]) {
  275. WCHAR AdapterName[256];
  276. DHCP_CLASS_INFO *Classes;
  277. ULONG Size, RetVal;
  278. ULONG i;
  279. // Check for the size and # of arguments.
  280. if( argc != 3 ) {
  281. fprintf(stderr, USAGE , argv[0]);
  282. exit(1);
  283. }
  284. // Now create a WSTR out of argv[2].
  285. if( strlen(argv[2]) != mbstowcs(AdapterName, argv[2], strlen(argv[2]))) {
  286. fprintf(stderr, "Could not convert %s to LPWSTR! sorry\n", argv[2]);
  287. exit(1);
  288. }
  289. AdapterName[strlen(argv[2])] = L'\0';
  290. Size = 0;
  291. Classes = NULL;
  292. RetVal = DhcpEnumClasses( 0, AdapterName, &Size, Classes);
  293. if( ERROR_MORE_DATA != RetVal ) {
  294. printf("Return Value for first call = %ld\n", RetVal);
  295. return;
  296. }
  297. printf("Size required is %ld\n", Size);
  298. if( 0 == Size ) return ;
  299. Classes = LocalAlloc(LMEM_FIXED, Size);
  300. if( NULL == Classes ) {
  301. printf("Could not allocate memory: %ld\n", GetLastError());
  302. return;
  303. }
  304. RetVal = DhcpEnumClasses(0, AdapterName, &Size, Classes);
  305. if( ERROR_SUCCESS != RetVal ) {
  306. printf("Return value for second call = %ld\n", RetVal);
  307. return;
  308. }
  309. printf("Returned # of classes = %ld\n", Size);
  310. for( i = 0; i != Size ; i ++ ) {
  311. ULONG j;
  312. printf("Class [%ld] = <%ws, %ws> Data[%ld] : ", i, Classes[i].ClassName, Classes[i].ClassDescr, Classes[i].ClassDataLen);
  313. for( j = 0; j != Classes[i].ClassDataLen ; j ++ ) {
  314. printf("%02X ", Classes[i].ClassData[j]);
  315. }
  316. printf("\n");
  317. }
  318. }
  319. void
  320. OptApiSetClass(int argc, char *argv[]) {
  321. WCHAR AdapterName[256];
  322. WCHAR UserClass[256];
  323. HKEY InterfacesKey, AdapterKey;
  324. DHCP_PNP_CHANGE Changes = {
  325. 0,
  326. 0,
  327. 0,
  328. 0,
  329. TRUE
  330. };
  331. ULONG Size, RetVal;
  332. ULONG i;
  333. // check for the size and # of arguments
  334. if( argc != 4 ) {
  335. fprintf(stderr, USAGE, argv[0]);
  336. exit(1);
  337. }
  338. // Now create a WSTR out of argv[2] and argv[3]
  339. if( strlen(argv[2]) != mbstowcs(AdapterName, argv[2], strlen(argv[2]))) {
  340. fprintf(stderr, "Could not convert %s to LPwSTR! sorry\n", argv[2]);
  341. exit(1);
  342. }
  343. UserClass[strlen(argv[3])] = L'\0';
  344. if( strlen(argv[3]) != mbstowcs(UserClass, argv[3], strlen(argv[3]))) {
  345. fprintf(stderr, "Could not convert %s to LPwSTR! sorry\n", argv[3]);
  346. exit(1);
  347. }
  348. UserClass[strlen(argv[3])] = L'\0';
  349. RetVal = RegOpenKeyExW(
  350. HKEY_LOCAL_MACHINE,
  351. L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
  352. 0,
  353. KEY_ALL_ACCESS,
  354. &InterfacesKey
  355. );
  356. if( ERROR_SUCCESS != RetVal ) {
  357. printf("Couldn't open Tcpip\\Interfaces key: %ld\n", RetVal);
  358. return;
  359. }
  360. RetVal = RegOpenKeyExW(
  361. InterfacesKey,
  362. AdapterName,
  363. 0,
  364. KEY_ALL_ACCESS,
  365. &AdapterKey
  366. );
  367. if( ERROR_SUCCESS != RetVal ) {
  368. printf("Couldn't open Tcpip\\Interfaces\\%ws key : %ld\n", AdapterName, RetVal);
  369. return;
  370. }
  371. RetVal = RegSetValueExW(
  372. AdapterKey,
  373. L"DhcpClassId",
  374. 0,
  375. REG_SZ,
  376. (LPBYTE)UserClass,
  377. (wcslen(UserClass)+1)*sizeof(WCHAR)
  378. );
  379. if( ERROR_SUCCESS != RetVal ) {
  380. printf("RegSetValueExW(DhcpClassId): %ld\n", RetVal);
  381. return;
  382. }
  383. RetVal = DhcpHandlePnPEvent(
  384. 0,
  385. 1,
  386. AdapterName,
  387. &Changes,
  388. NULL
  389. );
  390. printf("DhcpHandlePnPEvent: %ld\n", RetVal);
  391. }
  392. void __cdecl
  393. main(int argc, char *argv[]) {
  394. if(argc < 2) {
  395. fprintf(stderr, USAGE, argv[0]);
  396. exit(1);
  397. }
  398. if(!_stricmp(argv[1], "GetOptions")) {
  399. OptApiGetOptions(argc, argv);
  400. } else if(!_stricmp(argv[1], "TestEvents")) {
  401. OptApiTestEvents(argc, argv);
  402. } else if(!_stricmp(argv[1], "Release") ) {
  403. OptApiRelease(argc, argv);
  404. } else if(!_stricmp(argv[1], "Renew") ) {
  405. OptApiRenew(argc, argv);
  406. } else if(!_stricmp(argv[1], "EnumClasses" ) ){
  407. OptApiEnumClasses(argc, argv);
  408. } else if(!_stricmp(argv[1], "SetClass" ) ) {
  409. OptApiSetClass(argc, argv);
  410. } else {
  411. // currently support for no other command.
  412. fprintf(stderr, USAGE, argv[0]);
  413. exit(1);
  414. }
  415. exit(0);
  416. }