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.

3849 lines
109 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. parse.c
  5. Abstract:
  6. This source contains the functions that parse the lmhosts file.
  7. Author:
  8. Jim Stewart May 2, 1993
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include "hosts.h"
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include "parse.tmh"
  16. #ifdef VXD
  17. extern BOOL fInInit;
  18. extern BOOLEAN CachePrimed;
  19. #endif
  20. //
  21. // Returns 0 if equal, 1 if not equal. Used to avoid using c-runtime
  22. //
  23. #define strncmp( pch1, pch2, length ) \
  24. (!CTEMemEqu( pch1, pch2, length ) )
  25. //
  26. // Private Definitions
  27. //
  28. // As an lmhosts file is parsed, a #INCLUDE directive is interpreted
  29. // according to the INCLUDE_STATE at that instance. This state is
  30. // determined by the #BEGIN_ALTERNATE and #END_ALTERNATE directives.
  31. //
  32. //
  33. typedef enum _INCLUDE_STATE
  34. {
  35. MustInclude = 0, // shouldn't fail
  36. TryToInclude, // in alternate block
  37. SkipInclude // satisfied alternate
  38. // block
  39. } INCLUDE_STATE;
  40. //
  41. // LmpGetTokens() parses a line and returns the tokens in the following
  42. // order:
  43. //
  44. typedef enum _TOKEN_ORDER_
  45. {
  46. IpAddress = 0, // first token
  47. NbName, // 2nd token
  48. GroupName, // 3rd or 4th token
  49. NotUsed, // #PRE, if any
  50. NotUsed2, // #NOFNR, if any
  51. MaxTokens // this must be last
  52. } TOKEN_ORDER;
  53. //
  54. // As each line in an lmhosts file is parsed, it is classified into one of
  55. // the categories enumerated below.
  56. //
  57. // However, Preload is a special member of the enum.
  58. //
  59. //
  60. typedef enum _TYPE_OF_LINE
  61. {
  62. Comment = 0x0000, // comment line
  63. Ordinary = 0x0001, // ip_addr NetBIOS name
  64. Domain = 0x0002, // ... #DOM:name
  65. Include = 0x0003, // #INCLUDE file
  66. BeginAlternate = 0x0004, // #BEGIN_ALTERNATE
  67. EndAlternate = 0x0005, // #END_ALTERNATE
  68. ErrorLine = 0x0006, // Error in line
  69. NoFNR = 0x4000, // ... #NOFNR
  70. Preload = 0x8000 // ... #PRE
  71. } TYPE_OF_LINE;
  72. //
  73. // In an lmhosts file, the following are recognized as keywords:
  74. //
  75. // #BEGIN_ALTERNATE #END_ALTERNATE #PRE
  76. // #DOM: #INCLUDE
  77. //
  78. // Information about each keyword is kept in a KEYWORD structure.
  79. //
  80. //
  81. typedef struct _KEYWORD
  82. { // reserved keyword
  83. char *k_string; // NULL terminated
  84. size_t k_strlen; // length of token
  85. TYPE_OF_LINE k_type; // type of line
  86. int k_noperands; // max operands on line
  87. } KEYWORD, *PKEYWORD;
  88. typedef struct _LINE_CHARACTERISTICS_
  89. {
  90. int l_category:4; // enum _TYPE_OF_LINE
  91. int l_preload:1; // marked with #PRE ?
  92. unsigned int l_nofnr:1; // marked with #NOFNR
  93. } LINE_CHARACTERISTICS, *PLINE_CHARACTERISTICS;
  94. //
  95. // Do not allow DNS name queries for the following Name Types:
  96. //
  97. // Name Number(h) Type Usage
  98. // --------------------------------------------------------------------------
  99. // <computername> 01 Unique Messenger Service
  100. // <\\--__MSBROWSE__> 01 Group Master Browser
  101. // <domain> 1B Unique Domain Master Browser
  102. // <domain> 1C Group Domain Controllers
  103. // <INet~Services> 1C Group IIS
  104. // <domain> 1D Unique Master Browser
  105. // <domain> 1E Group Browser Service Elections
  106. #define IsValidDnsNameTag(_c) \
  107. ((_c != 0x01) && \
  108. ((_c < 0x1B) || (_c > 0x1E)))
  109. //
  110. // Local Variables
  111. //
  112. //
  113. // In an lmhosts file, the token '#' in any column usually denotes that
  114. // the rest of the line is to be ignored. However, a '#' may also be the
  115. // first character of a keyword.
  116. //
  117. // Keywords are divided into two groups:
  118. //
  119. // 1. decorations that must either be the 3rd or 4th token of a line,
  120. // 2. directives that must begin in column 0,
  121. //
  122. //
  123. KEYWORD Decoration[] =
  124. {
  125. DOMAIN_TOKEN, sizeof(DOMAIN_TOKEN) - 1, Domain, 5,
  126. PRELOAD_TOKEN, sizeof(PRELOAD_TOKEN) - 1, Preload, 5,
  127. NOFNR_TOKEN, sizeof(NOFNR_TOKEN) -1, NoFNR, 5,
  128. NULL, 0 // must be last
  129. };
  130. KEYWORD Directive[] =
  131. {
  132. INCLUDE_TOKEN, sizeof(INCLUDE_TOKEN) - 1, Include, 2,
  133. BEG_ALT_TOKEN, sizeof(BEG_ALT_TOKEN) - 1, BeginAlternate, 1,
  134. END_ALT_TOKEN, sizeof(END_ALT_TOKEN) - 1, EndAlternate, 1,
  135. NULL, 0 // must be last
  136. };
  137. //
  138. // Local Variables
  139. //
  140. //
  141. // Each preloaded lmhosts entry corresponds to NSUFFIXES NetBIOS names,
  142. // each with a 16th byte from Suffix[].
  143. //
  144. // For example, an lmhosts entry specifying "popcorn" causes the
  145. // following NetBIOS names to be added to nbt.sys' name cache:
  146. //
  147. // "POPCORN "
  148. // "POPCORN 0x0"
  149. // "POPCORN 0x3"
  150. //
  151. //
  152. #define NSUFFIXES 3
  153. UCHAR Suffix[] = { // LAN Manager Component
  154. 0x20, // server
  155. 0x0, // redirector
  156. 0x03 // messenger
  157. };
  158. #ifndef VXD
  159. //
  160. // this structure tracks names queries that are passed up to user mode
  161. // to resolve via DnsQueries
  162. //
  163. tLMHSVC_REQUESTS DnsQueries;
  164. tLMHSVC_REQUESTS CheckAddr;
  165. #endif
  166. tLMHSVC_REQUESTS LmHostQueries; // Track names queries passed for LMhost processing
  167. tDOMAIN_LIST DomainNames;
  168. //
  169. // Local (Private) Functions
  170. //
  171. LINE_CHARACTERISTICS
  172. LmpGetTokens (
  173. IN OUT PUCHAR line,
  174. OUT PUCHAR *token,
  175. IN OUT int *pnumtokens
  176. );
  177. PKEYWORD
  178. LmpIsKeyWord (
  179. IN PUCHAR string,
  180. IN PKEYWORD table
  181. );
  182. BOOLEAN
  183. LmpBreakRecursion(
  184. IN PUCHAR path,
  185. IN PUCHAR target,
  186. IN ULONG TargetLength
  187. );
  188. LONG
  189. HandleSpecial(
  190. IN char **pch);
  191. ULONG
  192. AddToDomainList (
  193. IN PUCHAR pName,
  194. IN tIPADDRESS IpAddress,
  195. IN PLIST_ENTRY pDomainHead,
  196. IN BOOLEAN fPreload
  197. );
  198. NTSTATUS
  199. ChangeStateOfName (
  200. IN tIPADDRESS IpAddress,
  201. IN NBT_WORK_ITEM_CONTEXT *pContext,
  202. IN OUT NBT_WORK_ITEM_CONTEXT **ppContext,
  203. IN USHORT NameAddFlags
  204. );
  205. VOID
  206. LmHostTimeout(
  207. PVOID pContext,
  208. PVOID pContext2,
  209. tTIMERQENTRY *pTimerQEntry
  210. );
  211. NBT_WORK_ITEM_CONTEXT *
  212. GetNameToFind(
  213. OUT PUCHAR pName
  214. );
  215. VOID
  216. GetContext (
  217. IN OUT NBT_WORK_ITEM_CONTEXT **ppContext
  218. );
  219. VOID
  220. MakeNewListCurrent (
  221. PLIST_ENTRY pTmpDomainList
  222. );
  223. VOID
  224. RemoveNameAndCompleteReq (
  225. IN NBT_WORK_ITEM_CONTEXT *pContext,
  226. IN NTSTATUS status
  227. );
  228. PCHAR
  229. Nbtstrcat( PUCHAR pch, PUCHAR pCat, LONG Len );
  230. //******************* Pageable Routine Declarations ****************
  231. #ifdef ALLOC_PRAGMA
  232. #pragma CTEMakePageable(PAGE, LmGetIpAddr)
  233. #pragma CTEMakePageable(PAGE, HandleSpecial)
  234. #pragma CTEMakePageable(PAGE, LmpGetTokens)
  235. #pragma CTEMakePageable(PAGE, LmpIsKeyWord)
  236. #pragma CTEMakePageable(PAGE, LmpBreakRecursion)
  237. #pragma CTEMakePageable(PAGE, AddToDomainList)
  238. #pragma CTEMakePageable(PAGE, LmExpandName)
  239. #pragma CTEMakePageable(PAGE, LmInclude)
  240. #pragma CTEMakePageable(PAGE, LmGetFullPath)
  241. #pragma CTEMakePageable(PAGE, PrimeCache)
  242. #pragma CTEMakePageable(PAGE, DelayedScanLmHostFile)
  243. #pragma CTEMakePageable(PAGE, NbtCompleteLmhSvcRequest)
  244. #endif
  245. //******************* Pageable Routine Declarations ****************
  246. //----------------------------------------------------------------------------
  247. unsigned long
  248. LmGetIpAddr (
  249. IN PUCHAR path,
  250. IN PUCHAR target,
  251. IN CHAR RecurseDepth,
  252. OUT BOOLEAN *bFindName
  253. )
  254. /*++
  255. Routine Description:
  256. This function searches the file for an lmhosts entry that can be
  257. mapped to the second level encoding. It then returns the ip address
  258. specified in that entry.
  259. This function is called recursively, via LmInclude() !!
  260. Arguments:
  261. path - a fully specified path to a lmhosts file
  262. target - the unencoded 16 byte NetBIOS name to look for
  263. RecurseDepth- the depth to which we can resurse -- 0 => no more recursion
  264. Return Value:
  265. The ip address (network byte order), or 0 if no appropriate entry was
  266. found.
  267. Note that in most contexts (but not here), ip address 0 signifies
  268. "this host."
  269. --*/
  270. {
  271. PUCHAR buffer;
  272. PLM_FILE pfile;
  273. NTSTATUS status;
  274. int count, nwords;
  275. INCLUDE_STATE incstate;
  276. PUCHAR token[MaxTokens];
  277. LINE_CHARACTERISTICS current;
  278. unsigned long inaddr, retval;
  279. UCHAR temp[NETBIOS_NAME_SIZE+1];
  280. CTEPagedCode();
  281. //
  282. // Check for infinitely recursive name lookup in a #INCLUDE.
  283. //
  284. if (LmpBreakRecursion(path, target, NETBIOS_NAME_SIZE-1) == TRUE)
  285. {
  286. return (0);
  287. }
  288. #ifdef VXD
  289. //
  290. // if we came here via nbtstat -R and InDos is set, report error: user
  291. // can try nbtstat -R again. (since nbtstat can only be run from DOS box,
  292. // can InDos be ever set??? Might as well play safe)
  293. //
  294. if ( !fInInit && GetInDosFlag() )
  295. {
  296. return(0);
  297. }
  298. #endif
  299. pfile = LmOpenFile(path);
  300. if (!pfile)
  301. {
  302. return((unsigned long) 0);
  303. }
  304. *bFindName = FALSE;
  305. inaddr = 0;
  306. incstate = MustInclude;
  307. while (buffer = LmFgets(pfile, &count))
  308. {
  309. nwords = MaxTokens;
  310. current = LmpGetTokens(buffer, token, &nwords);
  311. switch ((ULONG)current.l_category)
  312. {
  313. case ErrorLine:
  314. continue;
  315. case Domain:
  316. case Ordinary:
  317. if (current.l_preload ||
  318. ((nwords - 1) < NbName))
  319. {
  320. continue;
  321. }
  322. break;
  323. case Include:
  324. if (!RecurseDepth || (incstate == SkipInclude) || (nwords < 2))
  325. {
  326. continue;
  327. }
  328. retval = LmInclude(token[1], LmGetIpAddr, target, (CHAR) (RecurseDepth-1), bFindName);
  329. if (retval != 0) {
  330. if (incstate == TryToInclude)
  331. {
  332. incstate = SkipInclude;
  333. }
  334. } else {
  335. if (incstate == MustInclude)
  336. {
  337. IF_DBG(NBT_DEBUG_LMHOST)
  338. KdPrint(("Nbt.LmGetIpAddr: Can't #INCLUDE \"%s\"", token[1]));
  339. }
  340. continue;
  341. }
  342. inaddr = retval;
  343. goto found;
  344. case BeginAlternate:
  345. ASSERT(nwords == 1);
  346. incstate = TryToInclude;
  347. continue;
  348. case EndAlternate:
  349. ASSERT(nwords == 1);
  350. incstate = MustInclude;
  351. continue;
  352. default:
  353. continue;
  354. }
  355. if (strlen(token[NbName]) == (NETBIOS_NAME_SIZE))
  356. {
  357. if (strncmp(token[NbName], target, (NETBIOS_NAME_SIZE)) != 0)
  358. {
  359. continue;
  360. }
  361. } else
  362. {
  363. //
  364. // attempt to match, in a case insensitive manner, the first 15
  365. // bytes of the lmhosts entry with the target name.
  366. //
  367. LmExpandName(temp, token[NbName], 0);
  368. if (strncmp(temp, target, NETBIOS_NAME_SIZE - 1) != 0)
  369. {
  370. continue;
  371. }
  372. }
  373. if (current.l_nofnr)
  374. {
  375. *bFindName = TRUE;
  376. }
  377. status = ConvertDottedDecimalToUlong(token[IpAddress],&inaddr);
  378. if (!NT_SUCCESS(status))
  379. {
  380. inaddr = 0;
  381. }
  382. break;
  383. }
  384. found:
  385. status = LmCloseFile(pfile);
  386. ASSERT(status == STATUS_SUCCESS);
  387. if (!NT_SUCCESS(status))
  388. {
  389. *bFindName = FALSE;
  390. }
  391. IF_DBG(NBT_DEBUG_LMHOST)
  392. KdPrint(("Nbt.LmGetIpAddr: (\"%15.15s<%X>\") = %X\n",target,target[15],inaddr));
  393. return(inaddr);
  394. } // LmGetIpAddr
  395. //----------------------------------------------------------------------------
  396. LONG
  397. HandleSpecial(
  398. IN CHAR **pch)
  399. /*++
  400. Routine Description:
  401. This function converts ASCII hex into a ULONG.
  402. Arguments:
  403. Return Value:
  404. The ip address (network byte order), or 0 if no appropriate entry was
  405. found.
  406. Note that in most contexts (but not here), ip address 0 signifies
  407. "this host."
  408. --*/
  409. {
  410. int sval;
  411. int rval;
  412. char *sp = *pch;
  413. int i;
  414. CTEPagedCode();
  415. sp++;
  416. switch (*sp)
  417. {
  418. case '\\':
  419. // the second character is also a \ so return a \ and set pch to
  420. // point to the next character (\)
  421. //
  422. *pch = sp;
  423. return((int)'\\');
  424. default:
  425. // convert some number of characters to hex and increment pch
  426. // the expected format is "\0x03"
  427. //
  428. // sscanf(sp, "%2x%n", &sval, &rval);
  429. sval = 0;
  430. rval = 0;
  431. sp++;
  432. // check for the 0x part of the hex number
  433. if (*sp != 'x')
  434. {
  435. *pch = sp;
  436. return(-1);
  437. }
  438. sp++;
  439. for (i=0;(( i<2 ) && *sp) ;i++ )
  440. {
  441. if (*sp != ' ')
  442. {
  443. // convert from ASCII to hex, allowing capitals too
  444. //
  445. if (*sp >= 'a')
  446. {
  447. sval = *sp - 'a' + 10 + sval*16;
  448. }
  449. else
  450. if (*sp >= 'A')
  451. {
  452. sval = *sp - 'A' + 10 + sval*16;
  453. }
  454. else
  455. {
  456. sval = *sp - '0' + sval*16;
  457. }
  458. sp++;
  459. rval++;
  460. }
  461. else
  462. break;
  463. }
  464. if (rval < 1)
  465. {
  466. *pch = sp;
  467. return(-1);
  468. }
  469. *pch += (rval+2); // remember to account for the characters 0 and x
  470. return(sval);
  471. }
  472. }
  473. #define LMHASSERT(s) if (!(s)) \
  474. { retval.l_category = ErrorLine; return(retval); }
  475. //----------------------------------------------------------------------------
  476. LINE_CHARACTERISTICS
  477. LmpGetTokens (
  478. IN OUT PUCHAR line,
  479. OUT PUCHAR *token,
  480. IN OUT int *pnumtokens
  481. )
  482. /*++
  483. Routine Description:
  484. This function parses a line for tokens. A maximum of *pnumtokens
  485. are collected.
  486. Arguments:
  487. line - pointer to the NULL terminated line to parse
  488. token - an array of pointers to tokens collected
  489. *pnumtokens - on input, number of elements in the array, token[];
  490. on output, number of tokens collected in token[]
  491. Return Value:
  492. The characteristics of this lmhosts line.
  493. Notes:
  494. 1. Each token must be separated by white space. Hence, the keyword
  495. "#PRE" in the following line won't be recognized:
  496. 11.1.12.132 lothair#PRE
  497. 2. Any ordinary line can be decorated with a "#PRE", a "#DOM:name" or
  498. both. Hence, the following lines must all be recognized:
  499. 111.21.112.3 kernel #DOM:ntwins #PRE
  500. 111.21.112.4 orville #PRE #DOM:ntdev
  501. 111.21.112.7 cliffv4 #DOM:ntlan
  502. 111.21.112.132 lothair #PRE
  503. --*/
  504. {
  505. enum _PARSE
  506. { // current fsm state
  507. StartofLine,
  508. WhiteSpace,
  509. AmidstToken
  510. } state;
  511. PUCHAR pch; // current fsm input
  512. PUCHAR och;
  513. PKEYWORD keyword;
  514. int index, maxtokens, quoted, rchar;
  515. LINE_CHARACTERISTICS retval;
  516. CTEPagedCode();
  517. CTEZeroMemory(token, *pnumtokens * sizeof(PUCHAR));
  518. state = StartofLine;
  519. retval.l_category = Ordinary;
  520. retval.l_preload = 0;
  521. retval.l_nofnr = 0;
  522. maxtokens = *pnumtokens;
  523. index = 0;
  524. quoted = 0;
  525. for (pch = line; *pch; pch++)
  526. {
  527. switch (*pch)
  528. {
  529. //
  530. // does the '#' signify the start of a reserved keyword, or the
  531. // start of a comment ?
  532. //
  533. //
  534. case '#':
  535. if (quoted)
  536. {
  537. *och++ = *pch;
  538. continue;
  539. }
  540. keyword = LmpIsKeyWord(
  541. pch,
  542. (state == StartofLine) ? Directive : Decoration);
  543. if (keyword)
  544. {
  545. state = AmidstToken;
  546. maxtokens = keyword->k_noperands;
  547. switch (keyword->k_type)
  548. {
  549. case NoFNR:
  550. retval.l_nofnr = 1;
  551. continue;
  552. case Preload:
  553. retval.l_preload = 1;
  554. continue;
  555. default:
  556. LMHASSERT(maxtokens <= *pnumtokens);
  557. LMHASSERT(index < maxtokens);
  558. token[index++] = pch;
  559. retval.l_category = keyword->k_type;
  560. continue;
  561. }
  562. LMHASSERT(0);
  563. }
  564. if (state == StartofLine)
  565. {
  566. retval.l_category = Comment;
  567. }
  568. /* fall through */
  569. case '\r':
  570. case '\n':
  571. *pch = (UCHAR) NULL;
  572. if (quoted)
  573. {
  574. *och = (UCHAR) NULL;
  575. }
  576. goto done;
  577. case ' ':
  578. case '\t':
  579. if (quoted)
  580. {
  581. *och++ = *pch;
  582. continue;
  583. }
  584. if (state == AmidstToken)
  585. {
  586. state = WhiteSpace;
  587. *pch = (UCHAR) NULL;
  588. if (index == maxtokens)
  589. {
  590. goto done;
  591. }
  592. }
  593. continue;
  594. case '"':
  595. if ((state == AmidstToken) && quoted)
  596. {
  597. state = WhiteSpace;
  598. quoted = 0;
  599. *pch = (UCHAR) NULL;
  600. *och = (UCHAR) NULL;
  601. if (index == maxtokens)
  602. {
  603. goto done;
  604. }
  605. continue;
  606. }
  607. state = AmidstToken;
  608. quoted = 1;
  609. LMHASSERT(maxtokens <= *pnumtokens);
  610. LMHASSERT(index < maxtokens);
  611. token[index++] = pch + 1;
  612. och = pch + 1;
  613. continue;
  614. case '\\':
  615. if (quoted)
  616. {
  617. rchar = HandleSpecial(&pch);
  618. if (rchar == -1)
  619. {
  620. retval.l_category = ErrorLine;
  621. return(retval);
  622. }
  623. *och++ = (UCHAR)rchar;
  624. //
  625. // put null on end of string
  626. //
  627. continue;
  628. }
  629. default:
  630. if (quoted)
  631. {
  632. *och++ = *pch;
  633. continue;
  634. }
  635. if (state == AmidstToken)
  636. {
  637. continue;
  638. }
  639. state = AmidstToken;
  640. LMHASSERT(maxtokens <= *pnumtokens);
  641. LMHASSERT(index < maxtokens);
  642. token[index++] = pch;
  643. continue;
  644. }
  645. }
  646. done:
  647. //
  648. // if there is no name on the line, then return an error
  649. //
  650. if (index <= NbName && index != maxtokens)
  651. {
  652. retval.l_category = ErrorLine;
  653. }
  654. ASSERT(!*pch);
  655. ASSERT(maxtokens <= *pnumtokens);
  656. ASSERT(index <= *pnumtokens);
  657. *pnumtokens = index;
  658. return(retval);
  659. } // LmpGetTokens
  660. //----------------------------------------------------------------------------
  661. PKEYWORD
  662. LmpIsKeyWord (
  663. IN PUCHAR string,
  664. IN PKEYWORD table
  665. )
  666. /*++
  667. Routine Description:
  668. This function determines whether the string is a reserved keyword.
  669. Arguments:
  670. string - the string to search
  671. table - an array of keywords to look for
  672. Return Value:
  673. A pointer to the relevant keyword object, or NULL if unsuccessful
  674. --*/
  675. {
  676. size_t limit;
  677. PKEYWORD special;
  678. CTEPagedCode();
  679. limit = strlen(string);
  680. for (special = table; special->k_string; special++)
  681. {
  682. if (limit < special->k_strlen)
  683. {
  684. continue;
  685. }
  686. if ((limit >= special->k_strlen) &&
  687. !strncmp(string, special->k_string, special->k_strlen))
  688. {
  689. return(special);
  690. }
  691. }
  692. return((PKEYWORD) NULL);
  693. } // LmpIsKeyWord
  694. //----------------------------------------------------------------------------
  695. BOOLEAN
  696. LmpBreakRecursion(
  697. IN PUCHAR path,
  698. IN PUCHAR target,
  699. IN ULONG TargetLength
  700. )
  701. /*++
  702. Routine Description:
  703. This function checks that the file name we are about to open
  704. does not use the target name of this search, which would
  705. cause an infinite lookup loop.
  706. Arguments:
  707. path - a fully specified path to a lmhosts file
  708. target - the unencoded 16 byte NetBIOS name to look for
  709. Return Value:
  710. TRUE if the UNC server name in the file path is the same as the
  711. target of this search. FALSE otherwise.
  712. Notes:
  713. This function does not detect redirected drives.
  714. --*/
  715. {
  716. PCHAR keystring = "\\DosDevices\\UNC\\";
  717. PCHAR servername[NETBIOS_NAME_SIZE+1]; // for null on end
  718. PCHAR marker1;
  719. PCHAR marker2;
  720. PCHAR marker3;
  721. BOOLEAN retval = FALSE;
  722. tNAMEADDR *pNameAddr;
  723. ULONG uType;
  724. CTEPagedCode();
  725. //
  726. // Check for and extract the UNC server name
  727. //
  728. if ((path) && (strlen(path) > strlen(keystring)))
  729. {
  730. // check that the name is a unc name
  731. if (strncmp(path, keystring, strlen(keystring)) == 0)
  732. {
  733. marker1 = path + strlen(keystring); // the end of the \DosDevices\Unc\ string
  734. marker3 = &path[strlen(path)-1]; // the end of the whole path
  735. marker2 = strchr(marker1,'\\'); // the end of the server name
  736. if ((marker2) && // marker2 can be NULL if '\\' does not exist in the string
  737. (marker2 != marker3))
  738. {
  739. *marker2 = '\0';
  740. //
  741. // attempt to match, in a case insensitive manner, the
  742. // first 15 bytes of the lmhosts entry with the target
  743. // name.
  744. //
  745. LmExpandName((PUCHAR)servername, marker1, 0);
  746. if(strncmp((PUCHAR)servername, target, TargetLength) == 0)
  747. {
  748. //
  749. // break the recursion
  750. //
  751. retval = TRUE;
  752. IF_DBG(NBT_DEBUG_LMHOST)
  753. KdPrint(("Nbt.LmpBreakRecursion: Not including Lmhosts file <%s> because of recursive name\n",
  754. servername));
  755. }
  756. else
  757. {
  758. //
  759. // check if the name has been preloaded in the cache, and
  760. // if not, fail the request so we can't get into a loop
  761. // trying to include the remote file while trying to
  762. // resolve the remote name
  763. //
  764. pNameAddr = LockAndFindName(NBT_REMOTE,
  765. (PCHAR)servername,
  766. NbtConfig.pScope,
  767. &uType);
  768. if (!pNameAddr || !(pNameAddr->NameTypeState & PRELOADED) )
  769. {
  770. //
  771. // break the recursion
  772. //
  773. retval = TRUE;
  774. IF_DBG(NBT_DEBUG_LMHOST)
  775. KdPrint(("Nbt.LmpBreakRecursion: Not including Lmhosts #include because name not Preloaded %s\n",
  776. servername));
  777. }
  778. }
  779. *marker2 = '\\';
  780. }
  781. }
  782. }
  783. return(retval);
  784. }
  785. //----------------------------------------------------------------------------
  786. char *
  787. LmExpandName (
  788. OUT PUCHAR dest,
  789. IN PUCHAR source,
  790. IN UCHAR last
  791. )
  792. /*++
  793. Routine Description:
  794. This function expands an lmhosts entry into a full 16 byte NetBIOS
  795. name. It is padded with blanks up to 15 bytes; the 16th byte is the
  796. input parameter, last.
  797. This function does not encode 1st level names to 2nd level names nor
  798. vice-versa.
  799. Both dest and source are NULL terminated strings.
  800. Arguments:
  801. dest - sizeof(dest) must be NBT_NONCODED_NMSZ
  802. source - the lmhosts entry
  803. last - the 16th byte of the NetBIOS name
  804. Return Value:
  805. dest.
  806. --*/
  807. {
  808. char byte;
  809. char *retval = dest;
  810. char *src = source ;
  811. #ifndef VXD
  812. WCHAR unicodebuf[NETBIOS_NAME_SIZE+1];
  813. UNICODE_STRING unicode;
  814. STRING tmp;
  815. #endif
  816. NTSTATUS status;
  817. PUCHAR limit;
  818. CTEPagedCode();
  819. //
  820. // first, copy the source OEM string to the destination, pad it, and
  821. // add the last character.
  822. //
  823. limit = dest + NETBIOS_NAME_SIZE - 1;
  824. while ( (*source != '\0') && (dest < limit) )
  825. {
  826. *dest++ = *source++;
  827. }
  828. while(dest < limit)
  829. {
  830. *dest++ = ' ';
  831. }
  832. ASSERT(dest == (retval + NETBIOS_NAME_SIZE - 1));
  833. *dest = '\0';
  834. *(dest + 1) = '\0';
  835. dest = retval;
  836. #ifndef VXD
  837. //
  838. // Now, convert to unicode then to ANSI to force the OEM -> ANSI munge.
  839. // Then convert back to Unicode and uppercase the name. Finally convert
  840. // back to OEM.
  841. //
  842. unicode.Length = 0;
  843. unicode.MaximumLength = 2*(NETBIOS_NAME_SIZE+1);
  844. unicode.Buffer = unicodebuf;
  845. RtlInitString(&tmp, dest);
  846. status = RtlOemStringToUnicodeString(&unicode, &tmp, FALSE);
  847. if (!NT_SUCCESS(status))
  848. {
  849. IF_DBG(NBT_DEBUG_LMHOST)
  850. KdPrint (("Nbt.LmExpandName: Oem -> Unicode failed, status %X\n", status));
  851. goto oldupcase;
  852. }
  853. status = RtlUnicodeStringToAnsiString(&tmp, &unicode, FALSE);
  854. if (!NT_SUCCESS(status))
  855. {
  856. IF_DBG(NBT_DEBUG_LMHOST)
  857. KdPrint (("Nbt.LmExpandName: Unicode -> Ansi failed, status %X\n", status));
  858. goto oldupcase;
  859. }
  860. status = RtlAnsiStringToUnicodeString(&unicode, &tmp, FALSE);
  861. if (!NT_SUCCESS(status))
  862. {
  863. IF_DBG(NBT_DEBUG_LMHOST)
  864. KdPrint (("Nbt.LmExpandName: Ansi -> Unicode failed, status %X\n", status));
  865. goto oldupcase;
  866. }
  867. status = RtlUpcaseUnicodeStringToOemString(&tmp, &unicode, FALSE);
  868. if (!NT_SUCCESS(status))
  869. {
  870. IF_DBG(NBT_DEBUG_LMHOST)
  871. KdPrint (("Nbt.LmExpandName: Unicode upcase -> Oem failed, status %X\n", status));
  872. goto oldupcase;
  873. }
  874. // write the last byte to "0x20" or "0x03" or whatever
  875. // since we do not want it to go through the munge above.
  876. //
  877. dest[NETBIOS_NAME_SIZE-1] = last;
  878. return(retval);
  879. #endif
  880. oldupcase:
  881. for ( source = src ; dest < (retval + NETBIOS_NAME_SIZE - 1); dest++)
  882. {
  883. byte = *(source++);
  884. if (!byte)
  885. {
  886. break;
  887. }
  888. // Don't use the c-runtime (nt c defn. included first)
  889. // What about extended characters etc.? Since extended characters do
  890. // not normally part of netbios names, we will fix if requested
  891. *dest = (byte >= 'a' && byte <= 'z') ? byte-'a' + 'A' : byte ;
  892. // *dest = islower(byte) ? toupper(byte) : byte;
  893. }
  894. for (; dest < retval + NETBIOS_NAME_SIZE - 1; dest++)
  895. {
  896. *dest = ' ';
  897. }
  898. ASSERT(dest == (retval + NETBIOS_NAME_SIZE - 1));
  899. *dest = last;
  900. *(dest + 1) = (char) NULL;
  901. return(retval);
  902. } // LmExpandName
  903. //----------------------------------------------------------------------------
  904. unsigned long
  905. LmInclude(
  906. IN PUCHAR file,
  907. IN LM_PARSE_FUNCTION function,
  908. IN PUCHAR argument OPTIONAL,
  909. IN CHAR RecurseDepth,
  910. OUT BOOLEAN *NoFindName OPTIONAL
  911. )
  912. /*++
  913. Routine Description:
  914. LmInclude() is called to process a #INCLUDE directive in the lmhosts
  915. file.
  916. Arguments:
  917. file - the file to include
  918. function - function to parse the included file
  919. argument - optional second argument to the parse function
  920. RecurseDepth- the depth to which we can resurse -- 0 => no more recursion
  921. NoFindName - Are find names allowed for this address
  922. Return Value:
  923. The return value from the parse function. This should be -1 if the
  924. file could not be processed, or else some positive number.
  925. --*/
  926. {
  927. int retval;
  928. PUCHAR end;
  929. NTSTATUS status;
  930. PUCHAR path;
  931. CTEPagedCode();
  932. //
  933. // unlike C, treat both variations of the #INCLUDE directive identically:
  934. //
  935. // #INCLUDE file
  936. // #INCLUDE "file"
  937. //
  938. // If a leading '"' exists, skip over it.
  939. //
  940. if (*file == '"')
  941. {
  942. file++;
  943. end = strchr(file, '"');
  944. if (end)
  945. {
  946. *end = (UCHAR) NULL;
  947. }
  948. }
  949. //
  950. // check that the file to be included has been preloaded in the cache
  951. // since we do not want to have the name query come right back to here
  952. // to force another inclusion of the same remote file
  953. //
  954. #ifdef VXD
  955. return (*function)(file, argument, RecurseDepth, NoFindName ) ;
  956. #else
  957. status = LmGetFullPath(file, &path);
  958. if (status != STATUS_SUCCESS)
  959. {
  960. return(status);
  961. }
  962. // IF_DBG(NBT_DEBUG_LMHOST)
  963. KdPrint(("Nbt.LmInclude: #INCLUDE \"%s\"\n", path));
  964. retval = (*function) (path, argument, RecurseDepth, NoFindName);
  965. CTEMemFree(path);
  966. return(retval);
  967. #endif
  968. } // LmInclude
  969. #ifndef VXD // Not used by VXD
  970. //----------------------------------------------------------------------------
  971. NTSTATUS
  972. LmGetFullPath (
  973. IN PUCHAR target,
  974. OUT PUCHAR *ppath
  975. )
  976. /*++
  977. Routine Description:
  978. This function returns the full path of the lmhosts file. This is done
  979. by forming a string from the concatenation of the C strings
  980. DatabasePath and the string, file.
  981. Arguments:
  982. target - the name of the file. This can either be a full path name
  983. or a mere file name.
  984. path - a pointer to a UCHAR
  985. Return Value:
  986. STATUS_SUCCESS if successful.
  987. Notes:
  988. RtlMoveMemory() handles overlapped copies; RtlCopyMemory() doesn't.
  989. --*/
  990. {
  991. ULONG FileNameType;
  992. ULONG Len;
  993. PUCHAR path;
  994. CTEPagedCode();
  995. //
  996. // use a count to figure out what sort of string to build up
  997. //
  998. // 0 - local full path file name
  999. // 1 - local file name only, no path
  1000. // 2 - remote file name
  1001. // 3 - \SystemRoot\ starting file name, or \DosDevices\UNC\...
  1002. //
  1003. // if the target begins with a '\', or contains a DOS drive letter,
  1004. // then assume that it specifies a full path. Otherwise, prepend the
  1005. // directory used to specify the lmhost file itself.
  1006. //
  1007. //
  1008. if (target[1] == ':')
  1009. {
  1010. FileNameType = 0;
  1011. }
  1012. else
  1013. if (strncmp(&target[1],"SystemRoot",10) == 0)
  1014. {
  1015. FileNameType = 3;
  1016. }
  1017. else
  1018. if (strncmp(&target[0],"\\DosDevices\\",12) == 0)
  1019. {
  1020. FileNameType = 3;
  1021. }
  1022. else
  1023. if (strncmp(target,"\\DosDevices\\UNC\\",sizeof("\\DosDevices\\UNC\\")-1) == 0)
  1024. {
  1025. FileNameType = 3;
  1026. }
  1027. else
  1028. {
  1029. FileNameType = 1;
  1030. }
  1031. //
  1032. // does the directory specify a remote file ?
  1033. //
  1034. // If so, it must be prefixed with "\\DosDevices\\UNC", and the double
  1035. // slashes of the UNC name eliminated.
  1036. //
  1037. //
  1038. if ((target[1] == '\\') && (target[0] == '\\'))
  1039. {
  1040. FileNameType = 2;
  1041. }
  1042. path = NULL;
  1043. switch (FileNameType)
  1044. {
  1045. case 0:
  1046. //
  1047. // Full file name, put \DosDevices on front of name
  1048. //
  1049. Len = sizeof("\\DosDevices\\") + strlen(target);
  1050. path = NbtAllocMem (Len, NBT_TAG2('11'));
  1051. if (path)
  1052. {
  1053. ULONG Length=sizeof("\\DosDevices\\"); // Took out -1
  1054. strncpy(path,"\\DosDevices\\",Length);
  1055. Nbtstrcat(path,target,Len);
  1056. }
  1057. break;
  1058. case 1:
  1059. //
  1060. // only the file name is present, with no path, so use the path
  1061. // specified for the lmhost file in the registry NbtConfig.PathLength
  1062. // includes the last backslash of the path.
  1063. //
  1064. //Len = sizeof("\\DosDevices\\") + NbtConfig.PathLength + strlen(target);
  1065. CTEExAcquireResourceExclusive(&NbtConfig.Resource,TRUE); // # 247429
  1066. Len = NbtConfig.PathLength + strlen(target) +1;
  1067. path = NbtAllocMem (Len, NBT_TAG2('12'));
  1068. if (path)
  1069. {
  1070. //ULONG Length=sizeof("\\DosDevices") -1; // -1 not to count null
  1071. //strncpy(path,"\\DosDevices",Length);
  1072. strncpy(path,NbtConfig.pLmHosts,NbtConfig.PathLength);
  1073. path[NbtConfig.PathLength] = '\0';
  1074. Nbtstrcat(path,target,Len);
  1075. }
  1076. CTEExReleaseResource(&NbtConfig.Resource);
  1077. break;
  1078. case 2:
  1079. //
  1080. // Full file name, put \DosDevices\UNC on front of name and delete
  1081. // one of the two back slashes used for the remote name
  1082. //
  1083. Len = strlen(target);
  1084. path = NbtAllocMem (Len+sizeof("\\DosDevices\\UNC"), NBT_TAG2('13'));
  1085. if (path)
  1086. {
  1087. ULONG Length = sizeof("\\DosDevices\\UNC");
  1088. strncpy(path,"\\DosDevices\\UNC",Length);
  1089. // to delete the first \ from the two \\ on the front of the
  1090. // remote file name add one to target.
  1091. //
  1092. Nbtstrcat(path,target+1,Len+sizeof("\\DosDevices\\UNC"));
  1093. }
  1094. break;
  1095. case 3:
  1096. // the target is the full path
  1097. Len = strlen(target) + 1;
  1098. path = NbtAllocMem (Len, NBT_TAG2('14'));
  1099. if (path)
  1100. {
  1101. strncpy(path,target,Len);
  1102. }
  1103. break;
  1104. }
  1105. if (path)
  1106. {
  1107. *ppath = path;
  1108. return(STATUS_SUCCESS);
  1109. }
  1110. else
  1111. return(STATUS_UNSUCCESSFUL);
  1112. } // LmGetFullPath
  1113. //----------------------------------------------------------------------------
  1114. VOID
  1115. DelayedScanLmHostFile (
  1116. IN tDGRAM_SEND_TRACKING *pUnused1,
  1117. IN PVOID pUnused2,
  1118. IN PVOID pUnused3,
  1119. IN tDEVICECONTEXT *pDeviceContext
  1120. )
  1121. /*++
  1122. Routine Description:
  1123. This function is called by the Executive Worker thread to scan the
  1124. LmHost file looking for a name. The name to query is on a list in
  1125. the DNSQueries structure.
  1126. Arguments:
  1127. Context -
  1128. Return Value:
  1129. none
  1130. --*/
  1131. {
  1132. NTSTATUS status;
  1133. LONG IpAddress;
  1134. ULONG IpAddrsList[2];
  1135. BOOLEAN bFound;
  1136. NBT_WORK_ITEM_CONTEXT *pContext;
  1137. BOOLEAN DoingDnsResolve = FALSE;
  1138. UCHAR pName[NETBIOS_NAME_SIZE];
  1139. PUCHAR LmHostsPath;
  1140. ULONG LoopCount;
  1141. tDGRAM_SEND_TRACKING *pTracker;
  1142. tDGRAM_SEND_TRACKING *pTracker0;
  1143. CTEPagedCode();
  1144. LoopCount = 0;
  1145. while (TRUE)
  1146. {
  1147. // get the next name on the linked list of LmHost name queries that
  1148. // are pending
  1149. //
  1150. pContext = NULL;
  1151. DoingDnsResolve = FALSE;
  1152. if (!(pContext = GetNameToFind(pName)))
  1153. {
  1154. return;
  1155. }
  1156. LoopCount ++;
  1157. IF_DBG(NBT_DEBUG_LMHOST)
  1158. KdPrint(("Nbt.DelayedScanLmHostFile: Lmhosts pName = %15.15s<%X>,LoopCount=%X\n",
  1159. pName,pName[15],LoopCount));
  1160. status = STATUS_TIMEOUT;
  1161. //
  1162. // check if the name is in the lmhosts file or pass to Dns if
  1163. // DNS is enabled
  1164. //
  1165. IpAddress = 0;
  1166. if (NbtConfig.EnableLmHosts)
  1167. {
  1168. #ifdef VXD
  1169. //
  1170. // if for some reason PrimeCache failed at startup time
  1171. // then this is when we retry.
  1172. //
  1173. if (!CachePrimed)
  1174. {
  1175. if (PrimeCache (NbtConfig.pLmHosts, NULL, MAX_RECURSE_DEPTH, NULL) != -1)
  1176. {
  1177. CachePrimed = TRUE ;
  1178. }
  1179. }
  1180. #endif
  1181. //
  1182. // The NbtConfig.pLmHosts path can change if the registry is
  1183. // read during this interval
  1184. // We cannot acquire the ResourceLock here since reading the
  1185. // LmHosts file might result in File operations + network reads
  1186. // that could cause a deadlock (Worker threads / ResourceLock)!
  1187. // Best solution at this time is to copy the path onto a local
  1188. // buffer under the Resource lock, and then try to read the file!
  1189. //
  1190. CTEExAcquireResourceExclusive(&NbtConfig.Resource,TRUE);
  1191. if ((NbtConfig.pLmHosts) &&
  1192. (LmHostsPath = NbtAllocMem ((strlen(NbtConfig.pLmHosts)+1), NBT_TAG2('20'))))
  1193. {
  1194. CTEMemCopy (LmHostsPath, NbtConfig.pLmHosts, (strlen(NbtConfig.pLmHosts)+1));
  1195. CTEExReleaseResource(&NbtConfig.Resource);
  1196. IpAddress = LmGetIpAddr(LmHostsPath, pName, 1, &bFound);
  1197. CTEMemFree(LmHostsPath);
  1198. }
  1199. else
  1200. {
  1201. CTEExReleaseResource(&NbtConfig.Resource);
  1202. IpAddress = 0;
  1203. }
  1204. #ifdef VXD
  1205. //
  1206. // hmmm.. didn't find it in lmhosts: try hosts (if Dns is enabled)
  1207. //
  1208. if ((IpAddress == (ULONG)0) && (NbtConfig.ResolveWithDns))
  1209. {
  1210. IpAddress = LmGetIpAddr(NbtConfig.pHosts, pName, 1, &bFound);
  1211. }
  1212. #endif
  1213. }
  1214. if (IpAddress == (ULONG)0)
  1215. {
  1216. // check if the name query has been cancelled
  1217. //
  1218. LOCATION(0x61);
  1219. GetContext (&pContext);
  1220. //
  1221. // for some reason we didn't find our context: maybe cancelled.
  1222. // Go back to the big while loop...
  1223. //
  1224. if (!pContext)
  1225. {
  1226. continue;
  1227. }
  1228. //
  1229. // see if the name is in the 11.101.4.26 format: if so, we got the
  1230. // ipaddr! Use that ipaddr to get the server name
  1231. //
  1232. pTracker = ((NBT_WORK_ITEM_CONTEXT *)pContext)->pTracker;
  1233. pTracker0 = (tDGRAM_SEND_TRACKING *)((NBT_WORK_ITEM_CONTEXT *)pContext)->pClientContext;
  1234. if (pTracker0->Flags & (REMOTE_ADAPTER_STAT_FLAG|SESSION_SETUP_FLAG|DGRAM_SEND_FLAG))
  1235. {
  1236. IpAddress = Nbt_inet_addr(pTracker->pNameAddr->Name, pTracker0->Flags);
  1237. }
  1238. //
  1239. // yes, the name is the ipaddr: NbtCompleteLmhSvcRequest() starts
  1240. // the process of finding out server name for this ipaddr
  1241. //
  1242. if (IpAddress)
  1243. {
  1244. IpAddrsList[0] = IpAddress;
  1245. IpAddrsList[1] = 0;
  1246. //
  1247. // if this is in response to an adapter stat command (e.g.nbtstat -a) then
  1248. // don't try to find the server name (using remote adapter status!)
  1249. //
  1250. if (pTracker0->Flags & REMOTE_ADAPTER_STAT_FLAG)
  1251. {
  1252. //
  1253. // change the state to resolved if the name query is still pending
  1254. //
  1255. status = ChangeStateOfName(IpAddress, pContext, &pContext, NAME_RESOLVED_BY_IP);
  1256. }
  1257. else
  1258. {
  1259. NbtCompleteLmhSvcRequest(pContext, IpAddrsList, NBT_RESOLVE_WITH_DNS, 0, NULL, TRUE);
  1260. //
  1261. // done with this name query: go back to the big while loop
  1262. //
  1263. continue;
  1264. }
  1265. }
  1266. //
  1267. //
  1268. // inet_addr failed. If DNS resolution is enabled, try DNS
  1269. else if ((NbtConfig.ResolveWithDns) &&
  1270. (!(pTracker0->Flags & NO_DNS_RESOLUTION_FLAG)))
  1271. {
  1272. status = NbtProcessLmhSvcRequest (pContext, NBT_RESOLVE_WITH_DNS);
  1273. if (NT_SUCCESS(status))
  1274. {
  1275. DoingDnsResolve = TRUE;
  1276. }
  1277. }
  1278. }
  1279. else // if (IpAddress != (ULONG)0)
  1280. {
  1281. //
  1282. // change the state to resolved if the name query is still pending
  1283. //
  1284. status = ChangeStateOfName(IpAddress, NULL, &pContext, NAME_RESOLVED_BY_LMH);
  1285. }
  1286. //
  1287. // if DNS gets involved, then we wait for that to complete before calling
  1288. // completion routine.
  1289. //
  1290. if (!DoingDnsResolve)
  1291. {
  1292. LOCATION(0x60);
  1293. RemoveNameAndCompleteReq((NBT_WORK_ITEM_CONTEXT *)pContext, status);
  1294. }
  1295. }// of while(TRUE)
  1296. }
  1297. //----------------------------------------------------------------------------
  1298. ULONG
  1299. AddToDomainList (
  1300. IN PUCHAR pName,
  1301. IN tIPADDRESS IpAddress,
  1302. IN PLIST_ENTRY pDomainHead,
  1303. IN BOOLEAN fPreload
  1304. )
  1305. /*++
  1306. Routine Description:
  1307. This function adds a name and ip address to the list of domains that
  1308. are stored in a list.
  1309. Arguments:
  1310. Return Value:
  1311. --*/
  1312. {
  1313. PLIST_ENTRY pHead;
  1314. PLIST_ENTRY pEntry;
  1315. tNAMEADDR *pNameAddr=NULL;
  1316. tIPADDRESS *pIpAddr;
  1317. CTEPagedCode();
  1318. pHead = pEntry = pDomainHead;
  1319. if (!IsListEmpty(pDomainHead))
  1320. {
  1321. pNameAddr = FindInDomainList(pName,pDomainHead);
  1322. if (pNameAddr)
  1323. {
  1324. //
  1325. // the name matches, so add to the end of the ip address list
  1326. //
  1327. if (pNameAddr->CurrentLength < pNameAddr->MaxDomainAddrLength)
  1328. {
  1329. pIpAddr = pNameAddr->pLmhSvcGroupList;
  1330. while (*pIpAddr != (ULONG)-1) {
  1331. pIpAddr++;
  1332. }
  1333. *pIpAddr++ = IpAddress;
  1334. *pIpAddr = (ULONG)-1;
  1335. pNameAddr->CurrentLength += sizeof(ULONG);
  1336. }
  1337. else
  1338. {
  1339. //
  1340. // need to allocate more memory for for ip addresses
  1341. //
  1342. if (pIpAddr = NbtAllocMem (pNameAddr->MaxDomainAddrLength+INITIAL_DOM_SIZE, NBT_TAG2('08')))
  1343. {
  1344. CTEMemCopy(pIpAddr, pNameAddr->pLmhSvcGroupList, pNameAddr->MaxDomainAddrLength);
  1345. //
  1346. // Free the old chunk of memory and tack the new one on
  1347. // to the pNameaddr
  1348. //
  1349. CTEMemFree(pNameAddr->pLmhSvcGroupList);
  1350. pNameAddr->pLmhSvcGroupList = pIpAddr;
  1351. pIpAddr = (PULONG)((PUCHAR)pIpAddr + pNameAddr->MaxDomainAddrLength);
  1352. //
  1353. // our last entry was -1: overwrite that one
  1354. //
  1355. pIpAddr--;
  1356. *pIpAddr++ = IpAddress;
  1357. *pIpAddr = (ULONG)-1;
  1358. //
  1359. // update the number of addresses in the list so far
  1360. //
  1361. pNameAddr->MaxDomainAddrLength += INITIAL_DOM_SIZE;
  1362. pNameAddr->CurrentLength += sizeof(ULONG);
  1363. pNameAddr->Verify = REMOTE_NAME;
  1364. }
  1365. }
  1366. }
  1367. }
  1368. //
  1369. // check if we found the name or we need to add a new name
  1370. //
  1371. if (!pNameAddr)
  1372. {
  1373. //
  1374. // create a new name for the domain list
  1375. //
  1376. if (pNameAddr = NbtAllocMem (sizeof(tNAMEADDR), NBT_TAG2('09')))
  1377. {
  1378. CTEZeroMemory(pNameAddr,sizeof(tNAMEADDR));
  1379. pIpAddr = NbtAllocMem (INITIAL_DOM_SIZE, NBT_TAG2('10'));
  1380. if (pIpAddr)
  1381. {
  1382. CTEMemCopy(pNameAddr->Name,pName,NETBIOS_NAME_SIZE);
  1383. pNameAddr->pLmhSvcGroupList = pIpAddr;
  1384. *pIpAddr++ = IpAddress;
  1385. *pIpAddr = (ULONG)-1;
  1386. pNameAddr->NameTypeState = NAMETYPE_INET_GROUP;
  1387. pNameAddr->MaxDomainAddrLength = INITIAL_DOM_SIZE;
  1388. pNameAddr->CurrentLength = 2*sizeof(ULONG);
  1389. pNameAddr->Verify = REMOTE_NAME;
  1390. NBT_REFERENCE_NAMEADDR (pNameAddr, REF_NAME_REMOTE);
  1391. InsertHeadList(pDomainHead,&pNameAddr->Linkage);
  1392. }
  1393. else
  1394. {
  1395. CTEMemFree(pNameAddr);
  1396. pNameAddr = NULL;
  1397. }
  1398. }
  1399. }
  1400. if (pNameAddr && fPreload)
  1401. {
  1402. pNameAddr->fPreload = TRUE;
  1403. }
  1404. return(STATUS_SUCCESS);
  1405. }
  1406. //----------------------------------------------------------------------------
  1407. tNAMEADDR *
  1408. FindInDomainList (
  1409. IN PUCHAR pName,
  1410. IN PLIST_ENTRY pDomainHead
  1411. )
  1412. /*++
  1413. Routine Description:
  1414. This function finds a name in the domain list passed in.
  1415. Arguments:
  1416. name to find
  1417. head of list to look on
  1418. Return Value:
  1419. ptr to pNameaddr
  1420. --*/
  1421. {
  1422. PLIST_ENTRY pHead;
  1423. PLIST_ENTRY pEntry;
  1424. tNAMEADDR *pNameAddr;
  1425. pHead = pEntry = pDomainHead;
  1426. while ((pEntry = pEntry->Flink) != pHead)
  1427. {
  1428. pNameAddr = CONTAINING_RECORD(pEntry,tNAMEADDR,Linkage);
  1429. if (strncmp(pNameAddr->Name,pName,NETBIOS_NAME_SIZE) == 0)
  1430. {
  1431. return(pNameAddr);
  1432. }
  1433. }
  1434. return(NULL);
  1435. }
  1436. //----------------------------------------------------------------------------
  1437. VOID
  1438. MakeNewListCurrent (
  1439. PLIST_ENTRY pTmpDomainList
  1440. )
  1441. /*++
  1442. Routine Description:
  1443. This function frees the old entries on the DomainList and hooks up the
  1444. new entries
  1445. Arguments:
  1446. pTmpDomainList - list entry to the head of a new domain list
  1447. Return Value:
  1448. --*/
  1449. {
  1450. CTELockHandle OldIrq;
  1451. tNAMEADDR *pNameAddr;
  1452. PLIST_ENTRY pEntry;
  1453. PLIST_ENTRY pHead;
  1454. NTSTATUS status;
  1455. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1456. if (!IsListEmpty(pTmpDomainList))
  1457. {
  1458. //
  1459. // free the old list elements
  1460. //
  1461. pHead = &DomainNames.DomainList;
  1462. pEntry = pHead->Flink;
  1463. while (pEntry != pHead)
  1464. {
  1465. pNameAddr = CONTAINING_RECORD(pEntry,tNAMEADDR,Linkage);
  1466. pEntry = pEntry->Flink;
  1467. RemoveEntryList(&pNameAddr->Linkage);
  1468. //
  1469. // initialize linkage so that if the nameaddr is being
  1470. // referenced now, when it does get freed in a subsequent
  1471. // call to NBT_DEREFERENCE_NAMEADDR it will not
  1472. // remove it from any lists
  1473. //
  1474. InitializeListHead(&pNameAddr->Linkage);
  1475. //
  1476. // Since the name could be in use now we must dereference rather
  1477. // than just free it outright
  1478. //
  1479. NBT_DEREFERENCE_NAMEADDR (pNameAddr, REF_NAME_REMOTE, TRUE);
  1480. }
  1481. //
  1482. // See if any of the new names has to be preloaded!
  1483. //
  1484. pEntry = pTmpDomainList->Flink;
  1485. while (pEntry != pTmpDomainList)
  1486. {
  1487. pNameAddr = CONTAINING_RECORD(pEntry,tNAMEADDR,Linkage);
  1488. pEntry = pEntry->Flink;
  1489. if (pNameAddr->fPreload)
  1490. {
  1491. RemoveEntryList(&pNameAddr->Linkage);
  1492. InitializeListHead(&pNameAddr->Linkage);
  1493. status = AddToHashTable (NbtConfig.pRemoteHashTbl,
  1494. pNameAddr->Name,
  1495. NbtConfig.pScope,
  1496. 0,
  1497. 0,
  1498. pNameAddr,
  1499. &pNameAddr,
  1500. NULL,
  1501. NAME_RESOLVED_BY_LMH_P | NAME_ADD_INET_GROUP);
  1502. if ((status == STATUS_SUCCESS) ||
  1503. ((status == STATUS_PENDING) &&
  1504. (!(pNameAddr->NameTypeState & PRELOADED))))
  1505. {
  1506. //
  1507. // this prevents the name from being deleted by the Hash Timeout code
  1508. //
  1509. NBT_REFERENCE_NAMEADDR (pNameAddr, REF_NAME_PRELOADED);
  1510. pNameAddr->Ttl = 0xFFFFFFFF;
  1511. pNameAddr->NameTypeState |= PRELOADED | STATE_RESOLVED;
  1512. pNameAddr->NameTypeState &= ~STATE_CONFLICT;
  1513. pNameAddr->AdapterMask = (CTEULONGLONG)-1;
  1514. }
  1515. }
  1516. }
  1517. DomainNames.DomainList.Flink = pTmpDomainList->Flink;
  1518. DomainNames.DomainList.Blink = pTmpDomainList->Blink;
  1519. pTmpDomainList->Flink->Blink = &DomainNames.DomainList;
  1520. pTmpDomainList->Blink->Flink = &DomainNames.DomainList;
  1521. }
  1522. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1523. }
  1524. //----------------------------------------------------------------------------
  1525. NTSTATUS
  1526. NtProcessLmHSvcIrp(
  1527. IN tDEVICECONTEXT *pDeviceContext,
  1528. IN PVOID *pBuffer,
  1529. IN LONG Size,
  1530. IN PCTE_IRP pIrp,
  1531. IN enum eNbtLmhRequestType RequestType
  1532. )
  1533. /*++
  1534. Routine Description:
  1535. This function is used by LmHsvc Dll to collect requests for
  1536. Pinging IP addresses or querying through DNS.
  1537. The request is sent to LmhSvc in the buffer associated with
  1538. this request.
  1539. Arguments:
  1540. Return Value:
  1541. STATUS_PENDING if the buffer is to be held on to, the normal case.
  1542. Notes:
  1543. --*/
  1544. {
  1545. NTSTATUS status;
  1546. NTSTATUS Locstatus;
  1547. CTELockHandle OldIrq;
  1548. tIPADDR_BUFFER_DNS *pIpAddrBuf;
  1549. PVOID pClientCompletion;
  1550. PVOID pClientContext;
  1551. tDGRAM_SEND_TRACKING *pTracker;
  1552. ULONG IpAddrsList[MAX_IPADDRS_PER_HOST+1];
  1553. NBT_WORK_ITEM_CONTEXT *pContext;
  1554. BOOLEAN CompletingAnotherQuery = FALSE;
  1555. tLMHSVC_REQUESTS *pLmhRequest;
  1556. tDEVICECONTEXT *pDeviceContextRequest;
  1557. pIpAddrBuf = (tIPADDR_BUFFER_DNS *)pBuffer;
  1558. switch (RequestType)
  1559. {
  1560. case NBT_PING_IP_ADDRS:
  1561. {
  1562. pLmhRequest = &CheckAddr;
  1563. break;
  1564. }
  1565. case NBT_RESOLVE_WITH_DNS:
  1566. {
  1567. pLmhRequest = &DnsQueries;
  1568. break;
  1569. }
  1570. default:
  1571. {
  1572. ASSERTMSG ("Nbt.NtProcessLmHSvcIrp: ERROR - Invalid Request from LmhSvc Dll\n", 0);
  1573. return STATUS_UNSUCCESSFUL;
  1574. }
  1575. }
  1576. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1577. //
  1578. // If we already have an Irp posted, return this Irp -- Bug # 311924
  1579. //
  1580. if ((pLmhRequest->QueryIrp) &&
  1581. (!pLmhRequest->ResolvingNow))
  1582. {
  1583. CTESpinFree (&NbtConfig.JointLock,OldIrq);
  1584. KdPrint (("Nbt.NtProcessLmHSvcIrp: ERROR -- duplicate request Irp!\n"));
  1585. NTIoComplete (pIrp, STATUS_OBJECT_PATH_INVALID, 0);
  1586. NbtTrace(NBT_TRACE_NAMESRV, ("duplicate Lmhosts request"));
  1587. return STATUS_OBJECT_PATH_INVALID;
  1588. }
  1589. IoMarkIrpPending(pIrp);
  1590. pLmhRequest->QueryIrp = pIrp;
  1591. status = STATUS_PENDING;
  1592. if (pLmhRequest->ResolvingNow)
  1593. {
  1594. //
  1595. // if the client got tired of waiting for DNS, the NbtCancelWaitForLmhSvcIrp
  1596. // in ntisol.c will have cleared the pContext value when cancelling the
  1597. // irp, so check for that here.
  1598. //
  1599. if (pLmhRequest->Context)
  1600. {
  1601. pContext = (NBT_WORK_ITEM_CONTEXT *) pLmhRequest->Context;
  1602. pLmhRequest->Context = NULL;
  1603. pDeviceContextRequest = pContext->pDeviceContext;
  1604. if (NBT_REFERENCE_DEVICE (pDeviceContextRequest, REF_DEV_LMH, TRUE))
  1605. {
  1606. NbtCancelCancelRoutine (((tDGRAM_SEND_TRACKING *) (pContext->pClientContext))->pClientIrp);
  1607. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1608. ASSERT(sizeof(pIpAddrBuf->pwName) == DNS_NAME_BUFFER_LENGTH * sizeof(pIpAddrBuf->pwName[0]));
  1609. pIpAddrBuf->pwName[DNS_NAME_BUFFER_LENGTH-1] = 0;
  1610. NbtCompleteLmhSvcRequest (pContext,
  1611. pIpAddrBuf->IpAddrsList,
  1612. RequestType,
  1613. pIpAddrBuf->NameLen,
  1614. pIpAddrBuf->pwName,
  1615. (BOOLEAN)pIpAddrBuf->Resolved);
  1616. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1617. NBT_DEREFERENCE_DEVICE (pDeviceContextRequest, REF_DEV_LMH, TRUE);
  1618. }
  1619. else
  1620. {
  1621. ASSERT (0);
  1622. }
  1623. }
  1624. else
  1625. {
  1626. IF_DBG(NBT_DEBUG_NAMESRV)
  1627. KdPrint(("Nbt.NtProcessLmHSvcIrp[%s]: No Context!! *******\r\n",
  1628. (RequestType == NBT_RESOLVE_WITH_DNS ? "NBT_RESOLVE_WITH_DNS" : "NBT_PING_IP_ADDRS")));
  1629. }
  1630. pLmhRequest->ResolvingNow = FALSE;
  1631. //
  1632. // are there any more name query requests to process?
  1633. //
  1634. while (!IsListEmpty(&pLmhRequest->ToResolve))
  1635. {
  1636. PLIST_ENTRY pEntry;
  1637. pEntry = RemoveHeadList(&pLmhRequest->ToResolve);
  1638. pContext = CONTAINING_RECORD(pEntry,NBT_WORK_ITEM_CONTEXT,Linkage);
  1639. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1640. Locstatus = NbtProcessLmhSvcRequest (pContext, RequestType);
  1641. if (NT_SUCCESS(Locstatus))
  1642. {
  1643. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1644. CompletingAnotherQuery = TRUE;
  1645. break;
  1646. }
  1647. //
  1648. // if it failed then complete the irp now
  1649. //
  1650. IF_DBG(NBT_DEBUG_NAMESRV)
  1651. KdPrint(("Nbt.NtProcessLmHSvcIrp[%s]: NbtProcessLmhSvcRequest failed with %x\r\n",
  1652. (RequestType==NBT_RESOLVE_WITH_DNS ? "NBT_RESOLVE_WITH_DNS":"NBT_PING_IP_ADDRS"),
  1653. Locstatus));
  1654. pClientCompletion = pContext->ClientCompletion;
  1655. pClientContext = pContext->pClientContext;
  1656. pTracker = pContext->pTracker;
  1657. //
  1658. // Clear the Cancel Routine now
  1659. //
  1660. (VOID)NbtCancelCancelRoutine(((tDGRAM_SEND_TRACKING *)pClientContext)->pClientIrp);
  1661. if (pTracker)
  1662. {
  1663. if (pTracker->pNameAddr)
  1664. {
  1665. SetNameState (pTracker->pNameAddr, NULL, FALSE);
  1666. pTracker->pNameAddr = NULL;
  1667. }
  1668. //
  1669. // pTracker is NULL for Ping requests, hence this dereference is
  1670. // done only for Dns requests
  1671. //
  1672. NBT_DEREFERENCE_TRACKER(pTracker, FALSE);
  1673. }
  1674. CompleteClientReq(pClientCompletion, pClientContext, STATUS_BAD_NETWORK_PATH);
  1675. CTEMemFree(pContext);
  1676. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1677. }
  1678. }
  1679. //
  1680. // We are holding onto the Irp, so set the cancel routine.
  1681. // (Since we may have released the lock earlier, we also need
  1682. // to ensure that no other Query has completed the Irp!)
  1683. //
  1684. if ((!CompletingAnotherQuery) &&
  1685. (!pLmhRequest->ResolvingNow) &&
  1686. (pLmhRequest->QueryIrp == pIrp))
  1687. {
  1688. status = NTCheckSetCancelRoutine(pIrp, NbtCancelLmhSvcIrp, pDeviceContext);
  1689. if (!NT_SUCCESS(status))
  1690. {
  1691. // the irp got cancelled so complete it now
  1692. //
  1693. pLmhRequest->QueryIrp = NULL;
  1694. pLmhRequest->pIpAddrBuf = NULL;
  1695. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1696. NTIoComplete(pIrp,status,0);
  1697. }
  1698. else
  1699. {
  1700. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1701. status = STATUS_PENDING;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1707. }
  1708. return(status);
  1709. }
  1710. //----------------------------------------------------------------------------
  1711. NTSTATUS
  1712. NbtProcessLmhSvcRequest(
  1713. IN NBT_WORK_ITEM_CONTEXT *pContext,
  1714. IN enum eNbtLmhRequestType RequestType
  1715. )
  1716. /*++
  1717. Routine Description:
  1718. This function is called to pass a NBT request to ping IP addrs
  1719. or query DNS to the LmhSvc Dll
  1720. Arguments:
  1721. Return Value:
  1722. STATUS_PENDING if the buffer is to be held on to , the normal case.
  1723. Notes:
  1724. --*/
  1725. {
  1726. NTSTATUS status = STATUS_SUCCESS;
  1727. tIPADDR_BUFFER_DNS *pIpAddrBuf;
  1728. PCTE_IRP pIrp;
  1729. tDGRAM_SEND_TRACKING *pTracker;
  1730. tDGRAM_SEND_TRACKING *pClientTracker;
  1731. CTELockHandle OldIrq;
  1732. PCHAR pDestName;
  1733. ULONG NameLen, NumAddrs;
  1734. tLMHSVC_REQUESTS *pLmhRequest;
  1735. switch (RequestType)
  1736. {
  1737. case NBT_PING_IP_ADDRS:
  1738. {
  1739. pLmhRequest = &CheckAddr;
  1740. break;
  1741. }
  1742. case NBT_RESOLVE_WITH_DNS:
  1743. {
  1744. pLmhRequest = &DnsQueries;
  1745. break;
  1746. }
  1747. default:
  1748. {
  1749. ASSERTMSG ("Nbt.NbtProcessLmHSvcRequest: ERROR - Invalid Request type\n", 0);
  1750. return STATUS_UNSUCCESSFUL;
  1751. }
  1752. }
  1753. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1754. pContext->TimedOut = FALSE;
  1755. if ((!NBT_VERIFY_HANDLE (pContext->pDeviceContext, NBT_VERIFY_DEVCONTEXT)) ||
  1756. (!pLmhRequest->QueryIrp))
  1757. {
  1758. //
  1759. // Either the device is going away, or
  1760. // the irp either never made it down here, or it was cancelled,
  1761. // so pretend the name query timed out.
  1762. //
  1763. IF_DBG(NBT_DEBUG_NAMESRV)
  1764. KdPrint(("Nbt.NbtProcessLmhSvcRequest[%s]: QueryIrp is NULL, returning\r\n",
  1765. (RequestType == NBT_RESOLVE_WITH_DNS ? "NBT_RESOLVE_WITH_DNS" : "NBT_PING_IP_ADDRS")));
  1766. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1767. if (pLmhRequest->QueryIrp) {
  1768. NbtTrace(NBT_TRACE_NAMESRV, ("return STATUS_BAD_NETWORK_PATH because the device is going away"));
  1769. } else {
  1770. NbtTrace(NBT_TRACE_NAMESRV, ("LmHost services didn't start"));
  1771. }
  1772. return(STATUS_BAD_NETWORK_PATH);
  1773. }
  1774. else if (!pLmhRequest->ResolvingNow)
  1775. {
  1776. pIrp = pLmhRequest->QueryIrp;
  1777. if ((!pLmhRequest->pIpAddrBuf) &&
  1778. (!(pLmhRequest->pIpAddrBuf = (tIPADDR_BUFFER_DNS *)
  1779. MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority))))
  1780. {
  1781. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1782. NbtTrace(NBT_TRACE_NAMESRV, ("returns STATUS_UNSUCCESSFUL"));
  1783. return(STATUS_UNSUCCESSFUL);
  1784. }
  1785. pIpAddrBuf = pLmhRequest->pIpAddrBuf;
  1786. pLmhRequest->ResolvingNow = TRUE;
  1787. pLmhRequest->Context = pContext;
  1788. pTracker = pContext->pTracker; // this is the name query tracker (for Dns queries only)
  1789. pClientTracker = (tDGRAM_SEND_TRACKING *)pContext->pClientContext; // session setup tracker
  1790. switch (RequestType)
  1791. {
  1792. case NBT_PING_IP_ADDRS:
  1793. {
  1794. ASSERT(pTracker == NULL);
  1795. //
  1796. // copy the IP addrs for lmhsvc to ping (upto MAX_IPADDRS_PER_HOST) ...
  1797. //
  1798. NumAddrs = pClientTracker->NumAddrs > MAX_IPADDRS_PER_HOST ?
  1799. MAX_IPADDRS_PER_HOST : pClientTracker->NumAddrs;
  1800. CTEMemCopy(pIpAddrBuf->IpAddrsList, pClientTracker->IpList, NumAddrs * sizeof(ULONG));
  1801. pIpAddrBuf->IpAddrsList[NumAddrs] = 0;
  1802. break;
  1803. }
  1804. case NBT_RESOLVE_WITH_DNS:
  1805. {
  1806. WCHAR *UnicodeDestName;
  1807. UnicodeDestName = pClientTracker? pClientTracker->UnicodeDestName: NULL;
  1808. //
  1809. // whenever dest. name is 16 bytes long (or smaller), we have no
  1810. // way of knowing if its a netbios name or a dns name, so we presume
  1811. // it's netbios name, go to wins, broadcast etc. and then come to dns
  1812. // In this case, the name query tracker will be setup, so be non-null
  1813. //
  1814. if (pTracker)
  1815. {
  1816. pDestName = pTracker->pNameAddr->Name;
  1817. NameLen = NETBIOS_NAME_SIZE;
  1818. }
  1819. //
  1820. // if the dest name is longer than 16 bytes, it's got to be dns name so
  1821. // we bypass wins etc. and come straight to dns. In this case, we didn't
  1822. // set up a name query tracker so it will be null. Use the session setup
  1823. // tracker (i.e. pClientTracker) to get the dest name
  1824. //
  1825. else
  1826. {
  1827. ASSERT(pClientTracker);
  1828. pDestName = pClientTracker->pDestName;
  1829. NameLen = pClientTracker->RemoteNameLength;
  1830. }
  1831. if ((NameLen == NETBIOS_NAME_SIZE) &&
  1832. (!(IsValidDnsNameTag (pDestName[NETBIOS_NAME_SIZE-1]))))
  1833. {
  1834. NbtTrace(NBT_TRACE_NAMESRV, ("returns STATUS_BAD_NETWORK_PATH %02x",
  1835. (unsigned)pDestName[NETBIOS_NAME_SIZE-1]));
  1836. status = STATUS_BAD_NETWORK_PATH;
  1837. }
  1838. else
  1839. {
  1840. //
  1841. // Ignore the 16th byte only if it is a non-DNS name character (we should be
  1842. // safe below 0x20). This will allow queries to DNS names which are exactly 16
  1843. // characters long.
  1844. //
  1845. if (NameLen == NETBIOS_NAME_SIZE)
  1846. {
  1847. if ((pDestName[NETBIOS_NAME_SIZE-1] <= 0x20 ) ||
  1848. (pDestName[NETBIOS_NAME_SIZE-1] >= 0x7f ))
  1849. {
  1850. NameLen = NETBIOS_NAME_SIZE-1; // ignore 16th byte
  1851. }
  1852. }
  1853. else if (NameLen > DNS_MAX_NAME_LENGTH)
  1854. {
  1855. NameLen = DNS_MAX_NAME_LENGTH;
  1856. }
  1857. //
  1858. // copy the name to the Irps return buffer for lmhsvc to resolve with
  1859. // a gethostbyname call
  1860. //
  1861. if (UnicodeDestName) {
  1862. int len;
  1863. len = pClientTracker->UnicodeRemoteNameLength;
  1864. if (len > sizeof(pIpAddrBuf->pwName - sizeof(WCHAR))) {
  1865. len = sizeof(pIpAddrBuf->pwName) - sizeof(WCHAR);
  1866. }
  1867. ASSERT((len % sizeof(WCHAR)) == 0);
  1868. CTEMemCopy(pIpAddrBuf->pwName, UnicodeDestName, len);
  1869. pIpAddrBuf->pwName[len/sizeof(WCHAR)] = 0;
  1870. pIpAddrBuf->NameLen = len;
  1871. pIpAddrBuf->bUnicode = TRUE;
  1872. } else {
  1873. //
  1874. // I would like to maintain only UNICODE interface between NetBT and LmhSVC.
  1875. // But I cannot do RtlAnsiStringToUnicodeString here due to IRQ level here.
  1876. //
  1877. pIpAddrBuf->bUnicode = FALSE;
  1878. CTEMemCopy(pIpAddrBuf->pName, pDestName, NameLen);
  1879. pIpAddrBuf->pName[NameLen] = 0;
  1880. pIpAddrBuf->NameLen = NameLen;
  1881. }
  1882. }
  1883. break;
  1884. }
  1885. default:
  1886. {
  1887. //
  1888. // This code path should never be hit!
  1889. //
  1890. ASSERT(0);
  1891. }
  1892. } // switch
  1893. //
  1894. // Since datagrams are buffered there is no client irp to get cancelled
  1895. // since the client's irp is returned immediately -so this check
  1896. // is only for connections being setup or QueryFindname or
  1897. // nodestatus, where we allow the irp to
  1898. // be cancelled.
  1899. //
  1900. if ((NT_SUCCESS(status)) &&
  1901. (pClientTracker->pClientIrp))
  1902. {
  1903. //
  1904. // allow the client to cancel the name query Irp - no need to check
  1905. // if the client irp was already cancelled or not since the DNS query
  1906. // will complete and find no client request and stop.
  1907. //
  1908. status = NTCheckSetCancelRoutine(pClientTracker->pClientIrp, NbtCancelWaitForLmhSvcIrp,NULL);
  1909. }
  1910. //
  1911. // pass the irp up to lmhsvc.dll to do a gethostbyname call to
  1912. // sockets
  1913. // The Irp will return to NtDnsNameResolve, above
  1914. //
  1915. if (NT_SUCCESS(status))
  1916. {
  1917. pLmhRequest->pIpAddrBuf = NULL;
  1918. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1919. NTIoComplete(pLmhRequest->QueryIrp,STATUS_SUCCESS,0);
  1920. return (STATUS_PENDING);
  1921. }
  1922. //
  1923. // We failed to set the cancel routine, so undo setting up the
  1924. // the pLmhRequest structure.
  1925. //
  1926. NbtTrace(NBT_TRACE_NAMESRV, ("returns %!status!", status));
  1927. IF_DBG(NBT_DEBUG_NAMESRV)
  1928. KdPrint(("Nbt.NbtProcessLmhSvcRequest[%s]: CheckSet (submitting) failed with %x\r\n",
  1929. (RequestType == NBT_RESOLVE_WITH_DNS ? "NBT_RESOLVE_WITH_DNS" : "NBT_PING_IP_ADDRS"),status));
  1930. pLmhRequest->ResolvingNow = FALSE;
  1931. pLmhRequest->Context = NULL;
  1932. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1933. }
  1934. else
  1935. {
  1936. pClientTracker = (tDGRAM_SEND_TRACKING *)pContext->pClientContext;
  1937. //
  1938. // Since datagrams are buffered there is no client irp to get cancelled
  1939. // since the client's irp is returned immediately -so this check
  1940. // is only for connections being setup, where we allow the irp to
  1941. // be cancelled.
  1942. //
  1943. //
  1944. // allow the client to cancel the name query Irp
  1945. //
  1946. if (pClientTracker->pClientIrp) // check if this is the session setup tracker
  1947. {
  1948. status = NTCheckSetCancelRoutine(pClientTracker->pClientIrp, NbtCancelWaitForLmhSvcIrp,NULL);
  1949. }
  1950. if (NT_SUCCESS(status))
  1951. {
  1952. // the irp is busy resolving another name, so wait for it to return
  1953. // down here again, mean while, Queue the name query
  1954. //
  1955. InsertTailList(&pLmhRequest->ToResolve, &pContext->Linkage);
  1956. }
  1957. else
  1958. {
  1959. IF_DBG(NBT_DEBUG_NAMESRV)
  1960. KdPrint(("Nbt.NbtProcessLmhSvcRequest[%s]: CheckSet (queuing) failed with %x\r\n",
  1961. (RequestType == NBT_RESOLVE_WITH_DNS ? "NBT_RESOLVE_WITH_DNS" : "NBT_PING_IP_ADDRS"),status));
  1962. NbtTrace(NBT_TRACE_NAMESRV, ("returns %!status!", status));
  1963. }
  1964. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1965. }
  1966. if (NT_SUCCESS(status))
  1967. {
  1968. status = STATUS_PENDING;
  1969. }
  1970. return(status);
  1971. }
  1972. //----------------------------------------------------------------------------
  1973. extern
  1974. VOID
  1975. SetNameState(
  1976. IN tNAMEADDR *pNameAddr,
  1977. IN PULONG pIpList,
  1978. IN BOOLEAN IpAddrResolved
  1979. )
  1980. /*++
  1981. Routine Description:
  1982. This function dereferences the pNameAddr and sets the state to Released
  1983. just incase the dereference does not delete the entry right away, due to
  1984. another outstanding reference against the name.
  1985. Arguments:
  1986. Context -
  1987. Return Value:
  1988. none
  1989. --*/
  1990. {
  1991. CTELockHandle OldIrq;
  1992. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1993. if (IpAddrResolved)
  1994. {
  1995. pNameAddr->IpAddress = pIpList[0];
  1996. }
  1997. else
  1998. {
  1999. pNameAddr->NameTypeState &= ~NAME_STATE_MASK;
  2000. pNameAddr->NameTypeState |= STATE_RELEASED;
  2001. pNameAddr->pTracker = NULL;
  2002. }
  2003. ASSERT (pNameAddr->RefCount == 1);
  2004. InterlockedDecrement(&NbtConfig.lNumPendingNameQueries);
  2005. NBT_DEREFERENCE_NAMEADDR (pNameAddr, REF_NAME_QUERY_ON_NET, TRUE);
  2006. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2007. }
  2008. //----------------------------------------------------------------------------
  2009. VOID
  2010. NbtCompleteLmhSvcRequest(
  2011. IN NBT_WORK_ITEM_CONTEXT *Context,
  2012. IN ULONG *IpList,
  2013. IN enum eNbtLmhRequestType RequestType,
  2014. IN ULONG lNameLength,
  2015. IN PWSTR pwsName, // The rosolved name return by LmhSvc
  2016. IN BOOLEAN IpAddrResolved
  2017. )
  2018. /*++
  2019. Routine Description:
  2020. If the destination name is of the form 11.101.4.25 or is a dns name (i.e. of
  2021. the form ftp.microsoft.com) then we come to this function. In addition to
  2022. doing some house keeping, if the name did resolve then we also send out
  2023. a nodestatus request to find out the server name for that ipaddr
  2024. Arguments:
  2025. Context - (NBT_WORK_ITEM_CONTEXT)
  2026. IpList - Array of ipaddrs if resolved (i.e. IpAddrResolved is TRUE)
  2027. IpAddrResolved - TRUE if ipaddr could be resolved, FALSE otherwise
  2028. Return Value:
  2029. Nothing
  2030. Notes:
  2031. --*/
  2032. {
  2033. NTSTATUS status;
  2034. PVOID pClientCompletion;
  2035. tDGRAM_SEND_TRACKING *pTracker;
  2036. tDGRAM_SEND_TRACKING *pClientTracker;
  2037. ULONG TdiAddressType = TDI_ADDRESS_TYPE_NETBIOS;
  2038. ULONG IpAddrsList[MAX_IPADDRS_PER_HOST+1];
  2039. tDEVICECONTEXT *pDeviceContext;
  2040. int i;
  2041. tCONNECTELE *pConnEle;
  2042. CTEPagedCode();
  2043. IF_DBG(NBT_DEBUG_NAMESRV)
  2044. KdPrint(("Nbt.NbtCompleteLmhSvcRequest: Entered ...\n"));
  2045. pTracker = Context->pTracker;
  2046. pClientCompletion = Context->ClientCompletion;
  2047. pClientTracker = (tDGRAM_SEND_TRACKING *) Context->pClientContext;
  2048. pDeviceContext = pClientTracker->pDeviceContext;
  2049. // whether or not name resolved, we don't need this nameaddr anymore
  2050. // (if name resolved, then we do a node status to that addr and create
  2051. // a new nameaddr for the server name in ExtractServerName)
  2052. // pTracker is null if we went straight to dns (without wins etc)
  2053. if (pTracker)
  2054. {
  2055. //
  2056. // Set some info in case some client is still resolving the name
  2057. //
  2058. SetNameState (pTracker->pNameAddr, IpList, IpAddrResolved);
  2059. pTracker->pNameAddr = NULL;
  2060. }
  2061. (VOID)NbtCancelCancelRoutine (pClientTracker->pClientIrp);
  2062. pClientTracker->pTrackerWorker = NULL; // The original NameQuery Tracker will be dereferenced below
  2063. status = STATUS_BAD_NETWORK_PATH;
  2064. if (RequestType == NBT_RESOLVE_WITH_DNS)
  2065. {
  2066. TdiAddressType = ((pTracker == NULL) ? pClientTracker->AddressType: TDI_ADDRESS_TYPE_NETBIOS);
  2067. }
  2068. //
  2069. // If we failed to resolve it, set the state approriately!
  2070. //
  2071. if (!IpAddrResolved)
  2072. {
  2073. if ((TdiAddressType == TDI_ADDRESS_TYPE_NETBIOS_EX) &&
  2074. (pConnEle = pClientTracker->pConnEle)) // NULL if request was to send Datagram!
  2075. {
  2076. pConnEle->RemoteNameDoesNotExistInDNS = TRUE;
  2077. }
  2078. }
  2079. else if (NBT_VERIFY_HANDLE(pDeviceContext, NBT_VERIFY_DEVCONTEXT)) // check if this Device is still up!
  2080. {
  2081. // the name was resolved successfully!
  2082. switch (RequestType)
  2083. {
  2084. case NBT_RESOLVE_WITH_DNS:
  2085. {
  2086. // bug #20697, #95241
  2087. if (pwsName && pClientTracker->pNetbiosUnicodeEX &&
  2088. (pClientTracker->pNetbiosUnicodeEX->NameBufferType == NBT_READWRITE ||
  2089. pClientTracker->pNetbiosUnicodeEX->NameBufferType == NBT_WRITEONLY)) {
  2090. UNICODE_STRING temp;
  2091. temp = pClientTracker->pNetbiosUnicodeEX->RemoteName;
  2092. //
  2093. // Has the buffer changed?
  2094. //
  2095. if (memcmp(&temp, &pClientTracker->ucRemoteName, sizeof(UNICODE_STRING)) == 0) {
  2096. ASSERT(lNameLength <= (DNS_NAME_BUFFER_LENGTH-1) * sizeof(pwsName[0]));
  2097. ASSERT((lNameLength%sizeof(WCHAR)) == 0);
  2098. //
  2099. // Make sure we don't overrun the buffer
  2100. //
  2101. if (lNameLength > temp.MaximumLength - sizeof(WCHAR)) {
  2102. // Don't return STATUS_BUFFER_OVERFLOW since it is just a warning instead of error
  2103. status = STATUS_BUFFER_TOO_SMALL;
  2104. break;
  2105. }
  2106. CTEMemCopy(temp.Buffer, pwsName, lNameLength);
  2107. temp.Buffer[lNameLength/sizeof(WCHAR)] = 0;
  2108. temp.Length = (USHORT)lNameLength;
  2109. pClientTracker->pNetbiosUnicodeEX->NameBufferType = NBT_WRITTEN;
  2110. pClientTracker->pNetbiosUnicodeEX->RemoteName = temp;
  2111. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  2112. KdPrint(("netbt!NbtCompleteLmhSvcRequest: Update Unicode Name at %d of %s\n"
  2113. "\t\tDNS return (%ws)\n",
  2114. __LINE__, __FILE__, pwsName));
  2115. }
  2116. }
  2117. if ((TdiAddressType == TDI_ADDRESS_TYPE_NETBIOS) &&
  2118. (!IsDeviceNetbiosless(pDeviceContext))) // Can't do a NodeStatus on the SMB port
  2119. {
  2120. for (i=0; i<MAX_IPADDRS_PER_HOST; i++)
  2121. {
  2122. IpAddrsList[i] = IpList[i];
  2123. if (IpAddrsList[i] == 0)
  2124. {
  2125. break;
  2126. }
  2127. }
  2128. IpAddrsList[MAX_IPADDRS_PER_HOST] = 0;
  2129. pClientTracker->Flags |= NBT_DNS_SERVER; // Set this so that the completion will know
  2130. pClientTracker->CompletionRoutine = pClientCompletion;
  2131. status = NbtSendNodeStatus(pDeviceContext,
  2132. NULL,
  2133. IpAddrsList,
  2134. pClientTracker,
  2135. ExtractServerNameCompletion);
  2136. //
  2137. // If we succeeded in sending a Node status, exit now,
  2138. // without calling the completion routine
  2139. //
  2140. if (NT_SUCCESS(status))
  2141. {
  2142. // pTracker is null if we went straight to dns (without wins etc) or
  2143. // if this was a Ping request
  2144. if (pTracker)
  2145. {
  2146. NBT_DEREFERENCE_TRACKER(pTracker, FALSE);
  2147. }
  2148. CTEMemFree(Context);
  2149. return;
  2150. }
  2151. break;
  2152. }
  2153. //
  2154. // The Address is of type TDI_ADDRESS_TYPE_NETBIOS_EX,
  2155. // so now handle this scenario in the same way as for
  2156. // for a Ping request!
  2157. //
  2158. // NO break!
  2159. }
  2160. case NBT_PING_IP_ADDRS:
  2161. {
  2162. //
  2163. // add this server name to the remote hashtable
  2164. // Call into IP to determine the outgoing interface for this address
  2165. //
  2166. pDeviceContext = GetDeviceFromInterface (htonl(IpList[0]), TRUE);
  2167. status = LockAndAddToHashTable(NbtConfig.pRemoteHashTbl,
  2168. pClientTracker->pDestName,
  2169. NbtConfig.pScope,
  2170. IpList[0],
  2171. NBT_UNIQUE,
  2172. NULL,
  2173. NULL,
  2174. pDeviceContext,
  2175. (USHORT) ((RequestType == NBT_RESOLVE_WITH_DNS) ?
  2176. NAME_RESOLVED_BY_DNS :
  2177. NAME_RESOLVED_BY_WINS | NAME_RESOLVED_BY_BCAST));
  2178. if (pDeviceContext)
  2179. {
  2180. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_OUT_FROM_IP, FALSE);
  2181. }
  2182. //
  2183. // STATUS_PENDING will be returned if the name already existed
  2184. // in the hashtable
  2185. //
  2186. if (status == STATUS_PENDING)
  2187. {
  2188. status = STATUS_SUCCESS;
  2189. }
  2190. IF_DBG(NBT_DEBUG_NAMESRV)
  2191. KdPrint(("Nbt.NbtCompleteLmhSvcRequest: AddRecordToHashTable Status %lx\n",status));
  2192. break;
  2193. }
  2194. default:
  2195. {
  2196. ASSERT(0);
  2197. }
  2198. } // switch
  2199. }
  2200. // pTracker is null if we went straight to dns (without wins etc) or
  2201. // if this was a Ping request
  2202. if (pTracker)
  2203. {
  2204. NBT_DEREFERENCE_TRACKER(pTracker, FALSE);
  2205. }
  2206. NbtTrace(NBT_TRACE_NAMESRV, ("complete client request with %!status!", status));
  2207. CompleteClientReq(pClientCompletion, pClientTracker, status);
  2208. CTEMemFree(Context);
  2209. }
  2210. #endif // !VXD
  2211. //----------------------------------------------------------------------------
  2212. NTSTATUS
  2213. PreloadEntry(
  2214. IN PUCHAR name,
  2215. IN tIPADDRESS inaddr
  2216. )
  2217. /*++
  2218. Routine Description:
  2219. This function adds an lmhosts entry to nbt's name cache. For each
  2220. lmhosts entry, NSUFFIXES unique cache entries are created.
  2221. Even when some cache entries can't be created, this function doesn't
  2222. attempt to remove any that were successfully added to the cache.
  2223. Arguments:
  2224. name - the unencoded NetBIOS name specified in lmhosts
  2225. inaddr - the ip address, in host byte order
  2226. Return Value:
  2227. The number of new name cache entries created.
  2228. --*/
  2229. {
  2230. NTSTATUS status;
  2231. tNAMEADDR *pNameAddr;
  2232. LONG nentries;
  2233. LONG Len;
  2234. CHAR temp[NETBIOS_NAME_SIZE+1];
  2235. CTELockHandle OldIrq;
  2236. LONG NumberToAdd;
  2237. tDEVICECONTEXT *pDeviceContext;
  2238. // if all 16 bytes are present then only add that name exactly as it
  2239. // is.
  2240. //
  2241. Len = strlen(name);
  2242. //
  2243. // if this string is exactly 16 characters long, do not expand
  2244. // into 0x00, 0x03,0x20 names. Just add the single name as it is.
  2245. //
  2246. if (Len == NETBIOS_NAME_SIZE)
  2247. {
  2248. NumberToAdd = 1;
  2249. }
  2250. else
  2251. {
  2252. NumberToAdd = NSUFFIXES;
  2253. }
  2254. for (nentries = 0; nentries < NumberToAdd; nentries++)
  2255. {
  2256. // for names less than 16 bytes, expand out to 16 and put a 16th byte
  2257. // on according to the suffix array
  2258. //
  2259. if (Len != NETBIOS_NAME_SIZE)
  2260. {
  2261. LmExpandName(temp, name, Suffix[nentries]);
  2262. }
  2263. else
  2264. {
  2265. CTEMemCopy(temp,name,NETBIOS_NAME_SIZE);
  2266. }
  2267. pDeviceContext = GetDeviceFromInterface (htonl(inaddr), TRUE);
  2268. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2269. status = AddToHashTable (NbtConfig.pRemoteHashTbl,
  2270. temp,
  2271. NbtConfig.pScope,
  2272. inaddr,
  2273. NBT_UNIQUE,
  2274. NULL,
  2275. &pNameAddr,
  2276. pDeviceContext,
  2277. NAME_RESOLVED_BY_LMH_P);
  2278. // if the name is already in the hash table, the status code is
  2279. // status pending. This could happen if the preloads are purged
  2280. // when one is still being referenced by another part of the code,
  2281. // and was therefore not deleted. We do not want to add the name
  2282. // twice, so we just change the ip address to agree with the preload
  2283. // value
  2284. //
  2285. if ((status == STATUS_SUCCESS) ||
  2286. ((status == STATUS_PENDING) &&
  2287. (!(pNameAddr->NameTypeState & PRELOADED))))
  2288. {
  2289. //
  2290. // this prevents the name from being deleted by the Hash Timeout code
  2291. //
  2292. pNameAddr->NameTypeState |= PRELOADED | STATE_RESOLVED;
  2293. pNameAddr->NameTypeState &= ~STATE_CONFLICT;
  2294. pNameAddr->Ttl = 0xFFFFFFFF;
  2295. pNameAddr->Verify = REMOTE_NAME;
  2296. NBT_REFERENCE_NAMEADDR (pNameAddr, REF_NAME_PRELOADED);
  2297. if (pDeviceContext)
  2298. {
  2299. pNameAddr->AdapterMask |= pDeviceContext->AdapterMask;
  2300. }
  2301. }
  2302. else if (status == STATUS_PENDING)
  2303. {
  2304. pNameAddr->IpAddress = inaddr;
  2305. }
  2306. if (pDeviceContext)
  2307. {
  2308. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_OUT_FROM_IP, TRUE);
  2309. }
  2310. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2311. }
  2312. return(STATUS_SUCCESS);
  2313. } // PreloadEntry
  2314. //----------------------------------------------------------------------------
  2315. extern
  2316. VOID
  2317. RemovePreloads (
  2318. )
  2319. /*++
  2320. Routine Description:
  2321. This function removes preloaded entries from the remote hash table.
  2322. If it finds any of the preloaded entries are active with a ref count
  2323. above the base level of 2, then it returns true.
  2324. Arguments:
  2325. none
  2326. Return Value:
  2327. none
  2328. --*/
  2329. {
  2330. tNAMEADDR *pNameAddr;
  2331. PLIST_ENTRY pHead,pEntry;
  2332. CTELockHandle OldIrq;
  2333. tHASHTABLE *pHashTable;
  2334. BOOLEAN FoundActivePreload=FALSE;
  2335. LONG i;
  2336. //
  2337. // go through the remote table deleting names that have the PRELOAD
  2338. // bit set.
  2339. //
  2340. pHashTable = NbtConfig.pRemoteHashTbl;
  2341. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2342. for (i=0;i < pHashTable->lNumBuckets ;i++ )
  2343. {
  2344. pHead = &pHashTable->Bucket[i];
  2345. pEntry = pHead->Flink;
  2346. while (pEntry != pHead)
  2347. {
  2348. pNameAddr = CONTAINING_RECORD(pEntry,tNAMEADDR,Linkage);
  2349. pEntry = pEntry->Flink;
  2350. //
  2351. // Delete preloaded entries that are not in use by some other
  2352. // part of the code now. Note that preloaded entries start with
  2353. // a ref count of 2 so that the normal remote hashtimeout code
  2354. // will not delete them
  2355. //
  2356. if ((pNameAddr->NameTypeState & PRELOADED) &&
  2357. (pNameAddr->RefCount == 2))
  2358. {
  2359. NBT_DEREFERENCE_NAMEADDR (pNameAddr, REF_NAME_PRELOADED, TRUE);
  2360. NBT_DEREFERENCE_NAMEADDR (pNameAddr, REF_NAME_REMOTE, TRUE);
  2361. }
  2362. }
  2363. }
  2364. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2365. return;
  2366. }
  2367. //----------------------------------------------------------------------------
  2368. LONG
  2369. PrimeCache(
  2370. IN PUCHAR path,
  2371. IN PUCHAR ignored,
  2372. IN CHAR RecurseDepth,
  2373. OUT BOOLEAN *ignored2
  2374. )
  2375. /*++
  2376. Routine Description:
  2377. This function is called to prime the cache with entries in the lmhosts
  2378. file that are marked as preload entries.
  2379. Arguments:
  2380. path - a fully specified path to a lmhosts file
  2381. ignored - unused
  2382. RecurseDepth- the depth to which we can resurse -- 0 => no more recursion
  2383. Return Value:
  2384. Number of new cache entries that were added, or -1 if there was an
  2385. i/o error.
  2386. --*/
  2387. {
  2388. int nentries;
  2389. PUCHAR buffer;
  2390. PLM_FILE pfile;
  2391. NTSTATUS status;
  2392. int count, nwords;
  2393. unsigned long temp;
  2394. INCLUDE_STATE incstate;
  2395. PUCHAR token[MaxTokens];
  2396. ULONG inaddr;
  2397. LINE_CHARACTERISTICS current;
  2398. UCHAR Name[NETBIOS_NAME_SIZE+1];
  2399. ULONG IpAddr;
  2400. LIST_ENTRY TmpDomainList;
  2401. int domtoklen;
  2402. CTEPagedCode();
  2403. if (!NbtConfig.EnableLmHosts)
  2404. {
  2405. return(STATUS_SUCCESS);
  2406. }
  2407. InitializeListHead(&TmpDomainList);
  2408. //
  2409. // Check for infinitely recursive name lookup in a #INCLUDE.
  2410. //
  2411. if (LmpBreakRecursion(path, "", 1) == TRUE)
  2412. {
  2413. return (-1);
  2414. }
  2415. pfile = LmOpenFile(path);
  2416. if (!pfile)
  2417. {
  2418. return(-1);
  2419. }
  2420. nentries = 0;
  2421. incstate = MustInclude;
  2422. domtoklen = strlen(DOMAIN_TOKEN);
  2423. while (buffer = LmFgets(pfile, &count))
  2424. {
  2425. #ifndef VXD
  2426. if ((NbtConfig.MaxPreloadEntries - nentries) < 3)
  2427. {
  2428. break;
  2429. }
  2430. #else
  2431. if ( nentries >= (NbtConfig.MaxPreloadEntries - 3) )
  2432. {
  2433. break;
  2434. }
  2435. #endif
  2436. nwords = MaxTokens;
  2437. current = LmpGetTokens(buffer, token, &nwords);
  2438. // if there is and error or no name on the line, then continue
  2439. // to the next line.
  2440. //
  2441. if (current.l_category == ErrorLine)
  2442. {
  2443. IF_DBG(NBT_DEBUG_LMHOST)
  2444. KdPrint(("Nbt.PrimeCache: Error line in Lmhost file\n"));
  2445. continue;
  2446. }
  2447. if (current.l_category != BeginAlternate && current.l_category != EndAlternate) {
  2448. if (token[NbName] == NULL) {
  2449. IF_DBG(NBT_DEBUG_LMHOST)
  2450. KdPrint(("Nbt.PrimeCache: Error line in Lmhost file\n"));
  2451. continue;
  2452. }
  2453. }
  2454. if (current.l_preload)
  2455. {
  2456. status = ConvertDottedDecimalToUlong(token[IpAddress],&inaddr);
  2457. if (NT_SUCCESS(status))
  2458. {
  2459. status = PreloadEntry (token[NbName], inaddr);
  2460. if (NT_SUCCESS(status))
  2461. {
  2462. nentries++;
  2463. }
  2464. }
  2465. }
  2466. switch ((ULONG)current.l_category)
  2467. {
  2468. case Domain:
  2469. if ((nwords - 1) < GroupName)
  2470. {
  2471. continue;
  2472. }
  2473. //
  2474. // and add '1C' on the end
  2475. //
  2476. LmExpandName(Name, token[GroupName]+ domtoklen, SPECIAL_GROUP_SUFFIX);
  2477. status = ConvertDottedDecimalToUlong(token[IpAddress],&IpAddr);
  2478. if (NT_SUCCESS(status))
  2479. {
  2480. AddToDomainList (Name, IpAddr, &TmpDomainList, (BOOLEAN)current.l_preload);
  2481. }
  2482. continue;
  2483. case Include:
  2484. if (!RecurseDepth || ((incstate == SkipInclude) || (nwords < 2)))
  2485. {
  2486. continue;
  2487. }
  2488. #ifdef VXD
  2489. //
  2490. // the buffer which we read into is reused for the next file: we
  2491. // need the contents when we get back: back it up!
  2492. // if we can't allocate memory, just skip this include
  2493. //
  2494. if ( !BackupCurrentData(pfile) )
  2495. {
  2496. continue;
  2497. }
  2498. #endif
  2499. temp = LmInclude(token[1], PrimeCache, NULL, (CHAR) (RecurseDepth-1), NULL);
  2500. #ifdef VXD
  2501. //
  2502. // going back to previous file: restore the backed up data
  2503. //
  2504. RestoreOldData(pfile);
  2505. #endif
  2506. if (temp != -1)
  2507. {
  2508. if (incstate == TryToInclude)
  2509. {
  2510. incstate = SkipInclude;
  2511. }
  2512. nentries += temp;
  2513. continue;
  2514. }
  2515. continue;
  2516. case BeginAlternate:
  2517. ASSERT(nwords == 1);
  2518. incstate = TryToInclude;
  2519. continue;
  2520. case EndAlternate:
  2521. ASSERT(nwords == 1);
  2522. incstate = MustInclude;
  2523. continue;
  2524. default:
  2525. continue;
  2526. }
  2527. }
  2528. status = LmCloseFile(pfile);
  2529. ASSERT(status == STATUS_SUCCESS);
  2530. //
  2531. // make this the new domain list
  2532. //
  2533. MakeNewListCurrent(&TmpDomainList);
  2534. ASSERT(nentries >= 0);
  2535. return(nentries);
  2536. } // LmPrimeCache
  2537. //----------------------------------------------------------------------------
  2538. extern
  2539. VOID
  2540. GetContext(
  2541. IN OUT NBT_WORK_ITEM_CONTEXT **ppContext
  2542. )
  2543. /*++
  2544. Routine Description:
  2545. This function is called to get the context value to check if a name
  2546. query has been cancelled or not.
  2547. Arguments:
  2548. Context -
  2549. Return Value:
  2550. none
  2551. --*/
  2552. {
  2553. CTELockHandle OldIrq;
  2554. NBT_WORK_ITEM_CONTEXT *pContext;
  2555. //
  2556. // remove the Context value and return it.
  2557. //
  2558. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2559. if (pContext = LmHostQueries.Context)
  2560. {
  2561. if ((*ppContext) &&
  2562. (*ppContext != pContext))
  2563. {
  2564. pContext = NULL;
  2565. }
  2566. #ifndef VXD
  2567. else if (NbtCancelCancelRoutine(((tDGRAM_SEND_TRACKING *)(pContext->pClientContext))->pClientIrp)
  2568. == STATUS_CANCELLED)
  2569. {
  2570. pContext = NULL;
  2571. }
  2572. else
  2573. #endif // VXD
  2574. {
  2575. LmHostQueries.Context = NULL;
  2576. }
  2577. }
  2578. *ppContext = pContext;
  2579. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2580. }
  2581. //----------------------------------------------------------------------------
  2582. extern
  2583. NTSTATUS
  2584. ChangeStateOfName (
  2585. IN tIPADDRESS IpAddress,
  2586. IN NBT_WORK_ITEM_CONTEXT *pContext,
  2587. IN OUT NBT_WORK_ITEM_CONTEXT **ppContext,
  2588. IN USHORT NameAddFlags
  2589. )
  2590. /*++
  2591. Routine Description:
  2592. This function changes the state of a name and nulls the Context
  2593. value in lmhostqueries.
  2594. Arguments:
  2595. pContext - The Context value if it has been removed from the
  2596. LmHostQueries.Context ptr.
  2597. ppContext - The Context we are processing
  2598. Return Value:
  2599. none
  2600. --*/
  2601. {
  2602. NTSTATUS status;
  2603. CTELockHandle OldIrq;
  2604. tDEVICECONTEXT *pDeviceContext;
  2605. pDeviceContext = GetDeviceFromInterface(htonl(IpAddress), TRUE);
  2606. if (pContext == NULL)
  2607. {
  2608. //
  2609. // See if the name query is still active
  2610. //
  2611. pContext = *ppContext;
  2612. GetContext (&pContext);
  2613. }
  2614. if (pContext)
  2615. {
  2616. // convert broadcast addresses to zero since NBT interprets zero
  2617. // to be broadcast
  2618. //
  2619. if (IpAddress == (ULONG)-1)
  2620. {
  2621. IpAddress = 0;
  2622. }
  2623. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2624. status = AddToHashTable (NbtConfig.pRemoteHashTbl,
  2625. pContext->pTracker->pNameAddr->Name,
  2626. NbtConfig.pScope,
  2627. IpAddress,
  2628. NBT_UNIQUE,
  2629. NULL,
  2630. NULL,
  2631. pDeviceContext,
  2632. NameAddFlags);
  2633. //
  2634. // this will free the pNameAddr, so do not access this after this point
  2635. //
  2636. InterlockedDecrement(&NbtConfig.lNumPendingNameQueries);
  2637. NBT_DEREFERENCE_NAMEADDR (pContext->pTracker->pNameAddr, REF_NAME_QUERY_ON_NET, TRUE);
  2638. pContext->pTracker->pNameAddr = NULL;
  2639. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2640. *ppContext = pContext;
  2641. }
  2642. else
  2643. {
  2644. *ppContext = NULL;
  2645. }
  2646. if (pDeviceContext)
  2647. {
  2648. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_OUT_FROM_IP, FALSE);
  2649. }
  2650. return (STATUS_SUCCESS);
  2651. }
  2652. //----------------------------------------------------------------------------
  2653. VOID
  2654. RemoveLmHRequests(
  2655. IN tLMHSVC_REQUESTS *pLmHRequest,
  2656. IN PLIST_ENTRY pTmpHead,
  2657. IN tTIMERQENTRY *pTimerQEntry,
  2658. IN tDEVICECONTEXT *pDeviceContext
  2659. )
  2660. /*++
  2661. Routine Description:
  2662. This routine is called to find timed out entries in the queue of
  2663. lmhost or dns name queries.
  2664. Arguments:
  2665. Return Value:
  2666. The function value is the status of the operation.
  2667. --*/
  2668. {
  2669. PLIST_ENTRY pEntry;
  2670. NBT_WORK_ITEM_CONTEXT *pWiContext;
  2671. BOOLEAN fRestartTimer = FALSE;
  2672. //
  2673. // check the currently processing LMHOSTS entry
  2674. //
  2675. if (pLmHRequest->Context)
  2676. {
  2677. pWiContext = (NBT_WORK_ITEM_CONTEXT *) pLmHRequest->Context;
  2678. if ((pWiContext->TimedOut) || (pWiContext->pDeviceContext == pDeviceContext))
  2679. {
  2680. pLmHRequest->Context = NULL;
  2681. InsertTailList(pTmpHead, &pWiContext->Linkage);
  2682. #ifndef VXD
  2683. // Not for win95, MohsinA, 05-Dec-96.
  2684. NbtCancelCancelRoutine(((tDGRAM_SEND_TRACKING *) (pWiContext->pClientContext))->pClientIrp);
  2685. #endif
  2686. }
  2687. else
  2688. {
  2689. //
  2690. // restart the timer
  2691. //
  2692. fRestartTimer = TRUE;
  2693. pWiContext->TimedOut = TRUE;
  2694. }
  2695. }
  2696. //
  2697. // Check the list of queued entries
  2698. //
  2699. if (!IsListEmpty(&pLmHRequest->ToResolve))
  2700. {
  2701. //
  2702. // restart the timer
  2703. //
  2704. fRestartTimer = TRUE;
  2705. pEntry = pLmHRequest->ToResolve.Flink;
  2706. while (pEntry != &pLmHRequest->ToResolve)
  2707. {
  2708. pWiContext = CONTAINING_RECORD(pEntry,NBT_WORK_ITEM_CONTEXT,Linkage);
  2709. pEntry = pEntry->Flink;
  2710. if ((pWiContext->TimedOut) || (pWiContext->pDeviceContext == pDeviceContext))
  2711. {
  2712. //
  2713. // save on a temporary list and complete below
  2714. //
  2715. RemoveEntryList(&pWiContext->Linkage);
  2716. InsertTailList(pTmpHead, &pWiContext->Linkage);
  2717. }
  2718. else
  2719. {
  2720. pWiContext->TimedOut = TRUE;
  2721. }
  2722. }
  2723. }
  2724. if ((fRestartTimer) && (pTimerQEntry))
  2725. {
  2726. pTimerQEntry->Flags |= TIMER_RESTART;
  2727. }
  2728. }
  2729. //----------------------------------------------------------------------------
  2730. VOID
  2731. TimeoutLmHRequests(
  2732. IN tTIMERQENTRY *pTimerQEntry,
  2733. IN tDEVICECONTEXT *pDeviceContext,
  2734. IN BOOLEAN fLocked,
  2735. IN CTELockHandle *pJointLockOldIrq
  2736. )
  2737. {
  2738. PLIST_ENTRY pHead;
  2739. PLIST_ENTRY pEntry;
  2740. NBT_WORK_ITEM_CONTEXT *pWiContext;
  2741. LIST_ENTRY TmpHead;
  2742. InitializeListHead(&TmpHead);
  2743. if (!fLocked)
  2744. {
  2745. CTESpinLock(&NbtConfig.JointLock,*pJointLockOldIrq);
  2746. }
  2747. //
  2748. // check the currently processing LMHOSTS entry
  2749. //
  2750. RemoveLmHRequests (&LmHostQueries, &TmpHead, pTimerQEntry, pDeviceContext);
  2751. RemoveLmHRequests (&CheckAddr, &TmpHead, pTimerQEntry, pDeviceContext);
  2752. #ifndef VXD
  2753. RemoveLmHRequests (&DnsQueries, &TmpHead, pTimerQEntry, pDeviceContext);
  2754. #endif
  2755. CTESpinFree(&NbtConfig.JointLock,*pJointLockOldIrq);
  2756. if (!IsListEmpty(&TmpHead))
  2757. {
  2758. pHead = &TmpHead;
  2759. pEntry = pHead->Flink;
  2760. while (pEntry != pHead)
  2761. {
  2762. pWiContext = CONTAINING_RECORD(pEntry,NBT_WORK_ITEM_CONTEXT,Linkage);
  2763. pEntry = pEntry->Flink;
  2764. RemoveEntryList(&pWiContext->Linkage);
  2765. IF_DBG(NBT_DEBUG_LMHOST)
  2766. KdPrint(("Nbt.TimeoutLmHRequests: Context=<%p>, pDeviceContext=<%p>\n",
  2767. pWiContext, pDeviceContext));
  2768. RemoveNameAndCompleteReq(pWiContext,STATUS_TIMEOUT);
  2769. }
  2770. }
  2771. if (fLocked)
  2772. {
  2773. CTESpinLock(&NbtConfig.JointLock,*pJointLockOldIrq);
  2774. }
  2775. }
  2776. //----------------------------------------------------------------------------
  2777. VOID
  2778. LmHostTimeout(
  2779. PVOID pContext,
  2780. PVOID pContext2,
  2781. tTIMERQENTRY *pTimerQEntry
  2782. )
  2783. /*++
  2784. Routine Description:
  2785. This routine is called by the timer code when the timer expires. It
  2786. marks all items in Lmhosts/Dns q as timed out and completes any that have
  2787. already timed out with status timeout.
  2788. Arguments:
  2789. Return Value:
  2790. The function value is the status of the operation.
  2791. --*/
  2792. {
  2793. CTELockHandle OldIrq;
  2794. //
  2795. // If the timer is NULL, it means that the Timer is currently
  2796. // being stopped (usually at Unload time), so don't do anything!
  2797. //
  2798. if (!pTimerQEntry)
  2799. {
  2800. LmHostQueries.pTimer = NULL;
  2801. return;
  2802. }
  2803. TimeoutLmHRequests (pTimerQEntry, NULL, FALSE, &OldIrq);
  2804. // null the timer if we are not going to restart it.
  2805. //
  2806. if (!(pTimerQEntry->Flags & TIMER_RESTART))
  2807. {
  2808. LmHostQueries.pTimer = NULL;
  2809. }
  2810. }
  2811. //----------------------------------------------------------------------------
  2812. extern
  2813. VOID
  2814. StartLmHostTimer(
  2815. IN NBT_WORK_ITEM_CONTEXT *pContext,
  2816. IN BOOLEAN fLockedOnEntry
  2817. )
  2818. /*++
  2819. Routine Description
  2820. This routine handles setting up a timer to time the Lmhost entry.
  2821. The Joint Spin Lock may be held when this routine is called
  2822. Arguments:
  2823. Return Values:
  2824. VOID
  2825. --*/
  2826. {
  2827. NTSTATUS status;
  2828. tTIMERQENTRY *pTimerEntry;
  2829. CTELockHandle OldIrq;
  2830. if (!fLockedOnEntry)
  2831. {
  2832. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2833. }
  2834. pContext->TimedOut = FALSE;
  2835. //
  2836. // start the timer if it is not running
  2837. //
  2838. if (!LmHostQueries.pTimer)
  2839. {
  2840. status = StartTimer(LmHostTimeout,
  2841. NbtConfig.LmHostsTimeout,
  2842. NULL, // context value
  2843. NULL, // context2 value
  2844. NULL,
  2845. NULL,
  2846. NULL,
  2847. &pTimerEntry,
  2848. 0,
  2849. TRUE);
  2850. IF_DBG(NBT_DEBUG_NAMESRV)
  2851. KdPrint(("Nbt.StartLmHostTimer: Start Timer to time Lmhost Qing for pContext= %x,\n", pContext));
  2852. if (NT_SUCCESS(status))
  2853. {
  2854. LmHostQueries.pTimer = pTimerEntry;
  2855. }
  2856. else
  2857. {
  2858. // we failed to get a timer, but that is not
  2859. // then end of the world. The lmhost query will just
  2860. // not timeout in 30 seconds. It may take longer if
  2861. // it tries to include a remove file on a dead machine.
  2862. //
  2863. LmHostQueries.pTimer = NULL;
  2864. }
  2865. }
  2866. if (!fLockedOnEntry)
  2867. {
  2868. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2869. }
  2870. }
  2871. //----------------------------------------------------------------------------
  2872. NTSTATUS
  2873. LmHostQueueRequest(
  2874. IN tDGRAM_SEND_TRACKING *pTracker,
  2875. IN PVOID pClientContext,
  2876. IN PVOID ClientCompletion,
  2877. IN PVOID pDeviceContext
  2878. )
  2879. /*++
  2880. Routine Description:
  2881. This routine exists so that LmHost requests will not take up more than
  2882. one executive worker thread. If a thread is busy performing an Lmhost
  2883. request, new requests are queued otherwise we could run out of worker
  2884. threads and lock up the system.
  2885. The Joint Spin Lock is held when this routine is called
  2886. Arguments:
  2887. pTracker - the tracker block for context
  2888. DelayedWorkerRoutine - the routine for the Workerthread to call
  2889. pDeviceContext - dev context that initiated this
  2890. Return Value:
  2891. --*/
  2892. {
  2893. NTSTATUS status = STATUS_UNSUCCESSFUL;
  2894. NBT_WORK_ITEM_CONTEXT *pContext;
  2895. tDGRAM_SEND_TRACKING *pTrackClient;
  2896. PCTE_IRP pIrp;
  2897. BOOLEAN OnList;
  2898. if (pContext = (NBT_WORK_ITEM_CONTEXT *)NbtAllocMem(sizeof(NBT_WORK_ITEM_CONTEXT),NBT_TAG('V')))
  2899. {
  2900. pContext->pTracker = pTracker;
  2901. pContext->pClientContext = pClientContext;
  2902. pContext->ClientCompletion = ClientCompletion;
  2903. pContext->pDeviceContext = pDeviceContext;
  2904. pContext->TimedOut = FALSE;
  2905. if (LmHostQueries.ResolvingNow)
  2906. {
  2907. // Lmhosts is busy resolving another name, so wait for it to return
  2908. // mean while, Queue the name query
  2909. //
  2910. InsertTailList(&LmHostQueries.ToResolve,&pContext->Linkage);
  2911. OnList = TRUE;
  2912. }
  2913. else
  2914. {
  2915. LmHostQueries.Context = pContext;
  2916. LmHostQueries.ResolvingNow = TRUE;
  2917. OnList = FALSE;
  2918. if (!NT_SUCCESS (NTQueueToWorkerThread(NULL, DelayedScanLmHostFile,
  2919. pTracker,
  2920. pClientContext,
  2921. ClientCompletion,
  2922. pDeviceContext,
  2923. TRUE)))
  2924. {
  2925. LmHostQueries.Context = NULL;
  2926. LmHostQueries.ResolvingNow = FALSE;
  2927. CTEMemFree(pContext);
  2928. return (STATUS_UNSUCCESSFUL);
  2929. }
  2930. }
  2931. //
  2932. // To prevent this name query from languishing on the Lmhost Q when
  2933. // a #include on a dead machine is trying to be openned, start the
  2934. // connection setup timer
  2935. //
  2936. StartLmHostTimer(pContext, TRUE);
  2937. //
  2938. // this is the session setup tracker
  2939. //
  2940. #ifndef VXD
  2941. pTrackClient = (tDGRAM_SEND_TRACKING *)pClientContext;
  2942. if (pIrp = pTrackClient->pClientIrp)
  2943. {
  2944. //
  2945. // allow the client to cancel the name query Irp
  2946. //
  2947. // but do not call NTSetCancel... since it takes need to run
  2948. // at non DPC level, and it calls the completion routine
  2949. // which takes the JointLock that we already have.
  2950. //
  2951. status = NTCheckSetCancelRoutine(pTrackClient->pClientIrp, NbtCancelWaitForLmhSvcIrp,NULL);
  2952. if (status == STATUS_CANCELLED)
  2953. {
  2954. //
  2955. // since the name query is cancelled do not let lmhost processing
  2956. // handle it.
  2957. //
  2958. if (OnList)
  2959. {
  2960. RemoveEntryList(&pContext->Linkage);
  2961. }
  2962. else
  2963. {
  2964. //
  2965. // do not set resolving now to False since the work item
  2966. // has been queued to the worker thread
  2967. //
  2968. LmHostQueries.Context = NULL;
  2969. LmHostQueries.ResolvingNow = FALSE;
  2970. }
  2971. CTEMemFree(pContext);
  2972. }
  2973. return(status);
  2974. }
  2975. #endif
  2976. status = STATUS_SUCCESS;
  2977. }
  2978. return(status);
  2979. }
  2980. //----------------------------------------------------------------------------
  2981. extern
  2982. NBT_WORK_ITEM_CONTEXT *
  2983. GetNameToFind(
  2984. OUT PUCHAR pName
  2985. )
  2986. /*++
  2987. Routine Description:
  2988. This function is called to get the name to query from the LmHostQueries
  2989. list.
  2990. Arguments:
  2991. Context -
  2992. Return Value:
  2993. none
  2994. --*/
  2995. {
  2996. tDGRAM_SEND_TRACKING *pTracker;
  2997. CTELockHandle OldIrq;
  2998. NBT_WORK_ITEM_CONTEXT *Context;
  2999. PLIST_ENTRY pEntry;
  3000. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  3001. // if the context value has been cleared then that name query has been
  3002. // cancelled, so check for another one.
  3003. //
  3004. if (!(Context = LmHostQueries.Context))
  3005. {
  3006. //
  3007. // the current name query got canceled so see if there are any more
  3008. // to service
  3009. //
  3010. if (!IsListEmpty(&LmHostQueries.ToResolve))
  3011. {
  3012. pEntry = RemoveHeadList(&LmHostQueries.ToResolve);
  3013. Context = CONTAINING_RECORD(pEntry,NBT_WORK_ITEM_CONTEXT,Linkage);
  3014. LmHostQueries.Context = Context;
  3015. }
  3016. else
  3017. {
  3018. //
  3019. // no more names to resolve, so clear the flag
  3020. //
  3021. LmHostQueries.ResolvingNow = FALSE;
  3022. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  3023. return(NULL);
  3024. }
  3025. }
  3026. pTracker = ((NBT_WORK_ITEM_CONTEXT *)Context)->pTracker;
  3027. CTEMemCopy(pName,pTracker->pNameAddr->Name,NETBIOS_NAME_SIZE);
  3028. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  3029. return(Context);
  3030. }
  3031. //----------------------------------------------------------------------------
  3032. extern
  3033. VOID
  3034. RemoveNameAndCompleteReq (
  3035. IN NBT_WORK_ITEM_CONTEXT *pContext,
  3036. IN NTSTATUS status
  3037. )
  3038. /*++
  3039. Routine Description:
  3040. This function removes the name, cleans up the tracker
  3041. and then completes the clients request.
  3042. Arguments:
  3043. Context -
  3044. Return Value:
  3045. none
  3046. --*/
  3047. {
  3048. tDGRAM_SEND_TRACKING *pTracker;
  3049. PVOID pClientContext;
  3050. PVOID pClientCompletion;
  3051. CTELockHandle OldIrq;
  3052. // if pContext is null the name query was cancelled during the
  3053. // time it took to go read the lmhosts file, so don't do this
  3054. // stuff
  3055. //
  3056. if (pContext)
  3057. {
  3058. pTracker = pContext->pTracker;
  3059. pClientCompletion = pContext->ClientCompletion;
  3060. pClientContext = pContext->pClientContext;
  3061. CTEMemFree(pContext);
  3062. #ifndef VXD
  3063. //
  3064. // clear out the cancel routine if there is an irp involved
  3065. //
  3066. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  3067. NbtCancelCancelRoutine( ((tDGRAM_SEND_TRACKING *)(pClientContext))->pClientIrp );
  3068. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  3069. #endif
  3070. // remove the name from the hash table, since it did not resolve
  3071. if (pTracker)
  3072. {
  3073. if ((status != STATUS_SUCCESS) &&
  3074. (pTracker->pNameAddr))
  3075. {
  3076. SetNameState (pTracker->pNameAddr, NULL, FALSE);
  3077. pTracker->pNameAddr = NULL;
  3078. }
  3079. // free the tracker and call the completion routine.
  3080. //
  3081. NBT_DEREFERENCE_TRACKER(pTracker, FALSE);
  3082. }
  3083. if (pClientCompletion)
  3084. {
  3085. CompleteClientReq(pClientCompletion, pClientContext, status);
  3086. }
  3087. }
  3088. }
  3089. //----------------------------------------------------------------------------
  3090. //
  3091. // Alternative to the c-runtime
  3092. //
  3093. #ifndef VXD
  3094. PCHAR
  3095. Nbtstrcat( PUCHAR pch, PUCHAR pCat, LONG Len )
  3096. {
  3097. STRING StringIn;
  3098. STRING StringOut;
  3099. RtlInitAnsiString(&StringIn, pCat);
  3100. RtlInitAnsiString(&StringOut, pch);
  3101. StringOut.MaximumLength = (USHORT)Len;
  3102. //
  3103. // increment to include the null on the end of the string since
  3104. // we want that on the end of the final product
  3105. //
  3106. StringIn.Length++;
  3107. RtlAppendStringToString(&StringOut,&StringIn);
  3108. return(pch);
  3109. }
  3110. #else
  3111. #define Nbtstrcat( a,b,c ) strcat( a,b )
  3112. #endif