Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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