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.

1478 lines
36 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dnsrslvr.h>
  8. extern LPWSTR NetworkAddress;
  9. #define COMPUTE_STRING_HASH_2( _String, _ulHashTableSize, _lpulHash ) \
  10. { \
  11. PWCHAR _p = _String; \
  12. PWCHAR _ep = _p + wcslen( _String ); \
  13. ULONG h = 0; \
  14. \
  15. while( _p < _ep ) \
  16. { \
  17. h <<= 1; \
  18. h ^= *_p++; \
  19. } \
  20. \
  21. *_lpulHash = h % _ulHashTableSize; \
  22. }
  23. VOID
  24. GetStringA( char * );
  25. VOID
  26. GetStringW( WCHAR * );
  27. VOID
  28. PrintMenu( VOID );
  29. VOID
  30. DoFlushCache( VOID );
  31. VOID
  32. DoFlushCacheEntry( VOID );
  33. VOID
  34. DoFlushCacheEntryForType( VOID );
  35. VOID
  36. DoTrimCache( VOID );
  37. VOID
  38. DoReadCacheEntry( VOID );
  39. VOID
  40. DoQuery( VOID );
  41. VOID
  42. DoQueryThenCache( VOID );
  43. VOID
  44. DoDisplayCache( VOID );
  45. VOID
  46. DoGetAdapterInfo( VOID );
  47. VOID
  48. DoGetSearchList( VOID );
  49. VOID
  50. DoGetPrimaryDomainName( VOID );
  51. VOID
  52. DoGetIpAddressList( VOID );
  53. VOID
  54. DoGetHashTableStats( VOID );
  55. VOID
  56. DoGetHashTableIndex( VOID );
  57. VOID
  58. DoUpdateTest( VOID );
  59. VOID
  60. PrintServerInfo( PDNS_RPC_SERVER_INFO );
  61. VOID
  62. PrintIpAddress ( DWORD IpAddress );
  63. VOID
  64. PrintRecords (
  65. IN PDNS_RECORD pDnsRecord );
  66. VOID
  67. PrintRecord (
  68. IN PDNS_RECORD pDnsRecord );
  69. VOID
  70. PrintARecord (
  71. IN PDNS_RECORD pDnsRecord );
  72. VOID
  73. PrintSOARecord (
  74. IN PDNS_RECORD pDnsRecord );
  75. VOID
  76. PrintPTRRecord (
  77. IN PDNS_RECORD pDnsRecord );
  78. VOID
  79. PrintMINFORecord (
  80. IN PDNS_RECORD pDnsRecord );
  81. VOID
  82. PrintMXRecord (
  83. IN PDNS_RECORD pDnsRecord );
  84. VOID
  85. PrintHINFORecord (
  86. IN PDNS_RECORD pDnsRecord );
  87. VOID
  88. PrintNULLRecord (
  89. IN PDNS_RECORD pDnsRecord );
  90. VOID
  91. PrintWKSRecord (
  92. IN PDNS_RECORD pDnsRecord );
  93. VOID
  94. PrintAAAARecord (
  95. IN PDNS_RECORD pDnsRecord );
  96. VOID
  97. PrintSRVRecord (
  98. IN PDNS_RECORD pDnsRecord );
  99. VOID
  100. PrintWINSRecord (
  101. IN PDNS_RECORD pDnsRecord );
  102. VOID
  103. PrintWINSRRecord (
  104. IN PDNS_RECORD pDnsRecord );
  105. VOID
  106. GetCachedData(
  107. IN LPWSTR Name,
  108. IN WORD Type );
  109. _cdecl
  110. main(int argc, char **argv)
  111. {
  112. char String[256];
  113. WCHAR usNetName[100];
  114. LONG cch;
  115. DWORD Status = NO_ERROR;
  116. if ( argc == 2 )
  117. {
  118. cch = MultiByteToWideChar( CP_ACP,
  119. 0L,
  120. argv[1],
  121. -1,
  122. usNetName,
  123. 100 );
  124. if ( !cch )
  125. {
  126. Status = GetLastError();
  127. printf("Error %ux in MultiByteToWideChar\n", Status );
  128. return (Status);
  129. }
  130. NetworkAddress = usNetName;
  131. }
  132. Menu :
  133. PrintMenu();
  134. GetStringA( String );
  135. printf( "\n" );
  136. switch( atoi( String ) )
  137. {
  138. case 1 :
  139. DoFlushCache();
  140. break;
  141. case 2 :
  142. DoFlushCacheEntry();
  143. break;
  144. case 3 :
  145. DoFlushCacheEntryForType();
  146. break;
  147. case 4 :
  148. DoTrimCache();
  149. break;
  150. case 5 :
  151. DoReadCacheEntry();
  152. break;
  153. case 6 :
  154. DoQuery();
  155. break;
  156. case 7 :
  157. DoQueryThenCache();
  158. break;
  159. case 8 :
  160. DoDisplayCache();
  161. break;
  162. case 9 :
  163. DoUpdateTest();
  164. break;
  165. case 10 :
  166. DoGetAdapterInfo();
  167. break;
  168. case 11 :
  169. DoGetSearchList();
  170. break;
  171. case 12 :
  172. DoGetPrimaryDomainName();
  173. break;
  174. case 13 :
  175. DoGetIpAddressList();
  176. break;
  177. case 14 :
  178. DoGetHashTableStats();
  179. break;
  180. case 15 :
  181. DoGetHashTableIndex();
  182. break;
  183. case 16 :
  184. return( -1 );
  185. default :
  186. printf( "Invalid option\n" );
  187. }
  188. goto Menu;
  189. }
  190. VOID
  191. GetStringA( char * String )
  192. {
  193. WORD iter = 0;
  194. char ch = getchar();
  195. while ( ch != 0x0a )
  196. {
  197. String[iter] = ch;
  198. ch = getchar();
  199. iter++;
  200. }
  201. String[iter] = 0;
  202. }
  203. VOID
  204. GetStringW( WCHAR * String )
  205. {
  206. WORD iter = 0;
  207. WCHAR ch = getchar();
  208. while ( ch != 0x0a )
  209. {
  210. String[iter] = ch;
  211. ch = getchar();
  212. iter++;
  213. }
  214. String[iter] = 0;
  215. }
  216. VOID
  217. PrintMenu( VOID )
  218. {
  219. printf( "\n" );
  220. printf( "------------------------------------------------------\n" );
  221. printf( "| DNS Caching Resolver Service Client |\n" );
  222. printf( "------------------------------------------------------\n" );
  223. printf( "| |\n" );
  224. printf( "| 1) Flush cache |\n" );
  225. printf( "| 2) Flush a specific cache entry |\n" );
  226. printf( "| 3) Flush a specific record set from a cache entry |\n" );
  227. printf( "| 4) Trim cache (gets rid of records with old ttls) |\n" );
  228. printf( "| 5) Read a cache entry's record set |\n" );
  229. printf( "| 6) Query records for a DNS name and type |\n" );
  230. printf( "| 7) Query records then CacheRecordSet |\n" );
  231. printf( "| 8) Display cache contents |\n" );
  232. printf( "| 9) Perform update test with local machine account |\n" );
  233. printf( "| 10) Get the net adapter info for this machine |\n" );
  234. printf( "| 11) Get the DNS search list for this machine |\n" );
  235. printf( "| 12) Get the Primary Domain Name for this machine |\n" );
  236. printf( "| 13) Get the list of IP addresses for this machine |\n" );
  237. printf( "| 14) Get the DNS resolver cache statistics |\n" );
  238. printf( "| 15) Compute the hash table index for a name |\n" );
  239. printf( "| 16) Quit |\n" );
  240. printf( "| |\n" );
  241. printf( ">>> " );
  242. }
  243. VOID
  244. PrintIpAddress ( DWORD IpAddress )
  245. {
  246. printf( " %d.%d.%d.%d\n",
  247. ((BYTE *) &IpAddress)[0],
  248. ((BYTE *) &IpAddress)[1],
  249. ((BYTE *) &IpAddress)[2],
  250. ((BYTE *) &IpAddress)[3] );
  251. }
  252. VOID
  253. DoFlushCache( VOID )
  254. {
  255. DWORD Status = NO_ERROR;
  256. RpcTryExcept
  257. {
  258. CRrFlushCache( NULL );
  259. }
  260. RpcExcept(1)
  261. {
  262. Status = RpcExceptionCode();
  263. }
  264. RpcEndExcept
  265. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  266. Status == RPC_S_UNKNOWN_IF )
  267. {
  268. printf( "Error: DNS Caching Resolver Service is not running\n" );
  269. return;
  270. }
  271. }
  272. VOID
  273. DoFlushCacheEntry( VOID )
  274. {
  275. DWORD Status = NO_ERROR;
  276. WCHAR Name[256];
  277. printf( "Name: " );
  278. GetStringW( Name );
  279. printf( "\n" );
  280. RpcTryExcept
  281. {
  282. CRrFlushCacheEntry( NULL, Name );
  283. }
  284. RpcExcept(1)
  285. {
  286. Status = RpcExceptionCode();
  287. }
  288. RpcEndExcept
  289. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  290. Status == RPC_S_UNKNOWN_IF )
  291. {
  292. printf( "Error: DNS Caching Resolver Service is not running\n" );
  293. return;
  294. }
  295. }
  296. VOID
  297. DoFlushCacheEntryForType( VOID )
  298. {
  299. DWORD Status = NO_ERROR;
  300. WCHAR Name[256];
  301. char TypeStr[25];
  302. WORD Type;
  303. printf( "Name: " );
  304. GetStringW( Name );
  305. printf( "\n" );
  306. printf( "Type (0, 1, 2, etc): " );
  307. GetStringA( TypeStr );
  308. printf( "\n" );
  309. Type = atoi( TypeStr );
  310. RpcTryExcept
  311. {
  312. CRrFlushCacheEntryForType( NULL, Name, Type );
  313. }
  314. RpcExcept(1)
  315. {
  316. Status = RpcExceptionCode();
  317. }
  318. RpcEndExcept
  319. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  320. Status == RPC_S_UNKNOWN_IF )
  321. {
  322. printf( "Error: DNS Caching Resolver Service is not running\n" );
  323. return;
  324. }
  325. }
  326. VOID
  327. DoTrimCache( VOID )
  328. {
  329. DWORD Status = NO_ERROR;
  330. RpcTryExcept
  331. {
  332. CRrTrimCache( NULL );
  333. }
  334. RpcExcept(1)
  335. {
  336. Status = RpcExceptionCode();
  337. }
  338. RpcEndExcept
  339. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  340. Status == RPC_S_UNKNOWN_IF )
  341. {
  342. printf( "Error: DNS Caching Resolver Service is not running\n" );
  343. return;
  344. }
  345. }
  346. VOID
  347. DoReadCacheEntry( VOID )
  348. {
  349. DNS_STATUS DnsStatus = NO_ERROR;
  350. WCHAR Name[256];
  351. char TypeStr[25];
  352. WORD Type;
  353. PDNS_RECORD pDNSRecord = NULL;
  354. printf( "Name: " );
  355. GetStringW( Name );
  356. printf( "\n" );
  357. printf( "Type (0, 1, 2, etc): " );
  358. GetStringA( TypeStr );
  359. printf( "\n" );
  360. Type = atoi( TypeStr );
  361. RpcTryExcept
  362. {
  363. DnsStatus = CRrReadCacheEntry( NULL, Name, Type, &pDNSRecord );
  364. }
  365. RpcExcept(1)
  366. {
  367. DnsStatus = RpcExceptionCode();
  368. }
  369. RpcEndExcept
  370. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  371. DnsStatus == RPC_S_UNKNOWN_IF )
  372. {
  373. printf( "Error: DNS Caching Resolver Service is not running\n" );
  374. return;
  375. }
  376. if ( DnsStatus )
  377. {
  378. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  379. printf( "Error: 0x%.8x (%s)\n",
  380. DnsStatus, ErrorString );
  381. return;
  382. }
  383. PrintRecords ( ( PDNS_RECORD ) pDNSRecord );
  384. DnsFreeRRSet( (PDNS_RECORD) pDNSRecord, TRUE );
  385. }
  386. VOID
  387. DoQuery( VOID )
  388. {
  389. DNS_STATUS DnsStatus = NO_ERROR;
  390. WCHAR Name[256];
  391. char TypeStr[25];
  392. WORD Type;
  393. PDNS_RECORD pDNSRecord = NULL;
  394. printf( "Name: " );
  395. GetStringW( Name );
  396. printf( "\n" );
  397. printf( "Type (0, 1, 2, etc): " );
  398. GetStringA( TypeStr );
  399. printf( "\n" );
  400. Type = atoi( TypeStr );
  401. RpcTryExcept
  402. {
  403. DnsStatus = CRrQuery( NULL,
  404. Name,
  405. Type,
  406. 0,
  407. &pDNSRecord );
  408. }
  409. RpcExcept(1)
  410. {
  411. DnsStatus = RpcExceptionCode();
  412. }
  413. RpcEndExcept
  414. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  415. DnsStatus == RPC_S_UNKNOWN_IF )
  416. {
  417. printf( "Error: DNS Caching Resolver Service is not running\n" );
  418. return;
  419. }
  420. if ( DnsStatus )
  421. {
  422. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  423. printf( "Error: 0x%.8x (%s)\n",
  424. DnsStatus, ErrorString );
  425. return;
  426. }
  427. PrintRecords ( ( PDNS_RECORD ) pDNSRecord );
  428. DnsFreeRRSet( (PDNS_RECORD) pDNSRecord, TRUE );
  429. }
  430. VOID
  431. DoQueryThenCache( VOID )
  432. {
  433. DNS_STATUS DnsStatus = NO_ERROR;
  434. WCHAR Name[256];
  435. char TypeStr[25];
  436. WORD Type;
  437. PDNS_RECORD pDNSRecord = NULL;
  438. printf( "Name: " );
  439. GetStringW( Name );
  440. printf( "\n" );
  441. printf( "Type (0, 1, 2, etc): " );
  442. GetStringA( TypeStr );
  443. printf( "\n" );
  444. Type = atoi( TypeStr );
  445. RpcTryExcept
  446. {
  447. DnsStatus = CRrQuery( NULL,
  448. Name,
  449. Type,
  450. 0,
  451. &pDNSRecord );
  452. }
  453. RpcExcept(1)
  454. {
  455. DnsStatus = RpcExceptionCode();
  456. }
  457. RpcEndExcept
  458. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  459. DnsStatus == RPC_S_UNKNOWN_IF )
  460. {
  461. printf( "Error: DNS Caching Resolver Service is not running\n" );
  462. return;
  463. }
  464. if ( DnsStatus )
  465. {
  466. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  467. printf( "CRrQuery returned error: 0x%.8x (%s)\n",
  468. DnsStatus, ErrorString );
  469. return;
  470. }
  471. PrintRecords ( ( PDNS_RECORD ) pDNSRecord );
  472. RpcTryExcept
  473. {
  474. DnsStatus = CRrCacheRecordSet( NULL,
  475. Name,
  476. Type,
  477. 0,
  478. pDNSRecord );
  479. }
  480. RpcExcept(1)
  481. {
  482. DnsStatus = RpcExceptionCode();
  483. }
  484. RpcEndExcept
  485. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  486. DnsStatus == RPC_S_UNKNOWN_IF )
  487. {
  488. printf( "Error: DNS Caching Resolver Service is not running\n" );
  489. return;
  490. }
  491. if ( DnsStatus )
  492. {
  493. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  494. printf( "CRrCacheRecordSet returned error: 0x%.8x (%s)\n",
  495. DnsStatus, ErrorString );
  496. return;
  497. }
  498. DnsFreeRRSet( (PDNS_RECORD) pDNSRecord, TRUE );
  499. }
  500. VOID
  501. DoDisplayCache( VOID )
  502. {
  503. DNS_STATUS DnsStatus = NO_ERROR;
  504. WCHAR Name[256];
  505. char TypeStr[25];
  506. WORD Type;
  507. PDNS_RPC_CACHE_TABLE pDNSCacheTable = NULL;
  508. PDNS_RPC_CACHE_TABLE pTempDNSCacheTable = NULL;
  509. RpcTryExcept
  510. {
  511. DnsStatus = CRrReadCache( NULL,
  512. &pDNSCacheTable );
  513. }
  514. RpcExcept(1)
  515. {
  516. DnsStatus = RpcExceptionCode();
  517. }
  518. RpcEndExcept
  519. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  520. DnsStatus == RPC_S_UNKNOWN_IF )
  521. {
  522. printf( "Error: DNS Caching Resolver Service is not running\n" );
  523. return;
  524. }
  525. if ( DnsStatus )
  526. {
  527. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  528. printf( "Error: 0x%.8x (%s)\n",
  529. DnsStatus, ErrorString );
  530. return;
  531. }
  532. pTempDNSCacheTable = pDNSCacheTable;
  533. while ( pTempDNSCacheTable )
  534. {
  535. PDNS_RPC_CACHE_TABLE pNext = pTempDNSCacheTable->pNext;
  536. printf( " %S\n", pTempDNSCacheTable->Name );
  537. printf( " ------------------------------------------------------\n" );
  538. if ( pTempDNSCacheTable->Type1 != DNS_TYPE_ZERO )
  539. {
  540. printf( " Type: %d\n", pTempDNSCacheTable->Type1 );
  541. GetCachedData( pTempDNSCacheTable->Name,
  542. pTempDNSCacheTable->Type1 );
  543. printf( "\n" );
  544. }
  545. if ( pTempDNSCacheTable->Type2 != DNS_TYPE_ZERO )
  546. {
  547. printf( " Type: %d\n", pTempDNSCacheTable->Type2 );
  548. GetCachedData( pTempDNSCacheTable->Name,
  549. pTempDNSCacheTable->Type2 );
  550. printf( "\n" );
  551. }
  552. if ( pTempDNSCacheTable->Type3 != DNS_TYPE_ZERO )
  553. {
  554. printf( " Type: %d\n", pTempDNSCacheTable->Type3 );
  555. GetCachedData( pTempDNSCacheTable->Name,
  556. pTempDNSCacheTable->Type3 );
  557. printf( "\n" );
  558. }
  559. if ( pTempDNSCacheTable->Name )
  560. LocalFree( pTempDNSCacheTable->Name );
  561. LocalFree( pTempDNSCacheTable );
  562. pTempDNSCacheTable = pNext;
  563. }
  564. }
  565. VOID
  566. DoUpdateTest( VOID )
  567. {
  568. DNS_STATUS DnsStatus = NO_ERROR;
  569. WCHAR Name[256];
  570. char lpAddress[256];
  571. char TypeStr[25];
  572. WORD Type;
  573. PDNS_RECORD pDNSRecord = NULL;
  574. PDNS_RECORD pTempDNSRecord = NULL;
  575. BYTE Part1, Part2, Part3, Part4;
  576. IP_ADDRESS Address;
  577. LPSTR lpTemp = NULL;
  578. printf( "Name: " );
  579. GetStringW( Name );
  580. printf( "\n" );
  581. printf( "Server IP: " );
  582. GetStringA( lpAddress );
  583. printf( "\n" );
  584. lpTemp = strtok( lpAddress, "." );
  585. Part1 = atoi( lpTemp );
  586. lpTemp = strtok( NULL, "." );
  587. Part2 = atoi( lpTemp );
  588. lpTemp = strtok( NULL, "." );
  589. Part3 = atoi( lpTemp );
  590. lpTemp = strtok( NULL, "." );
  591. Part4 = atoi( lpTemp );
  592. ((BYTE *) &Address)[0] = Part1;
  593. ((BYTE *) &Address)[1] = Part2;
  594. ((BYTE *) &Address)[2] = Part3;
  595. ((BYTE *) &Address)[3] = Part4;
  596. printf( "\n Sendine update test for name (%S) to server (%d.%d.%d.%d) ...\n\n",
  597. Name,
  598. Part1,
  599. Part2,
  600. Part3,
  601. Part4 );
  602. RpcTryExcept
  603. {
  604. DnsStatus = CRrUpdateTest( NULL,
  605. Name,
  606. 0,
  607. Address );
  608. }
  609. RpcExcept(1)
  610. {
  611. DnsStatus = RpcExceptionCode();
  612. }
  613. RpcEndExcept
  614. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  615. DnsStatus == RPC_S_UNKNOWN_IF )
  616. {
  617. printf( "Error: DNS Caching Resolver Service is not running\n" );
  618. return;
  619. }
  620. if ( DnsStatus )
  621. {
  622. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  623. printf( "Error: 0x%.8x (%s)\n",
  624. DnsStatus, ErrorString );
  625. return;
  626. }
  627. }
  628. VOID
  629. DoGetAdapterInfo( VOID )
  630. {
  631. DNS_STATUS DnsStatus = NO_ERROR;
  632. PDNS_RPC_ADAPTER_INFO pAdapterInfo = NULL;
  633. PDNS_RPC_ADAPTER_INFO pTempAdapterInfo;
  634. DWORD iter = 1;
  635. RpcTryExcept
  636. {
  637. CRrGetAdapterInfo( NULL, &pAdapterInfo );
  638. }
  639. RpcExcept(1)
  640. {
  641. DnsStatus = RpcExceptionCode();
  642. }
  643. RpcEndExcept
  644. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  645. DnsStatus == RPC_S_UNKNOWN_IF )
  646. {
  647. printf( "Error: DNS Caching Resolver Service is not running\n" );
  648. return;
  649. }
  650. if ( DnsStatus )
  651. {
  652. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  653. printf( "Error: 0x%.8x (%s)\n",
  654. DnsStatus, ErrorString );
  655. return;
  656. }
  657. pTempAdapterInfo = pAdapterInfo;
  658. while ( pTempAdapterInfo )
  659. {
  660. printf( " Net Adapter Info (%d):\n", iter );
  661. printf( " -------------------------------------\n" );
  662. printf( " pszAdapterDomainName : %s\n",
  663. pTempAdapterInfo->pszAdapterDomainName );
  664. printf( " Flags : %d\n",
  665. pTempAdapterInfo->Flags );
  666. if ( pTempAdapterInfo->pServerInfo )
  667. PrintServerInfo( pTempAdapterInfo->pServerInfo );
  668. pTempAdapterInfo = pTempAdapterInfo->pNext;
  669. iter++;
  670. }
  671. }
  672. VOID
  673. DoGetSearchList( VOID )
  674. {
  675. DNS_STATUS DnsStatus = NO_ERROR;
  676. PDNS_RPC_SEARCH_LIST pSearchList = NULL;
  677. PDNS_RPC_SEARCH_LIST pTempSearchList;
  678. RpcTryExcept
  679. {
  680. CRrGetSearchList( NULL, &pSearchList );
  681. }
  682. RpcExcept(1)
  683. {
  684. DnsStatus = RpcExceptionCode();
  685. }
  686. RpcEndExcept
  687. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  688. DnsStatus == RPC_S_UNKNOWN_IF )
  689. {
  690. printf( "Error: DNS Caching Resolver Service is not running\n" );
  691. return;
  692. }
  693. if ( DnsStatus )
  694. {
  695. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  696. printf( "Error: 0x%.8x (%s)\n",
  697. DnsStatus, ErrorString );
  698. return;
  699. }
  700. pTempSearchList = pSearchList;
  701. printf( " Search List:\n" );
  702. printf( " -------------------------------------\n" );
  703. while ( pTempSearchList )
  704. {
  705. printf( " pszName : %s\n", pTempSearchList->pszName );
  706. pTempSearchList = pTempSearchList->pNext;
  707. }
  708. }
  709. VOID
  710. DoGetPrimaryDomainName( VOID )
  711. {
  712. DNS_STATUS DnsStatus = NO_ERROR;
  713. LPSTR PrimaryDomainName = NULL;
  714. RpcTryExcept
  715. {
  716. CRrGetPrimaryDomainName( NULL, &PrimaryDomainName );
  717. }
  718. RpcExcept(1)
  719. {
  720. DnsStatus = RpcExceptionCode();
  721. }
  722. RpcEndExcept
  723. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  724. DnsStatus == RPC_S_UNKNOWN_IF )
  725. {
  726. printf( "Error: DNS Caching Resolver Service is not running\n" );
  727. return;
  728. }
  729. if ( DnsStatus )
  730. {
  731. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  732. printf( "Error: 0x%.8x (%s)\n",
  733. DnsStatus, ErrorString );
  734. return;
  735. }
  736. printf( " Primary Domain Name : %s\n", PrimaryDomainName );
  737. }
  738. VOID
  739. DoGetIpAddressList( VOID )
  740. {
  741. DNS_STATUS DnsStatus = NO_ERROR;
  742. PDNS_IP_ADDR_LIST pIpAddrList = NULL;
  743. DWORD Count;
  744. DWORD iter;
  745. RpcTryExcept
  746. {
  747. Count = CRrGetIpAddressList( NULL, &pIpAddrList );
  748. }
  749. RpcExcept(1)
  750. {
  751. DnsStatus = RpcExceptionCode();
  752. }
  753. RpcEndExcept
  754. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  755. DnsStatus == RPC_S_UNKNOWN_IF )
  756. {
  757. printf( "Error: DNS Caching Resolver Service is not running\n" );
  758. return;
  759. }
  760. if ( DnsStatus )
  761. {
  762. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  763. printf( "Error: 0x%.8x (%s)\n",
  764. DnsStatus, ErrorString );
  765. return;
  766. }
  767. if ( Count && pIpAddrList )
  768. {
  769. printf( "%d Ip Addresses returned :\n", Count );
  770. for ( iter = 0; iter < Count; iter++ )
  771. {
  772. printf( "(%d) \t", iter+1 );
  773. PrintIpAddress( pIpAddrList->AddressArray[iter].ipAddress );
  774. printf( " \t" );
  775. PrintIpAddress( pIpAddrList->AddressArray[iter].subnetMask );
  776. }
  777. LocalFree( pIpAddrList );
  778. }
  779. else
  780. {
  781. printf( "No Ip Addresses found.\n" );
  782. }
  783. }
  784. VOID
  785. DoGetHashTableStats( VOID )
  786. {
  787. DNS_STATUS Status = NO_ERROR;
  788. DWORD CacheHashTableSize;
  789. DWORD CacheHashTableBucketSize;
  790. DWORD NumberOfCacheEntries;
  791. DWORD NumberOfRecords;
  792. DWORD NumberOfExpiredRecords;
  793. PDNS_STATS_TABLE pStatsTable = NULL;
  794. PDNS_STATS_TABLE pTempRow = NULL;
  795. RpcTryExcept
  796. {
  797. Status = CRrGetHashTableStats( NULL,
  798. &CacheHashTableSize,
  799. &CacheHashTableBucketSize,
  800. &NumberOfCacheEntries,
  801. &NumberOfRecords,
  802. &NumberOfExpiredRecords,
  803. &pStatsTable );
  804. }
  805. RpcExcept(1)
  806. {
  807. Status = RpcExceptionCode();
  808. }
  809. RpcEndExcept
  810. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  811. Status == RPC_S_UNKNOWN_IF )
  812. {
  813. printf( "Error: DNS Caching Resolver Service is not running\n" );
  814. return;
  815. }
  816. if ( Status )
  817. {
  818. LPSTR ErrorString = DnsStatusToErrorString_A( Status );
  819. printf( "Error: 0x%.8x (%s)\n",
  820. Status, ErrorString );
  821. return;
  822. }
  823. printf( " DNS Cache Hash Table Statistics\n" );
  824. printf( " -------------------------------------\n" );
  825. printf( " Cache hash table size : %d\n",
  826. CacheHashTableSize );
  827. printf( " Cache hash table bucket size : %d\n",
  828. CacheHashTableBucketSize );
  829. printf( " Number of cache entries : %d\n",
  830. NumberOfCacheEntries );
  831. printf( " Number of RR sets : %d\n",
  832. NumberOfRecords );
  833. printf( " Number of expired RR sets : %d\n",
  834. NumberOfExpiredRecords );
  835. printf( "\n DNS Cache Hast Table Histogram\n" );
  836. printf( " -------------------------------------\n" );
  837. pTempRow = pStatsTable;
  838. while ( pTempRow )
  839. {
  840. PDWORD_LIST_ITEM pTempItem = pTempRow->pListItem;
  841. DWORD count = 0;
  842. DWORD iter;
  843. printf( " |" );
  844. while ( pTempItem )
  845. {
  846. for ( iter = 0; iter < pTempItem->Value2; iter++ )
  847. printf( "x" );
  848. count += pTempItem->Value1 - pTempItem->Value2;
  849. pTempItem = pTempItem->pNext;
  850. }
  851. for ( iter = 0; iter < count; iter++ )
  852. printf( "*" );
  853. printf( "\n" );
  854. pTempRow = pTempRow->pNext;
  855. }
  856. }
  857. VOID
  858. DoGetHashTableIndex( VOID )
  859. {
  860. DWORD Status = NO_ERROR;
  861. WCHAR Name[256];
  862. DWORD index;
  863. DWORD CacheHashTableSize;
  864. DWORD CacheHashTableBucketSize;
  865. DWORD NumberOfCacheEntries;
  866. DWORD NumberOfRecords;
  867. DWORD NumberOfExpiredRecords;
  868. PDNS_STATS_TABLE pStatsTable = NULL;
  869. PDNS_STATS_TABLE pTempRow = NULL;
  870. RpcTryExcept
  871. {
  872. Status = CRrGetHashTableStats( NULL,
  873. &CacheHashTableSize,
  874. &CacheHashTableBucketSize,
  875. &NumberOfCacheEntries,
  876. &NumberOfRecords,
  877. &NumberOfExpiredRecords,
  878. &pStatsTable );
  879. }
  880. RpcExcept(1)
  881. {
  882. Status = RpcExceptionCode();
  883. }
  884. RpcEndExcept
  885. if ( Status == RPC_S_SERVER_UNAVAILABLE ||
  886. Status == RPC_S_UNKNOWN_IF )
  887. {
  888. printf( "Error: DNS Caching Resolver Service is not running\n" );
  889. return;
  890. }
  891. if ( Status )
  892. {
  893. LPSTR ErrorString = DnsStatusToErrorString_A( Status );
  894. printf( "Error: 0x%.8x (%s)\n",
  895. Status, ErrorString );
  896. return;
  897. }
  898. printf( " CacheHashTableSize : %d\n", CacheHashTableSize );
  899. printf( " CacheHashTableBucketSize : %d\n", CacheHashTableBucketSize );
  900. printf( " NumberOfCacheEntries : %d\n", NumberOfCacheEntries );
  901. printf( " NumberOfRecords : %d\n", NumberOfRecords );
  902. printf( " NumberOfExpiredRecords : %d\n", NumberOfExpiredRecords );
  903. printf( "Name: " );
  904. GetStringW( Name );
  905. printf( "\n" );
  906. COMPUTE_STRING_HASH_2( Name, CacheHashTableSize, &index );
  907. printf( " Hash table index for %S is : %d\n",
  908. Name,
  909. index );
  910. pTempRow = pStatsTable;
  911. }
  912. VOID
  913. PrintServerInfo( IN PDNS_RPC_SERVER_INFO pServerInfo )
  914. {
  915. PDNS_RPC_SERVER_INFO pTempServerInfo = pServerInfo;
  916. DWORD iter = 1;
  917. while ( pTempServerInfo )
  918. {
  919. printf( " Server Info (%d):\n", iter );
  920. printf( " -------------------------------------\n" );
  921. printf( " ipAddress : %d.%d.%d.%d\n",
  922. ((BYTE *) &pTempServerInfo->ipAddress)[0],
  923. ((BYTE *) &pTempServerInfo->ipAddress)[1],
  924. ((BYTE *) &pTempServerInfo->ipAddress)[2],
  925. ((BYTE *) &pTempServerInfo->ipAddress)[3] );
  926. printf( " Status : 0x%x\n", pTempServerInfo->Status );
  927. printf( " Priority : %d\n", pTempServerInfo->Priority );
  928. pTempServerInfo = pTempServerInfo->pNext;
  929. iter++;
  930. }
  931. }
  932. VOID
  933. PrintRecords (
  934. IN PDNS_RECORD pDnsRecord )
  935. {
  936. PDNS_RECORD pTemp = pDnsRecord;
  937. while ( pTemp )
  938. {
  939. PrintRecord( pTemp );
  940. pTemp = pTemp->pNext;
  941. }
  942. }
  943. VOID
  944. PrintRecord (
  945. IN PDNS_RECORD pDnsRecord )
  946. {
  947. if ( ! pDnsRecord )
  948. return;
  949. printf( " Record Name : %S\n", pDnsRecord->pName );
  950. printf( " Record Type : %d\n", pDnsRecord->wType );
  951. printf( " Time To Live : %d (seconds)\n", pDnsRecord->dwTtl );
  952. printf( " Data Length : %d\n", pDnsRecord->wDataLength );
  953. if ( pDnsRecord->Flags.S.Section == DNSREC_QUESTION )
  954. printf( " Section : Question Record\n" );
  955. else if ( pDnsRecord->Flags.S.Section == DNSREC_ANSWER )
  956. printf( " Section : Answer Record\n" );
  957. else if ( pDnsRecord->Flags.S.Section == DNSREC_AUTHORITY )
  958. printf( " Section : Authority Record\n" );
  959. else
  960. printf( " Section : Additional Record\n" );
  961. switch( pDnsRecord->wType )
  962. {
  963. case DNS_TYPE_A :
  964. PrintARecord( pDnsRecord );
  965. break;
  966. case DNS_TYPE_SOA :
  967. PrintSOARecord( pDnsRecord );
  968. break;
  969. case DNS_TYPE_PTR :
  970. case DNS_TYPE_NS :
  971. case DNS_TYPE_CNAME :
  972. case DNS_TYPE_MB :
  973. case DNS_TYPE_MD :
  974. case DNS_TYPE_MF :
  975. case DNS_TYPE_MG :
  976. case DNS_TYPE_MR :
  977. PrintPTRRecord( pDnsRecord );
  978. break;
  979. case DNS_TYPE_MINFO :
  980. case DNS_TYPE_RP :
  981. PrintMINFORecord( pDnsRecord );
  982. break;
  983. case DNS_TYPE_MX :
  984. case DNS_TYPE_AFSDB :
  985. case DNS_TYPE_RT :
  986. PrintMXRecord( pDnsRecord );
  987. break;
  988. case DNS_TYPE_HINFO :
  989. case DNS_TYPE_ISDN :
  990. case DNS_TYPE_TEXT :
  991. case DNS_TYPE_X25 :
  992. PrintHINFORecord( pDnsRecord );
  993. break;
  994. case DNS_TYPE_NULL :
  995. PrintNULLRecord( pDnsRecord );
  996. break;
  997. case DNS_TYPE_WKS :
  998. PrintWKSRecord( pDnsRecord );
  999. break;
  1000. case DNS_TYPE_AAAA :
  1001. PrintAAAARecord( pDnsRecord );
  1002. break;
  1003. case DNS_TYPE_SRV :
  1004. PrintSRVRecord( pDnsRecord );
  1005. break;
  1006. case DNS_TYPE_WINS :
  1007. PrintWINSRecord( pDnsRecord );
  1008. break;
  1009. case DNS_TYPE_NBSTAT :
  1010. PrintWINSRRecord( pDnsRecord );
  1011. break;
  1012. default :
  1013. printf( "Don't know how to print record type %d\n", pDnsRecord->wType );
  1014. }
  1015. }
  1016. VOID
  1017. PrintARecord (
  1018. IN PDNS_RECORD pDnsRecord )
  1019. {
  1020. printf( " A record :\n" );
  1021. printf( " ipAddress = %d.%d.%d.%d\n",
  1022. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[0],
  1023. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[1],
  1024. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[2],
  1025. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[3] );
  1026. printf( "\n" );
  1027. }
  1028. VOID
  1029. PrintSOARecord (
  1030. IN PDNS_RECORD pDnsRecord )
  1031. {
  1032. printf( " SOA record :\n" );
  1033. printf( " pNamePrimaryServer = %S\n",
  1034. pDnsRecord->Data.SOA.pNamePrimaryServer );
  1035. printf( " pNameAdministrator = %S\n",
  1036. pDnsRecord->Data.SOA.pNameAdministrator );
  1037. printf( " dwSerialNo = %d\n",
  1038. pDnsRecord->Data.SOA.dwSerialNo );
  1039. printf( " dwRefresh = %d\n",
  1040. pDnsRecord->Data.SOA.dwRefresh );
  1041. printf( " dwRetry = %d\n",
  1042. pDnsRecord->Data.SOA.dwRetry );
  1043. printf( " dwExpire = %d\n",
  1044. pDnsRecord->Data.SOA.dwExpire );
  1045. printf( " dwDefaultTtl = %d\n",
  1046. pDnsRecord->Data.SOA.dwDefaultTtl );
  1047. printf( "\n" );
  1048. }
  1049. VOID
  1050. PrintPTRRecord (
  1051. IN PDNS_RECORD pDnsRecord )
  1052. {
  1053. printf( " PTR, NS, CNAME, MB, MD, MF, MG, MR record :\n" );
  1054. printf( " nameHost = %S\n",
  1055. pDnsRecord->Data.PTR.pNameHost );
  1056. printf( "\n" );
  1057. }
  1058. VOID
  1059. PrintMINFORecord (
  1060. IN PDNS_RECORD pDnsRecord )
  1061. {
  1062. printf( " MINFO, RP record :\n" );
  1063. printf( " pNameMailbox = %S\n",
  1064. pDnsRecord->Data.MINFO.pNameMailbox );
  1065. printf( " pNameErrorsMailbox = %S\n",
  1066. pDnsRecord->Data.MINFO.pNameErrorsMailbox );
  1067. printf( "\n" );
  1068. }
  1069. VOID
  1070. PrintMXRecord (
  1071. IN PDNS_RECORD pDnsRecord )
  1072. {
  1073. printf( " MX, AFSDB, RT record :\n" );
  1074. printf( " pNameExchange = %S\n",
  1075. pDnsRecord->Data.MX.pNameExchange );
  1076. printf( " wPreference = %d\n",
  1077. pDnsRecord->Data.MX.wPreference );
  1078. printf( " Pad = %d\n",
  1079. pDnsRecord->Data.MX.Pad );
  1080. printf( "\n" );
  1081. }
  1082. VOID
  1083. PrintHINFORecord (
  1084. IN PDNS_RECORD pDnsRecord )
  1085. {
  1086. DWORD iter;
  1087. printf( " HINFO, ISDN, TEXT, X25 record :\n" );
  1088. printf( " dwStringCount = %d\n",
  1089. pDnsRecord->Data.HINFO.dwStringCount );
  1090. for ( iter = 0; iter < pDnsRecord->Data.HINFO.dwStringCount; iter ++ )
  1091. {
  1092. printf( " pStringArray[%d] = %S\n",
  1093. iter,
  1094. pDnsRecord->Data.HINFO.pStringArray[iter] );
  1095. }
  1096. printf( "\n" );
  1097. }
  1098. VOID
  1099. PrintNULLRecord (
  1100. IN PDNS_RECORD pDnsRecord )
  1101. {
  1102. printf( " NULL record :\n" );
  1103. printf( "\n" );
  1104. }
  1105. VOID
  1106. PrintWKSRecord (
  1107. IN PDNS_RECORD pDnsRecord )
  1108. {
  1109. printf( " WKS record :\n" );
  1110. printf( " ipAddress = %d.%d.%d.%d\n",
  1111. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[0],
  1112. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[1],
  1113. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[2],
  1114. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[3] );
  1115. printf( " chProtocol = %d\n",
  1116. pDnsRecord->Data.WKS.chProtocol );
  1117. printf( "\n" );
  1118. }
  1119. VOID
  1120. PrintAAAARecord (
  1121. IN PDNS_RECORD pDnsRecord )
  1122. {
  1123. printf( " AAAA record :\n" );
  1124. printf( " ipAddress = %d.%d.%d.%d\n",
  1125. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[0],
  1126. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[1],
  1127. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[2],
  1128. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[3] );
  1129. printf( "\n" );
  1130. }
  1131. VOID
  1132. PrintSRVRecord (
  1133. IN PDNS_RECORD pDnsRecord )
  1134. {
  1135. printf( " SRV record :\n" );
  1136. printf( " pNameTarget = %S\n",
  1137. pDnsRecord->Data.SRV.pNameTarget );
  1138. printf( " wPriority = %d\n",
  1139. pDnsRecord->Data.SRV.wPriority );
  1140. printf( " wWeight = %d\n",
  1141. pDnsRecord->Data.SRV.wWeight );
  1142. printf( " wPort = %d\n",
  1143. pDnsRecord->Data.SRV.wPort );
  1144. printf( " Pad = %d\n",
  1145. pDnsRecord->Data.SRV.Pad );
  1146. printf( "\n" );
  1147. }
  1148. VOID
  1149. PrintWINSRecord (
  1150. IN PDNS_RECORD pDnsRecord )
  1151. {
  1152. printf( " WINS record :\n" );
  1153. printf( " dwMappingFlag = %d\n",
  1154. pDnsRecord->Data.WINS.dwMappingFlag );
  1155. printf( " dwLookupTimeout = %d\n",
  1156. pDnsRecord->Data.WINS.dwLookupTimeout );
  1157. printf( " dwCacheTimeout = %d\n",
  1158. pDnsRecord->Data.WINS.dwCacheTimeout );
  1159. printf( " cWinsServerCount = %d\n",
  1160. pDnsRecord->Data.WINS.cWinsServerCount );
  1161. printf( "\n" );
  1162. }
  1163. VOID
  1164. PrintWINSRRecord (
  1165. IN PDNS_RECORD pDnsRecord )
  1166. {
  1167. printf( " NBSTAT record :\n" );
  1168. printf( " dwMappingFlag = %d\n",
  1169. pDnsRecord->Data.WINSR.dwMappingFlag );
  1170. printf( " dwLookupTimeout = %d\n",
  1171. pDnsRecord->Data.WINSR.dwLookupTimeout );
  1172. printf( " dwCacheTimeout = %d\n",
  1173. pDnsRecord->Data.WINSR.dwCacheTimeout );
  1174. printf( " pNameResultDomain = %S\n",
  1175. pDnsRecord->Data.WINSR.pNameResultDomain );
  1176. printf( "\n" );
  1177. }
  1178. VOID
  1179. GetCachedData(
  1180. IN LPWSTR Name,
  1181. IN WORD Type )
  1182. {
  1183. PDNS_RECORD pDNSRecord = NULL;
  1184. DNS_STATUS DnsStatus = NO_ERROR;
  1185. RpcTryExcept
  1186. {
  1187. DnsStatus = CRrReadCacheEntry( NULL, Name, Type, &pDNSRecord );
  1188. }
  1189. RpcExcept(1)
  1190. {
  1191. DnsStatus = RpcExceptionCode();
  1192. }
  1193. RpcEndExcept
  1194. if ( DnsStatus == RPC_S_SERVER_UNAVAILABLE ||
  1195. DnsStatus == RPC_S_UNKNOWN_IF )
  1196. {
  1197. printf( "Error: DNS Caching Resolver Service is not running\n" );
  1198. return;
  1199. }
  1200. if ( DnsStatus )
  1201. {
  1202. LPSTR ErrorString = DnsStatusToErrorString_A( DnsStatus );
  1203. printf( "Error: 0x%.8x (%s)\n",
  1204. DnsStatus, ErrorString );
  1205. return;
  1206. }
  1207. PrintRecords ( ( PDNS_RECORD ) pDNSRecord );
  1208. DnsFreeRRSet( (PDNS_RECORD) pDNSRecord, TRUE );
  1209. }