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.

1015 lines
22 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. dnsfile.c
  5. Abstract:
  6. This module contains code for the Simple DNS Server (formerly the DNS Proxy)
  7. to operate on the Hosts.ics file. Abridged from the DS tree.
  8. Author:
  9. Raghu Gatta (rgatta) 15-Nov-2000
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. //
  15. // EXTERNAL DECLARATIONS
  16. //
  17. extern "C"
  18. FILE *
  19. SockOpenNetworkDataBase(
  20. IN char *Database,
  21. OUT char *Pathname,
  22. IN int PathnameLen,
  23. IN char *OpenFlags
  24. );
  25. //
  26. // Locking Order:
  27. // (1) DnsFileInfo.Lock
  28. // (2) DnsTableLock
  29. // OR
  30. // (1) DnsGlobalInfoLock
  31. // (2) DnsTableLock
  32. //
  33. // Globals
  34. //
  35. IP_DNS_PROXY_FILE_INFO DnsFileInfo;
  36. ULONG
  37. DnsInitializeFileManagement(
  38. VOID
  39. )
  40. /*++
  41. Routine Description:
  42. This routine is called to initialize the file management module.
  43. Arguments:
  44. none.
  45. Return Value:
  46. ULONG - Win32 status code.
  47. Environment:
  48. Invoked internally in the context of an IP router-manager thread.
  49. (See 'RMDNS.C').
  50. --*/
  51. {
  52. ULONG Error = NO_ERROR;
  53. PROFILE("DnsInitializeFileManagement");
  54. ZeroMemory(&DnsFileInfo, sizeof(IP_DNS_PROXY_FILE_INFO));
  55. __try {
  56. InitializeCriticalSection(&DnsFileInfo.Lock);
  57. }
  58. __except(EXCEPTION_EXECUTE_HANDLER) {
  59. NhTrace(
  60. TRACE_FLAG_IF,
  61. "DnsInitializeFileManagement: exception %d creating lock",
  62. Error = GetExceptionCode()
  63. );
  64. }
  65. return Error;
  66. } // DnsInitializeFileManagement
  67. VOID
  68. DnsShutdownFileManagement(
  69. VOID
  70. )
  71. /*++
  72. Routine Description:
  73. This routine is called to shutdown the file management module.
  74. Arguments:
  75. none.
  76. Return Value:
  77. none.
  78. Environment:
  79. Invoked in an arbitrary thread context.
  80. --*/
  81. {
  82. PROFILE("DnsShutdownFileManagement");
  83. DnsEndHostsIcsFile();
  84. DeleteCriticalSection(&DnsFileInfo.Lock);
  85. } // DnsShutdownFileManagement
  86. BOOL
  87. DnsEndHostsIcsFile(
  88. VOID
  89. )
  90. /*++
  91. Routine Description:
  92. Close hosts file.
  93. Arguments:
  94. None.
  95. Globals:
  96. DnsFileInfo.HostFile -- host file ptr, tested and cleared
  97. Return Value:
  98. None.
  99. --*/
  100. {
  101. if (DnsFileInfo.HostFile)
  102. {
  103. fclose(DnsFileInfo.HostFile);
  104. DnsFileInfo.HostFile = NULL;
  105. }
  106. return TRUE;
  107. } // DnsEndHostsIcsFile
  108. BOOL
  109. DnsSetHostsIcsFile(
  110. BOOL fOverwrite
  111. )
  112. /*++
  113. Routine Description:
  114. Open hosts.ics file. If we write, we overwrite, else we read
  115. Arguments:
  116. None.
  117. Globals:
  118. DnsFileInfo.HostFile -- host file ptr, tested and set
  119. Return Value:
  120. None.
  121. --*/
  122. {
  123. LPVOID lpMsgBuf;
  124. UINT len;
  125. WCHAR hostDirectory[MAX_PATH*2];
  126. PCHAR mode = fOverwrite ? "w+t" : "rt";
  127. DnsEndHostsIcsFile();
  128. //
  129. // reset the host file name to hosts.ics
  130. //
  131. ZeroMemory(DnsFileInfo.HostFileName, HOSTDB_SIZE);
  132. DnsFileInfo.HostFile = SockOpenNetworkDataBase(
  133. HOSTSICSFILE,
  134. DnsFileInfo.HostFileName,
  135. HOSTDB_SIZE,
  136. mode
  137. );
  138. if(DnsFileInfo.HostFile == NULL)
  139. {
  140. NhTrace(
  141. TRACE_FLAG_DNS,
  142. "DnsSetHostsIcsFile: Unable to open %s file",
  143. HOSTSICSFILE
  144. );
  145. return FALSE;
  146. }
  147. else
  148. {
  149. /*
  150. NhTrace(
  151. TRACE_FLAG_DNS,
  152. "DnsSetHostsIcsFile: Successfully opened %s file",
  153. DnsFileInfo.HostFileName
  154. );
  155. */
  156. }
  157. return TRUE;
  158. } // DnsSetHostsIcsFile
  159. BOOL
  160. GetHostFromHostsIcsFile(
  161. BOOL fStartup
  162. )
  163. /*++
  164. Routine Description:
  165. Reads an entry from hosts.ics file.
  166. Arguments:
  167. fStartup set to TRUE if called on startup of protocol
  168. Globals:
  169. DnsFileInfo.HostFile -- host file ptr, tested and set
  170. DnsFileInfo.HostTime -- host timestamp
  171. DnsFileInfo.pHostName -- name ptr is set
  172. DnsFileInfo.Ip4Address -- IP4 address is set
  173. Return Value:
  174. TRUE if we were able to successfully able to read a line.
  175. FALSE if on EOF or no hosts file found.
  176. --*/
  177. {
  178. char *p, *ep;
  179. register char *cp, **q;
  180. //
  181. // we assume the hosts.ics file has already been opened
  182. //
  183. if (DnsFileInfo.HostFile == NULL)
  184. {
  185. return FALSE;
  186. }
  187. DnsFileInfo.HostLineBuf[BUFSIZ] = NULL;
  188. DnsFileInfo.pHostName = NULL;
  189. DnsFileInfo.Ip4Address = 0;
  190. ZeroMemory(&DnsFileInfo.HostTime, sizeof(SYSTEMTIME));
  191. //
  192. // loop until successfully read IP address
  193. // IP address starts on column 1
  194. //
  195. while( 1 )
  196. {
  197. // quit on EOF
  198. if ((p = fgets(DnsFileInfo.HostLineBuf, BUFSIZ, DnsFileInfo.HostFile)) == NULL)
  199. {
  200. if (!feof(DnsFileInfo.HostFile))
  201. {
  202. NhTrace(
  203. TRACE_FLAG_DNS,
  204. "GetHostFromHostsIcsFile: Error reading line"
  205. );
  206. }
  207. return FALSE;
  208. }
  209. // comment line -- skip
  210. if (*p == '#')
  211. {
  212. p++;
  213. //
  214. // if in startup mode, we skip first comment sign;
  215. // if there are more comment signs -- skip
  216. //
  217. if ((fStartup && *p == '#') || !fStartup)
  218. {
  219. continue;
  220. }
  221. }
  222. // null address terminate at EOL
  223. cp = strpbrk(p, "\n");
  224. if (cp != NULL)
  225. {
  226. *cp = '\0';
  227. }
  228. // whole line is characters - no whitespace -- skip
  229. cp = strpbrk(p, " \t");
  230. if ( cp == NULL )
  231. {
  232. continue;
  233. }
  234. // NULL terminate address string
  235. *cp++ = '\0';
  236. //
  237. // read address
  238. // - try IP4
  239. // - ignore IP6 for now
  240. // - otherwise skip
  241. //
  242. DnsFileInfo.Ip4Address = inet_addr(p);
  243. if (DnsFileInfo.Ip4Address != INADDR_NONE ||
  244. _strnicmp("255.255.255.255", p, 15) == 0)
  245. {
  246. break;
  247. }
  248. // invalid address, ignore line
  249. //
  250. // debug tracing
  251. //
  252. NhTrace(
  253. TRACE_FLAG_DNS,
  254. "GetHostFromHostsIcsFile: Error parsing host file address %s",
  255. p
  256. );
  257. continue;
  258. }
  259. // find the end of the string which was read
  260. ep = cp;
  261. while( *ep ) ep++;
  262. //
  263. // find name
  264. // - skip leading whitespace
  265. // - set global name ptr
  266. //
  267. while( *cp == ' ' || *cp == '\t' ) cp++;
  268. DnsFileInfo.pHostName = cp;
  269. // stop at trailing whitespace, NULL terminate
  270. cp = strpbrk(cp, " \t");
  271. if ( cp != NULL )
  272. {
  273. *cp++ = '\0';
  274. }
  275. else
  276. {
  277. // we have a name - but no timestamp
  278. NhTrace(
  279. TRACE_FLAG_DNS,
  280. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp",
  281. DnsFileInfo.pHostName
  282. );
  283. goto Failed;
  284. }
  285. // we dont have any support for aliases
  286. //
  287. // find the timestamp
  288. // - skip leading whitespace
  289. // - read the time values
  290. //
  291. while( *cp == ' ' || *cp == '\t' ) cp++;
  292. if ((cp >= ep) || (*cp != '#'))
  293. {
  294. NhTrace(
  295. TRACE_FLAG_DNS,
  296. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp",
  297. DnsFileInfo.pHostName
  298. );
  299. goto Failed;
  300. }
  301. cp++;
  302. while( *cp == ' ' || *cp == '\t' ) cp++; // now at first system time value
  303. if ((cp >= ep) || (*cp == '\0'))
  304. {
  305. NhTrace(
  306. TRACE_FLAG_DNS,
  307. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 1",
  308. DnsFileInfo.pHostName
  309. );
  310. goto Failed;
  311. }
  312. DnsFileInfo.HostTime.wYear = (WORD) atoi(cp);
  313. cp = strpbrk(cp, " \t");
  314. if (cp == NULL) goto Failed;
  315. while( *cp == ' ' || *cp == '\t' ) cp++;
  316. if ((cp >= ep) || (*cp == '\0'))
  317. {
  318. NhTrace(
  319. TRACE_FLAG_DNS,
  320. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 2",
  321. DnsFileInfo.pHostName
  322. );
  323. goto Failed;
  324. }
  325. DnsFileInfo.HostTime.wMonth = (WORD) atoi(cp);
  326. cp = strpbrk(cp, " \t");
  327. if (cp == NULL) goto Failed;
  328. while( *cp == ' ' || *cp == '\t' ) cp++;
  329. if ((cp >= ep) || (*cp == '\0'))
  330. {
  331. NhTrace(
  332. TRACE_FLAG_DNS,
  333. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 3",
  334. DnsFileInfo.pHostName
  335. );
  336. goto Failed;
  337. }
  338. DnsFileInfo.HostTime.wDayOfWeek = (WORD) atoi(cp);
  339. cp = strpbrk(cp, " \t");
  340. if (cp == NULL) goto Failed;
  341. while( *cp == ' ' || *cp == '\t' ) cp++;
  342. if ((cp >= ep) || (*cp == '\0'))
  343. {
  344. NhTrace(
  345. TRACE_FLAG_DNS,
  346. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 4",
  347. DnsFileInfo.pHostName
  348. );
  349. goto Failed;
  350. }
  351. DnsFileInfo.HostTime.wDay = (WORD) atoi(cp);
  352. cp = strpbrk(cp, " \t");
  353. if (cp == NULL) goto Failed;
  354. while( *cp == ' ' || *cp == '\t' ) cp++;
  355. if ((cp >= ep) || (*cp == '\0'))
  356. {
  357. NhTrace(
  358. TRACE_FLAG_DNS,
  359. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 5",
  360. DnsFileInfo.pHostName
  361. );
  362. goto Failed;
  363. }
  364. DnsFileInfo.HostTime.wHour = (WORD) atoi(cp);
  365. cp = strpbrk(cp, " \t");
  366. if (cp == NULL) goto Failed;
  367. while( *cp == ' ' || *cp == '\t' ) cp++;
  368. if ((cp >= ep) || (*cp == '\0'))
  369. {
  370. NhTrace(
  371. TRACE_FLAG_DNS,
  372. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 6",
  373. DnsFileInfo.pHostName
  374. );
  375. goto Failed;
  376. }
  377. DnsFileInfo.HostTime.wMinute = (WORD) atoi(cp);
  378. cp = strpbrk(cp, " \t");
  379. if (cp == NULL) goto Failed;
  380. while( *cp == ' ' || *cp == '\t' ) cp++;
  381. if ((cp >= ep) || (*cp == '\0'))
  382. {
  383. NhTrace(
  384. TRACE_FLAG_DNS,
  385. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 7",
  386. DnsFileInfo.pHostName
  387. );
  388. goto Failed;
  389. }
  390. DnsFileInfo.HostTime.wSecond = (WORD) atoi(cp);
  391. cp = strpbrk(cp, " \t");
  392. if (cp == NULL) goto Failed;
  393. while( *cp == ' ' || *cp == '\t' ) cp++;
  394. if ((cp >= ep) || (*cp == '\0'))
  395. {
  396. NhTrace(
  397. TRACE_FLAG_DNS,
  398. "GetHostFromHostsIcsFile: Error parsing host (%s) file timestamp @ 8",
  399. DnsFileInfo.pHostName
  400. );
  401. goto Failed;
  402. }
  403. DnsFileInfo.HostTime.wMilliseconds = (WORD) atoi(cp);
  404. //
  405. // successful entry read
  406. //
  407. /*
  408. NhTrace(
  409. TRACE_FLAG_DNS,
  410. "%s (%s) has timestamp: %04u-%02u-%02u %02u:%02u:%02u",
  411. DnsFileInfo.pHostName,
  412. inet_ntoa(*(PIN_ADDR)&DnsFileInfo.Ip4Address),
  413. DnsFileInfo.HostTime.wYear,
  414. DnsFileInfo.HostTime.wMonth,
  415. DnsFileInfo.HostTime.wDay,
  416. DnsFileInfo.HostTime.wHour,
  417. DnsFileInfo.HostTime.wMinute,
  418. DnsFileInfo.HostTime.wSecond
  419. );
  420. */
  421. return TRUE;
  422. Failed:
  423. // reset entries
  424. DnsFileInfo.HostLineBuf[0] = NULL;
  425. DnsFileInfo.pHostName = NULL;
  426. DnsFileInfo.Ip4Address = 0;
  427. ZeroMemory(&DnsFileInfo.HostTime, sizeof(SYSTEMTIME));
  428. return TRUE;
  429. } // GetHostFromHostsIcsFile
  430. VOID
  431. LoadHostsIcsFile(
  432. BOOL fStartup
  433. )
  434. /*++
  435. Routine Description:
  436. Read hosts file into our local cache.
  437. Arguments:
  438. fStartup set to TRUE if called on startup of protocol
  439. Globals:
  440. DnsFileInfo.HostFile -- host file ptr, tested and set then cleared
  441. DnsFileInfo.HostTime -- host timestamp
  442. DnsFileInfo.pHostName -- name ptr is read
  443. DnsFileInfo.Ip4Address -- IP4 address is read
  444. Return Value:
  445. None.
  446. --*/
  447. {
  448. register PCHAR * cp;
  449. FILETIME ftExpires;
  450. PWCHAR pszName;
  451. LPVOID lpMsgBuf;
  452. NhTrace(
  453. TRACE_FLAG_DNS,
  454. "LoadHostsIcsFile: Entering..."
  455. );
  456. ACQUIRE_LOCK(&DnsFileInfo);
  457. //
  458. // read entries from host file until exhausted
  459. //
  460. DnsSetHostsIcsFile(FALSE); // read only
  461. while (GetHostFromHostsIcsFile(fStartup))
  462. {
  463. if (DnsFileInfo.pHostName)
  464. {
  465. if (!SystemTimeToFileTime(&DnsFileInfo.HostTime, &ftExpires))
  466. {
  467. DWORD dwLastError = GetLastError();
  468. NhTrace(
  469. TRACE_FLAG_DNS,
  470. "LoadHostsIcsFile: SystemTimeToFileTime() failed"
  471. );
  472. lpMsgBuf = NULL;
  473. FormatMessage(
  474. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  475. FORMAT_MESSAGE_FROM_SYSTEM |
  476. FORMAT_MESSAGE_IGNORE_INSERTS,
  477. NULL,
  478. dwLastError,
  479. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  480. (LPTSTR) &lpMsgBuf,
  481. 0,
  482. NULL
  483. );
  484. NhTrace(
  485. TRACE_FLAG_DNS,
  486. "LoadHostsIcsFile: with message (0x%08x) %S",
  487. dwLastError,
  488. lpMsgBuf
  489. );
  490. if (lpMsgBuf) LocalFree(lpMsgBuf);
  491. // skip entry
  492. continue;
  493. }
  494. pszName = (PWCHAR) NH_ALLOCATE((strlen(DnsFileInfo.pHostName) + 1) * sizeof(WCHAR));
  495. mbstowcs(pszName, DnsFileInfo.pHostName, (strlen(DnsFileInfo.pHostName) + 1));
  496. NhTrace(
  497. TRACE_FLAG_DNS,
  498. "%S (%s) has timestamp: %04u-%02u-%02u %02u:%02u:%02u",
  499. pszName,
  500. inet_ntoa(*(PIN_ADDR)&DnsFileInfo.Ip4Address),
  501. DnsFileInfo.HostTime.wYear,
  502. DnsFileInfo.HostTime.wMonth,
  503. DnsFileInfo.HostTime.wDay,
  504. DnsFileInfo.HostTime.wHour,
  505. DnsFileInfo.HostTime.wMinute,
  506. DnsFileInfo.HostTime.wSecond
  507. );
  508. DnsAddAddressForName(
  509. pszName,
  510. DnsFileInfo.Ip4Address,
  511. ftExpires
  512. );
  513. NH_FREE(pszName);
  514. }
  515. }
  516. DnsEndHostsIcsFile();
  517. RELEASE_LOCK(&DnsFileInfo);
  518. //
  519. // now that we have everything in our table format,
  520. // write back a clean version to disk
  521. //
  522. SaveHostsIcsFile(FALSE);
  523. NhTrace(
  524. TRACE_FLAG_DNS,
  525. "LoadHostsIcsFile: ...Leaving."
  526. );
  527. } // LoadHostsIcsFile
  528. VOID
  529. SaveHostsIcsFile(
  530. BOOL fShutdown
  531. )
  532. /*++
  533. Routine Description:
  534. Write hosts file from our local cache.
  535. Arguments:
  536. fShutdown set to TRUE if called on shutdown of protocol
  537. Globals:
  538. DnsFileInfo.HostFile -- host file ptr, tested and set then cleared
  539. DnsFileInfo.HostTime -- host timestamp
  540. DnsFileInfo.pHostName -- name ptr is read
  541. DnsFileInfo.Ip4Address -- IP4 address is read
  542. Return Value:
  543. None.
  544. --*/
  545. {
  546. //DWORD dwSize = 0;
  547. //PWCHAR pszSuffix = NULL;
  548. UINT i;
  549. SYSTEMTIME stTime;
  550. LPVOID lpMsgBuf;
  551. NhTrace(
  552. TRACE_FLAG_DNS,
  553. "SaveHostsIcsFile: Entering..."
  554. );
  555. //
  556. // adding ourself as a special case
  557. //
  558. DnsAddSelf();
  559. //
  560. // get a copy of current ICS Domain suffix (used later on)
  561. // we dont play with it directly from the global copy
  562. // due to locking problems
  563. //
  564. /*
  565. EnterCriticalSection(&DnsGlobalInfoLock);
  566. if (DnsICSDomainSuffix)
  567. {
  568. dwSize = wcslen(DnsICSDomainSuffix) + 1;
  569. pszSuffix = reinterpret_cast<PWCHAR>(
  570. NH_ALLOCATE(sizeof(WCHAR) * dwSize)
  571. );
  572. if (!pszSuffix)
  573. {
  574. NhTrace(
  575. TRACE_FLAG_DNS,
  576. "SaveHostsIcsFile: allocation failed for "
  577. "DnsICSDomainSuffix copy"
  578. );
  579. }
  580. else
  581. {
  582. wcscpy(pszSuffix, DnsICSDomainSuffix);
  583. }
  584. }
  585. LeaveCriticalSection(&DnsGlobalInfoLock);
  586. */
  587. ACQUIRE_LOCK(&DnsFileInfo);
  588. //
  589. // write entries into host file
  590. //
  591. DnsSetHostsIcsFile(TRUE); // overwrite existing file if any
  592. if (DnsFileInfo.HostFile != NULL)
  593. {
  594. //
  595. // write default header string
  596. //
  597. if (fShutdown)
  598. {
  599. // add extra # at front
  600. fputc('#', DnsFileInfo.HostFile);
  601. }
  602. fputs(HOSTSICSFILE_HEADER, DnsFileInfo.HostFile);
  603. PDNS_ENTRY pDnsEntry;
  604. EnterCriticalSection(&DnsTableLock);
  605. pDnsEntry = (PDNS_ENTRY) RtlEnumerateGenericTable(&g_DnsTable, TRUE);
  606. while (pDnsEntry != NULL)
  607. {
  608. for (i = 0; i < pDnsEntry->cAddresses; i++)
  609. {
  610. //
  611. // dont add entries with invalid suffixes
  612. // (this could happen for example because the suffix
  613. // was changed in the registry)
  614. //
  615. //if (!IsSuffixValid(pDnsEntry->pszName, pszSuffix))
  616. //{
  617. // continue;
  618. //}
  619. //
  620. // dont add expired entries to the hosts.ics file
  621. //
  622. if (IsFileTimeExpired(&pDnsEntry->aAddressInfo[i].ftExpires))
  623. {
  624. continue;
  625. }
  626. if (!FileTimeToSystemTime(
  627. &pDnsEntry->aAddressInfo[i].ftExpires,
  628. &stTime))
  629. {
  630. NhTrace(
  631. TRACE_FLAG_DNS,
  632. "SaveHostsIcsFile: FileTimeToSystemTime() failed"
  633. );
  634. lpMsgBuf = NULL;
  635. FormatMessage(
  636. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  637. FORMAT_MESSAGE_FROM_SYSTEM |
  638. FORMAT_MESSAGE_IGNORE_INSERTS,
  639. NULL,
  640. GetLastError(),
  641. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  642. (LPTSTR) &lpMsgBuf,
  643. 0,
  644. NULL
  645. );
  646. NhTrace(
  647. TRACE_FLAG_DNS,
  648. "SaveHostsIcsFile: with message %S",
  649. lpMsgBuf
  650. );
  651. if (lpMsgBuf) LocalFree(lpMsgBuf);
  652. // skip entry
  653. continue;
  654. }
  655. if (fShutdown)
  656. {
  657. // add extra # at front
  658. fputc('#', DnsFileInfo.HostFile);
  659. }
  660. fprintf(
  661. DnsFileInfo.HostFile,
  662. "%s %S # %u %u %u %u %u %u %u %u\n",
  663. inet_ntoa(*(PIN_ADDR)&pDnsEntry->aAddressInfo[i].ulAddress),
  664. pDnsEntry->pszName,
  665. stTime.wYear,
  666. stTime.wMonth,
  667. stTime.wDayOfWeek,
  668. stTime.wDay,
  669. stTime.wHour,
  670. stTime.wMinute,
  671. stTime.wSecond,
  672. stTime.wMilliseconds
  673. );
  674. NhTrace(
  675. TRACE_FLAG_DNS,
  676. "adding entry: %s %S # %u %u %u %u %u %u %u %u\n",
  677. inet_ntoa(*(PIN_ADDR)&pDnsEntry->aAddressInfo[i].ulAddress),
  678. pDnsEntry->pszName,
  679. stTime.wYear,
  680. stTime.wMonth,
  681. stTime.wDayOfWeek,
  682. stTime.wDay,
  683. stTime.wHour,
  684. stTime.wMinute,
  685. stTime.wSecond,
  686. stTime.wMilliseconds
  687. );
  688. }
  689. pDnsEntry = (PDNS_ENTRY) RtlEnumerateGenericTable(&g_DnsTable, FALSE);
  690. }
  691. LeaveCriticalSection(&DnsTableLock);
  692. }
  693. DnsEndHostsIcsFile();
  694. RELEASE_LOCK(&DnsFileInfo);
  695. /*
  696. if (pszSuffix)
  697. {
  698. NH_FREE(pszSuffix);
  699. pszSuffix = NULL;
  700. }
  701. */
  702. NhTrace(
  703. TRACE_FLAG_DNS,
  704. "SaveHostsIcsFile: ...Leaving."
  705. );
  706. } // SaveHostsIcsFile
  707. BOOL
  708. IsFileTimeExpired(
  709. FILETIME *ftTime
  710. )
  711. {
  712. ULARGE_INTEGER uliTime, uliNow;
  713. FILETIME ftNow;
  714. GetSystemTimeAsFileTime(&ftNow);
  715. memcpy(&uliNow, &ftNow, sizeof(ULARGE_INTEGER));
  716. memcpy(&uliTime, ftTime, sizeof(ULARGE_INTEGER));
  717. return (uliTime.QuadPart < uliNow.QuadPart);
  718. } // IsFileTimeExpired
  719. BOOL
  720. IsSuffixValid(
  721. WCHAR *pszName,
  722. WCHAR *pszSuffix
  723. )
  724. /*++
  725. Routine Description:
  726. This routine is invoked to compare suffix on the end of the name
  727. with what the DNS component thinks of as the current suffix.
  728. Arguments:
  729. none.
  730. Return Value:
  731. TRUE or FALSE.
  732. Environment:
  733. Invoked in an arbitrary context.
  734. --*/
  735. {
  736. BOOL ret;
  737. PWCHAR start = pszName;
  738. size_t lenName = 0,
  739. lenSuffix = 0;
  740. if (!start)
  741. {
  742. return FALSE;
  743. }
  744. lenName = wcslen(pszName);
  745. lenSuffix = wcslen(pszSuffix);
  746. if (!lenName || !lenSuffix)
  747. {
  748. return FALSE;
  749. }
  750. if (lenName < lenSuffix)
  751. {
  752. return FALSE;
  753. }
  754. lenName -= lenSuffix;
  755. while (lenName--) start++;
  756. ret = _wcsicmp(start, pszSuffix);
  757. return (!ret);
  758. } // IsSuffixValid
  759. //
  760. // End dnsfile.c
  761. //