Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

861 lines
16 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. local.h
  5. Abstract:
  6. DNS Resolver.
  7. DNS Resolver service local include file.
  8. Author:
  9. Jim Gilroy (jamesg) March 2000
  10. Revision History:
  11. --*/
  12. #ifndef _LOCAL_INCLUDED_
  13. #define _LOCAL_INCLUDED_
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #ifndef _WINSOCK2API_
  20. #include <winsock2.h>
  21. #endif
  22. #include <sockreg.h>
  23. #include <windns.h>
  24. #include <netevent.h> // DNS events
  25. #define DNS_NEW_DEFS 1 // QFE builds use new definitions
  26. #include "dnsrslvr.h"
  27. #include <dnslibp.h>
  28. #include "registry.h"
  29. #include "dnsapip.h"
  30. #define ENABLE_DEBUG_LOGGING 1
  31. #include "logit.h"
  32. #include "dnsmsg.h"
  33. #include "heapdbg.h"
  34. //
  35. // Resolver debug flags
  36. //
  37. #define DNS_DBG_CACHE DNS_DBG_QUERY
  38. //
  39. // Cache definitions
  40. //
  41. #define NO_LOCK 0
  42. #define LOCKED 1
  43. #define ONE_YEAR_IN_SECONDS 60*60*24*365
  44. //
  45. // Bypassing cache completely check
  46. //
  47. #define COMBINED_BYPASS_CACHE_FLAG (DNS_QUERY_BYPASS_CACHE | DNS_QUERY_NO_HOSTS_FILE)
  48. #define SKIP_CACHE_LOOKUP(Flags) \
  49. ( ((Flags) & COMBINED_BYPASS_CACHE_FLAG) == COMBINED_BYPASS_CACHE_FLAG )
  50. //
  51. // Cache defintion
  52. //
  53. // Should be private to cache but is currently
  54. // exposed in enum routines
  55. //
  56. typedef struct _CacheEntry
  57. {
  58. struct _CacheEntry * pNext;
  59. PWSTR pName;
  60. DWORD Reserved;
  61. DWORD MaxCount;
  62. PDNS_RECORD Records[ 1 ];
  63. }
  64. CACHE_ENTRY, *PCACHE_ENTRY;
  65. //
  66. // Socket context
  67. //
  68. typedef struct _SocketContext
  69. {
  70. struct _SocketContext * pNext;
  71. SOCKET Socket;
  72. IP_ADDRESS IpAddress;
  73. OVERLAPPED Overlapped;
  74. PDNS_MSG_BUF pMsg;
  75. }
  76. SOCKET_CONTEXT, * PSOCKET_CONTEXT;
  77. #if 1
  78. //
  79. // Config
  80. //
  81. // Saved NT5 registry stuff.
  82. // In typical fashsion this was packed away in a few different places.
  83. // Keeping around until all of this is properly put to bed.
  84. //
  85. //
  86. // Registry value routine prototypes
  87. //
  88. #define DNS_DEFAULT_HASH_TABLE_SIZE 211 // A prime number
  89. #define DNS_DEFAULT_NEGATIVE_SOA_CACHE_TIME 10 // 10 seconds
  90. #define DNS_DEFAULT_NET_FAILURE_CACHE_TIME 30 // 30 seconds
  91. #define DNS_DEFAULT_ADAPTER_TIMEOUT_CACHE_TIME 2*60 // 2 minutes
  92. #define DNS_DEFAULT_MESSAGE_POPUP_LIMIT 0 // Don't allow!
  93. #define DNS_DEFAULT_MAX_SOA_TTL_LIMIT 5*60 // 5 minutes
  94. #define DNS_DEFAULT_RESET_SERVER_PRIORITIES_TIME 5*60 // 5 minutes
  95. #endif
  96. //
  97. // Event tags
  98. // Make recognizable DWORD tags for in memory log.
  99. //
  100. #define FOUR_CHARACTER_CONSTANT(a,b,c,d) \
  101. ((((DWORD)d) << 24) + (((DWORD)c) << 16) + (((DWORD)b) << 8) + ((DWORD)a))
  102. #define RES_EVENT_INITNET_START FOUR_CHARACTER_CONSTANT('N','e','t','+')
  103. #define RES_EVENT_INITNET_END FOUR_CHARACTER_CONSTANT('N','e','t','-')
  104. #define RES_EVENT_INITCACHE FOUR_CHARACTER_CONSTANT('C','a','c','+')
  105. #define RES_EVENT_REGISTER_SCH FOUR_CHARACTER_CONSTANT('S','c','h','+')
  106. #define RES_EVENT_CREATEEVENT FOUR_CHARACTER_CONSTANT('E','v','t','+')
  107. #define RES_EVENT_STARTRPC FOUR_CHARACTER_CONSTANT('R','p','c','+')
  108. #define RES_EVENT_STOPRPC FOUR_CHARACTER_CONSTANT('R','p','c','-')
  109. #define RES_EVENT_STATUS FOUR_CHARACTER_CONSTANT('s','t','a','t')
  110. #define RES_EVENT_SCH FOUR_CHARACTER_CONSTANT('S','c','h',' ')
  111. #define RES_EVENT_UPDATE_STATUS FOUR_CHARACTER_CONSTANT('U','p','d',' ')
  112. #define RES_EVENT_INITCRIT_START FOUR_CHARACTER_CONSTANT('I','C','S','+')
  113. #define RES_EVENT_INITCRIT_END FOUR_CHARACTER_CONSTANT('I','C','S','-')
  114. #define RES_EVENT_DELCRIT_START FOUR_CHARACTER_CONSTANT('D','C','S','+')
  115. #define RES_EVENT_DELCRIT_END FOUR_CHARACTER_CONSTANT('D','C','S','-')
  116. #define RES_EVENT_SHUTDOWN FOUR_CHARACTER_CONSTANT('S','h','u','t')
  117. #define RES_EVENT_FLUSH_CACHE FOUR_CHARACTER_CONSTANT('F','l','s','h')
  118. #define RES_EVENT_PNP_START FOUR_CHARACTER_CONSTANT('P','n','P','+')
  119. #define RES_EVENT_PNP_END FOUR_CHARACTER_CONSTANT('P','n','P','-')
  120. //
  121. // Service
  122. //
  123. extern HANDLE g_hStopEvent;
  124. extern BOOL g_StopFlag;
  125. extern BOOL g_WakeFlag;
  126. extern BOOL g_GarbageCollectFlag;
  127. extern BOOL g_LogTraceInfo;
  128. //
  129. // Config (config.c)
  130. //
  131. extern DWORD g_MaxSOACacheEntryTtlLimit;
  132. extern DWORD g_NegativeSOACacheTime;
  133. extern DWORD g_MessagePopupLimit;
  134. extern DWORD g_NetFailureCacheTime;
  135. //
  136. // Config info (config.c)
  137. //
  138. extern PDNS_NETINFO g_NetworkInfo;
  139. extern DWORD g_TimeOfLastPnPUpdate;
  140. //
  141. // Cache (cache.c)
  142. //
  143. extern PCACHE_ENTRY * g_HashTable;
  144. extern DWORD g_HashTableSize;
  145. extern DWORD g_EntryCount;
  146. extern DWORD g_RecordSetCount;
  147. //
  148. // Local IP info (ip.c)
  149. //
  150. extern PDNS_ADDR_ARRAY g_LocalAddrArray;
  151. //
  152. // Network failure caching
  153. //
  154. extern DWORD g_NetFailureTime;
  155. extern DNS_STATUS g_NetFailureStatus;
  156. extern DWORD g_TimedOutAdapterTime;
  157. extern DWORD g_ResetServerPrioritiesTime;
  158. extern BOOL g_fTimedOutAdapter;
  159. extern DNS_STATUS g_PreviousNetFailureStatus;
  160. extern DWORD g_MessagePopupStrikes;
  161. extern DWORD g_NumberOfMessagePopups;
  162. //
  163. // Multicast globals (mcast.c)
  164. //
  165. extern HANDLE g_hMulticastThread;
  166. extern BOOL g_MulticastStop;
  167. //
  168. // Locking
  169. //
  170. extern CRITICAL_SECTION CacheCritSec;
  171. extern CRITICAL_SECTION NetworkListCritSec;
  172. extern CRITICAL_SECTION NetworkFailureCritSec;
  173. #define LOCK_CACHE() Cache_Lock( 0 )
  174. #define LOCK_CACHE_NO_START() Cache_Lock( 1 )
  175. #define UNLOCK_CACHE() Cache_Unlock()
  176. #define LOCK_NET_LIST() EnterCriticalSection( &NetworkListCritSec )
  177. #define UNLOCK_NET_LIST() LeaveCriticalSection( &NetworkListCritSec )
  178. #define LOCK_NET_FAILURE() EnterCriticalSection( &NetworkFailureCritSec )
  179. #define UNLOCK_NET_FAILURE() LeaveCriticalSection( &NetworkFailureCritSec )
  180. //
  181. // Cache flush levels
  182. //
  183. // Note, these aren't bit flags, just made them that way for
  184. // easy reading.
  185. //
  186. #define FLUSH_LEVEL_NORMAL (0)
  187. #define FLUSH_LEVEL_INVALID (1)
  188. #define FLUSH_LEVEL_WIRE (2)
  189. #define FLUSH_LEVEL_STRONG (4)
  190. #define FLUSH_LEVEL_CLEANUP (8)
  191. #define FLUSH_LEVEL_GARBAGE (FLUSH_LEVEL_WIRE)
  192. //
  193. // Cache memory
  194. //
  195. // Note, heap global doesn't need exposure if functionalize
  196. //
  197. extern HANDLE g_CacheHeap;
  198. #define CACHE_HEAP_ALLOC_ZERO(size) \
  199. HeapAlloc( g_CacheHeap, HEAP_ZERO_MEMORY, (size) )
  200. #define CACHE_HEAP_ALLOC(size) \
  201. HeapAlloc( g_CacheHeap, 0, (size) )
  202. #define CACHE_HEAP_FREE(p) \
  203. HeapFree( g_CacheHeap, 0, (p) )
  204. //
  205. // Record and RPC memory:
  206. //
  207. // Note: most records are created by dnsapi heap -- from
  208. // query or hosts file routines. However, we do create
  209. // name error caching records ourselves using dnslib routines.
  210. //
  211. // This means -- until we either
  212. // - extend query or dnslib record creation interfaces to
  213. // include heap parameter
  214. // - explicitly free and recreate
  215. // - tag records (dnsapi\not) somehow (flags field)
  216. // that
  217. // dnsapi and dnslib heaps MUST be the same.
  218. // With dnsapi now potentially having it's own heap, this means
  219. // dnslib should use dnsapi heap.
  220. //
  221. // So we'll put off using the debug heap for dnslib.
  222. //
  223. //
  224. // Resolver allocators
  225. //
  226. PVOID
  227. Res_Alloc(
  228. IN DWORD Length,
  229. IN DWORD Tag,
  230. IN PSTR pszFile,
  231. IN DWORD LineNo
  232. );
  233. PVOID
  234. Res_AllocZero(
  235. IN DWORD Length,
  236. IN DWORD Tag,
  237. IN PSTR pszFile,
  238. IN DWORD LineNo
  239. );
  240. VOID
  241. Res_Free(
  242. IN OUT PVOID pMemory,
  243. IN DWORD Tag
  244. );
  245. #define RESHEAP_TAG_GENERAL 0
  246. #define RESHEAP_TAG_RECORD 1
  247. #define RESHEAP_TAG_RPC 2
  248. #define RESHEAP_TAG_MCAST 3
  249. //
  250. // General memory
  251. //
  252. #define GENERAL_HEAP_ALLOC(Size) \
  253. Res_Alloc( \
  254. Size, \
  255. RESHEAP_TAG_GENERAL, \
  256. __FILE__, \
  257. __LINE__ )
  258. #define GENERAL_HEAP_ALLOC_ZERO(Size) \
  259. Res_AllocZero( \
  260. Size, \
  261. RESHEAP_TAG_GENERAL, \
  262. __FILE__, \
  263. __LINE__ )
  264. #define GENERAL_HEAP_FREE(pMem) \
  265. Res_Free( \
  266. pMem, \
  267. RESHEAP_TAG_GENERAL )
  268. //
  269. // RPC allocs
  270. //
  271. #define RPC_HEAP_ALLOC(Size) \
  272. Res_Alloc( \
  273. Size, \
  274. RESHEAP_TAG_RPC, \
  275. __FILE__, \
  276. __LINE__ )
  277. #define RPC_HEAP_ALLOC_ZERO(Size) \
  278. Res_AllocZero( \
  279. Size, \
  280. RESHEAP_TAG_RPC, \
  281. __FILE__, \
  282. __LINE__ )
  283. #define RPC_HEAP_FREE(pMem) \
  284. Res_Free( \
  285. pMem, \
  286. RESHEAP_TAG_RPC )
  287. //
  288. // Record heap routines
  289. //
  290. #define RECORD_HEAP_ALLOC(Size) \
  291. Res_Alloc( \
  292. Size, \
  293. RESHEAP_TAG_RECORD, \
  294. __FILE__, \
  295. __LINE__ )
  296. #define RECORD_HEAP_ALLOC_ZERO(Size) \
  297. Res_AllocZero( \
  298. Size, \
  299. RESHEAP_TAG_RECORD, \
  300. __FILE__, \
  301. __LINE__ )
  302. #define RECORD_HEAP_FREE(pMem) \
  303. Res_Free( \
  304. pMem, \
  305. RESHEAP_TAG_RECORD )
  306. //
  307. // Mcast heap routines
  308. //
  309. #define MCAST_HEAP_ALLOC(Size) \
  310. Res_Alloc( \
  311. Size, \
  312. RESHEAP_TAG_MCAST, \
  313. __FILE__, \
  314. __LINE__ )
  315. #define MCAST_HEAP_ALLOC_ZERO(Size) \
  316. Res_AllocZero( \
  317. Size, \
  318. RESHEAP_TAG_MCAST, \
  319. __FILE__, \
  320. __LINE__ )
  321. #define MCAST_HEAP_FREE(pMem) \
  322. Res_Free( \
  323. pMem, \
  324. RESHEAP_TAG_MCAST )
  325. //
  326. // RECORD Access control
  327. //
  328. BOOL
  329. ClientThreadNotAllowedAccess(
  330. VOID
  331. );
  332. #if 0
  333. //
  334. // Old cache routines (cache.c)
  335. //
  336. VOID
  337. FlushCacheEntry(
  338. IN PWSTR pName
  339. );
  340. VOID
  341. FlushCacheEntryRecord(
  342. IN PWSTR pName,
  343. IN WORD Type
  344. );
  345. VOID
  346. TrimCache(
  347. VOID
  348. );
  349. VOID
  350. TrimCacheBucket(
  351. IN ULONG ulIndex,
  352. IN DWORD dwBucketSize,
  353. IN BOOL fSkipFirst
  354. );
  355. VOID
  356. ResizeCacheBucket(
  357. IN ULONG ulIndex,
  358. IN PDWORD pdwBucketSize
  359. );
  360. PCACHE_ENTRY
  361. CreateCacheEntry(
  362. IN PWSTR pName,
  363. IN BOOL fLoadingHostsFile
  364. );
  365. VOID
  366. FreeCacheEntry(
  367. IN OUT PCACHE_ENTRY pCacheEntry
  368. );
  369. VOID
  370. AddRecordToCacheEntry(
  371. IN OUT PCACHE_ENTRY pCacheEntry,
  372. IN PDNS_RECORD pRecordList,
  373. IN BOOL fFlushExisting,
  374. IN BOOL fLoadingHostsFile
  375. );
  376. VOID
  377. CacheAnyAdditionalRecords(
  378. IN OUT PDNS_RECORD pRecordList,
  379. IN BOOL fHostFile
  380. );
  381. PCACHE_ENTRY
  382. FindCacheEntry(
  383. IN PWSTR pName
  384. );
  385. PCACHE_ENTRY
  386. FindOrCreateCacheEntry(
  387. IN PWSTR pName,
  388. IN BOOL fHostsFile
  389. );
  390. PDNS_RECORD
  391. FindCacheEntryRecord(
  392. IN PCACHE_ENTRY pEntry,
  393. IN WORD Type
  394. );
  395. PDNS_RECORD
  396. FindCacheRecords(
  397. IN PWSTR pwsName,
  398. IN WORD wType
  399. );
  400. #define PrepareRecordSetForRpc(prr) Cache_RestoreRecordListForRpc(prr)
  401. #define PrepareRecordSetForCache(prr) Cache_PrepareRecordList(prr)
  402. #endif
  403. //
  404. // Cache routines (ncache.c)
  405. //
  406. VOID
  407. Cache_Lock(
  408. IN BOOL fNoStart
  409. );
  410. VOID
  411. Cache_Unlock(
  412. VOID
  413. );
  414. DNS_STATUS
  415. Cache_Initialize(
  416. VOID
  417. );
  418. DNS_STATUS
  419. Cache_Shutdown(
  420. VOID
  421. );
  422. DNS_STATUS
  423. Cache_Flush(
  424. VOID
  425. );
  426. VOID
  427. Cache_FlushRecords(
  428. IN PWSTR pName,
  429. IN DWORD FlushLevel,
  430. IN WORD Type
  431. );
  432. BOOL
  433. Cache_IsRecordTtlValid(
  434. IN PDNS_RECORD pRecord
  435. );
  436. //
  437. // Cache operations routines (cache.c)
  438. //
  439. BOOL
  440. Cache_ReadResults(
  441. OUT PDNS_RESULTS pResults,
  442. IN PWSTR pwsName,
  443. IN WORD wType
  444. );
  445. VOID
  446. Cache_PrepareRecordList(
  447. IN OUT PDNS_RECORD pRecordList
  448. );
  449. VOID
  450. Cache_RestoreRecordListForRpc(
  451. IN OUT PDNS_RECORD pRecordList
  452. );
  453. VOID
  454. Cache_RecordList(
  455. IN OUT PDNS_RECORD pRecordList
  456. );
  457. VOID
  458. Cache_RecordSetAtomic(
  459. IN PWSTR pwsName,
  460. IN WORD wType,
  461. IN PDNS_RECORD pRecordSet
  462. );
  463. VOID
  464. Cache_GarbageCollect(
  465. IN DWORD Flag
  466. );
  467. DNS_STATUS
  468. Cache_QueryResponse(
  469. IN OUT PQUERY_BLOB pBlob
  470. );
  471. BOOL
  472. Cache_GetRecordsForRpc(
  473. OUT PDNS_RECORD * ppRecordList,
  474. OUT PDNS_STATUS pStatus,
  475. IN PWSTR pwsName,
  476. IN WORD wType,
  477. IN DWORD Flags
  478. );
  479. //
  480. // Multicast routines (mcast.c)
  481. //
  482. VOID
  483. MulticastThread(
  484. VOID
  485. );
  486. VOID
  487. MulticastThreadSignalStop(
  488. VOID
  489. );
  490. //
  491. // Host file routines (notify.c)
  492. //
  493. VOID
  494. InitCacheWithHostFile(
  495. VOID
  496. );
  497. //
  498. // Notification (notify.c)
  499. //
  500. VOID
  501. ThreadShutdownWait(
  502. IN HANDLE hThread
  503. );
  504. HANDLE
  505. CreateHostsFileChangeHandle(
  506. VOID
  507. );
  508. VOID
  509. NotifyThread(
  510. VOID
  511. );
  512. VOID
  513. StartNotify(
  514. VOID
  515. );
  516. VOID
  517. ShutdownNotify(
  518. VOID
  519. );
  520. //
  521. // Config -- Network info (config.c)
  522. //
  523. VOID
  524. UpdateNetworkInfo(
  525. IN OUT PDNS_NETINFO pNetworkInfo
  526. );
  527. PDNS_NETINFO
  528. GrabNetworkInfo(
  529. VOID
  530. );
  531. VOID
  532. ZeroNetworkConfigGlobals(
  533. VOID
  534. );
  535. VOID
  536. CleanupNetworkInfo(
  537. VOID
  538. );
  539. BOOL
  540. IsKnownNetFailure(
  541. VOID
  542. );
  543. VOID
  544. SetKnownNetFailure(
  545. IN DNS_STATUS Status
  546. );
  547. VOID
  548. ReadRegistryConfig(
  549. VOID
  550. );
  551. VOID
  552. HandleConfigChange(
  553. IN PSTR pszReason,
  554. IN BOOL fCache_Flush
  555. );
  556. //
  557. // Net config (still remote.c)
  558. //
  559. #define THREE_MINUTES_FROM_SYSTEM_BOOT 180
  560. #define MAX_DNS_NOTIFICATION_LIST_SIZE 1000
  561. #define PNP_REFRESH_UPDATE_WINDOW 60
  562. BOOL
  563. IsKnownTimedOutAdapter(
  564. VOID
  565. );
  566. BOOL
  567. IsTimeToResetServerPriorities(
  568. VOID
  569. );
  570. //
  571. // Service notification (notesrv.c)
  572. //
  573. VOID
  574. SendServiceNotifications(
  575. VOID
  576. );
  577. VOID
  578. CleanupServiceNotification(
  579. VOID
  580. );
  581. //
  582. // In memory logging (memlog.c)
  583. //
  584. VOID
  585. LogEventInMemory(
  586. IN DWORD Checkpoint,
  587. IN DWORD Data
  588. );
  589. //
  590. // Event logging (dnsrslvr.c)
  591. //
  592. VOID
  593. ResolverLogEvent (
  594. IN DWORD MessageId,
  595. IN WORD EventType,
  596. IN DWORD StringCount,
  597. IN PWSTR * StringArray,
  598. IN DWORD ErrorCode
  599. );
  600. //
  601. // IP list and notification (ip.c)
  602. //
  603. DNS_STATUS
  604. IpNotifyThread(
  605. IN LPVOID pvDummy
  606. );
  607. VOID
  608. ZeroInitIpListGlobals(
  609. VOID
  610. );
  611. DNS_STATUS
  612. InitIpListAndNotification(
  613. VOID
  614. );
  615. VOID
  616. ShutdownIpListAndNotify(
  617. VOID
  618. );
  619. VOID
  620. ClearLocalAddrArray(
  621. VOID
  622. );
  623. PDNS_ADDR_ARRAY
  624. GetLocalAddrArray(
  625. VOID
  626. );
  627. DWORD
  628. RemoveClusterIpFromAddrArray(
  629. IN OUT PDNS_ADDR_ARRAY pAddrArray
  630. );
  631. PDNS_ADDR_ARRAY
  632. GetLocalAddrArrayFiltered(
  633. VOID
  634. );
  635. BOOL
  636. IsClusterAddress(
  637. IN OUT PQUERY_BLOB pBlob,
  638. IN PIP_UNION pIpUnion
  639. );
  640. //
  641. // Resolver log (logit.c)
  642. //
  643. // Special type routines for resolver logging.
  644. // General log open\print routines defined in logit.h.
  645. //
  646. VOID
  647. PrintNetworkInfoToLog(
  648. IN PDNS_NETINFO pNetworkInfo
  649. );
  650. VOID
  651. PrintIpAddressListToLog (
  652. IN PDNS_ADDRESS_INFO AddressInfoList,
  653. IN DWORD AddressInfoListCount
  654. );
  655. //
  656. // RPC server (rpc.c)
  657. //
  658. DNS_STATUS
  659. Rpc_Initialize(
  660. VOID
  661. );
  662. VOID
  663. Rpc_Shutdown(
  664. VOID
  665. );
  666. #endif // _LOCAL_INCLUDED_