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.

371 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: dnsutil.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _DNSUTIL_H
  11. #define _DNSUTIL_H
  12. /////////////////////////////////////////////////////////////////
  13. // C++ helper classes wrapping the raw DNS RPC API's
  14. #define DNS_TYPE_UNK DNS_TYPE_NULL // we treat the two as the same
  15. #ifdef USE_NDNC
  16. typedef enum
  17. {
  18. none = 0, // not AD integrated
  19. forest,
  20. domain,
  21. w2k,
  22. custom
  23. } ReplicationType;
  24. #endif
  25. //////////////////////////////////////////////////////////////
  26. // macros defining the DNS server version
  27. #define DNS_SRV_MAJOR_VERSION(dw) (LOBYTE(LOWORD(dw)))
  28. #define DNS_SRV_MINOR_VERSION(dw) (HIBYTE(LOWORD(dw)))
  29. #define DNS_SRV_BUILD_NUMBER(dw) (HIWORD(dw))
  30. #define DNS_SRV_MAJOR_VERSION_NT_4 (0x4)
  31. #define DNS_SRV_MAJOR_VERSION_NT_5 (0x5)
  32. #define DNS_SRV_MINOR_VERSION_WIN2K (0x0)
  33. #define DNS_SRV_MINOR_VERSION_WHISTLER (0x1)
  34. #define DNS_SRV_BUILD_NUMBER_WHISTLER (2230)
  35. #define DNS_SRV_BUILD_NUMBER_WHISTLER_NEW_SECURITY_SETTINGS (2474)
  36. #define DNS_SRV_VERSION_NT_4 DNS_SRV_MAJOR_VERSION_NT_4
  37. //////////////////////////////////////////////////////////////
  38. // MACROS & DEFINES
  39. #define MAX_DNS_NAME_LEN 255
  40. // record enumeration
  41. #define NEXT_DWORD(cb) ((cb + 3) & ~3)
  42. //#define IS_DWORD_ALIGNED(pv) (((int)(void *)pv & 3) == 0)
  43. #define DNS_SIZE_OF_DNS_RPC_RR(pDnsRecord)\
  44. (SIZEOF_DNS_RPC_RECORD_HEADER + pDnsRecord->wDataLength)
  45. #define DNS_NEXT_RECORD(pDnsRecord) \
  46. (DNS_RPC_RECORD *)((BYTE*)pDnsRecord + ((DNS_SIZE_OF_DNS_RPC_RR(pDnsRecord) + 3) & ~3))
  47. // RR field manipulation
  48. //#define INET_NTOA(s) (inet_ntoa (*(in_addr *)&(s)))
  49. #define REVERSE_WORD_BYTES(w) \
  50. MAKEWORD(HIBYTE(w), LOBYTE(w))
  51. // DNS keywords
  52. #define INADDR_ARPA_SUFFIX _T(".in-addr.arpa")
  53. #define ARPA_SUFFIX _T(".arpa")
  54. #define IP6_INT_SUFFIX _T(".ipv6.int")
  55. #define AUTOCREATED_0 _T("0.in-addr.arpa")
  56. #define AUTOCREATED_127 _T("127.in-addr.arpa")
  57. #define AUTOCREATED_255 _T("255.in-addr.arpa")
  58. // formatting of IPv4 address to string
  59. extern LPCWSTR g_szIpStringFmt;
  60. #define IP_STRING_MAX_LEN 20 // safe size for a wsprintf buffer
  61. #define IP_STRING_FMT_ARGS(x) \
  62. FOURTH_IPADDRESS(x), THIRD_IPADDRESS(x), SECOND_IPADDRESS(x), FIRST_IPADDRESS(x)
  63. //////////////////////////////////////////////////////////////
  64. // macros and functions for UTF8 <-> UNICODE conversion
  65. // modified from the ATL 1.1 ones in atlbase.h
  66. inline LPWSTR WINAPI DnsUtf8ToWHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  67. {
  68. _ASSERTE(lpa != NULL);
  69. _ASSERTE(lpw != NULL);
  70. // verify that no illegal character present
  71. // since lpw was allocated based on the size of lpa
  72. // don't worry about the number of chars
  73. lpw[0] = '\0';
  74. MultiByteToWideChar(CP_UTF8, 0, lpa, -1, lpw, nChars);
  75. return lpw;
  76. }
  77. inline LPSTR WINAPI DnsWToUtf8Helper(LPSTR lpa, LPCWSTR lpw, int nChars)
  78. {
  79. _ASSERTE(lpw != NULL);
  80. _ASSERTE(lpa != NULL);
  81. // verify that no illegal character present
  82. // since lpa was allocated based on the size of lpw
  83. // don't worry about the number of chars
  84. lpa[0] = '\0';
  85. WideCharToMultiByte(CP_UTF8, 0, lpw, -1, lpa, nChars, NULL, NULL);
  86. return lpa;
  87. }
  88. inline void WINAPI DnsUtf8ToWCStringHelper(CString& sz, LPCSTR lpa, int nBytes)
  89. {
  90. if ( (lpa == NULL) || (nBytes <= 0) )
  91. {
  92. sz.Empty();
  93. return;
  94. }
  95. // allocate buffer in string (worst case scenario + NULL)
  96. int nWideChars = nBytes + 1;
  97. LPWSTR lpw = sz.GetBuffer(nWideChars);
  98. int nWideCharsLen = MultiByteToWideChar(CP_UTF8, 0, lpa, nBytes, lpw, nWideChars);
  99. sz.ReleaseBuffer(nWideCharsLen);
  100. }
  101. #define UTF8_LEN lstrlenA
  102. #define UTF8_TO_W(lpa) (\
  103. ((LPCSTR)lpa == NULL) ? NULL : (\
  104. _convert = (lstrlenA(lpa)+1),\
  105. DnsUtf8ToWHelper((LPWSTR) alloca(_convert*2), lpa, _convert)))
  106. #define W_TO_UTF8(lpw) (\
  107. ((LPCWSTR)lpw == NULL) ? NULL : (\
  108. _convert = (lstrlenW(lpw)+1)*4,\
  109. DnsWToUtf8Helper((LPSTR) alloca(_convert), lpw, _convert)))
  110. #define UTF8_TO_CW(lpa) ((LPCWSTR)UTF8_TO_W(lpa))
  111. #define W_TO_CUTF8(lpw) ((LPCSTR)W_TO_UTF8(lpw))
  112. //////////////////////////////////////////////////////////////
  113. // estimate UTF8 len of a UNICODE string
  114. inline int UTF8StringLen(LPCWSTR lpszWideString)
  115. {
  116. USES_CONVERSION;
  117. LPSTR lpszUTF8 = W_TO_UTF8(lpszWideString);
  118. return UTF8_LEN(lpszUTF8);
  119. }
  120. ///////////////////////////////////////////////////////////////
  121. // General Purpose Utility Functions
  122. WORD CharToNumber(WCHAR ch);
  123. BYTE HexCharToByte(WCHAR ch);
  124. BOOL IsValidIPString(LPCWSTR lpsz);
  125. DNS_STATUS ValidateDnsNameAgainstServerFlags(LPCWSTR lpszName,
  126. DNS_NAME_FORMAT format,
  127. DWORD serverNameChecking);
  128. DWORD IPStringToAddr(LPCWSTR lpsz);
  129. inline void FormatIpAddress(LPWSTR lpszBuf, DWORD dwIpAddr)
  130. {
  131. wsprintf(lpszBuf, g_szIpStringFmt, IP_STRING_FMT_ARGS(dwIpAddr));
  132. }
  133. inline void FormatIpAddress(CString& szBuf, DWORD dwIpAddr)
  134. {
  135. LPWSTR lpszBuf = szBuf.GetBuffer(IP_STRING_MAX_LEN);
  136. wsprintf(lpszBuf, g_szIpStringFmt, IP_STRING_FMT_ARGS(dwIpAddr));
  137. szBuf.ReleaseBuffer();
  138. }
  139. void ReverseString(LPWSTR p, LPWSTR q);
  140. int ReverseIPString(LPWSTR lpsz);
  141. BOOL RemoveInAddrArpaSuffix(LPWSTR lpsz);
  142. BOOL IsValidDnsZoneName(CString& szName, BOOL bFwd);
  143. ///////////////////////////////////////////////////////////////
  144. // definitions and helper functions for IPv6 format
  145. void FormatIPv6Addr(CString& szAddr, IPV6_ADDRESS* ipv6Addr);
  146. ///////////////////////////////////////////////////////////////
  147. // flags for different states and options of DNS objects
  148. // use LOWORD only because used in CTreeNode::m_dwNodeFlags
  149. #define TN_FLAG_DNS_RECORD_FULL_NAME (0x01) // use full name to display resource records
  150. #define TN_FLAG_DNS_RECORD_SHOW_TTL (0x02) // show TLL in record properties
  151. typedef CArray<IP_ADDRESS,IP_ADDRESS> CIpAddressArray;
  152. //////////////////////////////////////////////////////////////////////////////
  153. // CBlob
  154. template <class T> class CBlob
  155. {
  156. public:
  157. CBlob()
  158. {
  159. m_pData = NULL;
  160. m_nSize = 0;
  161. }
  162. ~CBlob()
  163. {
  164. Empty();
  165. }
  166. UINT GetSize() { return m_nSize;}
  167. T* GetData() { return m_pData;}
  168. void Set(T* pData, UINT nSize)
  169. {
  170. ASSERT(pData != NULL);
  171. Empty();
  172. m_pData = (BYTE*)malloc(sizeof(T)*nSize);
  173. ASSERT(m_pData != NULL);
  174. if (m_pData != NULL)
  175. {
  176. m_nSize = nSize;
  177. memcpy(m_pData, pData, sizeof(T)*nSize);
  178. }
  179. }
  180. UINT Get(T* pData)
  181. {
  182. ASSERT(pData != NULL);
  183. memcpy(pData, m_pData, sizeof(T)*m_nSize);
  184. return m_nSize;
  185. }
  186. void Empty()
  187. {
  188. if (m_pData != NULL)
  189. {
  190. free(m_pData);
  191. m_pData = NULL;
  192. m_nSize = 0;
  193. }
  194. }
  195. private:
  196. T* m_pData;
  197. UINT m_nSize;
  198. };
  199. typedef CBlob<BYTE> CByteBlob;
  200. typedef CBlob<WORD> CWordBlob;
  201. //////////////////////////////////////////////////////////////////////////////
  202. // CDNSServerInfoEx
  203. // definitions for the extended server flags (NT 5.0) only
  204. #define SERVER_REGKEY_ARR_SIZE 6
  205. #define SERVER_REGKEY_ARR_INDEX_NO_RECURSION 0
  206. #define SERVER_REGKEY_ARR_INDEX_BIND_SECONDARIES 1
  207. #define SERVER_REGKEY_ARR_INDEX_STRICT_FILE_PARSING 2
  208. #define SERVER_REGKEY_ARR_INDEX_ROUND_ROBIN 3
  209. #define SERVER_REGKEY_ARR_LOCAL_NET_PRIORITY 4
  210. #define SERVER_REGKEY_ARR_CACHE_POLLUTION 5
  211. extern LPCSTR _DnsServerRegkeyStringArr[];
  212. class CDNSServerInfoEx : public CObjBase
  213. {
  214. public:
  215. CDNSServerInfoEx();
  216. ~CDNSServerInfoEx();
  217. DNS_STATUS Query(LPCTSTR lpszServerName);
  218. BOOL HasData(){ return m_pServInfo != NULL;}
  219. void FreeInfo();
  220. // obtained from DnsGetServerInfo() RPC call (NT 4.0)
  221. DNS_RPC_SERVER_INFO* m_pServInfo;
  222. // error codes for query
  223. DNS_STATUS m_errServInfo;
  224. private:
  225. void QueryRegKeyOptionsHelper(LPCSTR lpszAnsiServerName);
  226. };
  227. //////////////////////////////////////////////////////////////////////////////
  228. // CDNSZoneInfoEx
  229. class CDNSZoneInfoEx : public CObjBase
  230. {
  231. public:
  232. CDNSZoneInfoEx();
  233. ~CDNSZoneInfoEx();
  234. DNS_STATUS Query(LPCTSTR lpszServerName, LPCTSTR lpszZoneName, DWORD dwServerVersion);
  235. BOOL HasData(){ return m_pZoneInfo != NULL;}
  236. void FreeInfo();
  237. // struct obtained from DnssrvGetZoneInfo() 5.0 RPC call (NT 4.0 format)
  238. DNS_RPC_ZONE_INFO* m_pZoneInfo;
  239. // obtained from DnssrvQueryZoneDwordProperty() (NT 5.0)
  240. // UINT m_nAllowsDynamicUpdate;
  241. // error codes for query
  242. DNS_STATUS m_errZoneInfo;
  243. // DNS_STATUS m_errAllowsDynamicUpdate;
  244. };
  245. ///////////////////////////////////////////////////////////////////////////////
  246. //////////////////// ERROR MESSAGES HANDLING //////////////////////////////////
  247. ///////////////////////////////////////////////////////////////////////////////
  248. int DNSMessageBox(LPCTSTR lpszText, UINT nType = MB_OK);
  249. int DNSMessageBox(UINT nIDPrompt, UINT nType = MB_OK);
  250. int DNSErrorDialog(DNS_STATUS err, LPCTSTR lpszErrorMsg = NULL);
  251. int DNSErrorDialog(DNS_STATUS err, UINT nErrorMsgID);
  252. void DNSDisplaySystemError(DWORD dwErr);
  253. void DNSCreateErrorMessage(DNS_STATUS err, UINT nErrorMsgID, CString& refszMessage);
  254. int DNSConfirmOperation(UINT nMsgID, CTreeNode* p);
  255. class CDNSErrorInfo
  256. {
  257. public:
  258. static BOOL GetErrorString(DNS_STATUS err, CString& szError);
  259. static BOOL GetErrorStringFromTable(DNS_STATUS err, CString& szError);
  260. static BOOL GetErrorStringFromWin32(DNS_STATUS err, CString& szError);
  261. };
  262. ///////////////////////////////////////////////////////////////////////////////
  263. //
  264. // Security KEY, SIG 6-bit values to base64 character mapping
  265. //
  266. #define SECURITY_PAD_CHAR (L'=')
  267. WCHAR Dns_SecurityBase64CharToBits(IN WCHAR wch64);
  268. DNS_STATUS Dns_SecurityBase64StringToKey(OUT PBYTE pKey,
  269. OUT PDWORD pKeyLength,
  270. IN PWSTR pchString,
  271. IN DWORD cchLength);
  272. PWSTR Dns_SecurityKeyToBase64String(IN PBYTE pKey,
  273. IN DWORD KeyLength,
  274. OUT PWSTR pchBuffer);
  275. DNS_STATUS Dns_SecurityHexToKey(OUT PBYTE pKey,
  276. OUT PDWORD pKeyLength,
  277. IN PWSTR pchString,
  278. IN DWORD cchLength);
  279. void Dns_SecurityKeyToHexString(PBYTE pKey,
  280. DWORD KeyLength,
  281. CString& strref);
  282. void TimetToFileTime( time_t t, LPFILETIME pft );
  283. DWORD FileTimeToTimet(FILETIME* pft);
  284. void ConvertTTLToSystemTime(TIME_ZONE_INFORMATION*,
  285. DWORD dwTTL,
  286. SYSTEMTIME* pSysTime);
  287. DWORD ConvertSystemTimeToTTL(SYSTEMTIME* pSysTime);
  288. BOOL ConvertTTLToLocalTimeString(const DWORD dwTTL,
  289. CString& strref);
  290. #endif // _DNSUTIL_H