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.

472 lines
13 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 <dnsapi.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. VOID
  11. PrintRecord (
  12. IN PDNS_RECORD pDnsRecord );
  13. VOID
  14. PrintARecord (
  15. IN PDNS_RECORD pDnsRecord );
  16. VOID
  17. PrintSOARecord (
  18. IN PDNS_RECORD pDnsRecord );
  19. VOID
  20. PrintPTRRecord (
  21. IN PDNS_RECORD pDnsRecord );
  22. VOID
  23. PrintMINFORecord (
  24. IN PDNS_RECORD pDnsRecord );
  25. VOID
  26. PrintMXRecord (
  27. IN PDNS_RECORD pDnsRecord );
  28. VOID
  29. PrintHINFORecord (
  30. IN PDNS_RECORD pDnsRecord );
  31. VOID
  32. PrintNULLRecord (
  33. IN PDNS_RECORD pDnsRecord );
  34. VOID
  35. PrintWKSRecord (
  36. IN PDNS_RECORD pDnsRecord );
  37. VOID
  38. PrintAAAARecord (
  39. IN PDNS_RECORD pDnsRecord );
  40. VOID
  41. PrintSRVRecord (
  42. IN PDNS_RECORD pDnsRecord );
  43. VOID
  44. PrintWINSRecord (
  45. IN PDNS_RECORD pDnsRecord );
  46. VOID
  47. PrintWINSRRecord (
  48. IN PDNS_RECORD pDnsRecord );
  49. #define MAX_NR_OF_DNS_SERVERS 3
  50. _cdecl
  51. main(int argc, char **argv)
  52. {
  53. DWORD Status = NO_ERROR;
  54. DWORD dwOptions = DNS_QUERY_STANDARD;
  55. PDNS_RECORD pDNSRecord = NULL;
  56. PDNS_RECORD pTempDNSRecord = NULL;
  57. WORD wType;
  58. LONG cch;
  59. BOOL bBadUsage = FALSE;
  60. UINT uIdx;
  61. UINT uPos;
  62. UINT uIdx1;
  63. IP_ADDRESS ipAddress[MAX_NR_OF_DNS_SERVERS];
  64. PIP_ARRAY pIpArray = NULL;
  65. if ( argc < 3 )
  66. bBadUsage = TRUE;
  67. else
  68. {
  69. wType = atoi( argv[2] );
  70. if (argc > 3)
  71. {
  72. if ( isalpha((int)argv[3][0]) )
  73. {
  74. uIdx = 4;
  75. uPos = 0;
  76. while ( !bBadUsage && uPos < strlen( argv[3] ) )
  77. {
  78. switch (argv[3][uPos])
  79. {
  80. case 'b':
  81. case 'B':
  82. dwOptions |= DNS_QUERY_BYPASS_CACHE;
  83. break;
  84. case 'a':
  85. case 'A':
  86. dwOptions |= DNS_QUERY_ACCEPT_PARTIAL_UDP;
  87. break;
  88. case 'u':
  89. case 'U':
  90. dwOptions |= DNS_QUERY_USE_TCP_ONLY;
  91. break;
  92. case 'n':
  93. case 'N':
  94. dwOptions |= DNS_QUERY_NO_RECURSION;
  95. break;
  96. case 's':
  97. case 'S':
  98. break;
  99. default:
  100. bBadUsage = TRUE;
  101. }
  102. uPos++;
  103. }
  104. }
  105. else
  106. uIdx = 3;
  107. uIdx1 = 0;
  108. while ( !bBadUsage &&
  109. ( uIdx < (UINT) argc ) &&
  110. ( uIdx1 < MAX_NR_OF_DNS_SERVERS ) )
  111. {
  112. ipAddress[uIdx1] = inet_addr(argv[uIdx++]);
  113. if ( INADDR_NONE != ipAddress[uIdx1] )
  114. uIdx1++;
  115. else
  116. bBadUsage = TRUE;
  117. }
  118. if (!bBadUsage && 0 != uIdx1)
  119. {
  120. pIpArray = DnsCreateIpArray( uIdx1 );
  121. if ( ! pIpArray )
  122. {
  123. printf("Error on DnsCreateIpArray\n");
  124. return( ERROR_OUTOFMEMORY );
  125. }
  126. for ( uIdx = 0; uIdx < uIdx1 ; uIdx++ )
  127. pIpArray->aipAddrs[uIdx] = ipAddress[uIdx];
  128. }
  129. }
  130. }
  131. if ( !bBadUsage )
  132. {
  133. printf( "\nDnsQuery_A( %s, %d, ... ) ...\n\n", argv[1], wType );
  134. Status = DnsQuery_A( argv[1],
  135. wType,
  136. dwOptions,
  137. pIpArray,
  138. &pDNSRecord,
  139. NULL );
  140. if ( Status )
  141. {
  142. printf( "Dns_Query_A call failed with error: 0x%.8X\n", Status );
  143. }
  144. printf( "Query found record(s) ...\n\n" );
  145. pTempDNSRecord = pDNSRecord;
  146. while ( pTempDNSRecord )
  147. {
  148. printf( " Record:\n" );
  149. printf( " -------------------------------------\n" );
  150. printf( " Name Owner : %s\n", pTempDNSRecord->nameOwner );
  151. printf( " Type : %d\n", pTempDNSRecord->wType );
  152. printf( " Data Length : %d\n", pTempDNSRecord->wDataLength );
  153. printf( " Ttl (mins) : %d\n", pTempDNSRecord->dwTtl/60 );
  154. printf( " Flags : 0x%X\n\n", pTempDNSRecord->Flags.W );
  155. PrintRecord( pTempDNSRecord );
  156. pTempDNSRecord = pTempDNSRecord->pNext;
  157. }
  158. DnsFreeRRSet( pDNSRecord, TRUE );
  159. }
  160. else // bBadUsage
  161. {
  162. printf( "\nUsage: dnsqry <DNS Name> <Type> [<Options>] [<ServList>]\n" );
  163. printf( "\nWhere:\n" );
  164. printf( " DNS Name - Server_X.dbsd-test.microsoft.com\n" );
  165. printf( " Type - 1 (DNS_TYPE_A), 2 (DNS_TYPE_NS), 3 (DNS_TYPE_MD), ...\n" );
  166. printf( " Options - b (BYPASS_CACHE), a (ACCEPT_PARTIAL_UDP), u (USE_TCP_ONLY), n (NO_RECURSION)\n");
  167. printf( " Options can be combined.\n");
  168. printf( " ServList - Targeted DNS Servers IPs (e.g. 157.55.192.155 172.31.48.217)\n");
  169. return(-1);
  170. }
  171. return(0);
  172. }
  173. VOID
  174. PrintRecord (
  175. IN PDNS_RECORD pDnsRecord )
  176. {
  177. switch( pDnsRecord->wType )
  178. {
  179. case DNS_TYPE_A :
  180. PrintARecord( pDnsRecord );
  181. break;
  182. case DNS_TYPE_SOA :
  183. PrintSOARecord( pDnsRecord );
  184. break;
  185. case DNS_TYPE_PTR :
  186. case DNS_TYPE_NS :
  187. case DNS_TYPE_CNAME :
  188. case DNS_TYPE_MB :
  189. case DNS_TYPE_MD :
  190. case DNS_TYPE_MF :
  191. case DNS_TYPE_MG :
  192. case DNS_TYPE_MR :
  193. PrintPTRRecord( pDnsRecord );
  194. break;
  195. case DNS_TYPE_MINFO :
  196. case DNS_TYPE_RP :
  197. PrintMINFORecord( pDnsRecord );
  198. break;
  199. case DNS_TYPE_MX :
  200. case DNS_TYPE_AFSDB :
  201. case DNS_TYPE_RT :
  202. PrintMXRecord( pDnsRecord );
  203. break;
  204. case DNS_TYPE_HINFO :
  205. case DNS_TYPE_ISDN :
  206. case DNS_TYPE_TEXT :
  207. case DNS_TYPE_X25 :
  208. PrintHINFORecord( pDnsRecord );
  209. break;
  210. case DNS_TYPE_NULL :
  211. PrintNULLRecord( pDnsRecord );
  212. break;
  213. case DNS_TYPE_WKS :
  214. PrintWKSRecord( pDnsRecord );
  215. break;
  216. case DNS_TYPE_AAAA :
  217. PrintAAAARecord( pDnsRecord );
  218. break;
  219. case DNS_TYPE_SRV :
  220. PrintSRVRecord( pDnsRecord );
  221. break;
  222. case DNS_TYPE_WINS :
  223. PrintWINSRecord( pDnsRecord );
  224. break;
  225. case DNS_TYPE_NBSTAT :
  226. PrintWINSRRecord( pDnsRecord );
  227. break;
  228. default :
  229. printf( "Don't know how to print record type %d\n", pDnsRecord->wType );
  230. }
  231. }
  232. VOID
  233. PrintARecord (
  234. IN PDNS_RECORD pDnsRecord )
  235. {
  236. printf( " A record :\n" );
  237. printf( " ipAddress = %d.%d.%d.%d\n",
  238. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[0],
  239. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[1],
  240. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[2],
  241. ((BYTE *) &pDnsRecord->Data.A.ipAddress)[3] );
  242. printf( "\n" );
  243. }
  244. VOID
  245. PrintSOARecord (
  246. IN PDNS_RECORD pDnsRecord )
  247. {
  248. printf( " SOA record :\n" );
  249. printf( " namePrimaryServer = %s\n",
  250. pDnsRecord->Data.SOA.namePrimaryServer );
  251. printf( " nameAdministrator = %s\n",
  252. pDnsRecord->Data.SOA.nameAdministrator );
  253. printf( " dwSerialNo = %d\n",
  254. pDnsRecord->Data.SOA.dwSerialNo );
  255. printf( " dwRefresh = %d\n",
  256. pDnsRecord->Data.SOA.dwRefresh );
  257. printf( " dwRetry = %d\n",
  258. pDnsRecord->Data.SOA.dwRetry );
  259. printf( " dwExpire = %d\n",
  260. pDnsRecord->Data.SOA.dwExpire );
  261. printf( " dwDefaultTtl = %d\n",
  262. pDnsRecord->Data.SOA.dwDefaultTtl );
  263. printf( "\n" );
  264. }
  265. VOID
  266. PrintPTRRecord (
  267. IN PDNS_RECORD pDnsRecord )
  268. {
  269. printf( " PTR, NS, CNAME, MB, MD, MF, MG, MR record :\n" );
  270. printf( " nameHost = %s\n",
  271. pDnsRecord->Data.PTR.nameHost );
  272. printf( "\n" );
  273. }
  274. VOID
  275. PrintMINFORecord (
  276. IN PDNS_RECORD pDnsRecord )
  277. {
  278. printf( " MINFO, RP record :\n" );
  279. printf( " nameMailbox = %s\n",
  280. pDnsRecord->Data.MINFO.nameMailbox );
  281. printf( " nameErrorsMailbox = %s\n",
  282. pDnsRecord->Data.MINFO.nameErrorsMailbox );
  283. printf( "\n" );
  284. }
  285. VOID
  286. PrintMXRecord (
  287. IN PDNS_RECORD pDnsRecord )
  288. {
  289. printf( " MX, AFSDB, RT record :\n" );
  290. printf( " nameExchange = %s\n",
  291. pDnsRecord->Data.MX.nameExchange );
  292. printf( " wPreference = %d\n",
  293. pDnsRecord->Data.MX.wPreference );
  294. printf( " Pad = %d\n",
  295. pDnsRecord->Data.MX.Pad );
  296. printf( "\n" );
  297. }
  298. VOID
  299. PrintHINFORecord (
  300. IN PDNS_RECORD pDnsRecord )
  301. {
  302. DWORD iter;
  303. printf( " HINFO, ISDN, TEXT, X25 record :\n" );
  304. printf( " dwStringCount = %d\n",
  305. pDnsRecord->Data.HINFO.dwStringCount );
  306. for ( iter = 0; iter < pDnsRecord->Data.HINFO.dwStringCount; iter ++ )
  307. {
  308. printf( " pStringArray[%d] = %s\n",
  309. iter,
  310. pDnsRecord->Data.HINFO.pStringArray[iter] );
  311. }
  312. printf( "\n" );
  313. }
  314. VOID
  315. PrintNULLRecord (
  316. IN PDNS_RECORD pDnsRecord )
  317. {
  318. printf( " NULL record :\n" );
  319. printf( " dwByteCount = %d\n",
  320. pDnsRecord->Data.Null.dwByteCount );
  321. printf( "\n" );
  322. }
  323. VOID
  324. PrintWKSRecord (
  325. IN PDNS_RECORD pDnsRecord )
  326. {
  327. printf( " WKS record :\n" );
  328. printf( " ipAddress = %d.%d.%d.%d\n",
  329. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[0],
  330. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[1],
  331. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[2],
  332. ((BYTE *) &pDnsRecord->Data.WKS.ipAddress)[3] );
  333. printf( " chProtocol = %d\n",
  334. pDnsRecord->Data.WKS.chProtocol );
  335. printf( "\n" );
  336. }
  337. VOID
  338. PrintAAAARecord (
  339. IN PDNS_RECORD pDnsRecord )
  340. {
  341. printf( " AAAA record :\n" );
  342. printf( " ipAddress = %d.%d.%d.%d\n",
  343. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[0],
  344. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[1],
  345. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[2],
  346. ((BYTE *) &pDnsRecord->Data.AAAA.ipv6Address)[3] );
  347. printf( "\n" );
  348. }
  349. VOID
  350. PrintSRVRecord (
  351. IN PDNS_RECORD pDnsRecord )
  352. {
  353. printf( " SRV record :\n" );
  354. printf( " nameTarget = %s\n",
  355. pDnsRecord->Data.SRV.nameTarget );
  356. printf( " wPriority = %d\n",
  357. pDnsRecord->Data.SRV.wPriority );
  358. printf( " wWeight = %d\n",
  359. pDnsRecord->Data.SRV.wWeight );
  360. printf( " wPort = %d\n",
  361. pDnsRecord->Data.SRV.wPort );
  362. printf( " Pad = %d\n",
  363. pDnsRecord->Data.SRV.Pad );
  364. printf( "\n" );
  365. }
  366. VOID
  367. PrintWINSRecord (
  368. IN PDNS_RECORD pDnsRecord )
  369. {
  370. printf( " WINS record :\n" );
  371. printf( " dwMappingFlag = %d\n",
  372. pDnsRecord->Data.WINS.dwMappingFlag );
  373. printf( " dwLookupTimeout = %d\n",
  374. pDnsRecord->Data.WINS.dwLookupTimeout );
  375. printf( " dwCacheTimeout = %d\n",
  376. pDnsRecord->Data.WINS.dwCacheTimeout );
  377. printf( " cWinsServerCount = %d\n",
  378. pDnsRecord->Data.WINS.cWinsServerCount );
  379. printf( "\n" );
  380. }
  381. VOID
  382. PrintWINSRRecord (
  383. IN PDNS_RECORD pDnsRecord )
  384. {
  385. printf( " NBSTAT record :\n" );
  386. printf( " dwMappingFlag = %d\n",
  387. pDnsRecord->Data.WINSR.dwMappingFlag );
  388. printf( " dwLookupTimeout = %d\n",
  389. pDnsRecord->Data.WINSR.dwLookupTimeout );
  390. printf( " dwCacheTimeout = %d\n",
  391. pDnsRecord->Data.WINSR.dwCacheTimeout );
  392. printf( " nameResultDomain = %s\n",
  393. pDnsRecord->Data.WINSR.nameResultDomain );
  394. printf( "\n" );
  395. }