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.

1004 lines
22 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. dhcpcli.c
  5. Abstract:
  6. This is the API tester for the DHCP client.
  7. To build, 'nmake UMTEST=dhcpcli'
  8. Author:
  9. Manny Weiser (mannyw) 1-Dec-1992
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. Madan Appiah (madana) 21-Oct-1993
  14. --*/
  15. #include "precomp.h"
  16. #include "dhcploc.h"
  17. #include "dhcppro.h"
  18. #include "dhcpcapi.h"
  19. #include "lmcons.h"
  20. #include "conio.h"
  21. DWORD // win32 status
  22. DhcpAcquireParametersByBroadcast( // acquire/renew a lease
  23. IN LPWSTR AdapterName // adapter to acquire lease on
  24. );
  25. #define IP_ADDRESS_STRING L"IPAddress"
  26. #define IP_ADDRESS_STRING_TYPE REG_MULTI_SZ
  27. #define SUBNET_MASK_STRING L"SubnetMask"
  28. #define SUBNET_MASK_STRING_TYPE REG_MULTI_SZ
  29. #define USAGE_MESSAGE0 \
  30. "Usage: dhcpcli { " \
  31. "renew,release,set,enabledhcp,disabledhcp,timestamp," \
  32. "leasetest,fbrefresh, reinit } parameters\n"
  33. #define USAGE_MESSAGE1 "Usage: dhcpcli renew adapter\n"
  34. #define USAGE_MESSAGE2 "Usage: dhcpcli release adapter\n"
  35. #define USAGE_MESSAGE3 "Usage: dhcpcli set ip-address subnet-mask adapter index \n"
  36. #define USAGE_MESSAGE4 "Usage: dhcpcli enabledhcp adapter\n"
  37. #define USAGE_MESSAGE5 "Usage: dhcpcli disabledhcp adapter ip-address subnet-mask \n"
  38. #define USAGE_MESSAGE6 "Usage: dhcpcli timestamp Value\n"
  39. #define USAGE_MESSAGE7 "Usage: dhcpcli leasetest adapter-ip-addres hardware-address\n"
  40. #define USAGE_MESSAGE8 "Usage: dhcpcli fbrefresh adapter\n"
  41. #define USAGE_MESSAGE9 "Usage: dhcpcli reinit adapter\n"
  42. DWORD
  43. GetRegistryString(
  44. HKEY Key,
  45. LPWSTR ValueStringName,
  46. LPWSTR *String,
  47. LPDWORD StringSize
  48. )
  49. /*++
  50. Routine Description:
  51. This function retrieves the specified string value from the
  52. registry. It allocates local memory for the returned string.
  53. Arguments:
  54. Key : registry handle to the key where the value is.
  55. ValueStringName : name of the value string.
  56. String : pointer to a location where the string pointer is returned.
  57. StringSize : size of the string data returned. Optional
  58. Return Value:
  59. The status of the operation.
  60. --*/
  61. {
  62. DWORD Error;
  63. DWORD LocalValueType;
  64. DWORD ValueSize;
  65. LPWSTR LocalString;
  66. DhcpAssert( *String == NULL );
  67. //
  68. // Query DataType and BufferSize.
  69. //
  70. Error = RegQueryValueEx(
  71. Key,
  72. ValueStringName,
  73. 0,
  74. &LocalValueType,
  75. NULL,
  76. &ValueSize );
  77. if( Error != ERROR_SUCCESS ) {
  78. return(Error);
  79. }
  80. DhcpAssert( (LocalValueType == REG_SZ) ||
  81. (LocalValueType == REG_MULTI_SZ) );
  82. if( ValueSize == 0 ) {
  83. *String = NULL;
  84. *StringSize = 0;
  85. return( ERROR_SUCCESS );
  86. }
  87. //
  88. // now allocate memory for string data.
  89. //
  90. LocalString = DhcpAllocateMemory( ValueSize );
  91. if(LocalString == NULL) {
  92. return( ERROR_NOT_ENOUGH_MEMORY );
  93. }
  94. //
  95. // Now query the string data.
  96. //
  97. Error = RegQueryValueEx(
  98. Key,
  99. ValueStringName,
  100. 0,
  101. &LocalValueType,
  102. (LPBYTE)(LocalString),
  103. &ValueSize );
  104. if( Error != ERROR_SUCCESS ) {
  105. DhcpFreeMemory(LocalString);
  106. return(Error);
  107. }
  108. *String = LocalString;
  109. if( StringSize != NULL ) {
  110. *StringSize = ValueSize;
  111. }
  112. return( ERROR_SUCCESS );
  113. }
  114. DWORD
  115. RegSetIpAddress(
  116. HKEY KeyHandle,
  117. LPWSTR ValueName,
  118. DWORD ValueType,
  119. DHCP_IP_ADDRESS IpAddress
  120. )
  121. /*++
  122. Routine Description:
  123. This function sets IpAddress Value in the registry.
  124. Arguments:
  125. KeyHandle - handle to the key.
  126. ValueName - name of the value field.
  127. ValueType - Type of the value field.
  128. IpAddress - Ipaddress to be set.
  129. Return Value:
  130. Registry Error.
  131. --*/
  132. {
  133. DWORD Error;
  134. LPSTR AnsiAddressString;
  135. WCHAR UnicodeAddressBuf[DOT_IP_ADDR_SIZE];
  136. LPWSTR UnicodeAddressString;
  137. LPWSTR MultiIpAddressString = NULL;
  138. LPWSTR NewMultiIpAddressString = NULL;
  139. DWORD MultiIpAddressStringSize;
  140. DWORD NewMultiIpAddressStringSize;
  141. DWORD FirstOldIpAddressSize;
  142. AnsiAddressString = inet_ntoa( *(struct in_addr *)&IpAddress );
  143. UnicodeAddressString = DhcpOemToUnicode(
  144. AnsiAddressString,
  145. UnicodeAddressBuf );
  146. DhcpAssert( UnicodeAddressString != NULL );
  147. if( ValueType == REG_SZ ) {
  148. Error = RegSetValueEx(
  149. KeyHandle,
  150. ValueName,
  151. 0,
  152. ValueType,
  153. (LPBYTE)UnicodeAddressString,
  154. (wcslen(UnicodeAddressString) + 1) * sizeof(WCHAR) );
  155. goto Cleanup;
  156. }
  157. DhcpAssert( ValueType == REG_MULTI_SZ );
  158. //
  159. // replace the first IpAddress.
  160. //
  161. //
  162. // query current multi-IpAddress string.
  163. //
  164. Error = GetRegistryString(
  165. KeyHandle,
  166. ValueName,
  167. &MultiIpAddressString,
  168. &MultiIpAddressStringSize );
  169. if( Error != ERROR_SUCCESS ) {
  170. goto Cleanup;
  171. }
  172. //
  173. // allocate new address string.
  174. //
  175. DhcpAssert(MultiIpAddressString != NULL);
  176. FirstOldIpAddressSize =
  177. (wcslen(MultiIpAddressString) + 1) * sizeof(WCHAR);
  178. NewMultiIpAddressStringSize =
  179. MultiIpAddressStringSize - FirstOldIpAddressSize +
  180. (wcslen(UnicodeAddressString) + 1) * sizeof(WCHAR);
  181. NewMultiIpAddressString = DhcpAllocateMemory( NewMultiIpAddressStringSize );
  182. if( NewMultiIpAddressString == NULL ) {
  183. Error = ERROR_NOT_ENOUGH_MEMORY;
  184. goto Cleanup;
  185. }
  186. //
  187. // make new address string first.
  188. //
  189. wcscpy( NewMultiIpAddressString, UnicodeAddressString );
  190. //
  191. // copy rest of the old addresses
  192. //
  193. RtlCopyMemory(
  194. (LPBYTE)NewMultiIpAddressString +
  195. (wcslen(UnicodeAddressString) + 1) * sizeof(WCHAR),
  196. (LPBYTE)MultiIpAddressString + FirstOldIpAddressSize,
  197. MultiIpAddressStringSize - FirstOldIpAddressSize );
  198. Error = RegSetValueEx(
  199. KeyHandle,
  200. ValueName,
  201. 0,
  202. ValueType,
  203. (LPBYTE)NewMultiIpAddressString,
  204. NewMultiIpAddressStringSize );
  205. Cleanup:
  206. if( MultiIpAddressString != NULL) {
  207. DhcpFreeMemory( MultiIpAddressString );
  208. }
  209. if( NewMultiIpAddressString != NULL) {
  210. DhcpFreeMemory( NewMultiIpAddressString );
  211. }
  212. return( Error );
  213. }
  214. EnableDhcp(
  215. char **argv
  216. )
  217. {
  218. DWORD Error;
  219. LPWSTR RegKey = NULL;
  220. HKEY KeyHandle = NULL;
  221. WCHAR AdapterNameBuffer[PATHLEN];
  222. LPWSTR AdapterName;
  223. DWORD EnableDhcp;
  224. AdapterName = DhcpOemToUnicode( argv[0], AdapterNameBuffer );
  225. if( AdapterName == NULL ) {
  226. Error = ERROR_NOT_ENOUGH_MEMORY;
  227. goto Cleanup;
  228. }
  229. //
  230. // make NIC IP parameter key.
  231. //
  232. RegKey = DhcpAllocateMemory(
  233. (wcslen(DHCP_SERVICES_KEY) +
  234. wcslen(REGISTRY_CONNECT_STRING) +
  235. wcslen(AdapterName) +
  236. wcslen(DHCP_ADAPTER_PARAMETERS_KEY) + 1) *
  237. sizeof(WCHAR) ); // termination char.
  238. if( RegKey == NULL ) {
  239. Error = ERROR_NOT_ENOUGH_MEMORY;
  240. goto Cleanup;
  241. }
  242. #if defined(_PNP_POWER_)
  243. wcscpy( RegKey, DHCP_SERVICES_KEY );
  244. wcscat( RegKey, DHCP_ADAPTER_PARAMETERS_KEY );
  245. wcscat( RegKey, REGISTRY_CONNECT_STRING );
  246. wcscat( RegKey, AdapterName );
  247. #else
  248. wcscpy( RegKey, DHCP_SERVICES_KEY );
  249. wcscat( RegKey, REGISTRY_CONNECT_STRING );
  250. wcscat( RegKey, AdapterName );
  251. wcscat( RegKey, DHCP_ADAPTER_PARAMETERS_KEY );
  252. #endif _PNP_POWER_
  253. //
  254. // open this key.
  255. //
  256. Error = RegOpenKeyEx(
  257. HKEY_LOCAL_MACHINE,
  258. RegKey,
  259. 0, // Reserved field
  260. DHCP_CLIENT_KEY_ACCESS,
  261. &KeyHandle
  262. );
  263. if( Error != ERROR_SUCCESS ) {
  264. goto Cleanup;
  265. }
  266. //
  267. // Set DHCP switch.
  268. //
  269. EnableDhcp = TRUE;
  270. Error = RegSetValueEx(
  271. KeyHandle,
  272. DHCP_ENABLE_STRING,
  273. 0,
  274. DHCP_ENABLE_STRING_TYPE,
  275. (LPBYTE)&EnableDhcp,
  276. sizeof(EnableDhcp) );
  277. if( Error != ERROR_SUCCESS ) {
  278. goto Cleanup;
  279. }
  280. //
  281. // set Ipaddress and SubnetMask to zero.
  282. //
  283. Error = RegSetIpAddress(
  284. KeyHandle,
  285. IP_ADDRESS_STRING,
  286. IP_ADDRESS_STRING_TYPE,
  287. 0 );
  288. if( Error != ERROR_SUCCESS ) {
  289. goto Cleanup;
  290. }
  291. Error = RegSetIpAddress(
  292. KeyHandle,
  293. DHCP_IP_ADDRESS_STRING,
  294. DHCP_IP_ADDRESS_STRING_TYPE,
  295. 0 );
  296. if( Error != ERROR_SUCCESS ) {
  297. goto Cleanup;
  298. }
  299. Error = RegSetIpAddress(
  300. KeyHandle,
  301. SUBNET_MASK_STRING,
  302. SUBNET_MASK_STRING_TYPE,
  303. 0xff );
  304. if( Error != ERROR_SUCCESS ) {
  305. goto Cleanup;
  306. }
  307. Error = RegSetIpAddress(
  308. KeyHandle,
  309. DHCP_SUBNET_MASK_STRING,
  310. DHCP_SUBNET_MASK_STRING_TYPE,
  311. 0xff );
  312. if( Error != ERROR_SUCCESS ) {
  313. goto Cleanup;
  314. }
  315. //
  316. // Call config API to indicate to DHCP service.
  317. //
  318. Error = DhcpNotifyConfigChange(
  319. L"",
  320. AdapterName,
  321. TRUE,
  322. 0, // AdapterIndex
  323. 0, // IpAddress
  324. 0, // SubnetMask
  325. DhcpEnable );
  326. Cleanup:
  327. if( RegKey != NULL ) {
  328. DhcpFreeMemory( RegKey );
  329. }
  330. if( KeyHandle != NULL ) {
  331. RegCloseKey( KeyHandle );
  332. }
  333. return(Error);
  334. }
  335. DisableDhcp(
  336. char **argv
  337. )
  338. {
  339. DWORD Error;
  340. LPWSTR RegKey = NULL;
  341. HKEY KeyHandle = NULL;
  342. DWORD IpAddress;
  343. DWORD SubnetMask;
  344. WCHAR AdapterNameBuffer[PATHLEN];
  345. LPWSTR AdapterName;
  346. DWORD EnableDhcp;
  347. AdapterName = DhcpOemToUnicode( argv[0], AdapterNameBuffer );
  348. if( AdapterName == NULL ) {
  349. Error = ERROR_NOT_ENOUGH_MEMORY;
  350. goto Cleanup;
  351. }
  352. IpAddress = inet_addr( argv[1] );
  353. SubnetMask = inet_addr( argv[2] );
  354. //
  355. // make NIC IP parameter key.
  356. //
  357. RegKey = DhcpAllocateMemory(
  358. (wcslen(DHCP_SERVICES_KEY) +
  359. wcslen(REGISTRY_CONNECT_STRING) +
  360. wcslen(AdapterName) +
  361. wcslen(DHCP_ADAPTER_PARAMETERS_KEY) + 1) *
  362. sizeof(WCHAR) ); // termination char.
  363. if( RegKey == NULL ) {
  364. Error = ERROR_NOT_ENOUGH_MEMORY;
  365. goto Cleanup;
  366. }
  367. #if defined(_PNP_POWER_)
  368. wcscpy( RegKey, DHCP_SERVICES_KEY );
  369. wcscat( RegKey, DHCP_ADAPTER_PARAMETERS_KEY );
  370. wcscat( RegKey, REGISTRY_CONNECT_STRING );
  371. wcscat( RegKey, AdapterName );
  372. #else
  373. wcscpy( RegKey, DHCP_SERVICES_KEY );
  374. wcscat( RegKey, REGISTRY_CONNECT_STRING );
  375. wcscat( RegKey, AdapterName );
  376. wcscat( RegKey, DHCP_ADAPTER_PARAMETERS_KEY );
  377. #endif _PNP_POWER_
  378. //
  379. // open this key.
  380. //
  381. Error = RegOpenKeyEx(
  382. HKEY_LOCAL_MACHINE,
  383. RegKey,
  384. 0, // Reserved field
  385. DHCP_CLIENT_KEY_ACCESS,
  386. &KeyHandle
  387. );
  388. if( Error != ERROR_SUCCESS ) {
  389. goto Cleanup;
  390. }
  391. //
  392. // Set DHCP switch.
  393. //
  394. EnableDhcp = FALSE;
  395. Error = RegSetValueEx(
  396. KeyHandle,
  397. DHCP_ENABLE_STRING,
  398. 0,
  399. DHCP_ENABLE_STRING_TYPE,
  400. (LPBYTE)&EnableDhcp,
  401. sizeof(EnableDhcp) );
  402. if( Error != ERROR_SUCCESS ) {
  403. goto Cleanup;
  404. }
  405. //
  406. // set Ipaddress and SubnetMask to zero.
  407. //
  408. Error = RegSetIpAddress(
  409. KeyHandle,
  410. IP_ADDRESS_STRING,
  411. IP_ADDRESS_STRING_TYPE,
  412. IpAddress );
  413. if( Error != ERROR_SUCCESS ) {
  414. goto Cleanup;
  415. }
  416. Error = RegSetIpAddress(
  417. KeyHandle,
  418. DHCP_IP_ADDRESS_STRING,
  419. DHCP_IP_ADDRESS_STRING_TYPE,
  420. 0 );
  421. if( Error != ERROR_SUCCESS ) {
  422. goto Cleanup;
  423. }
  424. Error = RegSetIpAddress(
  425. KeyHandle,
  426. SUBNET_MASK_STRING,
  427. SUBNET_MASK_STRING_TYPE,
  428. SubnetMask );
  429. if( Error != ERROR_SUCCESS ) {
  430. goto Cleanup;
  431. }
  432. Error = RegSetIpAddress(
  433. KeyHandle,
  434. DHCP_SUBNET_MASK_STRING,
  435. DHCP_SUBNET_MASK_STRING_TYPE,
  436. 0xff );
  437. if( Error != ERROR_SUCCESS ) {
  438. goto Cleanup;
  439. }
  440. //
  441. // Call config API to indicate to DHCP service.
  442. //
  443. Error = DhcpNotifyConfigChange(
  444. L"",
  445. AdapterName,
  446. TRUE,
  447. 0, // AdapterIndex
  448. IpAddress,
  449. SubnetMask,
  450. DhcpDisable );
  451. Cleanup:
  452. if( RegKey != NULL ) {
  453. DhcpFreeMemory( RegKey );
  454. }
  455. if( KeyHandle != NULL ) {
  456. RegCloseKey( KeyHandle );
  457. }
  458. return(Error);
  459. }
  460. SetIpAddress(
  461. char **argv
  462. )
  463. {
  464. DWORD Error;
  465. DWORD IpAddress;
  466. DWORD SubnetMask;
  467. WCHAR AdapterNameBuffer[PATHLEN];
  468. LPWSTR AdapterName;
  469. DWORD AdapterIndex;
  470. IpAddress = inet_addr( argv[0] );
  471. SubnetMask = inet_addr( argv[1] );
  472. AdapterName = DhcpOemToUnicode( argv[2], AdapterNameBuffer );
  473. if( AdapterName == NULL ) {
  474. return( ERROR_NOT_ENOUGH_MEMORY );
  475. }
  476. AdapterIndex = atoi( argv[3] );
  477. Error = DhcpNotifyConfigChange(
  478. L"",
  479. AdapterName,
  480. TRUE,
  481. AdapterIndex,
  482. IpAddress,
  483. SubnetMask,
  484. IgnoreFlag );
  485. return( Error );
  486. }
  487. VOID
  488. PrintClientLeaseInfo(
  489. LPDHCP_LEASE_INFO LeaseInfo
  490. )
  491. {
  492. DWORD i;
  493. DWORD DataLength;
  494. LPBYTE Data;
  495. if( LeaseInfo == NULL ) {
  496. printf( "LeaseInfo is NULL .\n" );
  497. }
  498. printf("ClientInfo :\n");
  499. printf("\tClient Hardware Address = ");
  500. DataLength = LeaseInfo->ClientUID.ClientUIDLength;
  501. Data = LeaseInfo->ClientUID.ClientUID;
  502. for( i = 0; i < DataLength; i++ ) {
  503. printf("%.2lx", (DWORD)Data[i]);
  504. }
  505. printf( "\n" );
  506. printf("\tIP Address = %s.\n",
  507. DhcpIpAddressToDottedString(LeaseInfo->IpAddress));
  508. printf("\tSubnetMask = %s.\n",
  509. DhcpIpAddressToDottedString(LeaseInfo->SubnetMask));
  510. printf("\tDhcpServerAddress = %s.\n",
  511. DhcpIpAddressToDottedString(LeaseInfo->DhcpServerAddress));
  512. printf("\tLease = %ld secs.\n", LeaseInfo->Lease );
  513. printf("\tLease Obtained at %s", ctime(&LeaseInfo->LeaseObtained) );
  514. printf("\tT1 time is %s", ctime(&LeaseInfo->T1Time) );
  515. printf("\tT2 time is %s", ctime(&LeaseInfo->T2Time) );
  516. printf("\tLease Expires at %s", ctime(&LeaseInfo->LeaseExpires) );
  517. return;
  518. }
  519. DWORD
  520. DhcpTestLease(
  521. LPSTR AdapterIpAddressString,
  522. LPSTR HardwareAddressString
  523. )
  524. {
  525. #define MAX_ADDRESS_LENGTH 64 // 64 bytes
  526. DWORD Error;
  527. DWORD AdapterIpAddress;
  528. DHCP_CLIENT_UID ClientUID;
  529. BYTE Address[MAX_ADDRESS_LENGTH];
  530. DWORD i;
  531. LPDHCP_LEASE_INFO LeaseInfo = NULL;
  532. LPDHCP_OPTION_INFO OptionInfo;
  533. DHCP_OPTION_LIST OptionList;
  534. CHAR ch;
  535. AdapterIpAddress = ntohl( inet_addr( AdapterIpAddressString ) );
  536. ClientUID.ClientUIDLength = strlen(HardwareAddressString);
  537. if( ClientUID.ClientUIDLength % 2 != 0 ) {
  538. //
  539. // address must be even length.
  540. //
  541. printf("DhcpTestLease: Hardware address must be even length.\n");
  542. return( ERROR_INVALID_PARAMETER );
  543. }
  544. ClientUID.ClientUIDLength /= 2;
  545. if( ClientUID.ClientUIDLength > MAX_ADDRESS_LENGTH ) {
  546. printf("DhcpTestLease: hardware address is too long.\n");
  547. return( ERROR_INVALID_PARAMETER );
  548. }
  549. i = DhcpStringToHwAddress( (LPSTR)Address, HardwareAddressString );
  550. DhcpAssert( i == ClientUID.ClientUIDLength );
  551. ClientUID.ClientUID = Address;
  552. OptionList.NumOptions = 0;
  553. OptionList.OptionIDArray = NULL;
  554. Error = DhcpLeaseIpAddress(
  555. AdapterIpAddress, // any subnet.
  556. &ClientUID,
  557. 0, // desired address.
  558. &OptionList, // option list - not supported
  559. &LeaseInfo, // lease info returned.
  560. &OptionInfo); // option data returned.
  561. if( Error != ERROR_SUCCESS ) {
  562. printf("DhcpLeaseIpAddress failed, %ld.\n", Error);
  563. goto Cleanup;
  564. }
  565. PrintClientLeaseInfo( LeaseInfo );
  566. printf("Renew the lease ? (Y/N) ");
  567. do {
  568. ch = (CHAR)getchar();
  569. } while ( (ch != 'Y') && (ch != 'y') && (ch != 'N') && (ch != 'n') );
  570. printf("%c\n", ch);
  571. if( (ch == 'N') || (ch == 'n') ) {
  572. printf( "NOTE: YOU HAVE CONSUMED AN IP ADDRESS FROM "
  573. "THE DHCP SERVER. \n");
  574. Error = ERROR_SUCCESS;
  575. goto Cleanup;
  576. }
  577. Error = DhcpRenewIpAddressLease(
  578. AdapterIpAddress,
  579. LeaseInfo,
  580. &OptionList, // option list - not supported
  581. &OptionInfo); // option data returned.
  582. if( Error != ERROR_SUCCESS ) {
  583. printf("DhcpRenewIpAddressLease failed, %ld.\n", Error);
  584. goto Cleanup;
  585. }
  586. PrintClientLeaseInfo( LeaseInfo );
  587. printf("Release the lease ? (Y/N) ");
  588. do {
  589. ch = (CHAR)getchar();
  590. } while ( (ch != 'Y') && (ch != 'y') && (ch != 'N') && (ch != 'n') );
  591. printf("%c\n", ch);
  592. if( (ch == 'N') || (ch == 'n') ) {
  593. printf( "NOTE: YOU HAVE CONSUMED AN IP ADDRESS FROM "
  594. "THE DHCP SERVER. \n");
  595. Error = ERROR_SUCCESS;
  596. goto Cleanup;
  597. }
  598. Error = DhcpReleaseIpAddressLease(
  599. AdapterIpAddress,
  600. LeaseInfo );
  601. if( Error != ERROR_SUCCESS ) {
  602. printf("DhcpReleaseIpAddressLease failed, %ld.\n", Error);
  603. goto Cleanup;
  604. }
  605. Error = ERROR_SUCCESS;
  606. Cleanup:
  607. if( LeaseInfo != NULL ) {
  608. DhcpFreeMemory( LeaseInfo );
  609. }
  610. return( Error );
  611. }
  612. DWORD __cdecl
  613. main(
  614. int argc,
  615. char **argv
  616. )
  617. {
  618. DWORD error;
  619. WCHAR AdapterNameBuffer[PATHLEN];
  620. LPWSTR AdapterName;
  621. //
  622. // seed random generator.
  623. //
  624. srand( GetCurrentTime() );
  625. if ( argc < 2) {
  626. printf( USAGE_MESSAGE0 );
  627. return( 1 );
  628. }
  629. if ( _stricmp( argv[1], "renew" ) == 0 ) {
  630. if ( argc < 3) {
  631. printf( USAGE_MESSAGE1 );
  632. return( 1 );
  633. }
  634. AdapterName = DhcpOemToUnicode( argv[2], AdapterNameBuffer );
  635. if( AdapterName == NULL ) {
  636. error = ERROR_NOT_ENOUGH_MEMORY;
  637. }
  638. else {
  639. error = DhcpAcquireParameters( AdapterName );
  640. }
  641. } else if ( _stricmp( argv[1], "fbrefresh" ) == 0 ) {
  642. if ( argc < 3) {
  643. printf( USAGE_MESSAGE8 );
  644. return( 1 );
  645. }
  646. AdapterName = DhcpOemToUnicode( argv[2], AdapterNameBuffer );
  647. if( AdapterName == NULL ) {
  648. error = ERROR_NOT_ENOUGH_MEMORY;
  649. }
  650. else {
  651. error = DhcpFallbackRefreshParams( AdapterName );
  652. }
  653. } else if ( _stricmp( argv[1], "release" ) == 0 ) {
  654. if ( argc < 3) {
  655. printf( USAGE_MESSAGE2 );
  656. return( 1 );
  657. }
  658. AdapterName = DhcpOemToUnicode( argv[2], AdapterNameBuffer );
  659. if( AdapterName == NULL ) {
  660. error = ERROR_NOT_ENOUGH_MEMORY;
  661. }
  662. else {
  663. error = DhcpReleaseParameters( AdapterName );
  664. }
  665. } else if ( _stricmp( argv[1], "set" ) == 0 ) {
  666. if ( argc < 6) {
  667. printf( USAGE_MESSAGE3 );
  668. return( 1 );
  669. }
  670. error = SetIpAddress( &argv[2] );
  671. } else if ( _stricmp( argv[1], "enabledhcp" ) == 0 ) {
  672. if ( argc < 3) {
  673. printf( USAGE_MESSAGE4 );
  674. return( 1 );
  675. }
  676. error = EnableDhcp( &argv[2] );
  677. } else if ( _stricmp( argv[1], "disabledhcp" ) == 0 ) {
  678. if ( argc < 5) {
  679. printf( USAGE_MESSAGE5 );
  680. return( 1 );
  681. }
  682. error = DisableDhcp( &argv[2] );
  683. } else if ( _stricmp( argv[1], "timestamp" ) == 0 ) {
  684. time_t Time;
  685. char *endptr;
  686. if ( argc < 3) {
  687. printf( USAGE_MESSAGE6 );
  688. return( 1 );
  689. }
  690. Time = strtol( argv[2], &endptr, 0 );
  691. if( (endptr != NULL) && (*endptr != '\0') ) {
  692. printf( "Invalid Input, %s\n", endptr );
  693. return( 1 );
  694. }
  695. printf("TimeStamp = %s\n", ctime(&Time) );
  696. error = ERROR_SUCCESS;
  697. } else if ( _stricmp( argv[1], "leasetest" ) == 0 ) {
  698. if ( argc < 3) {
  699. printf( USAGE_MESSAGE7 );
  700. return( 1 );
  701. }
  702. error = DhcpTestLease( argv[2], argv[3] );
  703. } else if ( _stricmp( argv[1], "reinit" ) == 0 ) {
  704. if ( argc < 3) {
  705. printf( USAGE_MESSAGE9 );
  706. return( 1 );
  707. }
  708. AdapterName = DhcpOemToUnicode( argv[2], AdapterNameBuffer );
  709. if( AdapterName == NULL ) {
  710. error = ERROR_NOT_ENOUGH_MEMORY;
  711. }
  712. else {
  713. error = DhcpAcquireParametersByBroadcast( AdapterName );
  714. }
  715. } else {
  716. printf("Unknown function %s\n", argv[1] );
  717. error = ERROR_INVALID_FUNCTION;
  718. }
  719. if( error != ERROR_SUCCESS ) {
  720. printf("Result = %d\n", error );
  721. }
  722. else {
  723. printf("Command completed successfully.\n");
  724. }
  725. return(0);
  726. }
  727. #if DBG
  728. VOID
  729. DhcpPrintRoutine(
  730. IN DWORD DebugFlag,
  731. IN LPSTR Format,
  732. ...
  733. )
  734. {
  735. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  736. va_list arglist;
  737. char OutputBuffer[MAX_PRINTF_LEN];
  738. ULONG length = 0;
  739. //
  740. // Put a the information requested by the caller onto the line
  741. //
  742. va_start(arglist, Format);
  743. length += (ULONG) vsprintf(&OutputBuffer[length], Format, arglist);
  744. va_end(arglist);
  745. DhcpAssert(length <= MAX_PRINTF_LEN);
  746. //
  747. // Output to the debug terminal,
  748. //
  749. printf( "%s", OutputBuffer);
  750. }
  751. #endif // DBG