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.

700 lines
13 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. local.h
  5. Abstract:
  6. Domain Name System (DNS) API
  7. Dns API local include file
  8. Author:
  9. Jim Gilroy (jamesg) May 1997
  10. Revision History:
  11. --*/
  12. #ifndef _DNSAPILOCAL_INCLUDED_
  13. #define _DNSAPILOCAL_INCLUDED_
  14. // should just build as unicode
  15. //#define UNICODE 1
  16. #include <nt.h> // build for Win95 compatibility
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. #include <windows.h>
  20. // headers are messed up
  21. // neither ntdef.h nor winnt.h brings in complete set, so depending
  22. // on whether you include nt.h or not you end up with different set
  23. #define MINCHAR 0x80
  24. #define MAXCHAR 0x7f
  25. #define MINSHORT 0x8000
  26. #define MAXSHORT 0x7fff
  27. #define MINLONG 0x80000000
  28. #define MAXLONG 0x7fffffff
  29. #define MAXBYTE 0xff
  30. #define MAXUCHAR 0xff
  31. #define MAXWORD 0xffff
  32. #define MAXUSHORT 0xffff
  33. #define MAXDWORD 0xffffffff
  34. #define MAXULONG 0xffffffff
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #include <basetyps.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <limits.h>
  41. #include <align.h> // Alignment macros
  42. #include <windns.h> // SDK DNS definitions
  43. #define DNSAPI_INTERNAL 1
  44. #include <dnsapi.h>
  45. #include "dnsrslvr.h" // Resolver RPC definitions
  46. #include <rpcasync.h> // Exception filter
  47. #include "dnslibp.h" // DNS library
  48. #include "registry.h"
  49. #include "message.h" // dnslib message def
  50. //#include "dnsrslvr.h" // Resolver RPC definitions
  51. #include "dnsapip.h" // Private DNS definitions
  52. #include "queue.h"
  53. #include "rtlstuff.h" // Handy macros from NT RTL
  54. #include "trace.h"
  55. #include "heapdbg.h" // dnslib debug heap
  56. //
  57. // Build sanity check
  58. // - we still have a few private structs that need RPC
  59. // exposed in dnslib.h; make sure they are in sync
  60. //
  61. C_ASSERT( sizeof(DNSLIB_SEARCH_NAME) == sizeof(RPC_SEARCH_NAME) );
  62. C_ASSERT( sizeof(DNSLIB_SEARCH_LIST) == sizeof(RPC_SEARCH_LIST) );
  63. C_ASSERT( sizeof(DNSLIB_SERVER_INFO) == sizeof(RPC_DNS_SERVER_INFO) );
  64. C_ASSERT( sizeof(DNSLIB_ADAPTER) == sizeof(RPC_DNS_ADAPTER) );
  65. C_ASSERT( sizeof(DNSLIB_NETINFO) == sizeof(RPC_DNS_NETINFO) );
  66. //
  67. // Use winsock2
  68. //
  69. #define DNS_WINSOCK_VERSION (0x0202) // Winsock 2.2
  70. //
  71. // Dll instance handle
  72. //
  73. extern HINSTANCE g_hInstanceDll;
  74. //
  75. // General CS
  76. // protects initialization and available for other random needs
  77. //
  78. CRITICAL_SECTION g_GeneralCS;
  79. #define LOCK_GENERAL() EnterCriticalSection( &g_GeneralCS )
  80. #define UNLOCK_GENERAL() LeaveCriticalSection( &g_GeneralCS )
  81. //
  82. // Init Levels
  83. //
  84. #define INITLEVEL_ZERO (0)
  85. #define INITLEVEL_BASE (0x00000001)
  86. #define INITLEVEL_DEBUG (0x00000010)
  87. #define INITLEVEL_QUERY (0x00000100)
  88. #define INITLEVEL_REGISTRATION (0x00001000)
  89. #define INITLEVEL_SECURE_UPDATE (0x00010000)
  90. // Combined
  91. #define INITLEVEL_ALL (0xffffffff)
  92. //
  93. // Limit on update adapters
  94. //
  95. #define UPDATE_ADAPTER_LIMIT 100
  96. //
  97. // Network info defs
  98. //
  99. // Empty (not-set) ServerIndex field in adapter info
  100. #define EMPTY_SERVER_INDEX ((DWORD)(-1))
  101. //
  102. // Event logging
  103. // - currently set to disable in any code we pick up from server
  104. //
  105. VOID
  106. DnsLogEvent (
  107. DWORD MessageId,
  108. WORD EventType,
  109. DWORD NumberOfSubStrings,
  110. LPSTR *SubStrings,
  111. DWORD ErrorCode );
  112. #define DNS_LOG_EVENT(a,b,c,d)
  113. //
  114. // Debug
  115. //
  116. #ifdef ASSERT
  117. #undef ASSERT
  118. #endif
  119. #define ASSERT(a) DNS_ASSERT(a)
  120. // standard -- unflagged ASSERT()
  121. // - defintion directly from ntrtl.h
  122. // this should have been plain vanilla ASSERT(), but
  123. // it is used too often
  124. #if DBG
  125. #define RTL_ASSERT(exp) \
  126. ((!(exp)) ? \
  127. (RtlAssert( #exp, __FILE__, __LINE__, NULL ),FALSE) : \
  128. TRUE)
  129. #else
  130. #define RTL_ASSERT(exp)
  131. #endif
  132. //
  133. // Single async socket for internal use
  134. //
  135. // If want async socket i/o then can create single async socket, with
  136. // corresponding event and always use it. Requires winsock 2.2
  137. //
  138. extern SOCKET DnsSocket;
  139. extern OVERLAPPED DnsSocketOverlapped;
  140. extern HANDLE hDnsSocketEvent;
  141. //
  142. // App shutdown flag
  143. //
  144. extern BOOLEAN fApplicationShutdown;
  145. //
  146. // Global config -- From DnsLib
  147. // -- set in DnsRegInit()
  148. // OR in DnsReadRegistryGlobals()
  149. // -- declaration in registry.h
  150. //
  151. //
  152. // Runtime globals (dnsapi.c)
  153. //
  154. extern DWORD g_NetFailureTime;
  155. extern DNS_STATUS g_NetFailureStatus;
  156. extern IP_ADDRESS g_LastDNSServerUpdated;
  157. //
  158. // Global critical sections
  159. //
  160. extern CRITICAL_SECTION g_RegistrationListCS;
  161. extern CRITICAL_SECTION g_RegistrationThreadCS;
  162. extern CRITICAL_SECTION g_QueueCS;
  163. extern CRITICAL_SECTION g_NetFailureCS;
  164. //
  165. // Heap operations
  166. //
  167. #define ALLOCATE_HEAP(size) Dns_AllocZero( size )
  168. #define ALLOCATE_HEAP_ZERO(size) Dns_AllocZero( size )
  169. #define REALLOCATE_HEAP(p,size) Dns_Realloc( (p), (size) )
  170. #define FREE_HEAP(p) Dns_Free( p )
  171. //
  172. // Winsock stop\stop
  173. //
  174. DNS_STATUS
  175. Dnsp_InitializeWinsock(
  176. VOID
  177. );
  178. VOID
  179. Dnsp_CleanupWinsock(
  180. VOID
  181. );
  182. //
  183. // RPC Exception filters
  184. //
  185. #if 0
  186. #define DNS_RPC_EXCEPTION_FILTER \
  187. (((RpcExceptionCode() != STATUS_ACCESS_VIOLATION) && \
  188. (RpcExceptionCode() != STATUS_DATATYPE_MISALIGNMENT) && \
  189. (RpcExceptionCode() != STATUS_PRIVILEGED_INSTRUCTION) && \
  190. (RpcExceptionCode() != STATUS_ILLEGAL_INSTRUCTION)) \
  191. ? 0x1 : EXCEPTION_CONTINUE_SEARCH )
  192. #endif
  193. #define DNS_RPC_EXCEPTION_FILTER I_RpcExceptionFilter( RpcExceptionCode() )
  194. //
  195. // During setup need to cleanup after winsock
  196. //
  197. #define GUI_MODE_SETUP_WS_CLEANUP( _mode ) \
  198. { \
  199. if ( _mode ) \
  200. { \
  201. Dns_CleanupWinsock(); \
  202. } \
  203. }
  204. //
  205. // Dummy server status codes (server states)
  206. //
  207. // Note: how this works
  208. // - these are BIT flags, but designed to be distinguishabled
  209. // from any real status code by the high 0xf
  210. // - they are OR'd together as send\timeout state progresses
  211. // - do NOT test equality for any of these but NEW
  212. // - test for state by traditional (flag&state == state) method
  213. //
  214. // The alternative here is to add a separate "state" field along
  215. // with status to each server. Since real status is easy to
  216. // distinguish, this seems acceptable.
  217. //
  218. #define DNSSS_NEW (0xf0000000)
  219. #define DNSSS_SENT (0xf1000000)
  220. #define DNSSS_SENT_OPT (0xf1100000)
  221. #define DNSSS_TIMEOUT_OPT (0xf1300000)
  222. #define DNSSS_SENT_NON_OPT (0xf1010000)
  223. #define DNSSS_TIMEOUT_NON_OPT (0xf1030000)
  224. #define TEST_DNSSS_NEW(status) ((status) == DNSSS_NEW)
  225. #define TEST_SERVER_STATUS_NEW(pserver) ((pserver)->Status == DNSSS_NEW)
  226. #define TEST_DNSSS_STATUS(status, state) (((status) & (state)) == (state))
  227. #define TEST_SERVER_STATUS(pserver, state) (((pserver)->Status & (state)) == (state))
  228. #define SET_SERVER_STATUS(pserver, state) ((pserver)->Status |= state)
  229. #define TEST_DNSSS_VALID_RECV(status) ((LONG)status >= 0 )
  230. #define TEST_SERVER_VALID_RECV(status) ((LONG)(pserver)->Status >= 0 )
  231. //
  232. // Local Prototypes
  233. //
  234. // Routines shared between dnsapi.dll modules, but not exported
  235. //
  236. // Note, i've included some other functions in here because the external
  237. // definition seems help "encourage" the creation of symbols in retail
  238. // builds
  239. //
  240. //
  241. // Config stuff
  242. //
  243. BOOL
  244. DnsApiInit(
  245. IN DWORD InitLevel
  246. );
  247. DWORD
  248. Reg_ReadRegistryGlobal(
  249. IN DNS_REGID GlobalId
  250. );
  251. //
  252. // Query (query.c)
  253. //
  254. DNS_STATUS
  255. DoQuickQueryEx(
  256. IN OUT PDNS_MSG_BUF *,
  257. OUT PDNS_RECORD *,
  258. IN LPSTR,
  259. IN WORD,
  260. IN DWORD,
  261. IN PDNS_NETINFO,
  262. IN BOOL
  263. );
  264. BOOL
  265. IsEmptyDnsResponse(
  266. IN PDNS_RECORD pRecordList
  267. );
  268. BOOL
  269. ValidateQueryTld(
  270. IN PSTR pTld
  271. );
  272. BOOL
  273. ValidateQueryName(
  274. IN PQUERY_BLOB pBlob,
  275. IN PSTR pName,
  276. IN PSTR pDomain
  277. );
  278. PSTR
  279. getNextQueryName(
  280. OUT PSTR pNameBuffer,
  281. IN DWORD QueryCount,
  282. IN PSTR pszName,
  283. IN DWORD NameLength,
  284. IN DWORD NameAttributes,
  285. IN PDNS_NETINFO pNetInfo,
  286. OUT PDWORD pSuffixFlags
  287. );
  288. PSTR
  289. Query_GetNextName(
  290. IN OUT PQUERY_BLOB pBlob
  291. );
  292. //
  293. // Update FAZ utilities (faz.c)
  294. //
  295. DNS_STATUS
  296. DoQuickFAZ(
  297. OUT PDNS_NETINFO * ppNetworkInfo,
  298. IN LPSTR pszName,
  299. IN PIP_ARRAY aipServerList OPTIONAL
  300. );
  301. DWORD
  302. GetDnsServerListsForUpdate(
  303. IN OUT PIP_ARRAY * DnsServerListArray,
  304. IN DWORD ArrayLength,
  305. IN DWORD Flags
  306. );
  307. DNS_STATUS
  308. CollapseDnsServerListsForUpdate(
  309. IN OUT PIP_ARRAY * DnsServerListArray,
  310. OUT PDNS_NETINFO * NetworkInfoArray,
  311. IN OUT PDWORD pNetCount,
  312. IN LPSTR pszUpdateName
  313. );
  314. PIP_ARRAY
  315. GetNameServersListForDomain(
  316. IN LPSTR pDomainName,
  317. IN PIP_ARRAY aipServers
  318. );
  319. BOOL
  320. ValidateZoneNameForUpdate(
  321. IN LPSTR pszZone
  322. );
  323. //
  324. // Status (dnsapi.c)
  325. //
  326. BOOL
  327. IsKnownNetFailure(
  328. VOID
  329. );
  330. VOID
  331. SetKnownNetFailure(
  332. IN DNS_STATUS Status
  333. );
  334. BOOL
  335. IsLocalIpAddress(
  336. IN IP_ADDRESS IpAddress
  337. );
  338. PDNS_NETINFO
  339. GetAdapterListFromCache(
  340. VOID
  341. );
  342. //
  343. // IP Help API (iphelp.c)
  344. //
  345. VOID
  346. IpHelp_Cleanup(
  347. VOID
  348. );
  349. DNS_STATUS
  350. IpHelp_GetAdaptersInfo(
  351. OUT PIP_ADAPTER_INFO * ppAdapterInfo
  352. );
  353. DNS_STATUS
  354. IpHelp_GetPerAdapterInfo(
  355. IN DWORD AdapterIndex,
  356. OUT PIP_PER_ADAPTER_INFO * ppPerAdapterInfo
  357. );
  358. DNS_STATUS
  359. IpHelp_GetBestInterface(
  360. IN IP4_ADDRESS Ip4Addr,
  361. OUT PDWORD pdwInterfaceIndex
  362. );
  363. DNS_STATUS
  364. IpHelp_ParseIpAddressString(
  365. IN OUT PIP_ARRAY pIpArray,
  366. IN PIP_ADDR_STRING pIpAddrString,
  367. IN BOOL fGetSubnetMask,
  368. IN BOOL fReverse
  369. );
  370. //
  371. // Environment variable reading (regfig.c)
  372. //
  373. BOOL
  374. Reg_ReadDwordEnvar(
  375. IN DWORD dwFlag,
  376. OUT PENVAR_DWORD_INFO pEnvar
  377. );
  378. //
  379. // Hosts file reading (hostfile.c)
  380. //
  381. BOOL
  382. QueryHostFile(
  383. IN OUT PQUERY_BLOB pBlob
  384. );
  385. //
  386. // Resolver (resolver.c)
  387. //
  388. BOOL
  389. WINAPI
  390. DnsFlushResolverCacheEntry_UTF8(
  391. IN PSTR pszName
  392. );
  393. //
  394. // Heap (memory.c)
  395. //
  396. VOID
  397. Heap_Initialize(
  398. VOID
  399. );
  400. VOID
  401. Heap_Cleanup(
  402. VOID
  403. );
  404. //
  405. // Network info (netinfo.c)
  406. //
  407. VOID
  408. InitNetworkInfo(
  409. VOID
  410. );
  411. VOID
  412. CleanupNetworkInfo(
  413. VOID
  414. );
  415. PSTR
  416. SearchList_GetNextName(
  417. IN OUT PSEARCH_LIST pSearchList,
  418. IN BOOL fReset,
  419. OUT PDWORD pdwSuffixFlags OPTIONAL
  420. );
  421. PIP_ARRAY
  422. NetInfo_ConvertToIpArray(
  423. IN PDNS_NETINFO pNetworkInfo
  424. );
  425. PDNS_NETINFO
  426. NetInfo_CreateForUpdate(
  427. IN PSTR pszZone,
  428. IN PSTR pszServerName,
  429. IN PIP_ARRAY pServerArray,
  430. IN DWORD dwFlags
  431. );
  432. PSTR
  433. NetInfo_UpdateZoneName(
  434. IN PDNS_NETINFO pNetInfo
  435. );
  436. PSTR
  437. NetInfo_UpdateServerName(
  438. IN PDNS_NETINFO pNetInfo
  439. );
  440. BOOL
  441. NetInfo_IsForUpdate(
  442. IN PDNS_NETINFO pNetInfo
  443. );
  444. PDNS_NETINFO
  445. NetInfo_CreateFromIpArray(
  446. IN PIP4_ARRAY pDnsServers,
  447. IN IP4_ADDRESS ServerIp,
  448. IN BOOL fSearchInfo,
  449. IN PDNS_NETINFO pNetInfo OPTIONAL
  450. );
  451. VOID
  452. NetInfo_MarkDirty(
  453. VOID
  454. );
  455. PDNS_NETINFO
  456. NetInfo_Get(
  457. IN BOOL fForce,
  458. IN BOOL fGetIpAddresses
  459. );
  460. #define GetNetworkInfo() NetInfo_Get( FALSE, TRUE )
  461. PIP4_ARRAY
  462. GetDnsServerList(
  463. IN BOOL fForce
  464. );
  465. // Private but used in servlist.c
  466. PDNS_ADAPTER
  467. AdapterInfo_Copy(
  468. IN PDNS_ADAPTER pAdapter
  469. );
  470. PDNS_NETINFO
  471. NetInfo_Alloc(
  472. IN DWORD AdapterCount
  473. );
  474. BOOL
  475. NetInfo_AddAdapter(
  476. IN OUT PDNS_NETINFO pNetInfo,
  477. IN PDNS_ADAPTER pAdapter
  478. );
  479. //
  480. // Old routines
  481. //
  482. VOID
  483. Dns_ResetNetworkInfo(
  484. IN PDNS_NETINFO pNetworkInfo
  485. );
  486. BOOL
  487. Dns_DisableTimedOutAdapters(
  488. IN PDNS_NETINFO pNetworkInfo
  489. );
  490. BOOL
  491. Dns_ShouldNameErrorBeCached(
  492. IN PDNS_NETINFO pNetworkInfo
  493. );
  494. BOOL
  495. Dns_PingAdapterServers(
  496. IN PDNS_ADAPTER pAdapterInfo
  497. );
  498. //
  499. // Internal routines for system public config (dnsapi.c)
  500. //
  501. PDNS_NETWORK_INFORMATION
  502. WINAPI
  503. GetNetworkInformation(
  504. VOID
  505. );
  506. PDNS_SEARCH_INFORMATION
  507. WINAPI
  508. GetSearchInformation(
  509. VOID
  510. );
  511. VOID
  512. WINAPI
  513. FreeAdapterInformation(
  514. IN OUT PDNS_ADAPTER_INFORMATION pAdapterInformation
  515. );
  516. VOID
  517. WINAPI
  518. FreeSearchInformation(
  519. IN OUT PDNS_SEARCH_INFORMATION pSearchInformation
  520. );
  521. VOID
  522. WINAPI
  523. FreeNetworkInformation(
  524. IN OUT PDNS_NETWORK_INFORMATION pNetworkInformation
  525. );
  526. //
  527. // local IP info (localip.c)
  528. //
  529. PIP4_ARRAY
  530. LocalIp_GetIp4Array(
  531. VOID
  532. );
  533. #endif // _DNSAPILOCAL_INCLUDED_