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.

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