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.

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