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.

470 lines
13 KiB

  1. /*++
  2. Copyright (C) 1999 Microsoft Corporation
  3. Module Name:
  4. mdhcptst.c
  5. --*/
  6. #include "precomp.h"
  7. // only when the api is defined!
  8. #include <dhcploc.h>
  9. #include <dhcppro.h>
  10. #include <mdhcpcli.h>
  11. #define CREATE_NEW_REQUEST_FROM_RESPONSE(pRequest, pResponse) { \
  12. (pRequest)->LeaseStartTime = (pRequest)->MaxLeaseStartTime = 0; \
  13. (pRequest)->LeaseDuration = (pRequest)->MinLeaseDuration = 0; \
  14. (pRequest)->MinAddrCount = (pRequest)->AddrCount = (pResponse)->AddrCount; \
  15. (pRequest)->ServerAddress = (pResponse)->ServerAddress; \
  16. memcpy((pRequest)->pAddrBuf, (pResponse)->pAddrBuf, sizeof (DWORD)*(pRequest)->AddrCount); \
  17. }
  18. typedef enum _cmd{
  19. EnumScope = 1,
  20. RequestAddr = 2,
  21. RenewAddr = 3,
  22. ReleaseAddr = 4,
  23. ExitLoop = 99
  24. } COMMAND;
  25. PMCAST_SCOPE_ENTRY gScopeList = NULL;
  26. DWORD gScopeCount = 0;
  27. LPMCAST_CLIENT_UID gAddrRequestId = NULL;
  28. typedef struct _LeaseEntry {
  29. IPNG_ADDRESS ScopeID;
  30. PMCAST_LEASE_RESPONSE pLeaseInfo;
  31. LPMCAST_CLIENT_UID pRequestID;
  32. LIST_ENTRY Linkage;
  33. } LEASE_ENTRY, *PLEASE_ENTRY;
  34. LIST_ENTRY gLeaseList;
  35. void
  36. InitializeGlobalData()
  37. {
  38. InitializeListHead(&gLeaseList);
  39. return;
  40. }
  41. void
  42. DisplayMenu()
  43. {
  44. printf("MDHCP Test Menu\n");
  45. printf("===============\n");
  46. printf("[1] - Enumerate Scopes:\n");
  47. printf("[2] - Request Address:\n");
  48. printf("[3] - Renew Address:\n");
  49. printf("[4] - Release Address: \n");
  50. printf("[99] - Exit:\n");
  51. }
  52. int
  53. GetCommand()
  54. {
  55. DWORD cmd;
  56. printf("Enter choice:");
  57. if(scanf("%d",&cmd)) return cmd;
  58. return 0;
  59. }
  60. DWORD
  61. EnumScopes()
  62. {
  63. DWORD Error;
  64. DWORD BufLen;
  65. if ( gScopeList ) {
  66. DhcpFreeMemory( gScopeList );
  67. gScopeList = NULL;
  68. }
  69. Error = McastEnumerateScopes(
  70. AF_INET,
  71. TRUE,
  72. NULL,
  73. &BufLen,
  74. &gScopeCount
  75. );
  76. if (ERROR_SUCCESS != Error) {
  77. printf("Could not get the scope buf length, %ld\n",Error );
  78. return Error;
  79. }
  80. gScopeList = DhcpAllocateMemory( BufLen );
  81. Error = McastEnumerateScopes(
  82. AF_INET,
  83. FALSE,
  84. gScopeList,
  85. &BufLen,
  86. &gScopeCount);
  87. if (ERROR_SUCCESS != Error) {
  88. printf("Could not get the scope list- 2nd call, %ld\n",Error );
  89. DhcpFreeMemory( gScopeList );
  90. gScopeList = NULL;
  91. return Error;
  92. }
  93. DhcpAssert( gScopeCount > 0 );
  94. return Error;
  95. }
  96. void
  97. DisplayScopes()
  98. {
  99. DWORD i;
  100. PMCAST_SCOPE_ENTRY pScope = gScopeList;
  101. for (i = 0;i<gScopeCount; i++,pScope++) {
  102. printf("[%d] - ScopeId %lx, LastAddr %lx, ttl %d, Name %ws\n",
  103. i+1,ntohl(pScope->ScopeCtx.ScopeID.IpAddrV4),
  104. ntohl(pScope->LastAddr.IpAddrV4), pScope->TTL, pScope->ScopeDesc.Buffer);
  105. }
  106. return;
  107. }
  108. void
  109. PrintLeaseInfo(PMCAST_LEASE_RESPONSE pLeaseInfo, BOOL Verbose )
  110. {
  111. DHCP_IP_ADDRESS IpAddress = *(DWORD UNALIGNED *)pLeaseInfo->pAddrBuf;
  112. time_t tempTime;
  113. printf("Obtained IPAddress - %s\n",inet_ntoa(*(struct in_addr *)&IpAddress));
  114. if ( Verbose ) {
  115. tempTime = pLeaseInfo->LeaseEndTime;
  116. printf("Expires - %.19s", asctime(localtime(&tempTime)));
  117. }
  118. }
  119. VOID
  120. DisplayCurrentLeases()
  121. {
  122. PLEASE_ENTRY pLeaseEntry;
  123. PLIST_ENTRY p;
  124. DWORD i;
  125. for (p = gLeaseList.Flink,i=1; p != &gLeaseList; p = p->Flink,i++ ) {
  126. pLeaseEntry = CONTAINING_RECORD(p, LEASE_ENTRY, Linkage);
  127. printf("[%d] ", i);PrintLeaseInfo( pLeaseEntry->pLeaseInfo, FALSE );
  128. }
  129. return;
  130. }
  131. PLEASE_ENTRY
  132. SelectFromCurrentLease(COMMAND cmd)
  133. {
  134. PLIST_ENTRY p;
  135. DWORD index;
  136. DWORD i;
  137. PLEASE_ENTRY pLeaseEntry = NULL;
  138. if (cmd != RenewAddr && cmd != ReleaseAddr) {
  139. DhcpAssert( FALSE );
  140. }
  141. printf("CURRENT LEASE ASSIGNMENTS\n");
  142. printf("-------------------------\n");
  143. DisplayCurrentLeases();
  144. printf("Select the lease you want to %s\n", RenewAddr == cmd ? "Renew" : "Release" );
  145. index = GetCommand();
  146. if ( !index ) {
  147. printf("Lease index invalid\n");
  148. return NULL;
  149. }
  150. for (p = gLeaseList.Flink,i=0; p != &gLeaseList; p = p->Flink ) {
  151. if (++i == index) {
  152. pLeaseEntry = CONTAINING_RECORD(p, LEASE_ENTRY, Linkage);
  153. return pLeaseEntry;
  154. }
  155. }
  156. printf("Error:invalid selection, choose the index from the list of the leases above\n");
  157. return NULL;
  158. }
  159. DWORD
  160. ReleaseAddress()
  161. {
  162. PLEASE_ENTRY pLeaseEntry;
  163. PMCAST_LEASE_REQUEST AddrRequest;
  164. DWORD Error;
  165. DWORD i;
  166. pLeaseEntry = SelectFromCurrentLease(ReleaseAddr);
  167. if (!pLeaseEntry) {
  168. return ERROR_FILE_NOT_FOUND;
  169. }
  170. AddrRequest = DhcpAllocateMemory( sizeof( *AddrRequest ) + sizeof(DHCP_IP_ADDRESS));
  171. if (!AddrRequest) {
  172. printf("Failed to allocate lease info struct\n");
  173. return ERROR_NOT_ENOUGH_MEMORY;
  174. }
  175. RtlZeroMemory(AddrRequest, sizeof(*AddrRequest) + sizeof(DHCP_IP_ADDRESS));
  176. AddrRequest->pAddrBuf = (PBYTE) AddrRequest + sizeof(*AddrRequest);
  177. CREATE_NEW_REQUEST_FROM_RESPONSE(AddrRequest, pLeaseEntry->pLeaseInfo);
  178. Error = McastReleaseAddress(
  179. AF_INET,
  180. pLeaseEntry->pRequestID,
  181. AddrRequest
  182. );
  183. if (ERROR_SUCCESS == Error ) {
  184. printf("Lease Released successfully\n");
  185. RemoveEntryList( &pLeaseEntry->Linkage );
  186. // free the old lease structure.
  187. DhcpFreeMemory( pLeaseEntry->pRequestID );
  188. DhcpFreeMemory( pLeaseEntry->pLeaseInfo );
  189. }
  190. DhcpFreeMemory(AddrRequest);
  191. return Error;
  192. }
  193. DWORD
  194. RenewAddress()
  195. {
  196. PLEASE_ENTRY pLeaseEntry;
  197. PMCAST_LEASE_REQUEST AddrRequest;
  198. PMCAST_LEASE_RESPONSE AddrResponse;
  199. PMCAST_LEASE_RESPONSE pLeaseInfo;
  200. PMCAST_SCOPE_ENTRY Scope;
  201. DWORD Error;
  202. DWORD i;
  203. pLeaseEntry = SelectFromCurrentLease(RenewAddr);
  204. if (!pLeaseEntry) {
  205. return ERROR_FILE_NOT_FOUND;
  206. }
  207. pLeaseInfo = pLeaseEntry->pLeaseInfo;
  208. // find the scope ctx for this scope id.
  209. if (pLeaseEntry->ScopeID.IpAddrV4) {
  210. for (i=0;i<gScopeCount;i++) {
  211. if (pLeaseEntry->ScopeID.IpAddrV4 == gScopeList[i].ScopeCtx.ScopeID.IpAddrV4) {
  212. Scope = &gScopeList[i];
  213. break;
  214. }
  215. }
  216. if (i >= gScopeCount) {
  217. printf("Could not find the scope ctx for the scope id %ld of this address\n",pLeaseEntry->ScopeID.IpAddrV4);
  218. return ERROR_FILE_NOT_FOUND;
  219. }
  220. } else {
  221. // default scope
  222. Scope = NULL;
  223. }
  224. AddrRequest = DhcpAllocateMemory( sizeof( *AddrRequest ) + sizeof(DHCP_IP_ADDRESS));
  225. if (!AddrRequest) {
  226. printf("Failed to allocate lease info struct\n");
  227. return ERROR_NOT_ENOUGH_MEMORY;
  228. }
  229. RtlZeroMemory(AddrRequest, sizeof(*AddrRequest) + sizeof(DHCP_IP_ADDRESS));
  230. AddrRequest->pAddrBuf = (PBYTE) AddrRequest + sizeof(*AddrRequest);
  231. CREATE_NEW_REQUEST_FROM_RESPONSE(AddrRequest, pLeaseInfo);
  232. AddrResponse = DhcpAllocateMemory( sizeof( *AddrResponse ) + sizeof(DHCP_IP_ADDRESS));
  233. if (!AddrResponse) {
  234. printf("Failed to allocate lease info struct\n");
  235. return ERROR_NOT_ENOUGH_MEMORY;
  236. }
  237. RtlZeroMemory(AddrResponse, sizeof(*AddrResponse) + sizeof(DHCP_IP_ADDRESS));
  238. AddrResponse->pAddrBuf = (PBYTE) AddrResponse + sizeof(*AddrResponse);
  239. AddrResponse->AddrCount = 1;
  240. Error = McastRenewAddress(
  241. AF_INET,
  242. pLeaseEntry->pRequestID,
  243. AddrRequest,
  244. AddrResponse
  245. );
  246. if (ERROR_SUCCESS == Error ) {
  247. printf("Lease Renew'd successfully\n");
  248. PrintLeaseInfo( AddrResponse, TRUE );
  249. pLeaseEntry->pLeaseInfo = AddrResponse;
  250. // free the old lease structure.
  251. DhcpFreeMemory( pLeaseInfo );
  252. } else {
  253. DhcpFreeMemory( AddrResponse );
  254. }
  255. DhcpFreeMemory( AddrRequest );
  256. return Error;
  257. }
  258. DWORD
  259. RequestAddress()
  260. {
  261. PMCAST_LEASE_REQUEST AddrRequest;
  262. PMCAST_LEASE_RESPONSE AddrResponse;
  263. LPMCAST_CLIENT_UID pRequestID;
  264. DWORD index;
  265. DWORD Error;
  266. PLEASE_ENTRY pLeaseEntry;
  267. DisplayScopes();
  268. printf("Select the scope entry(0 for default scope)\n");
  269. index = GetCommand();
  270. if ( index > gScopeCount ) {
  271. printf("Scope index out of range\n");
  272. return ERROR_INVALID_PARAMETER;
  273. }
  274. AddrResponse = DhcpAllocateMemory( sizeof( *AddrResponse ) + sizeof(DHCP_IP_ADDRESS));
  275. if (!AddrResponse) {
  276. printf("Failed to allocate lease info struct\n");
  277. return ERROR_NOT_ENOUGH_MEMORY;
  278. }
  279. RtlZeroMemory(AddrResponse, sizeof(*AddrResponse)+ sizeof(DHCP_IP_ADDRESS));
  280. AddrResponse->pAddrBuf = (PBYTE) AddrResponse + sizeof(*AddrResponse);
  281. AddrRequest = DhcpAllocateMemory( sizeof( *AddrRequest ));
  282. if (!AddrRequest) {
  283. printf("Failed to allocate lease info struct\n");
  284. return ERROR_NOT_ENOUGH_MEMORY;
  285. }
  286. RtlZeroMemory(AddrRequest, sizeof(*AddrRequest));
  287. AddrRequest->AddrCount = AddrResponse->AddrCount = 1;
  288. pRequestID = DhcpAllocateMemory(sizeof(*pRequestID) + MCAST_CLIENT_ID_LEN);
  289. if (!pRequestID) {
  290. return ERROR_NOT_ENOUGH_MEMORY;
  291. }
  292. pRequestID->ClientUID = (char *)pRequestID + sizeof(*pRequestID);
  293. pRequestID->ClientUIDLength = MCAST_CLIENT_ID_LEN;
  294. Error = McastGenUID( pRequestID );
  295. DhcpAssert( ERROR_SUCCESS == Error );
  296. Error = McastRequestAddress(
  297. AF_INET,
  298. pRequestID,
  299. index ? &gScopeList[index-1].ScopeCtx : NULL,
  300. AddrRequest,
  301. AddrResponse
  302. );
  303. if (ERROR_SUCCESS == Error ) {
  304. printf("Lease obtained successfully\n");
  305. PrintLeaseInfo( AddrResponse, TRUE );
  306. // now copy this lease into our global structure.
  307. pLeaseEntry = DhcpAllocateMemory( sizeof(LEASE_ENTRY) );
  308. if (!pLeaseEntry) {
  309. printf("Failed to allocate lease entry item\n");
  310. return ERROR_NOT_ENOUGH_MEMORY;
  311. }
  312. pLeaseEntry->pLeaseInfo = AddrResponse;
  313. pLeaseEntry->ScopeID.IpAddrV4 = index ? gScopeList[index-1].ScopeCtx.ScopeID.IpAddrV4 : 0;
  314. pLeaseEntry->pRequestID = pRequestID;
  315. InsertTailList(&gLeaseList, &pLeaseEntry->Linkage);
  316. } else {
  317. DhcpFreeMemory( pRequestID );
  318. DhcpFreeMemory( AddrResponse );
  319. }
  320. DhcpFreeMemory( AddrRequest );
  321. return Error;
  322. }
  323. int
  324. __cdecl
  325. main( int argc, char *argv[])
  326. {
  327. DWORD cmd;
  328. DWORD Error;
  329. DWORD Version;
  330. InitializeGlobalData();
  331. Version = MCAST_API_CURRENT_VERSION;
  332. if (ERROR_SUCCESS != McastApiStartup(&Version)) {
  333. printf("Current version %d not supported, Api Impl version %d\n",
  334. MCAST_API_CURRENT_VERSION, Version);
  335. return 0;
  336. }
  337. DisplayMenu();
  338. while(cmd = GetCommand() ){
  339. switch (cmd) {
  340. case EnumScope:
  341. Error = EnumScopes();
  342. if (ERROR_SUCCESS != Error ) {
  343. printf("Enumerate Scope returned failures, %ld\n",Error);
  344. } else {
  345. DisplayScopes();
  346. }
  347. break;
  348. case RequestAddr:
  349. Error = RequestAddress();
  350. if (ERROR_SUCCESS != Error ) {
  351. printf("RequestAddress returned failures, %ld\n",Error);
  352. }
  353. break;
  354. case RenewAddr:
  355. Error = RenewAddress();
  356. if (ERROR_SUCCESS != Error ) {
  357. printf("RenewAddress returned failures, %ld\n",Error);
  358. }
  359. break;
  360. case ReleaseAddr:
  361. Error = ReleaseAddress();
  362. if (ERROR_SUCCESS != Error ) {
  363. printf("ReleaseAddress returned failures, %ld\n",Error);
  364. }
  365. break;
  366. case ExitLoop:
  367. printf("Exiting\n");
  368. return 0;
  369. default:
  370. printf("invalid choice\n");
  371. break;
  372. }
  373. DisplayMenu();
  374. }
  375. return 0;
  376. }
  377. #if DBG
  378. VOID
  379. DhcpPrintRoutine(
  380. IN DWORD DebugFlag,
  381. IN LPSTR Format,
  382. ...
  383. )
  384. {
  385. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  386. va_list arglist;
  387. char OutputBuffer[MAX_PRINTF_LEN];
  388. ULONG length = 0;
  389. //
  390. // Put a the information requested by the caller onto the line
  391. //
  392. va_start(arglist, Format);
  393. length += (ULONG) vsprintf(&OutputBuffer[length], Format, arglist);
  394. va_end(arglist);
  395. DhcpAssert(length <= MAX_PRINTF_LEN);
  396. //
  397. // Output to the debug terminal,
  398. //
  399. printf( "%s", OutputBuffer);
  400. }
  401. #endif // DBG