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.

3765 lines
81 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. query.c
  5. Abstract:
  6. Domain Name System (DNS) API
  7. Query routines.
  8. Author:
  9. Jim Gilroy (jamesg) January, 1997
  10. Revision History:
  11. --*/
  12. #include "local.h"
  13. //
  14. // TTL for answering IP string queries
  15. // (use a week)
  16. //
  17. #define IPSTRING_RECORD_TTL (604800)
  18. //
  19. // Max number of server's we'll ever bother to extract from packet
  20. // (much more and you're out of UDP packet space anyway)
  21. //
  22. #define MAX_NAME_SERVER_COUNT (20)
  23. //
  24. // Query utilities
  25. //
  26. // DCR: move to library packet stuff
  27. //
  28. BOOL
  29. IsEmptyDnsResponse(
  30. IN PDNS_RECORD pRecordList
  31. )
  32. /*++
  33. Routine Description:
  34. Check for no-answer response.
  35. Arguments:
  36. pRecordList -- record list to check
  37. Return Value:
  38. TRUE if no-answers
  39. FALSE if answers
  40. --*/
  41. {
  42. PDNS_RECORD prr = pRecordList;
  43. BOOL fempty = TRUE;
  44. while ( prr )
  45. {
  46. if ( prr->Flags.S.Section == DNSREC_ANSWER )
  47. {
  48. fempty = FALSE;
  49. break;
  50. }
  51. prr = prr->pNext;
  52. }
  53. return fempty;
  54. }
  55. BOOL
  56. IsEmptyDnsResponseFromResolver(
  57. IN PDNS_RECORD pRecordList
  58. )
  59. /*++
  60. Routine Description:
  61. Check for no-answer response.
  62. Arguments:
  63. pRecordList -- record list to check
  64. Return Value:
  65. TRUE if no-answers
  66. FALSE if answers
  67. --*/
  68. {
  69. PDNS_RECORD prr = pRecordList;
  70. BOOL fempty = TRUE;
  71. //
  72. // resolver sends every thing back as ANSWER section
  73. // or section==0 for host file
  74. //
  75. //
  76. // DCR: this is lame because the query interface to the
  77. // resolver is lame
  78. //
  79. while ( prr )
  80. {
  81. if ( prr->Flags.S.Section == DNSREC_ANSWER ||
  82. prr->Flags.S.Section == 0 )
  83. {
  84. fempty = FALSE;
  85. break;
  86. }
  87. prr = prr->pNext;
  88. }
  89. return fempty;
  90. }
  91. VOID
  92. FixupNameOwnerPointers(
  93. IN OUT PDNS_RECORD pRecord
  94. )
  95. /*++
  96. Routine Description:
  97. None.
  98. Arguments:
  99. None.
  100. Return Value:
  101. None.
  102. --*/
  103. {
  104. PDNS_RECORD prr = pRecord;
  105. PTSTR pname = pRecord->pName;
  106. DNSDBG( TRACE, ( "FixupNameOwnerPointers()\n" ));
  107. while ( prr )
  108. {
  109. if ( prr->pName == NULL )
  110. {
  111. prr->pName = pname;
  112. }
  113. else
  114. {
  115. pname = prr->pName;
  116. }
  117. prr = prr->pNext;
  118. }
  119. }
  120. BOOL
  121. IsCacheableNameError(
  122. IN PDNS_NETINFO pNetInfo
  123. )
  124. /*++
  125. Routine Description:
  126. Determine if name error is cacheable.
  127. To this is essentially a check that DNS received results on
  128. all networks.
  129. Arguments:
  130. pNetInfo -- pointer to network info used in query
  131. Return Value:
  132. TRUE if name error cacheable.
  133. FALSE otherwise (some network did not respond)
  134. --*/
  135. {
  136. DWORD iter;
  137. PDNS_ADAPTER padapter;
  138. DNSDBG( TRACE, ( "IsCacheableNameError()\n" ));
  139. if ( !pNetInfo )
  140. {
  141. ASSERT( FALSE );
  142. return TRUE;
  143. }
  144. //
  145. // check each adapter
  146. // - any that are capable of responding (have DNS servers)
  147. // MUST have responded in order for response to be
  148. // cacheable
  149. //
  150. // DCR: return flags DCR
  151. // - adapter queried flag
  152. // - got response flag (valid response flag?)
  153. // - explict negative answer flag
  154. //
  155. // DCR: cachable negative should come back directly from query
  156. // perhaps in netinfo as flag -- "negative on all adapters"
  157. //
  158. for ( iter = 0; iter < pNetInfo->AdapterCount; iter++ )
  159. {
  160. padapter = pNetInfo->AdapterArray[iter];
  161. if ( ( padapter->InfoFlags & DNS_FLAG_IGNORE_ADAPTER ) ||
  162. ( padapter->RunFlags & RUN_FLAG_STOP_QUERY_ON_ADAPTER ) )
  163. {
  164. continue;
  165. }
  166. // if negative answer on adapter -- fine
  167. if ( padapter->Status == DNS_ERROR_RCODE_NAME_ERROR ||
  168. padapter->Status == DNS_INFO_NO_RECORDS )
  169. {
  170. ASSERT( padapter->RunFlags & RUN_FLAG_STOP_QUERY_ON_ADAPTER );
  171. continue;
  172. }
  173. // note, the above should map one-to-one with query stop
  174. ASSERT( !(padapter->RunFlags & RUN_FLAG_STOP_QUERY_ON_ADAPTER) );
  175. // if adapter has no DNS server -- fine
  176. // in this case PnP before useful, and the PnP event
  177. // will flush the cache
  178. if ( padapter->ServerCount == 0 )
  179. {
  180. continue;
  181. }
  182. // otherwise, this adapter was queried but could not produce a response
  183. DNSDBG( TRACE, (
  184. "IsCacheableNameError() -- FALSE\n"
  185. "\tadapter %d (%S) did not receive response\n"
  186. "\treturn status = %d\n"
  187. "\treturn flags = %08x\n",
  188. iter,
  189. padapter->pszAdapterGuidName,
  190. padapter->Status,
  191. padapter->RunFlags ));
  192. return FALSE;
  193. }
  194. return TRUE;
  195. }
  196. //
  197. // Query name building utils
  198. //
  199. BOOL
  200. ValidateQueryTld(
  201. IN PSTR pTld
  202. )
  203. /*++
  204. Routine Description:
  205. Validate query TLD
  206. Arguments:
  207. pTld -- TLD to validate
  208. Return Value:
  209. TRUE if valid
  210. FALSE otherwise
  211. --*/
  212. {
  213. //
  214. // numeric
  215. //
  216. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_NUMERIC )
  217. {
  218. if ( Dns_IsNameNumeric_A( pTld ) )
  219. {
  220. return FALSE;
  221. }
  222. }
  223. //
  224. // bogus TLDs
  225. //
  226. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_WORKGROUP )
  227. {
  228. if ( Dns_NameCompare_UTF8(
  229. "workgroup",
  230. pTld ))
  231. {
  232. return FALSE;
  233. }
  234. }
  235. // not sure about these
  236. // probably won't turn on screening by default
  237. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_DOMAIN )
  238. {
  239. if ( Dns_NameCompare_UTF8(
  240. "domain",
  241. pTld ))
  242. {
  243. return FALSE;
  244. }
  245. }
  246. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_OFFICE )
  247. {
  248. if ( Dns_NameCompare_UTF8(
  249. "office",
  250. pTld ))
  251. {
  252. return FALSE;
  253. }
  254. }
  255. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_HOME )
  256. {
  257. if ( Dns_NameCompare_UTF8(
  258. "home",
  259. pTld ))
  260. {
  261. return FALSE;
  262. }
  263. }
  264. return TRUE;
  265. }
  266. BOOL
  267. ValidateQueryName(
  268. IN PQUERY_BLOB pBlob,
  269. IN PSTR pName,
  270. IN PSTR pDomain
  271. )
  272. /*++
  273. Routine Description:
  274. Validate name for wire query.
  275. Arguments:
  276. pBlob -- query blob
  277. pName -- name; may be any sort of name
  278. pDomain -- domain name to append
  279. Return Value:
  280. TRUE if name query will be valid.
  281. FALSE otherwise.
  282. --*/
  283. {
  284. WORD wtype;
  285. PSTR pnameTld;
  286. PSTR pdomainTld;
  287. // no screening -- bail
  288. if ( g_ScreenBadTlds == 0 )
  289. {
  290. return TRUE;
  291. }
  292. // only screening for standard types
  293. // - A, AAAA, SRV
  294. wtype = pBlob->wType;
  295. if ( wtype != DNS_TYPE_A &&
  296. wtype != DNS_TYPE_AAAA &&
  297. wtype != DNS_TYPE_SRV )
  298. {
  299. return TRUE;
  300. }
  301. // get name TLD
  302. pnameTld = Dns_GetTldForName( pName );
  303. //
  304. // if no domain appended
  305. // - exclude single label
  306. // - exclude bad TLD (numeric, bogus domain)
  307. // - but allow root queries
  308. //
  309. // DCR: MS DCS screening
  310. // screen
  311. // _msdcs.<name>
  312. // will probably be unappended query
  313. //
  314. if ( !pDomain )
  315. {
  316. if ( !pnameTld ||
  317. !ValidateQueryTld( pnameTld ) )
  318. {
  319. goto Failed;
  320. }
  321. return TRUE;
  322. }
  323. //
  324. // domain appended
  325. // - exclude bad TLD (numeric, bogus domain)
  326. // - exclude matching TLD
  327. //
  328. pdomainTld = Dns_GetTldForName( pDomain );
  329. if ( !pdomainTld )
  330. {
  331. pdomainTld = pDomain;
  332. }
  333. if ( !ValidateQueryTld( pdomainTld ) )
  334. {
  335. goto Failed;
  336. }
  337. // screen repeated TLD
  338. if ( g_ScreenBadTlds & DNS_TLD_SCREEN_REPEATED )
  339. {
  340. if ( Dns_NameCompare_UTF8(
  341. pnameTld,
  342. pdomainTld ) )
  343. {
  344. goto Failed;
  345. }
  346. }
  347. return TRUE;
  348. Failed:
  349. DNSDBG( QUERY, (
  350. "Failed invalid query name:\n"
  351. "\tname %s\n"
  352. "\tdomain %s\n",
  353. pName,
  354. pDomain ));
  355. return FALSE;
  356. }
  357. PSTR
  358. GetNextAdapterDomainName(
  359. IN OUT PDNS_NETINFO pNetInfo
  360. )
  361. /*++
  362. Routine Description:
  363. Get next adapter domain name to query.
  364. Arguments:
  365. pNetInfo -- DNS Network info for query;
  366. adapter data will be modified (InfoFlags field)
  367. to indicate which adapter to query and which
  368. to skip query on
  369. Return Value:
  370. Ptr to domain name (UTF8) to query.
  371. NULL if no more domain names to query.
  372. --*/
  373. {
  374. DWORD iter;
  375. PSTR pqueryDomain = NULL;
  376. DNSDBG( TRACE, ( "GetNextAdapterDomainName()\n" ));
  377. if ( ! pNetInfo )
  378. {
  379. ASSERT( FALSE );
  380. return NULL;
  381. }
  382. IF_DNSDBG( OFF )
  383. {
  384. DnsDbg_NetworkInfo(
  385. "Net info to get adapter domain name from: ",
  386. pNetInfo );
  387. }
  388. //
  389. // check each adapter
  390. // - first unqueried adapter with name is chosen
  391. // - other adapters with
  392. // - matching name => included in query
  393. // - non-matching => turned OFF for query
  394. //
  395. // DCR: query on\off should use adapter dynamic flags
  396. //
  397. for ( iter = 0; iter < pNetInfo->AdapterCount; iter++ )
  398. {
  399. PDNS_ADAPTER padapter;
  400. PSTR pdomain;
  401. padapter = pNetInfo->AdapterArray[iter];
  402. // netinfo should come in with name specific flags clean
  403. DNS_ASSERT( !(padapter->RunFlags & RUN_FLAG_SINGLE_NAME_MASK) );
  404. //
  405. // ignore
  406. // - ignored adapter OR
  407. // - previously queried adapter domain
  408. // note: it can't match any "fresh" domain we come up with
  409. // as we always collect all matches
  410. if ( (padapter->InfoFlags & DNS_FLAG_IGNORE_ADAPTER)
  411. ||
  412. (padapter->RunFlags & RUN_FLAG_QUERIED_ADAPTER_DOMAIN) )
  413. {
  414. padapter->RunFlags |= RUN_FLAG_STOP_QUERY_ON_ADAPTER;
  415. continue;
  416. }
  417. // no domain name -- always off
  418. pdomain = padapter->pszAdapterDomain;
  419. if ( !pdomain )
  420. {
  421. padapter->RunFlags |= (RUN_FLAG_QUERIED_ADAPTER_DOMAIN |
  422. RUN_FLAG_STOP_QUERY_ON_ADAPTER);
  423. continue;
  424. }
  425. // first "fresh" domain name -- save, turn on and flag as used
  426. if ( !pqueryDomain )
  427. {
  428. pqueryDomain = pdomain;
  429. padapter->RunFlags |= RUN_FLAG_QUERIED_ADAPTER_DOMAIN;
  430. continue;
  431. }
  432. // other "fresh" domain names
  433. // - if matches query domain => on for query
  434. // - no match => off
  435. if ( Dns_NameCompare_UTF8(
  436. pqueryDomain,
  437. pdomain ) )
  438. {
  439. padapter->RunFlags |= RUN_FLAG_QUERIED_ADAPTER_DOMAIN;
  440. continue;
  441. }
  442. else
  443. {
  444. padapter->RunFlags |= RUN_FLAG_STOP_QUERY_ON_ADAPTER;
  445. continue;
  446. }
  447. }
  448. //
  449. // if no adapter domain name -- clear STOP flag
  450. // - all adapters participate in other names (name devolution)
  451. //
  452. if ( !pqueryDomain )
  453. {
  454. for ( iter = 0; iter < pNetInfo->AdapterCount; iter++ )
  455. {
  456. PDNS_ADAPTER padapter = pNetInfo->AdapterArray[iter];
  457. padapter->RunFlags &= (~RUN_FLAG_SINGLE_NAME_MASK );
  458. }
  459. DNSDBG( INIT2, (
  460. "GetNextAdapterDomainName out of adapter names.\n" ));
  461. pNetInfo->ReturnFlags |= RUN_FLAG_QUERIED_ADAPTER_DOMAIN;
  462. }
  463. IF_DNSDBG( INIT2 )
  464. {
  465. if ( pqueryDomain )
  466. {
  467. DnsDbg_NetworkInfo(
  468. "Net info after adapter name select: ",
  469. pNetInfo );
  470. }
  471. }
  472. DNSDBG( INIT2, (
  473. "Leaving GetNextAdapterDomainName() => %s\n",
  474. pqueryDomain ));
  475. return pqueryDomain;
  476. }
  477. PSTR
  478. GetNextDomainNameToAppend(
  479. IN OUT PDNS_NETINFO pNetInfo,
  480. OUT PDWORD pSuffixFlags
  481. )
  482. /*++
  483. Routine Description:
  484. Get next adapter domain name to query.
  485. Arguments:
  486. pNetInfo -- DNS Network info for query;
  487. adapter data will be modified (RunFlags field)
  488. to indicate which adapter to query and which
  489. to skip query on
  490. pSuffixFlags -- flags associated with the use of this suffix
  491. Return Value:
  492. Ptr to domain name (UTF8) to query.
  493. NULL if no more domain names to query.
  494. --*/
  495. {
  496. PSTR psearchName;
  497. PSTR pdomain;
  498. //
  499. // search list if real search list
  500. //
  501. // if suffix flags zero, then this is REAL search list
  502. // or is PDN name
  503. //
  504. psearchName = SearchList_GetNextName(
  505. pNetInfo->pSearchList,
  506. FALSE, // not reset
  507. pSuffixFlags );
  508. if ( psearchName && (*pSuffixFlags == 0) )
  509. {
  510. // found regular search name -- done
  511. DNSDBG( INIT2, (
  512. "getNextDomainName from search list => %s, %d\n",
  513. psearchName,
  514. *pSuffixFlags ));
  515. return( psearchName );
  516. }
  517. //
  518. // try adapter domain names
  519. //
  520. // but ONLY if search list is dummy; if real we only
  521. // use search list entries
  522. //
  523. // DCR_CLEANUP: eliminate bogus search list
  524. //
  525. if ( pNetInfo->InfoFlags & DNS_FLAG_DUMMY_SEARCH_LIST
  526. &&
  527. ! (pNetInfo->ReturnFlags & RUN_FLAG_QUERIED_ADAPTER_DOMAIN) )
  528. {
  529. pdomain = GetNextAdapterDomainName( pNetInfo );
  530. if ( pdomain )
  531. {
  532. *pSuffixFlags = DNS_QUERY_USE_QUICK_TIMEOUTS;
  533. DNSDBG( INIT2, (
  534. "getNextDomainName from adapter domain name => %s, %d\n",
  535. pdomain,
  536. *pSuffixFlags ));
  537. // back the search list up one tick
  538. // we queried through it above, so if it was returing
  539. // a name, we need to get that name again on next query
  540. if ( psearchName )
  541. {
  542. ASSERT( pNetInfo->pSearchList->CurrentNameIndex > 0 );
  543. pNetInfo->pSearchList->CurrentNameIndex--;
  544. }
  545. return( pdomain );
  546. }
  547. }
  548. //
  549. // DCR_CLEANUP: remove devolution from search list and do explicitly
  550. // - its cheap (or do it once and save, but store separately)
  551. //
  552. //
  553. // finally use and devolved search names (or other nonsense)
  554. //
  555. *pSuffixFlags = DNS_QUERY_USE_QUICK_TIMEOUTS;
  556. DNSDBG( INIT2, (
  557. "getNextDomainName from devolution\\other => %s, %d\n",
  558. psearchName,
  559. *pSuffixFlags ));
  560. return( psearchName );
  561. }
  562. PSTR
  563. GetNextQueryName(
  564. IN OUT PQUERY_BLOB pBlob
  565. )
  566. /*++
  567. Routine Description:
  568. Get next name to query.
  569. Arguments:
  570. pBlob - blob of query information
  571. Uses:
  572. NameOriginalWire
  573. NameAttributes
  574. QueryCount
  575. pNetworkInfo
  576. Sets:
  577. NameWire -- set with appended wire name
  578. pNetworkInfo -- runtime flags set to indicate which adapters are
  579. queried
  580. NameFlags -- set with properties of name
  581. fAppendedName -- set when name appended
  582. Return Value:
  583. Ptr to name to query with.
  584. - will be orginal name on first query if name is multilabel name
  585. - otherwise will be NameWire buffer which will contain appended name
  586. composed of pszName and some domain name
  587. NULL if no more names to append
  588. --*/
  589. {
  590. PSTR pnameOrig = pBlob->NameOriginalWire;
  591. PSTR pdomainName = NULL;
  592. PSTR pnameBuf;
  593. DWORD queryCount = pBlob->QueryCount;
  594. DWORD nameAttributes = pBlob->NameAttributes;
  595. DNSDBG( TRACE, (
  596. "GetNextQueryName( %p )\n",
  597. pBlob ));
  598. // default suffix flags
  599. pBlob->NameFlags = 0;
  600. //
  601. // FQDN
  602. // - send FQDN only
  603. //
  604. if ( nameAttributes & DNS_NAME_IS_FQDN )
  605. {
  606. if ( queryCount == 0 )
  607. {
  608. #if 0
  609. // currently won't even validate FQDN
  610. if ( ValidateQueryName(
  611. pBlob,
  612. pnameOrig,
  613. NULL ) )
  614. {
  615. return pnameOrig;
  616. }
  617. #endif
  618. return pnameOrig;
  619. }
  620. DNSDBG( QUERY, (
  621. "No append for FQDN name %s -- end query.\n",
  622. pnameOrig ));
  623. return NULL;
  624. }
  625. //
  626. // multilabel
  627. // - first pass on name itself -- if valid
  628. //
  629. // DCR: intelligent choice on multi-label whether append first
  630. // or go to wire first (example foo.ntdev) could append
  631. // first
  632. //
  633. if ( nameAttributes & DNS_NAME_MULTI_LABEL )
  634. {
  635. if ( queryCount == 0 )
  636. {
  637. if ( ValidateQueryName(
  638. pBlob,
  639. pnameOrig,
  640. NULL ) )
  641. {
  642. return pnameOrig;
  643. }
  644. }
  645. if ( !g_AppendToMultiLabelName )
  646. {
  647. DNSDBG( QUERY, (
  648. "No append allowed on multi-label name %s -- end query.\n",
  649. pnameOrig ));
  650. return NULL;
  651. }
  652. // falls through to appending on multi-label names
  653. }
  654. //
  655. // not FQDN -- append a domain name
  656. // - next search name (if available)
  657. // - otherwise next adapter domain name
  658. //
  659. pnameBuf = pBlob->NameWire;
  660. while ( 1 )
  661. {
  662. pdomainName = GetNextDomainNameToAppend(
  663. pBlob->pNetworkInfo,
  664. & pBlob->NameFlags );
  665. if ( !pdomainName )
  666. {
  667. DNSDBG( QUERY, (
  668. "No more domain names to append -- end query\n" ));
  669. return NULL;
  670. }
  671. if ( !ValidateQueryName(
  672. pBlob,
  673. pnameOrig,
  674. pdomainName ) )
  675. {
  676. continue;
  677. }
  678. // append domain name to name
  679. if ( Dns_NameAppend_A(
  680. pnameBuf,
  681. DNS_MAX_NAME_BUFFER_LENGTH,
  682. pnameOrig,
  683. pdomainName ) )
  684. {
  685. pBlob->fAppendedName = TRUE;
  686. break;
  687. }
  688. }
  689. DNSDBG( QUERY, (
  690. "GetNextQueryName() result => %s\n",
  691. pnameBuf ));
  692. return pnameBuf;
  693. }
  694. DNS_STATUS
  695. QueryDirectEx(
  696. IN OUT PDNS_MSG_BUF * ppMsgResponse,
  697. OUT PDNS_RECORD * ppResponseRecords,
  698. IN PDNS_HEADER pHeader,
  699. IN BOOL fNoHeaderCounts,
  700. IN PDNS_NAME pszQuestionName,
  701. IN WORD wQuestionType,
  702. IN PDNS_RECORD pRecords,
  703. IN DWORD dwFlags,
  704. IN PIP_ARRAY aipDnsServers,
  705. IN OUT PDNS_NETINFO pNetInfo
  706. )
  707. /*++
  708. Routine Description:
  709. Query.
  710. Arguments:
  711. ppMsgResponse -- addr to recv ptr to response buffer; caller MUST
  712. free buffer
  713. ppResponseRecord -- address to receive ptr to record list returned from query
  714. pHead -- DNS header to send
  715. fNoHeaderCounts - do NOT include record counts in copying header
  716. pszQuestionName -- DNS name to query;
  717. Unicode string if dwFlags has DNSQUERY_UNICODE_NAME set.
  718. ANSI string otherwise.
  719. wType -- query type
  720. pRecords -- address to receive ptr to record list returned from query
  721. dwFlags -- query flags
  722. aipDnsServers -- specific DNS servers to query;
  723. OPTIONAL, if specified overrides normal list associated with machine
  724. pDnsNetAdapters -- DNS servers to query; if NULL get current list
  725. Return Value:
  726. ERROR_SUCCESS if successful.
  727. Error code on failure.
  728. --*/
  729. {
  730. PDNS_MSG_BUF psendMsg;
  731. DNS_STATUS status = DNS_ERROR_NO_MEMORY;
  732. DNSDBG( QUERY, (
  733. "QueryDirectEx()\n"
  734. "\tname %s\n"
  735. "\ttype %d\n"
  736. "\theader %p\n"
  737. "\t - counts %d\n"
  738. "\trecords %p\n"
  739. "\tflags %08x\n"
  740. "\trecv msg %p\n"
  741. "\trecv records %p\n"
  742. "\tserver IPs %p\n"
  743. "\tadapter list %p\n",
  744. pszQuestionName,
  745. wQuestionType,
  746. pHeader,
  747. fNoHeaderCounts,
  748. pRecords,
  749. dwFlags,
  750. ppMsgResponse,
  751. ppResponseRecords,
  752. aipDnsServers,
  753. pNetInfo ));
  754. //
  755. // build send packet
  756. //
  757. psendMsg = Dns_BuildPacket(
  758. pHeader,
  759. fNoHeaderCounts,
  760. pszQuestionName,
  761. wQuestionType,
  762. pRecords,
  763. dwFlags,
  764. FALSE // query, not an update
  765. );
  766. if ( !psendMsg )
  767. {
  768. status = ERROR_INVALID_NAME;
  769. goto Cleanup;
  770. }
  771. #if MULTICAST_ENABLED
  772. //
  773. // QUESTION: mcast test is not complete here
  774. // - should first test that we actually do it
  775. // including whether we have DNS servers
  776. // FIXME: then when we do do it -- encapsulate it
  777. // ShouldMulicastQuery()
  778. //
  779. // Check to see if name is for something in the multicast local domain.
  780. // If so, set flag to multicast this query only.
  781. //
  782. if ( Dns_NameCompareEx( pszQuestionName,
  783. ( dwFlags & DNSQUERY_UNICODE_NAME ) ?
  784. (LPSTR) MULTICAST_DNS_LOCAL_DOMAIN_W :
  785. MULTICAST_DNS_LOCAL_DOMAIN,
  786. 0,
  787. ( dwFlags & DNSQUERY_UNICODE_NAME ) ?
  788. DnsCharSetUnicode :
  789. DnsCharSetUtf8 ) ==
  790. DnsNameCompareRightParent )
  791. {
  792. dwFlags |= DNS_QUERY_MULTICAST_ONLY;
  793. }
  794. #endif
  795. //
  796. // send query and receive response
  797. //
  798. Trace_LogQueryEvent(
  799. psendMsg,
  800. wQuestionType );
  801. status = Dns_SendAndRecv(
  802. psendMsg,
  803. ppMsgResponse,
  804. ppResponseRecords,
  805. dwFlags,
  806. aipDnsServers,
  807. pNetInfo );
  808. Trace_LogResponseEvent(
  809. psendMsg,
  810. ( ppResponseRecords && *ppResponseRecords )
  811. ? (*ppResponseRecords)->wType
  812. : 0,
  813. status );
  814. Cleanup:
  815. FREE_HEAP( psendMsg );
  816. DNSDBG( QUERY, (
  817. "Leaving QueryDirectEx(), status = %s (%d)\n",
  818. Dns_StatusString(status),
  819. status ));
  820. return( status );
  821. }
  822. DNS_STATUS
  823. Query_SingleName(
  824. IN OUT PQUERY_BLOB pBlob
  825. )
  826. /*++
  827. Routine Description:
  828. Query single name.
  829. Arguments:
  830. pBlob - query blob
  831. Return Value:
  832. ERROR_SUCCESS if successful.
  833. Error code on failure.
  834. --*/
  835. {
  836. PDNS_MSG_BUF psendMsg = NULL;
  837. DNS_STATUS status = DNS_ERROR_NO_MEMORY;
  838. DWORD flags = pBlob->Flags;
  839. DNSDBG( QUERY, (
  840. "Query_SingleName( %p )\n",
  841. pBlob ));
  842. IF_DNSDBG( QUERY )
  843. {
  844. DnsDbg_QueryBlob(
  845. "Enter Query_SingleName()",
  846. pBlob );
  847. }
  848. //
  849. // cache\hostfile callback on appended name
  850. // - note that queried name was already done
  851. // (in resolver or in Query_Main())
  852. //
  853. if ( pBlob->pfnQueryCache && pBlob->fAppendedName )
  854. {
  855. if ( (pBlob->pfnQueryCache)( pBlob ) )
  856. {
  857. status = pBlob->Status;
  858. goto Cleanup;
  859. }
  860. }
  861. //
  862. // if wire disallowed -- stop here
  863. //
  864. if ( flags & DNS_QUERY_NO_WIRE_QUERY )
  865. {
  866. status = DNS_ERROR_NAME_NOT_FOUND_LOCALLY;
  867. pBlob->Status = status;
  868. goto Cleanup;
  869. }
  870. //
  871. // build send packet
  872. //
  873. psendMsg = Dns_BuildPacket(
  874. NULL, // no header
  875. 0, // no header counts
  876. pBlob->pNameWire,
  877. pBlob->wType,
  878. NULL, // no records
  879. flags,
  880. FALSE // query, not an update
  881. );
  882. if ( !psendMsg )
  883. {
  884. status = DNS_ERROR_INVALID_NAME;
  885. goto Cleanup;
  886. }
  887. #if MULTICAST_ENABLED
  888. //
  889. // QUESTION: mcast test is not complete here
  890. // - should first test that we actually do it
  891. // including whether we have DNS servers
  892. // FIXME: then when we do do it -- encapsulate it
  893. // ShouldMulicastQuery()
  894. //
  895. // Check to see if name is for something in the multicast local domain.
  896. // If so, set flag to multicast this query only.
  897. //
  898. if ( Dns_NameCompareEx(
  899. pBlob->pName,
  900. ( flags & DNSQUERY_UNICODE_NAME )
  901. ? (LPSTR) MULTICAST_DNS_LOCAL_DOMAIN_W
  902. : MULTICAST_DNS_LOCAL_DOMAIN,
  903. 0,
  904. ( flags & DNSQUERY_UNICODE_NAME )
  905. ? DnsCharSetUnicode
  906. : DnsCharSetUtf8 )
  907. == DnsNameCompareRightParent )
  908. {
  909. flags |= DNS_QUERY_MULTICAST_ONLY;
  910. }
  911. #endif
  912. //
  913. // send query and receive response
  914. //
  915. Trace_LogQueryEvent(
  916. psendMsg,
  917. pBlob->wType );
  918. status = Dns_SendAndRecv(
  919. psendMsg,
  920. (flags & DNS_QUERY_RETURN_MESSAGE)
  921. ? &pBlob->pRecvMsg
  922. : NULL,
  923. & pBlob->pRecords,
  924. flags,
  925. pBlob->pDnsServers,
  926. pBlob->pNetworkInfo
  927. );
  928. Trace_LogResponseEvent(
  929. psendMsg,
  930. ( pBlob->pRecords )
  931. ? (pBlob->pRecords)->wType
  932. : 0,
  933. status );
  934. Cleanup:
  935. FREE_HEAP( psendMsg );
  936. DNSDBG( QUERY, (
  937. "Leaving Query_SingleName(), status = %s (%d)\n",
  938. Dns_StatusString(status),
  939. status ));
  940. IF_DNSDBG( QUERY )
  941. {
  942. DnsDbg_QueryBlob(
  943. "Blob leaving Query_SingleName()",
  944. pBlob );
  945. }
  946. return( status );
  947. }
  948. DNS_STATUS
  949. Query_Main(
  950. IN OUT PQUERY_BLOB pBlob
  951. )
  952. /*++
  953. Routine Description:
  954. Main query routine.
  955. Does all the query processing
  956. - local lookup
  957. - name appending
  958. - cache\hostfile lookup on appended name
  959. - query to server
  960. Arguments:
  961. pBlob -- query info blob
  962. Return Value:
  963. ERROR_SUCCESS if successful response.
  964. DNS_INFO_NO_RECORDS on no records for type response.
  965. DNS_ERROR_RCODE_NAME_ERROR on name error.
  966. DNS_ERROR_INVALID_NAME on bad name.
  967. None
  968. --*/
  969. {
  970. DNS_STATUS status = DNS_ERROR_NAME_NOT_FOUND_LOCALLY;
  971. PSTR pdomainName = NULL;
  972. PDNS_RECORD precords;
  973. DWORD queryFlags;
  974. DWORD suffixFlags = 0;
  975. DWORD nameAttributes;
  976. DNS_STATUS bestQueryStatus = ERROR_SUCCESS;
  977. BOOL fcacheNegative = TRUE;
  978. DWORD flagsIn = pBlob->Flags;
  979. PDNS_NETINFO pnetInfo = pBlob->pNetworkInfo;
  980. DWORD nameLength;
  981. DWORD bufLength;
  982. DWORD queryCount;
  983. DNSDBG( TRACE, (
  984. "Query_Main( %p )\n"
  985. "\t%S, f=%08x, type=%d, time = %d\n",
  986. pBlob,
  987. pBlob->pNameOrig,
  988. flagsIn,
  989. pBlob->wType,
  990. Dns_GetCurrentTimeInSeconds()
  991. ));
  992. //
  993. // clear out params
  994. //
  995. pBlob->pRecords = NULL;
  996. pBlob->pLocalRecords = NULL;
  997. pBlob->fCacheNegative = FALSE;
  998. pBlob->fNoIpLocal = FALSE;
  999. pBlob->NetFailureStatus = ERROR_SUCCESS;
  1000. //
  1001. // convert name to wire format
  1002. //
  1003. bufLength = DNS_MAX_NAME_BUFFER_LENGTH;
  1004. nameLength = Dns_NameCopy(
  1005. pBlob->NameOriginalWire,
  1006. & bufLength,
  1007. (PSTR) pBlob->pNameOrig,
  1008. 0, // name is NULL terminated
  1009. DnsCharSetUnicode,
  1010. DnsCharSetWire );
  1011. if ( nameLength == 0 )
  1012. {
  1013. return DNS_ERROR_INVALID_NAME;
  1014. }
  1015. nameLength--;
  1016. pBlob->NameLength = nameLength;
  1017. pBlob->pNameOrigWire = pBlob->NameOriginalWire;
  1018. //
  1019. // determine name properties
  1020. // - determines number and order of name queries
  1021. //
  1022. nameAttributes = Dns_GetNameAttributes( pBlob->NameOriginalWire );
  1023. if ( flagsIn & DNS_QUERY_TREAT_AS_FQDN )
  1024. {
  1025. nameAttributes |= DNS_NAME_IS_FQDN;
  1026. }
  1027. pBlob->NameAttributes = nameAttributes;
  1028. //
  1029. // hostfile lookup
  1030. // - called in process
  1031. // - hosts file lookup allowed
  1032. // -> then must do hosts file lookup before appending\queries
  1033. //
  1034. // note: this matches the hostsfile\cache lookup in resolver
  1035. // before call; hosts file queries to appended names are
  1036. // handled together by callback in Query_SingleName()
  1037. //
  1038. // we MUST make this callback here, because it must PRECEDE
  1039. // the local name call, as some customers specifically direct
  1040. // some local mappings in the hosts file
  1041. //
  1042. if ( pBlob->pfnQueryCache == QueryHostFile
  1043. &&
  1044. ! (flagsIn & DNS_QUERY_NO_HOSTS_FILE) )
  1045. {
  1046. pBlob->pNameWire = pBlob->pNameOrigWire;
  1047. if ( QueryHostFile( pBlob ) )
  1048. {
  1049. status = pBlob->Status;
  1050. goto Done;
  1051. }
  1052. }
  1053. //
  1054. // check for local name
  1055. // - if successful, skip wire query
  1056. //
  1057. if ( ! (flagsIn & DNS_QUERY_NO_LOCAL_NAME) )
  1058. {
  1059. status = GetRecordsForLocalName( pBlob );
  1060. if ( status == ERROR_SUCCESS &&
  1061. !pBlob->fNoIpLocal )
  1062. {
  1063. DNS_ASSERT( pBlob->pRecords &&
  1064. pBlob->pRecords == pBlob->pLocalRecords );
  1065. goto Done;
  1066. }
  1067. }
  1068. //
  1069. // query until
  1070. // - successfull
  1071. // - exhaust names to query with
  1072. //
  1073. queryCount = 0;
  1074. while ( 1 )
  1075. {
  1076. PSTR pqueryName;
  1077. // clean name specific info from list
  1078. if ( queryCount != 0 )
  1079. {
  1080. NetInfo_Clean(
  1081. pnetInfo,
  1082. CLEAR_LEVEL_SINGLE_NAME );
  1083. }
  1084. //
  1085. // next query name
  1086. //
  1087. pqueryName = GetNextQueryName( pBlob );
  1088. if ( !pqueryName )
  1089. {
  1090. if ( queryCount == 0 )
  1091. {
  1092. status = DNS_ERROR_INVALID_NAME;
  1093. }
  1094. break;
  1095. }
  1096. pBlob->QueryCount = ++queryCount;
  1097. pBlob->pNameWire = pqueryName;
  1098. DNSDBG( QUERY, (
  1099. "Query %d is for name %s\n",
  1100. queryCount,
  1101. pqueryName ));
  1102. //
  1103. // set flags
  1104. // - passed in flags
  1105. // - unicode results
  1106. // - flags for this particular suffix
  1107. pBlob->Flags = flagsIn | pBlob->NameFlags;
  1108. //
  1109. // clear any previously received records (shouldn't be any)
  1110. //
  1111. if ( pBlob->pRecords )
  1112. {
  1113. DNS_ASSERT( FALSE );
  1114. Dns_RecordListFree( pBlob->pRecords );
  1115. pBlob->pRecords = NULL;
  1116. }
  1117. //
  1118. // do the query for name
  1119. // includes
  1120. // - cache or hostfile lookup
  1121. // - wire query
  1122. //
  1123. status = Query_SingleName( pBlob );
  1124. //
  1125. // clean out records on "non-response"
  1126. //
  1127. // DCR: need to fix record return
  1128. // - should keep records on any response (best response)
  1129. // just make sure NO_RECORDS rcode is mapped
  1130. //
  1131. // the only time we keep them is FAZ
  1132. // - ALLOW_EMPTY_AUTH flag set
  1133. // - sending FQDN (or more precisely doing single query)
  1134. //
  1135. precords = pBlob->pRecords;
  1136. if ( precords )
  1137. {
  1138. if ( IsEmptyDnsResponse( precords ) )
  1139. {
  1140. if ( (flagsIn & DNS_QUERY_ALLOW_EMPTY_AUTH_RESP)
  1141. &&
  1142. ( (nameAttributes & DNS_NAME_IS_FQDN)
  1143. ||
  1144. ((nameAttributes & DNS_NAME_MULTI_LABEL) &&
  1145. !g_AppendToMultiLabelName ) ) )
  1146. {
  1147. // stop here as caller (probably FAZ code)
  1148. // wants to get the authority records
  1149. DNSDBG( QUERY, (
  1150. "Returning empty query response with authority records.\n" ));
  1151. break;
  1152. }
  1153. else
  1154. {
  1155. Dns_RecordListFree( precords );
  1156. pBlob->pRecords = NULL;
  1157. if ( status == NO_ERROR )
  1158. {
  1159. status = DNS_INFO_NO_RECORDS;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. // successful query -- done
  1165. if ( status == ERROR_SUCCESS )
  1166. {
  1167. RTL_ASSERT( precords );
  1168. break;
  1169. }
  1170. #if 0
  1171. //
  1172. // DCR_FIX0: lost adapter timeout from early in multi-name query
  1173. // - callback here or some other approach
  1174. //
  1175. // this is resolver version
  1176. //
  1177. // reset server priorities on failures
  1178. // do here to avoid washing out info in retry with new name
  1179. //
  1180. if ( status != ERROR_SUCCESS &&
  1181. (pnetInfo->ReturnFlags & DNS_FLAG_RESET_SERVER_PRIORITY) )
  1182. {
  1183. if ( g_AdapterTimeoutCacheTime &&
  1184. Dns_DisableTimedOutAdapters( pnetInfo ) )
  1185. {
  1186. fadapterTimedOut = TRUE;
  1187. SetKnownTimedOutAdapter();
  1188. }
  1189. }
  1190. //
  1191. // DCR_CLEANUP: lost intermediate timed out adapter deal
  1192. //
  1193. if ( status != NO_ERROR &&
  1194. (pnetInfo->ReturnFlags & DNS_FLAG_RESET_SERVER_PRIORITY) )
  1195. {
  1196. Dns_DisableTimedOutAdapters( pnetInfo );
  1197. }
  1198. #endif
  1199. //
  1200. // save first query error (for some errors)
  1201. //
  1202. if ( queryCount == 1 &&
  1203. ( status == DNS_ERROR_RCODE_NAME_ERROR ||
  1204. status == DNS_INFO_NO_RECORDS ||
  1205. status == DNS_ERROR_INVALID_NAME ||
  1206. status == DNS_ERROR_RCODE_SERVER_FAILURE ||
  1207. status == DNS_ERROR_RCODE_FORMAT_ERROR ) )
  1208. {
  1209. DNSDBG( QUERY, (
  1210. "Saving bestQueryStatus %d\n",
  1211. status ));
  1212. bestQueryStatus = status;
  1213. }
  1214. //
  1215. // continue with other queries on some errors
  1216. //
  1217. // on NAME_ERROR or NO_RECORDS response
  1218. // - check if this negative result will be
  1219. // cacheable, if it holds up
  1220. //
  1221. // note: the reason we check every time is that when the
  1222. // query involves several names, one or more may fail
  1223. // with one network timing out, YET the final name
  1224. // queried indeed is a NAME_ERROR everywhere; hence
  1225. // we can not do the check just once on the final
  1226. // negative response;
  1227. // in short, every negative response must be determinative
  1228. // in order for us to cache
  1229. //
  1230. if ( status == DNS_ERROR_RCODE_NAME_ERROR ||
  1231. status == DNS_INFO_NO_RECORDS )
  1232. {
  1233. if ( fcacheNegative )
  1234. {
  1235. fcacheNegative = IsCacheableNameError( pnetInfo );
  1236. }
  1237. if ( status == DNS_INFO_NO_RECORDS )
  1238. {
  1239. DNSDBG( QUERY, (
  1240. "Saving bestQueryStatus %d\n",
  1241. status ));
  1242. bestQueryStatus = status;
  1243. }
  1244. continue;
  1245. }
  1246. // server failure may indicate intermediate or remote
  1247. // server timeout and hence also makes any final
  1248. // name error determination uncacheable
  1249. else if ( status == DNS_ERROR_RCODE_SERVER_FAILURE )
  1250. {
  1251. fcacheNegative = FALSE;
  1252. continue;
  1253. }
  1254. // busted name errors
  1255. // - just continue with next query
  1256. else if ( status == DNS_ERROR_INVALID_NAME ||
  1257. status == DNS_ERROR_RCODE_FORMAT_ERROR )
  1258. {
  1259. continue;
  1260. }
  1261. //
  1262. // other errors -- ex. timeout and winsock -- are terminal
  1263. //
  1264. else
  1265. {
  1266. fcacheNegative = FALSE;
  1267. break;
  1268. }
  1269. }
  1270. DNSDBG( QUERY, (
  1271. "Query_Main() -- name loop termination\n"
  1272. "\tstatus = %d\n"
  1273. "\tquery count = %d\n",
  1274. status,
  1275. queryCount ));
  1276. //
  1277. // if no queries then invalid name
  1278. // - either name itself is invalid
  1279. // OR
  1280. // - single part name and don't have anything to append
  1281. //
  1282. DNS_ASSERT( queryCount != 0 ||
  1283. status == DNS_ERROR_INVALID_NAME );
  1284. //
  1285. // success -- prioritize record data
  1286. //
  1287. // to prioritize
  1288. // - prioritize is set
  1289. // - have more than one A record
  1290. // - can get IP list
  1291. //
  1292. // note: need the callback because resolver uses directly
  1293. // local copy of IP address info, whereas direct query
  1294. // RPC's a copy over from the resolver
  1295. //
  1296. // alternative would be some sort of "set IP source"
  1297. // function that resolver would call when there's a
  1298. // new list; then could have common function that
  1299. // picks up source if available or does RPC
  1300. //
  1301. if ( status == ERROR_SUCCESS )
  1302. {
  1303. if ( g_PrioritizeRecordData &&
  1304. Dns_RecordListCount( precords, DNS_TYPE_A ) > 1 &&
  1305. pBlob->pfnGetAddrArray )
  1306. {
  1307. PDNS_ADDR_ARRAY paddrArray = (pBlob->pfnGetAddrArray)();
  1308. if ( paddrArray )
  1309. {
  1310. pBlob->pRecords = Dns_PrioritizeRecordSetEx(
  1311. precords,
  1312. paddrArray );
  1313. FREE_HEAP( paddrArray );
  1314. }
  1315. }
  1316. }
  1317. //
  1318. // no-op common negative response
  1319. // doing this for perf to skip extensive status code check below
  1320. //
  1321. else if ( status == DNS_ERROR_RCODE_NAME_ERROR ||
  1322. status == DNS_INFO_NO_RECORDS )
  1323. {
  1324. // no-op
  1325. }
  1326. //
  1327. // timeout indicates possible network problem
  1328. // winsock errors indicate definite network problem
  1329. //
  1330. else if (
  1331. status == ERROR_TIMEOUT ||
  1332. status == WSAEFAULT ||
  1333. status == WSAENOTSOCK ||
  1334. status == WSAENETDOWN ||
  1335. status == WSAENETUNREACH ||
  1336. status == WSAEPFNOSUPPORT ||
  1337. status == WSAEAFNOSUPPORT ||
  1338. status == WSAEHOSTDOWN ||
  1339. status == WSAEHOSTUNREACH )
  1340. {
  1341. pBlob->NetFailureStatus = status;
  1342. }
  1343. #if 0
  1344. //
  1345. // DCR: not sure when to free message buffer
  1346. //
  1347. // - it is reused in Dns_QueryLib call, so no leak
  1348. // - point is when to return it
  1349. // - old QuickQueryEx() would dump when going around again?
  1350. // not sure of the point of that
  1351. //
  1352. //
  1353. // going around again -- free up message buffer
  1354. //
  1355. if ( ppMsgResponse && *ppMsgResponse )
  1356. {
  1357. FREE_HEAP( *ppMsgResponse );
  1358. *ppMsgResponse = NULL;
  1359. }
  1360. #endif
  1361. //
  1362. // use NO-IP local name?
  1363. //
  1364. // if matched local name but had no IPs (IP6 currently)
  1365. // then use default here if not successful wire query
  1366. //
  1367. if ( pBlob->fNoIpLocal )
  1368. {
  1369. if ( status != ERROR_SUCCESS )
  1370. {
  1371. Dns_RecordListFree( pBlob->pRecords );
  1372. pBlob->pRecords = pBlob->pLocalRecords;
  1373. status = ERROR_SUCCESS;
  1374. pBlob->Status = status;
  1375. }
  1376. else
  1377. {
  1378. Dns_RecordListFree( pBlob->pLocalRecords );
  1379. pBlob->pLocalRecords = NULL;
  1380. }
  1381. }
  1382. //
  1383. // if error, use "best" error
  1384. // this is either
  1385. // - original query response
  1386. // - or NO_RECORDS response found later
  1387. //
  1388. if ( status != ERROR_SUCCESS && bestQueryStatus )
  1389. {
  1390. status = bestQueryStatus;
  1391. pBlob->Status = status;
  1392. }
  1393. //
  1394. // set negative response cacheability
  1395. //
  1396. pBlob->fCacheNegative = fcacheNegative;
  1397. Done:
  1398. DNS_ASSERT( !pBlob->pLocalRecords ||
  1399. pBlob->pLocalRecords == pBlob->pRecords );
  1400. DNSDBG( TRACE, (
  1401. "Leave Query_Main()\n"
  1402. "\tstatus = %d\n"
  1403. "\ttime = %d\n",
  1404. status,
  1405. Dns_GetCurrentTimeInSeconds()
  1406. ));
  1407. IF_DNSDBG( QUERY )
  1408. {
  1409. DnsDbg_QueryBlob(
  1410. "Blob leaving Query_Main()",
  1411. pBlob );
  1412. }
  1413. //
  1414. // DCR_HACK: remove me
  1415. //
  1416. // must return some records on success query
  1417. //
  1418. // not sure this is true on referral -- if so it's because we flag
  1419. // as referral
  1420. //
  1421. ASSERT( status != ERROR_SUCCESS || pBlob->pRecords != NULL );
  1422. return status;
  1423. }
  1424. DNS_STATUS
  1425. Query_InProcess(
  1426. IN OUT PQUERY_BLOB pBlob
  1427. )
  1428. /*++
  1429. Routine Description:
  1430. Main direct in-process query routine.
  1431. Arguments:
  1432. pBlob -- query info blob
  1433. Return Value:
  1434. ERROR_SUCCESS if successful.
  1435. DNS RCODE error for RCODE response.
  1436. DNS_INFO_NO_RECORDS for no records response.
  1437. ERROR_TIMEOUT on complete lookup failure.
  1438. ErrorCode on local failure.
  1439. --*/
  1440. {
  1441. DNS_STATUS status = NO_ERROR;
  1442. PDNS_NETINFO pnetInfo;
  1443. PDNS_NETINFO pnetInfoLocal = NULL;
  1444. PDNS_NETINFO pnetInfoOriginal;
  1445. DNS_STATUS statusNetFailure = NO_ERROR;
  1446. DNSDBG( TRACE, (
  1447. "Query_InProcess( %p )\n",
  1448. pBlob ));
  1449. //
  1450. // skip queries in "net down" situation
  1451. //
  1452. if ( IsKnownNetFailure() )
  1453. {
  1454. status = GetLastError();
  1455. goto Cleanup;
  1456. }
  1457. //
  1458. // get network info
  1459. //
  1460. pnetInfo = pnetInfoOriginal = pBlob->pNetworkInfo;
  1461. //
  1462. // explicit DNS server list -- build into network info
  1463. // - requires info from current list for search list or PDN
  1464. // - then dump current list and use private version
  1465. //
  1466. if ( pBlob->pDnsServers )
  1467. {
  1468. pnetInfo = NetInfo_CreateFromIpArray(
  1469. pBlob->pDnsServers,
  1470. 0, // no specific server
  1471. TRUE, // build search info
  1472. pnetInfo // use existing netinfo
  1473. );
  1474. if ( !pnetInfo )
  1475. {
  1476. status = DNS_ERROR_NO_MEMORY;
  1477. goto Cleanup;
  1478. }
  1479. pnetInfoLocal = pnetInfo;
  1480. }
  1481. //
  1482. // no network info -- get it
  1483. //
  1484. else if ( !pnetInfo )
  1485. {
  1486. pnetInfoLocal = pnetInfo = GetNetworkInfo();
  1487. if ( ! pnetInfo )
  1488. {
  1489. status = DNS_ERROR_NO_DNS_SERVERS;
  1490. goto Cleanup;
  1491. }
  1492. }
  1493. pBlob->pNetworkInfo = pnetInfo;
  1494. //
  1495. // make actual query to DNS servers
  1496. //
  1497. pBlob->pfnQueryCache = QueryHostFile;
  1498. pBlob->pfnGetAddrArray = DnsGetLocalAddrArray;
  1499. status = Query_Main( pBlob );
  1500. //
  1501. // save net failure
  1502. // - but not if passed in network info
  1503. // only meaningful if its standard info
  1504. //
  1505. // DCR: fix statusNetFailure mess
  1506. //
  1507. if ( statusNetFailure )
  1508. {
  1509. if ( !pBlob->pDnsServers )
  1510. {
  1511. SetKnownNetFailure( status );
  1512. }
  1513. }
  1514. //
  1515. // cleanup
  1516. //
  1517. Cleanup:
  1518. NetInfo_Free( pnetInfoLocal );
  1519. pBlob->pNetworkInfo = pnetInfoOriginal;
  1520. GUI_MODE_SETUP_WS_CLEANUP( g_InNTSetupMode );
  1521. return status;
  1522. }
  1523. //
  1524. // Query utilities
  1525. //
  1526. DNS_STATUS
  1527. GetDnsServerRRSet(
  1528. OUT PDNS_RECORD * ppRecord,
  1529. IN BOOLEAN fUnicode
  1530. )
  1531. /*++
  1532. Routine Description:
  1533. Create record list of None.
  1534. Arguments:
  1535. None.
  1536. Return Value:
  1537. None.
  1538. --*/
  1539. {
  1540. PDNS_NETINFO pnetInfo;
  1541. DWORD iter;
  1542. PDNS_RECORD prr;
  1543. DNS_RRSET rrSet;
  1544. DNS_CHARSET charSet = fUnicode ? DnsCharSetUnicode : DnsCharSetUtf8;
  1545. DNSDBG( QUERY, (
  1546. "GetDnsServerRRSet()\n" ));
  1547. DNS_RRSET_INIT( rrSet );
  1548. pnetInfo = GetNetworkInfo();
  1549. if ( !pnetInfo )
  1550. {
  1551. goto Done;
  1552. }
  1553. //
  1554. // loop through all adapters build record for each DNS server
  1555. //
  1556. for ( iter = 0;
  1557. iter < pnetInfo->AdapterCount;
  1558. iter++ )
  1559. {
  1560. PDNS_ADAPTER padapter = pnetInfo->AdapterArray[iter];
  1561. PSTR pname;
  1562. DWORD jiter;
  1563. if ( !padapter )
  1564. {
  1565. continue;
  1566. }
  1567. // DCR: goofy way to expose aliases
  1568. //
  1569. // if register the adapter's domain name, make it record name
  1570. // this
  1571. pname = padapter->pszAdapterDomain;
  1572. if ( !pname ||
  1573. !( padapter->InfoFlags & DNS_FLAG_REGISTER_DOMAIN_NAME ) )
  1574. {
  1575. pname = ".";
  1576. }
  1577. for ( jiter = 0; jiter < padapter->ServerCount; jiter++ )
  1578. {
  1579. // DCR: IP6 DNS servers
  1580. IP_UNION ipUnion;
  1581. IPUNION_SET_IP4( &ipUnion, padapter->ServerArray[jiter].IpAddress );
  1582. prr = Dns_CreateForwardRecord(
  1583. pname,
  1584. & ipUnion,
  1585. 0, // no TTL
  1586. DnsCharSetUtf8, // name is UTF8
  1587. charSet // result set
  1588. );
  1589. if ( prr )
  1590. {
  1591. prr->Flags.S.Section = DNSREC_ANSWER;
  1592. DNS_RRSET_ADD( rrSet, prr );
  1593. }
  1594. }
  1595. }
  1596. Done:
  1597. NetInfo_Free( pnetInfo );
  1598. *ppRecord = prr = rrSet.pFirstRR;
  1599. DNSDBG( QUERY, (
  1600. "Leave GetDnsServerRRSet() => %d\n",
  1601. (prr ? ERROR_SUCCESS : DNS_ERROR_NO_DNS_SERVERS) ));
  1602. return (prr ? ERROR_SUCCESS : DNS_ERROR_NO_DNS_SERVERS);
  1603. }
  1604. //
  1605. // DNS Query API
  1606. //
  1607. DNS_STATUS
  1608. WINAPI
  1609. privateNarrowToWideQuery(
  1610. IN PCSTR pszName,
  1611. IN WORD wType,
  1612. IN DWORD Options,
  1613. IN PIP_ARRAY pDnsServers OPTIONAL,
  1614. OUT PDNS_RECORD * ppResultSet OPTIONAL,
  1615. IN OUT PDNS_MSG_BUF * ppMessageResponse OPTIONAL,
  1616. IN DNS_CHARSET CharSet
  1617. )
  1618. /*++
  1619. Routine Description:
  1620. Convert narrow to wide query.
  1621. This routine simple avoids duplicate code in ANSI
  1622. and UTF8 query routines.
  1623. Arguments:
  1624. pszName -- name to query
  1625. wType -- type of query
  1626. Options -- flags to query
  1627. pDnsServers -- array of DNS servers to use in query
  1628. ppResultSet -- addr to receive result DNS records
  1629. ppMessageResponse -- addr to receive response message
  1630. CharSet -- char set of original query
  1631. Return Value:
  1632. ERROR_SUCCESS on success.
  1633. DNS RCODE error on query with RCODE
  1634. DNS_INFO_NO_RECORDS on no records response.
  1635. ErrorCode on failure.
  1636. --*/
  1637. {
  1638. DNS_STATUS status = NO_ERROR;
  1639. PDNS_RECORD prrList = NULL;
  1640. PWSTR pwideName = NULL;
  1641. WORD nameLength;
  1642. if ( !pszName )
  1643. {
  1644. return ERROR_INVALID_PARAMETER;
  1645. }
  1646. nameLength = (WORD) strlen( pszName );
  1647. pwideName = ALLOCATE_HEAP( (nameLength + 1) * sizeof(WCHAR) );
  1648. if ( !pwideName )
  1649. {
  1650. return DNS_ERROR_NO_MEMORY;
  1651. }
  1652. if ( !Dns_NameCopy(
  1653. (PSTR) pwideName,
  1654. NULL,
  1655. (PSTR) pszName,
  1656. nameLength,
  1657. CharSet,
  1658. DnsCharSetUnicode ) )
  1659. {
  1660. status = ERROR_INVALID_NAME;
  1661. goto Done;
  1662. }
  1663. status = DnsQuery_W(
  1664. pwideName,
  1665. wType,
  1666. Options,
  1667. pDnsServers,
  1668. ppResultSet ? &prrList : NULL,
  1669. ppMessageResponse
  1670. );
  1671. //
  1672. // convert result records back to ANSI (or UTF8)
  1673. //
  1674. if ( ppResultSet && prrList )
  1675. {
  1676. *ppResultSet = Dns_RecordSetCopyEx(
  1677. prrList,
  1678. DnsCharSetUnicode,
  1679. CharSet
  1680. );
  1681. if ( ! *ppResultSet )
  1682. {
  1683. status = DNS_ERROR_NO_MEMORY;
  1684. }
  1685. Dns_RecordListFree( prrList );
  1686. }
  1687. //
  1688. // cleanup
  1689. //
  1690. Done:
  1691. FREE_HEAP( pwideName );
  1692. return status;
  1693. }
  1694. DNS_STATUS
  1695. WINAPI
  1696. DnsQuery_UTF8(
  1697. IN PCSTR pszName,
  1698. IN WORD wType,
  1699. IN DWORD Options,
  1700. IN PIP_ARRAY pDnsServers OPTIONAL,
  1701. OUT PDNS_RECORD * ppResultSet OPTIONAL,
  1702. IN OUT PDNS_MSG_BUF * ppMessageResponse OPTIONAL
  1703. )
  1704. /*++
  1705. Routine Description:
  1706. Public UTF8 query.
  1707. Arguments:
  1708. pszName -- name to query
  1709. wType -- type of query
  1710. Options -- flags to query
  1711. pDnsServers -- array of DNS servers to use in query
  1712. ppResultSet -- addr to receive result DNS records
  1713. ppMessageResponse -- addr to receive response message
  1714. Return Value:
  1715. ERROR_SUCCESS on success.
  1716. DNS RCODE error on query with RCODE
  1717. DNS_INFO_NO_RECORDS on no records response.
  1718. ErrorCode on failure.
  1719. --*/
  1720. {
  1721. return privateNarrowToWideQuery(
  1722. pszName,
  1723. wType,
  1724. Options,
  1725. pDnsServers,
  1726. ppResultSet,
  1727. ppMessageResponse,
  1728. DnsCharSetUtf8
  1729. );
  1730. }
  1731. DNS_STATUS
  1732. WINAPI
  1733. DnsQuery_A(
  1734. IN PCSTR pszName,
  1735. IN WORD wType,
  1736. IN DWORD Options,
  1737. IN PIP_ARRAY pDnsServers OPTIONAL,
  1738. OUT PDNS_RECORD * ppResultSet OPTIONAL,
  1739. IN OUT PDNS_MSG_BUF * ppMessageResponse OPTIONAL
  1740. )
  1741. /*++
  1742. Routine Description:
  1743. Public ANSI query.
  1744. Arguments:
  1745. pszName -- name to query
  1746. wType -- type of query
  1747. Options -- flags to query
  1748. pDnsServers -- array of DNS servers to use in query
  1749. ppResultSet -- addr to receive result DNS records
  1750. ppMessageResponse -- addr to receive resulting message
  1751. Return Value:
  1752. ERROR_SUCCESS on success.
  1753. DNS RCODE error on query with RCODE
  1754. DNS_INFO_NO_RECORDS on no records response.
  1755. ErrorCode on failure.
  1756. --*/
  1757. {
  1758. return privateNarrowToWideQuery(
  1759. pszName,
  1760. wType,
  1761. Options,
  1762. pDnsServers,
  1763. ppResultSet,
  1764. ppMessageResponse,
  1765. DnsCharSetAnsi
  1766. );
  1767. }
  1768. DNS_STATUS
  1769. WINAPI
  1770. DnsQuery_W(
  1771. IN PCWSTR pwsName,
  1772. IN WORD wType,
  1773. IN DWORD Options,
  1774. IN PIP_ARRAY pDnsServers OPTIONAL,
  1775. IN OUT PDNS_RECORD * ppResultSet OPTIONAL,
  1776. IN OUT PDNS_MSG_BUF * ppMessageResponse OPTIONAL
  1777. )
  1778. /*++
  1779. Routine Description:
  1780. Public unicode query API
  1781. Note, this unicode version is the main routine.
  1782. The other public API call back through it.
  1783. Arguments:
  1784. pszName -- name to query
  1785. wType -- type of query
  1786. Options -- flags to query
  1787. pDnsServers -- array of DNS servers to use in query
  1788. ppResultSet -- addr to receive result DNS records
  1789. ppMessageResponse -- addr to receive resulting message
  1790. Return Value:
  1791. ERROR_SUCCESS on success.
  1792. DNS RCODE error on query with RCODE
  1793. DNS_INFO_NO_RECORDS on no records response.
  1794. ErrorCode on failure.
  1795. --*/
  1796. {
  1797. DNS_STATUS status = NO_ERROR;
  1798. PDNS_NETINFO pnetInfo = NULL;
  1799. PDNS_RECORD prpcRecord = NULL;
  1800. DWORD rpcStatus = NO_ERROR;
  1801. PQUERY_BLOB pblob;
  1802. PWSTR pnameLocal = NULL;
  1803. DNSDBG( TRACE, (
  1804. "\n\nDnsQuery_W()\n"
  1805. "\tName = %S\n"
  1806. "\twType = %d\n"
  1807. "\tOptions = %08x\n"
  1808. "\tpDnsServers = %p\n"
  1809. "\tppMessage = %p\n",
  1810. pwsName,
  1811. wType,
  1812. Options,
  1813. pDnsServers,
  1814. ppMessageResponse ));
  1815. //
  1816. // must ask for some kind of results
  1817. //
  1818. if ( !ppResultSet && !ppMessageResponse )
  1819. {
  1820. return ERROR_INVALID_PARAMETER;
  1821. }
  1822. //
  1823. // NULL name indicates localhost lookup
  1824. //
  1825. // DCR: NULL name lookup for localhost could be improved
  1826. // - support NULL all the way through to wire
  1827. // - have local IP routines just accept it
  1828. //
  1829. if ( !pwsName )
  1830. {
  1831. pnameLocal = (PWSTR) Reg_GetHostName( DnsCharSetUnicode );
  1832. if ( !pnameLocal )
  1833. {
  1834. return DNS_ERROR_NAME_NOT_FOUND_LOCALLY;
  1835. }
  1836. pwsName = (PCWSTR) pnameLocal;
  1837. Options |= DNS_QUERY_CACHE_ONLY;
  1838. }
  1839. // clear OUT params
  1840. if ( ppResultSet )
  1841. {
  1842. *ppResultSet = NULL;
  1843. }
  1844. if ( ppMessageResponse )
  1845. {
  1846. *ppMessageResponse = NULL;
  1847. }
  1848. //
  1849. // IP string queries
  1850. //
  1851. if ( ppResultSet )
  1852. {
  1853. PDNS_RECORD prr;
  1854. prr = Dns_CreateRecordForIpString_W(
  1855. pwsName,
  1856. wType,
  1857. IPSTRING_RECORD_TTL );
  1858. if ( prr )
  1859. {
  1860. *ppResultSet = prr;
  1861. status = ERROR_SUCCESS;
  1862. goto Done;
  1863. }
  1864. }
  1865. //
  1866. // empty type A query get DNS servers
  1867. //
  1868. // DCR_CLEANUP: DnsQuery empty name query for DNS servers?
  1869. // need better\safer approach to this
  1870. // is this SDK doc'd
  1871. //
  1872. if ( ppResultSet &&
  1873. !ppMessageResponse &&
  1874. wType == DNS_TYPE_A &&
  1875. ( !wcscmp( pwsName, L"" ) ||
  1876. !wcscmp( pwsName, DNS_SERVER_QUERY_NAME ) ) )
  1877. {
  1878. status = GetDnsServerRRSet(
  1879. ppResultSet,
  1880. TRUE // unicode
  1881. );
  1882. goto Done;
  1883. }
  1884. //
  1885. // BYPASS_CACHE
  1886. // - required if want message buffer or specify server
  1887. // list -- just set flag
  1888. // - incompatible with CACHE_ONLY
  1889. // - required to get EMPTY_AUTH_RESPONSE
  1890. //
  1891. if ( ppMessageResponse ||
  1892. pDnsServers ||
  1893. (Options & DNS_QUERY_ALLOW_EMPTY_AUTH_RESP) )
  1894. {
  1895. Options |= DNS_QUERY_BYPASS_CACHE;
  1896. //Options |= DNS_QUERY_NO_CACHE_DATA;
  1897. }
  1898. //
  1899. // do direct query?
  1900. // - not RPC-able type
  1901. // - want message buffer
  1902. // - specifying DNS servers
  1903. // - want EMPTY_AUTH response records
  1904. //
  1905. // DCR: currently by-passing for type==ALL
  1906. // this may be too common to do that; may want to
  1907. // go to cache then determine if security records
  1908. // or other stuff require us to query in process
  1909. //
  1910. // DCR: not clear what the EMPTY_AUTH benefit is
  1911. //
  1912. // DCR: currently BYPASSing whenever BYPASS is set
  1913. // because otherwise we miss the hosts file
  1914. // if fix so lookup in cache, but screen off non-hosts
  1915. // data, then could resume going to cache
  1916. //
  1917. if ( !Dns_IsRpcRecordType(wType) &&
  1918. !(Options & DNS_QUERY_CACHE_ONLY) )
  1919. {
  1920. goto InProcessQuery;
  1921. }
  1922. if ( Options & DNS_QUERY_BYPASS_CACHE )
  1923. #if 0
  1924. if ( (Options & DNS_QUERY_BYPASS_CACHE) &&
  1925. ( ppMessageResponse ||
  1926. pDnsServers ||
  1927. (Options & DNS_QUERY_ALLOW_EMPTY_AUTH_RESP) ) )
  1928. #endif
  1929. {
  1930. if ( Options & DNS_QUERY_CACHE_ONLY )
  1931. {
  1932. status = ERROR_INVALID_PARAMETER;
  1933. goto Done;
  1934. }
  1935. goto InProcessQuery;
  1936. }
  1937. //
  1938. // querying through cache
  1939. // - get cluster-filtering info
  1940. //
  1941. if ( g_IsServer )
  1942. {
  1943. ENVAR_DWORD_INFO filterInfo;
  1944. Reg_ReadDwordEnvar(
  1945. RegIdFilterClusterIp,
  1946. &filterInfo );
  1947. if ( filterInfo.fFound && filterInfo.Value )
  1948. {
  1949. Options |= DNSP_QUERY_FILTER_CLUSTER;
  1950. }
  1951. }
  1952. rpcStatus = NO_ERROR;
  1953. RpcTryExcept
  1954. {
  1955. status = R_ResolverQuery(
  1956. NULL,
  1957. (PWSTR) pwsName,
  1958. wType,
  1959. Options,
  1960. &prpcRecord );
  1961. }
  1962. RpcExcept( DNS_RPC_EXCEPTION_FILTER )
  1963. {
  1964. rpcStatus = RpcExceptionCode();
  1965. }
  1966. RpcEndExcept
  1967. //
  1968. // cache unavailable
  1969. // - bail if just querying cache
  1970. // - otherwise query direct
  1971. if ( rpcStatus != NO_ERROR )
  1972. {
  1973. DNSDBG( TRACE, (
  1974. "DnsQuery_W() RPC failed status = %d\n",
  1975. rpcStatus ));
  1976. goto InProcessQuery;
  1977. }
  1978. if ( status == DNS_ERROR_NO_TCPIP )
  1979. {
  1980. DNSDBG( TRACE, (
  1981. "DnsQuery_W() NO_TCPIP error!\n"
  1982. "\tassume resolver security problem -- query in process!\n"
  1983. ));
  1984. RTL_ASSERT( !prpcRecord );
  1985. goto InProcessQuery;
  1986. }
  1987. //
  1988. // return records
  1989. // - screen out empty-auth responses
  1990. //
  1991. // DCR_FIX1: cache should convert and return NO_RECORDS response
  1992. // directly (no need to do this here)
  1993. //
  1994. // DCR: UNLESS we allow return of these records
  1995. //
  1996. if ( prpcRecord )
  1997. {
  1998. FixupNameOwnerPointers( prpcRecord );
  1999. if ( IsEmptyDnsResponseFromResolver( prpcRecord ) )
  2000. {
  2001. Dns_RecordListFree( prpcRecord );
  2002. prpcRecord = NULL;
  2003. if ( status == NO_ERROR )
  2004. {
  2005. status = DNS_INFO_NO_RECORDS;
  2006. }
  2007. }
  2008. *ppResultSet = prpcRecord;
  2009. }
  2010. RTL_ASSERT( status!=NO_ERROR || prpcRecord );
  2011. goto Done;
  2012. //
  2013. // query directly -- either skipping cache or it's unavailable
  2014. //
  2015. InProcessQuery:
  2016. DNSDBG( TRACE, (
  2017. "DnsQuery_W() -- doing in process query\n"
  2018. "\tpname = %S\n"
  2019. "\ttype = %d\n",
  2020. pwsName,
  2021. wType ));
  2022. //
  2023. // load query blob
  2024. //
  2025. // DCR: set some sort of "want message buffer" flag if ppMessageResponse
  2026. // exists
  2027. //
  2028. pblob = ALLOCATE_HEAP_ZERO( sizeof(*pblob) );
  2029. if ( !pblob )
  2030. {
  2031. status = DNS_ERROR_NO_MEMORY;
  2032. goto Done;
  2033. }
  2034. pblob->pNameOrig = (PWSTR) pwsName;
  2035. pblob->wType = wType;
  2036. pblob->Flags = Options | DNSQUERY_UNICODE_OUT;
  2037. pblob->pDnsServers = pDnsServers;
  2038. //
  2039. // query
  2040. // - then set OUT params
  2041. status = Query_InProcess( pblob );
  2042. if ( ppResultSet )
  2043. {
  2044. *ppResultSet = pblob->pRecords;
  2045. RTL_ASSERT( status!=NO_ERROR || *ppResultSet );
  2046. }
  2047. else
  2048. {
  2049. Dns_RecordListFree( pblob->pRecords );
  2050. }
  2051. if ( ppMessageResponse )
  2052. {
  2053. *ppMessageResponse = pblob->pRecvMsg;
  2054. }
  2055. FREE_HEAP( pblob );
  2056. Done:
  2057. // sanity check
  2058. if ( status==NO_ERROR &&
  2059. ppResultSet &&
  2060. !*ppResultSet )
  2061. {
  2062. RTL_ASSERT( FALSE );
  2063. status = DNS_INFO_NO_RECORDS;
  2064. }
  2065. if ( pnameLocal )
  2066. {
  2067. FREE_HEAP( pnameLocal );
  2068. }
  2069. DNSDBG( TRACE, (
  2070. "Leave DnsQuery_W()\n"
  2071. "\tstatus = %d\n"
  2072. "\tresult set = %p\n\n\n",
  2073. status,
  2074. *ppResultSet ));
  2075. return( status );
  2076. }
  2077. //
  2078. // DnsQueryEx() routines
  2079. //
  2080. DNS_STATUS
  2081. WINAPI
  2082. ShimDnsQueryEx(
  2083. IN OUT PDNS_QUERY_INFO pQueryInfo
  2084. )
  2085. /*++
  2086. Routine Description:
  2087. Query DNS -- shim for main SDK query routine.
  2088. Arguments:
  2089. pQueryInfo -- blob describing query
  2090. Return Value:
  2091. ERROR_SUCCESS if successful query.
  2092. Error code on failure.
  2093. --*/
  2094. {
  2095. PDNS_RECORD prrResult;
  2096. WORD type = pQueryInfo->Type;
  2097. DNS_STATUS status;
  2098. DNS_LIST listAnswer;
  2099. DNS_LIST listAlias;
  2100. DNS_LIST listAdditional;
  2101. DNS_LIST listAuthority;
  2102. DNSDBG( TRACE, ( "ShimDnsQueryEx()\n" ));
  2103. //
  2104. // DCR: temp hack is to pass this to DNSQuery
  2105. //
  2106. status = DnsQuery_W(
  2107. (PWSTR) pQueryInfo->pName,
  2108. type,
  2109. pQueryInfo->Flags,
  2110. pQueryInfo->pDnsServers,
  2111. & prrResult,
  2112. NULL );
  2113. pQueryInfo->Status = status;
  2114. //
  2115. // cut result records appropriately
  2116. //
  2117. pQueryInfo->pAnswerRecords = NULL;
  2118. pQueryInfo->pAliasRecords = NULL;
  2119. pQueryInfo->pAdditionalRecords = NULL;
  2120. pQueryInfo->pAuthorityRecords = NULL;
  2121. if ( prrResult )
  2122. {
  2123. PDNS_RECORD prr;
  2124. PDNS_RECORD pnextRR;
  2125. DNS_LIST_STRUCT_INIT( listAnswer );
  2126. DNS_LIST_STRUCT_INIT( listAlias );
  2127. DNS_LIST_STRUCT_INIT( listAdditional );
  2128. DNS_LIST_STRUCT_INIT( listAuthority );
  2129. //
  2130. // break list into section specific lists
  2131. // - section 0 for hostfile records
  2132. // - note, this does pull RR sets apart, but
  2133. // they, being in same section, should immediately
  2134. // be rejoined
  2135. //
  2136. pnextRR = prrResult;
  2137. while ( prr = pnextRR )
  2138. {
  2139. pnextRR = prr->pNext;
  2140. prr->pNext = NULL;
  2141. if ( prr->Flags.S.Section == 0 ||
  2142. prr->Flags.S.Section == DNSREC_ANSWER )
  2143. {
  2144. if ( prr->wType == DNS_TYPE_CNAME &&
  2145. type != DNS_TYPE_CNAME )
  2146. {
  2147. DNS_LIST_STRUCT_ADD( listAlias, prr );
  2148. continue;
  2149. }
  2150. else
  2151. {
  2152. DNS_LIST_STRUCT_ADD( listAnswer, prr );
  2153. continue;
  2154. }
  2155. }
  2156. else if ( prr->Flags.S.Section == DNSREC_ADDITIONAL )
  2157. {
  2158. DNS_LIST_STRUCT_ADD( listAdditional, prr );
  2159. continue;
  2160. }
  2161. else
  2162. {
  2163. DNS_LIST_STRUCT_ADD( listAuthority, prr );
  2164. continue;
  2165. }
  2166. }
  2167. // pack stuff back into blob
  2168. pQueryInfo->pAnswerRecords = listAnswer.pFirst;
  2169. pQueryInfo->pAliasRecords = listAlias.pFirst;
  2170. pQueryInfo->pAuthorityRecords = listAuthority.pFirst;
  2171. pQueryInfo->pAdditionalRecords = listAdditional.pFirst;
  2172. //pQueryInfo->pSigRecords = listSig.pFirst;
  2173. //
  2174. // convert result records back to ANSI (or UTF8)
  2175. // - convert each result set
  2176. // - then paste back into query blob
  2177. //
  2178. // DCR_FIX0: handle issue of failure on conversion
  2179. //
  2180. if ( pQueryInfo->CharSet != DnsCharSetUnicode )
  2181. {
  2182. PDNS_RECORD prr;
  2183. PDNS_RECORD * prrSetPtr;
  2184. prrSetPtr = & pQueryInfo->pAnswerRecords;
  2185. for ( prrSetPtr = & pQueryInfo->pAnswerRecords;
  2186. prrSetPtr <= & pQueryInfo->pAdditionalRecords;
  2187. prrSetPtr++ )
  2188. {
  2189. prr = *prrSetPtr;
  2190. *prrSetPtr = Dns_RecordSetCopyEx(
  2191. prr,
  2192. DnsCharSetUnicode,
  2193. pQueryInfo->CharSet
  2194. );
  2195. Dns_RecordListFree( prr );
  2196. }
  2197. }
  2198. }
  2199. //
  2200. // replace name for originally narrow queries
  2201. //
  2202. if ( pQueryInfo->CharSet != DnsCharSetUnicode )
  2203. {
  2204. ASSERT( pQueryInfo->CharSet != 0 );
  2205. ASSERT( pQueryInfo->pReservedName != NULL );
  2206. FREE_HEAP( pQueryInfo->pName );
  2207. pQueryInfo->pName = (LPTSTR) pQueryInfo->pReservedName;
  2208. pQueryInfo->pReservedName = NULL;
  2209. }
  2210. //
  2211. // indicate return if async
  2212. //
  2213. if ( pQueryInfo->hEvent )
  2214. {
  2215. SetEvent( pQueryInfo->hEvent );
  2216. }
  2217. return( status );
  2218. }
  2219. DNS_STATUS
  2220. WINAPI
  2221. CombinedQueryEx(
  2222. IN OUT PDNS_QUERY_INFO pQueryInfo,
  2223. IN DNS_CHARSET CharSet
  2224. )
  2225. /*++
  2226. Routine Description:
  2227. Convert narrow to wide query.
  2228. This routine simple avoids duplicate code in ANSI
  2229. and UTF8 query routines.
  2230. Arguments:
  2231. pQueryInfo -- query info blob
  2232. CharSet -- char set of original query
  2233. Return Value:
  2234. ERROR_SUCCESS on success.
  2235. DNS RCODE error on query with RCODE
  2236. DNS_INFO_NO_RECORDS on no records response.
  2237. ErrorCode on failure.
  2238. --*/
  2239. {
  2240. DNS_STATUS status = NO_ERROR;
  2241. PWSTR pwideName = NULL;
  2242. HANDLE hthread;
  2243. DWORD threadId;
  2244. DNSDBG( TRACE, (
  2245. "CombinedQueryEx( %S%s, type=%d, flag=%08x, event=%p )\n",
  2246. PRINT_STRING_WIDE_CHARSET( pQueryInfo->pName, CharSet ),
  2247. PRINT_STRING_ANSI_CHARSET( pQueryInfo->pName, CharSet ),
  2248. pQueryInfo->Type,
  2249. pQueryInfo->Flags,
  2250. pQueryInfo->hEvent ));
  2251. //
  2252. // set CharSet
  2253. //
  2254. pQueryInfo->CharSet = CharSet;
  2255. if ( CharSet == DnsCharSetUnicode )
  2256. {
  2257. pQueryInfo->pReservedName = 0;
  2258. }
  2259. //
  2260. // if narrow name
  2261. // - allocate wide name copy
  2262. // - swap in wide name and make query wide
  2263. //
  2264. // DCR: allow NULL name? for local machine name?
  2265. //
  2266. else if ( CharSet == DnsCharSetAnsi ||
  2267. CharSet == DnsCharSetUtf8 )
  2268. {
  2269. WORD nameLength;
  2270. PSTR pnameNarrow;
  2271. pnameNarrow = pQueryInfo->pName;
  2272. if ( !pnameNarrow )
  2273. {
  2274. return ERROR_INVALID_PARAMETER;
  2275. }
  2276. nameLength = (WORD) strlen( pnameNarrow );
  2277. pwideName = ALLOCATE_HEAP( (nameLength + 1) * sizeof(WCHAR) );
  2278. if ( !pwideName )
  2279. {
  2280. return DNS_ERROR_NO_MEMORY;
  2281. }
  2282. if ( !Dns_NameCopy(
  2283. (PSTR) pwideName,
  2284. NULL,
  2285. pnameNarrow,
  2286. nameLength,
  2287. CharSet,
  2288. DnsCharSetUnicode ) )
  2289. {
  2290. status = ERROR_INVALID_NAME;
  2291. goto Failed;
  2292. }
  2293. pQueryInfo->pName = (LPTSTR) pwideName;
  2294. pQueryInfo->pReservedName = pnameNarrow;
  2295. }
  2296. //
  2297. // async?
  2298. // - if event exists we are async
  2299. // - spin up thread and call it
  2300. //
  2301. if ( pQueryInfo->hEvent )
  2302. {
  2303. hthread = CreateThread(
  2304. NULL, // no security
  2305. 0, // default stack
  2306. ShimDnsQueryEx,
  2307. pQueryInfo, // param
  2308. 0, // run immediately
  2309. & threadId
  2310. );
  2311. if ( !hthread )
  2312. {
  2313. DNS_STATUS status = GetLastError();
  2314. DNSDBG( ANY, (
  2315. "Failed to create thread = %d\n",
  2316. status ));
  2317. if ( status == ERROR_SUCCESS )
  2318. {
  2319. status = DNS_ERROR_NO_MEMORY;
  2320. }
  2321. goto Failed;
  2322. }
  2323. CloseHandle( hthread );
  2324. return( ERROR_IO_PENDING );
  2325. }
  2326. //
  2327. // otherwise make direct async call
  2328. //
  2329. return ShimDnsQueryEx( pQueryInfo );
  2330. Failed:
  2331. FREE_HEAP( pwideName );
  2332. return( status );
  2333. }
  2334. DNS_STATUS
  2335. WINAPI
  2336. DnsQueryExW(
  2337. IN OUT PDNS_QUERY_INFO pQueryInfo
  2338. )
  2339. /*++
  2340. Routine Description:
  2341. Query DNS -- main SDK query routine.
  2342. Arguments:
  2343. pQueryInfo -- blob describing query
  2344. Return Value:
  2345. ERROR_SUCCESS if successful query.
  2346. ERROR_IO_PENDING if successful async start.
  2347. Error code on failure.
  2348. --*/
  2349. {
  2350. DNSDBG( TRACE, (
  2351. "DnsQueryExW( %S, type=%d, flag=%08x, event=%p )\n",
  2352. pQueryInfo->pName,
  2353. pQueryInfo->Type,
  2354. pQueryInfo->Flags,
  2355. pQueryInfo->hEvent ));
  2356. return CombinedQueryEx( pQueryInfo, DnsCharSetUnicode );
  2357. }
  2358. DNS_STATUS
  2359. WINAPI
  2360. DnsQueryExA(
  2361. IN OUT PDNS_QUERY_INFO pQueryInfo
  2362. )
  2363. /*++
  2364. Routine Description:
  2365. Query DNS -- main SDK query routine.
  2366. Arguments:
  2367. pQueryInfo -- blob describing query
  2368. Return Value:
  2369. ERROR_SUCCESS if successful query.
  2370. ERROR_IO_PENDING if successful async start.
  2371. Error code on failure.
  2372. --*/
  2373. {
  2374. DNSDBG( TRACE, (
  2375. "DnsQueryExA( %s, type=%d, flag=%08x, event=%p )\n",
  2376. pQueryInfo->pName,
  2377. pQueryInfo->Type,
  2378. pQueryInfo->Flags,
  2379. pQueryInfo->hEvent ));
  2380. return CombinedQueryEx( pQueryInfo, DnsCharSetAnsi );
  2381. }
  2382. DNS_STATUS
  2383. WINAPI
  2384. DnsQueryExUTF8(
  2385. IN OUT PDNS_QUERY_INFO pQueryInfo
  2386. )
  2387. /*++
  2388. Routine Description:
  2389. Query DNS -- main SDK query routine.
  2390. Arguments:
  2391. pQueryInfo -- blob describing query
  2392. Return Value:
  2393. ERROR_SUCCESS if successful query.
  2394. ERROR_IO_PENDING if successful async start.
  2395. Error code on failure.
  2396. --*/
  2397. {
  2398. DNSDBG( TRACE, (
  2399. "DnsQueryExUTF8( %s, type=%d, flag=%08x, event=%p )\n",
  2400. pQueryInfo->pName,
  2401. pQueryInfo->Type,
  2402. pQueryInfo->Flags,
  2403. pQueryInfo->hEvent ));
  2404. return CombinedQueryEx( pQueryInfo, DnsCharSetUtf8 );
  2405. }
  2406. //
  2407. // Name collision API
  2408. //
  2409. // DCR_QUESTION: name collision -- is there any point to this?
  2410. //
  2411. DNS_STATUS
  2412. WINAPI
  2413. DnsCheckNameCollision_UTF8(
  2414. IN PCSTR pszName,
  2415. IN DWORD Options
  2416. )
  2417. /*++
  2418. Routine Description:
  2419. None.
  2420. Arguments:
  2421. None.
  2422. Return Value:
  2423. None.
  2424. --*/
  2425. {
  2426. DNS_STATUS status = NO_ERROR;
  2427. PDNS_RECORD prrList = NULL;
  2428. PDNS_RECORD prr = NULL;
  2429. DWORD iter;
  2430. BOOL fmatch = FALSE;
  2431. WORD wtype = DNS_TYPE_A;
  2432. if ( !pszName )
  2433. {
  2434. return ERROR_INVALID_PARAMETER;
  2435. }
  2436. if ( Options & DNS_CHECK_AGAINST_HOST_ANY )
  2437. {
  2438. wtype = DNS_TYPE_ANY;
  2439. }
  2440. status = DnsQuery_UTF8( pszName,
  2441. wtype,
  2442. DNS_QUERY_BYPASS_CACHE,
  2443. NULL,
  2444. &prrList,
  2445. NULL );
  2446. if ( status == DNS_ERROR_RCODE_NAME_ERROR ||
  2447. status == DNS_INFO_NO_RECORDS )
  2448. {
  2449. Dns_RecordListFree( prrList );
  2450. return NO_ERROR;
  2451. }
  2452. if ( status == NO_ERROR &&
  2453. Options == DNS_CHECK_AGAINST_HOST_ANY )
  2454. {
  2455. Dns_RecordListFree( prrList );
  2456. return DNS_ERROR_RCODE_YXRRSET;
  2457. }
  2458. if ( status == NO_ERROR &&
  2459. Options == DNS_CHECK_AGAINST_HOST_DOMAIN_NAME )
  2460. {
  2461. char TestName[DNS_MAX_NAME_LENGTH * 2];
  2462. PSTR pszHostName = Reg_GetHostName( DnsCharSetUtf8 );
  2463. PSTR pszPrimaryDomain = Reg_GetPrimaryDomainName( DnsCharSetUtf8 );
  2464. fmatch = TRUE;
  2465. strcpy( TestName, pszHostName );
  2466. if ( pszPrimaryDomain )
  2467. {
  2468. strcat( TestName, "." );
  2469. strcat( TestName, pszPrimaryDomain );
  2470. }
  2471. if ( Dns_NameCompare_UTF8( pszHostName, (PSTR)pszName ) )
  2472. {
  2473. fmatch = TRUE;
  2474. }
  2475. if ( !fmatch &&
  2476. pszPrimaryDomain &&
  2477. Dns_NameCompare_UTF8( TestName, (PSTR)pszName ) )
  2478. {
  2479. fmatch = TRUE;
  2480. }
  2481. if ( !fmatch )
  2482. {
  2483. PDNS_NETINFO pNetInfo = GetNetworkInfo();
  2484. if ( pNetInfo )
  2485. {
  2486. for ( iter = 0; iter < pNetInfo->AdapterCount; iter++ )
  2487. {
  2488. PSTR pszDomain = pNetInfo->AdapterArray[iter]->
  2489. pszAdapterDomain;
  2490. if ( pszDomain )
  2491. {
  2492. strcpy( TestName, pszHostName );
  2493. strcat( TestName, "." );
  2494. strcat( TestName, pszDomain );
  2495. if ( Dns_NameCompare_UTF8( TestName, (PSTR)pszName ) )
  2496. fmatch = TRUE;
  2497. }
  2498. }
  2499. }
  2500. NetInfo_Free( pNetInfo );
  2501. }
  2502. FREE_HEAP( pszHostName );
  2503. FREE_HEAP( pszPrimaryDomain );
  2504. if ( fmatch )
  2505. {
  2506. Dns_RecordListFree( prrList );
  2507. return DNS_ERROR_RCODE_YXRRSET;
  2508. }
  2509. }
  2510. if ( status == NO_ERROR )
  2511. {
  2512. PDNS_ADDRESS_INFO pAddressInfo = NULL;
  2513. DWORD Count = DnsGetIpAddressInfoList( &pAddressInfo );
  2514. if ( Count == 0 )
  2515. {
  2516. Dns_RecordListFree( prrList );
  2517. return DNS_ERROR_RCODE_YXRRSET;
  2518. }
  2519. prr = prrList;
  2520. while ( prr )
  2521. {
  2522. fmatch = FALSE;
  2523. if ( prr->Flags.S.Section != DNSREC_ANSWER )
  2524. {
  2525. prr = prr->pNext;
  2526. continue;
  2527. }
  2528. if ( prr->wType == DNS_TYPE_CNAME )
  2529. {
  2530. FREE_HEAP( pAddressInfo );
  2531. Dns_RecordListFree( prrList );
  2532. return DNS_ERROR_RCODE_YXRRSET;
  2533. }
  2534. for ( iter = 0; iter < Count; iter++ )
  2535. {
  2536. if ( prr->Data.A.IpAddress == pAddressInfo[iter].ipAddress )
  2537. {
  2538. fmatch = TRUE;
  2539. }
  2540. }
  2541. if ( !fmatch )
  2542. {
  2543. FREE_HEAP( pAddressInfo );
  2544. Dns_RecordListFree( prrList );
  2545. return DNS_ERROR_RCODE_YXRRSET;
  2546. }
  2547. prr = prr->pNext;
  2548. }
  2549. FREE_HEAP( pAddressInfo );
  2550. Dns_RecordListFree( prrList );
  2551. return NO_ERROR;
  2552. }
  2553. return status;
  2554. }
  2555. DNS_STATUS
  2556. WINAPI
  2557. DnsCheckNameCollision_A(
  2558. IN PCSTR pszName,
  2559. IN DWORD Options
  2560. )
  2561. /*++
  2562. Routine Description:
  2563. None.
  2564. Arguments:
  2565. None.
  2566. Return Value:
  2567. None.
  2568. --*/
  2569. {
  2570. PSTR pUtf8Name = NULL;
  2571. DNS_STATUS status = NO_ERROR;
  2572. //
  2573. // DCR_CLEANUP: fix unnecessary alloc
  2574. // DCR_PERF: eliminate alloc
  2575. //
  2576. if ( !pszName )
  2577. {
  2578. return ERROR_INVALID_PARAMETER;
  2579. }
  2580. pUtf8Name = Dns_NameCopyAllocate(
  2581. (PSTR) pszName,
  2582. 0,
  2583. DnsCharSetAnsi,
  2584. DnsCharSetUtf8 );
  2585. if ( !pUtf8Name )
  2586. {
  2587. return DNS_ERROR_NO_MEMORY;
  2588. }
  2589. status = DnsCheckNameCollision_UTF8( pUtf8Name, Options );
  2590. FREE_HEAP( pUtf8Name );
  2591. return status;
  2592. }
  2593. DNS_STATUS
  2594. WINAPI
  2595. DnsCheckNameCollision_W(
  2596. IN PCWSTR pszName,
  2597. IN DWORD Options
  2598. )
  2599. /*++
  2600. Routine Description:
  2601. None.
  2602. Arguments:
  2603. None.
  2604. Return Value:
  2605. None.
  2606. --*/
  2607. {
  2608. //
  2609. // DCR_CLEANUP: fix unnecessary alloc
  2610. // DCR_PERF: eliminate alloc
  2611. //
  2612. DNS_STATUS status = NO_ERROR;
  2613. PSTR lpTempName = NULL;
  2614. WORD nameLength;
  2615. if ( !pszName )
  2616. {
  2617. return ERROR_INVALID_PARAMETER;
  2618. }
  2619. nameLength = (WORD)wcslen( pszName );
  2620. lpTempName = ALLOCATE_HEAP( (nameLength + 1) * sizeof(WCHAR) );
  2621. if ( lpTempName == NULL )
  2622. {
  2623. return DNS_ERROR_NO_MEMORY;
  2624. }
  2625. Dns_NameCopy( lpTempName,
  2626. NULL,
  2627. (PSTR) pszName,
  2628. 0,
  2629. DnsCharSetUnicode,
  2630. DnsCharSetUtf8 );
  2631. status = DnsCheckNameCollision_UTF8( lpTempName, Options );
  2632. FREE_HEAP( lpTempName );
  2633. return status;
  2634. }
  2635. //
  2636. // Roll your own query utilities
  2637. //
  2638. BOOL
  2639. WINAPI
  2640. DnsWriteQuestionToBuffer_W(
  2641. IN OUT PDNS_MESSAGE_BUFFER pDnsBuffer,
  2642. IN OUT LPDWORD pdwBufferSize,
  2643. IN PWSTR pszName,
  2644. IN WORD wType,
  2645. IN WORD Xid,
  2646. IN BOOL fRecursionDesired
  2647. )
  2648. /*++
  2649. Routine Description:
  2650. None.
  2651. Arguments:
  2652. None.
  2653. Return Value:
  2654. None.
  2655. --*/
  2656. {
  2657. //
  2658. // DCR_CLEANUP: duplicate code with routine below ... surprise!
  2659. // - eliminate duplicate
  2660. // - probably can just pick up library routine
  2661. //
  2662. PCHAR pch;
  2663. PCHAR pbufferEnd = NULL;
  2664. if ( *pdwBufferSize >= DNS_MAX_UDP_PACKET_BUFFER_LENGTH )
  2665. {
  2666. pbufferEnd = (PCHAR)pDnsBuffer + *pdwBufferSize;
  2667. // clear header
  2668. RtlZeroMemory( pDnsBuffer, sizeof(DNS_HEADER) );
  2669. // set for rewriting
  2670. pch = pDnsBuffer->MessageBody;
  2671. // write question name
  2672. pch = Dns_WriteDottedNameToPacket(
  2673. pch,
  2674. pbufferEnd,
  2675. (PCHAR) pszName,
  2676. NULL,
  2677. 0,
  2678. TRUE );
  2679. if ( !pch )
  2680. {
  2681. return FALSE;
  2682. }
  2683. // write question structure
  2684. *(UNALIGNED WORD *) pch = htons( wType );
  2685. pch += sizeof(WORD);
  2686. *(UNALIGNED WORD *) pch = DNS_RCLASS_INTERNET;
  2687. pch += sizeof(WORD);
  2688. // set question RR section count
  2689. pDnsBuffer->MessageHead.QuestionCount = htons( 1 );
  2690. pDnsBuffer->MessageHead.RecursionDesired = (BOOLEAN)fRecursionDesired;
  2691. pDnsBuffer->MessageHead.Xid = htons( Xid );
  2692. *pdwBufferSize = (DWORD)(pch - (PCHAR)pDnsBuffer);
  2693. return TRUE;
  2694. }
  2695. else
  2696. {
  2697. *pdwBufferSize = DNS_MAX_UDP_PACKET_BUFFER_LENGTH;
  2698. return FALSE;
  2699. }
  2700. }
  2701. BOOL
  2702. WINAPI
  2703. DnsWriteQuestionToBuffer_UTF8(
  2704. IN OUT PDNS_MESSAGE_BUFFER pDnsBuffer,
  2705. IN OUT PDWORD pdwBufferSize,
  2706. IN PSTR pszName,
  2707. IN WORD wType,
  2708. IN WORD Xid,
  2709. IN BOOL fRecursionDesired
  2710. )
  2711. /*++
  2712. Routine Description:
  2713. None.
  2714. Arguments:
  2715. None.
  2716. Return Value:
  2717. None.
  2718. --*/
  2719. {
  2720. PCHAR pch;
  2721. PCHAR pbufferEnd = NULL;
  2722. if ( *pdwBufferSize >= DNS_MAX_UDP_PACKET_BUFFER_LENGTH )
  2723. {
  2724. pbufferEnd = (PCHAR)pDnsBuffer + *pdwBufferSize;
  2725. // clear header
  2726. RtlZeroMemory( pDnsBuffer, sizeof(DNS_HEADER) );
  2727. // set for rewriting
  2728. pch = pDnsBuffer->MessageBody;
  2729. // write question name
  2730. pch = Dns_WriteDottedNameToPacket(
  2731. pch,
  2732. pbufferEnd,
  2733. pszName,
  2734. NULL,
  2735. 0,
  2736. FALSE );
  2737. if ( !pch )
  2738. {
  2739. return FALSE;
  2740. }
  2741. // write question structure
  2742. *(UNALIGNED WORD *) pch = htons( wType );
  2743. pch += sizeof(WORD);
  2744. *(UNALIGNED WORD *) pch = DNS_RCLASS_INTERNET;
  2745. pch += sizeof(WORD);
  2746. // set question RR section count
  2747. pDnsBuffer->MessageHead.QuestionCount = htons( 1 );
  2748. pDnsBuffer->MessageHead.RecursionDesired = (BOOLEAN)fRecursionDesired;
  2749. pDnsBuffer->MessageHead.Xid = htons( Xid );
  2750. *pdwBufferSize = (DWORD)(pch - (PCHAR)pDnsBuffer);
  2751. return TRUE;
  2752. }
  2753. else
  2754. {
  2755. *pdwBufferSize = DNS_MAX_UDP_PACKET_BUFFER_LENGTH;
  2756. return FALSE;
  2757. }
  2758. }
  2759. //
  2760. // Record list to\from results
  2761. //
  2762. VOID
  2763. CombineRecordsInBlob(
  2764. IN PDNS_RESULTS pResults,
  2765. OUT PDNS_RECORD * ppRecords
  2766. )
  2767. /*++
  2768. Routine Description:
  2769. Query DNS -- shim for main SDK query routine.
  2770. Arguments:
  2771. pQueryInfo -- blob describing query
  2772. Return Value:
  2773. ERROR_SUCCESS if successful query.
  2774. Error code on failure.
  2775. --*/
  2776. {
  2777. PDNS_RECORD prr;
  2778. DNSDBG( TRACE, ( "CombineRecordsInBlob()\n" ));
  2779. //
  2780. // combine records back into one list
  2781. //
  2782. // note, working backwards so only touch records once
  2783. //
  2784. prr = Dns_RecordListAppend(
  2785. pResults->pAuthorityRecords,
  2786. pResults->pAdditionalRecords
  2787. );
  2788. prr = Dns_RecordListAppend(
  2789. pResults->pAnswerRecords,
  2790. prr
  2791. );
  2792. prr = Dns_RecordListAppend(
  2793. pResults->pAliasRecords,
  2794. prr
  2795. );
  2796. *ppRecords = prr;
  2797. }
  2798. VOID
  2799. BreakRecordsIntoBlob(
  2800. OUT PDNS_RESULTS pResults,
  2801. IN PDNS_RECORD pRecords,
  2802. IN WORD wType
  2803. )
  2804. /*++
  2805. Routine Description:
  2806. Break single record list into results pblob->
  2807. Arguments:
  2808. pResults -- results to fill in
  2809. pRecords -- record list
  2810. Return Value:
  2811. None
  2812. --*/
  2813. {
  2814. PDNS_RECORD prr;
  2815. PDNS_RECORD pnextRR;
  2816. DNS_LIST listAnswer;
  2817. DNS_LIST listAlias;
  2818. DNS_LIST listAdditional;
  2819. DNS_LIST listAuthority;
  2820. DNSDBG( TRACE, ( "BreakRecordsIntoBlob()\n" ));
  2821. //
  2822. // clear blob
  2823. //
  2824. RtlZeroMemory(
  2825. pResults,
  2826. sizeof(*pResults) );
  2827. //
  2828. // init building lists
  2829. //
  2830. DNS_LIST_STRUCT_INIT( listAnswer );
  2831. DNS_LIST_STRUCT_INIT( listAlias );
  2832. DNS_LIST_STRUCT_INIT( listAdditional );
  2833. DNS_LIST_STRUCT_INIT( listAuthority );
  2834. //
  2835. // break list into section specific lists
  2836. // - note, this does pull RR sets apart, but
  2837. // they, being in same section, should immediately
  2838. // be rejoined
  2839. //
  2840. // - note, hostfile records made have section=0
  2841. // this is no longer the case but preserve until
  2842. // know this is solid and determine what section==0
  2843. // means
  2844. //
  2845. pnextRR = pRecords;
  2846. while ( prr = pnextRR )
  2847. {
  2848. pnextRR = prr->pNext;
  2849. prr->pNext = NULL;
  2850. if ( prr->Flags.S.Section == 0 ||
  2851. prr->Flags.S.Section == DNSREC_ANSWER )
  2852. {
  2853. if ( prr->wType == DNS_TYPE_CNAME &&
  2854. wType != DNS_TYPE_CNAME )
  2855. {
  2856. DNS_LIST_STRUCT_ADD( listAlias, prr );
  2857. continue;
  2858. }
  2859. else
  2860. {
  2861. DNS_LIST_STRUCT_ADD( listAnswer, prr );
  2862. continue;
  2863. }
  2864. }
  2865. else if ( prr->Flags.S.Section == DNSREC_ADDITIONAL )
  2866. {
  2867. DNS_LIST_STRUCT_ADD( listAdditional, prr );
  2868. continue;
  2869. }
  2870. else
  2871. {
  2872. DNS_LIST_STRUCT_ADD( listAuthority, prr );
  2873. continue;
  2874. }
  2875. }
  2876. // pack stuff into blob
  2877. pResults->pAnswerRecords = listAnswer.pFirst;
  2878. pResults->pAliasRecords = listAlias.pFirst;
  2879. pResults->pAuthorityRecords = listAuthority.pFirst;
  2880. pResults->pAdditionalRecords = listAdditional.pFirst;
  2881. }
  2882. //
  2883. // Random discontinued
  2884. //
  2885. #if 0
  2886. IP_ADDRESS
  2887. findHostIpInRecordList(
  2888. IN PDNS_RECORD pRecord,
  2889. IN PDNS_NAME pszHostName
  2890. )
  2891. /*++
  2892. Routine Description:
  2893. Find IP for hostname, if its A record is in list.
  2894. Arguments:
  2895. pRecord - incoming RR set
  2896. pszHostName - hostname to find
  2897. Return Value:
  2898. IP address matching hostname, if A record for hostname found.
  2899. Zero if not found.
  2900. --*/
  2901. {
  2902. //
  2903. // DCR: find best A record for name
  2904. // currently only finds first; this is harmless but in
  2905. // disjoint net situation would need sorted to find best
  2906. //
  2907. while ( pRecord )
  2908. {
  2909. if ( pRecord->wType == DNS_TYPE_A &&
  2910. Dns_NameCompare(
  2911. pRecord->pName,
  2912. pszHostName ) )
  2913. {
  2914. return( pRecord->Data.A.IpAddress );
  2915. }
  2916. pRecord = pRecord->pNext;
  2917. }
  2918. return( 0 );
  2919. }
  2920. #endif
  2921. //
  2922. // End query.c
  2923. //