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.

176 lines
4.0 KiB

  1. //==================================================================
  2. // Copyright (C) Microsoft 1998.
  3. // Author: RameshV
  4. // Desription: test program to test the lease APIs for RAS.
  5. //===================================================================
  6. #include "precomp.h"
  7. #include <dhcploc.h>
  8. #include <dhcppro.h>
  9. DHCP_OPTION_LIST DhcpOptList;
  10. DWORD AdapterAddress;
  11. DWORD Error;
  12. DHCP_CLIENT_UID ClientUID;
  13. LPDHCP_LEASE_INFO LeaseInfo;
  14. DHCP_LEASE_INFO LeaseInfo2;
  15. LPDHCP_OPTION_INFO OptionInfo;
  16. void
  17. PrintLeaseInfo(
  18. LPDHCP_LEASE_INFO LeaseInfo
  19. )
  20. {
  21. DWORD Address, Mask, ServerAddress;
  22. if( NULL == LeaseInfo ) {
  23. printf("LeaseInfo = NULL\n");
  24. return;
  25. }
  26. Address = htonl(LeaseInfo->IpAddress);
  27. Mask = htonl(LeaseInfo->SubnetMask);
  28. ServerAddress = htonl(LeaseInfo->DhcpServerAddress);
  29. printf("LeaseInfo: \n");
  30. printf("\tIpAddress: %s\n", inet_ntoa(*(struct in_addr *)&Address));
  31. printf("\tSubnetMask: %s\n", inet_ntoa(*(struct in_addr *)&Mask));
  32. printf("\tDhcpServerAddress: %s\n", inet_ntoa(*(struct in_addr *)&ServerAddress));
  33. printf("\tLease: 0x%lx\n", LeaseInfo->Lease);
  34. }
  35. void
  36. AcquireX(LPSTR clientid)
  37. {
  38. ClientUID.ClientUIDLength = strlen(clientid);
  39. ClientUID.ClientUID = clientid;
  40. Error = DhcpLeaseIpAddress(
  41. AdapterAddress,
  42. &ClientUID,
  43. 0,
  44. NULL,
  45. &LeaseInfo,
  46. &OptionInfo
  47. );
  48. if( ERROR_SUCCESS != Error ) {
  49. printf("DhcpLeaseIpAddress: 0x%lx (%ld)\n", Error, Error);
  50. return;
  51. }
  52. PrintLeaseInfo(LeaseInfo);
  53. }
  54. void
  55. ReleaseOrRenew(BOOL fRelease, DWORD ipaddr, DWORD servaddr, LPSTR clientid)
  56. {
  57. ClientUID.ClientUIDLength = strlen(clientid);
  58. ClientUID.ClientUID = clientid;
  59. AcquireX(clientid);
  60. if( fRelease ) {
  61. Error = DhcpReleaseIpAddressLease(
  62. AdapterAddress,
  63. LeaseInfo
  64. );
  65. } else {
  66. Error = DhcpRenewIpAddressLease(
  67. AdapterAddress,
  68. LeaseInfo,
  69. NULL,
  70. &OptionInfo
  71. );
  72. }
  73. if( ERROR_SUCCESS != Error ) {
  74. printf("Error; 0x%lx (%ld)\n", Error, Error);
  75. return;
  76. }
  77. printf("After the renew: \n");
  78. PrintLeaseInfo(LeaseInfo);
  79. }
  80. void
  81. ReleaseX(DWORD ipaddr, DWORD servaddr, LPSTR clientid)
  82. {
  83. ReleaseOrRenew(TRUE, ipaddr, servaddr, clientid);
  84. }
  85. void
  86. RenewX(DWORD ipaddr, DWORD servaddr, LPSTR clientid)
  87. {
  88. ReleaseOrRenew(FALSE, ipaddr, servaddr, clientid);
  89. }
  90. void
  91. Release(
  92. int argc,
  93. char *argv[]
  94. )
  95. {
  96. if( argc != 3 ) {
  97. printf("usage: Release ip-address server-address client-id-string\n");
  98. return;
  99. }
  100. ReleaseX(inet_addr(argv[0]), inet_addr(argv[1]), argv[2]);
  101. }
  102. void
  103. Renew(
  104. int argc,
  105. char *argv[]
  106. )
  107. {
  108. if( argc != 3 ) {
  109. printf("usage: Renew ip-address server-address client-id-string\n");
  110. return;
  111. }
  112. RenewX(inet_addr(argv[0]), inet_addr(argv[1]), argv[2]);
  113. }
  114. void
  115. Acquire(
  116. int argc,
  117. char * argv[]
  118. )
  119. {
  120. if( argc != 1 ) {
  121. printf("usage: acquire client-id-string\n");
  122. return;
  123. }
  124. AcquireX(argv[0]);
  125. }
  126. void _cdecl main(int argc, char *argv[]) {
  127. WSADATA WsaData;
  128. WSAStartup( 0x0101, &WsaData );
  129. if( argc < 3 ) {
  130. printf("Usage: %s [adapter-address] cmd <options>"
  131. "\n\t where cmd is one of Release,Acquire,Renew\n", argv[0]);
  132. return;
  133. }
  134. AdapterAddress = htonl(inet_addr(argv[1]));
  135. argv ++; argc --;
  136. if( 0 == _stricmp(argv[1], "Release") ) {
  137. Release(argc-2, &argv[2]);
  138. } else if( 0 == _stricmp(argv[1], "Renew") ) {
  139. Renew(argc-2, &argv[2]);
  140. } else if( 0 == _stricmp(argv[1], "Acquire") ) {
  141. Acquire(argc-2, &argv[2]);
  142. } else {
  143. printf("Usage: %s cmd <options>"
  144. "\n\t where cmd is one of Release,Acquire,Renew\n", argv[0]);
  145. }
  146. }