PageRenderTime 86ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/Network/DnsApi.cs

http://interlace.googlecode.com/
C# | 159 lines | 102 code | 29 blank | 28 comment | 3 complexity | 97e3ff227730a079dfee6bd747eec83e MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Runtime.InteropServices;
  30. using System.Text;
  31. #endregion
  32. namespace Interlace.Network
  33. {
  34. static class DnsApi
  35. {
  36. public const int DNS_TYPE_A = 1;
  37. public const int DNS_TYPE_NS = 2;
  38. public const int DNS_TYPE_MD = 3;
  39. public const int DNS_TYPE_MF = 4;
  40. public const int DNS_TYPE_CNAME = 5;
  41. public const int DNS_TYPE_SOA = 6;
  42. public const int DNS_TYPE_MB = 7;
  43. public const int DNS_TYPE_MG = 8;
  44. public const int DNS_TYPE_MR = 9;
  45. public const int DNS_TYPE_NULL = 10;
  46. public const int DNS_TYPE_WKS = 11;
  47. public const int DNS_TYPE_PTR = 12;
  48. public const int DNS_TYPE_HINFO = 13;
  49. public const int DNS_TYPE_MINFO = 14;
  50. public const int DNS_TYPE_MX = 15;
  51. public const int DNS_TYPE_TEXT = 16;
  52. public const uint DNS_QUERY_STANDARD = 0x00000000;
  53. public const uint DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 0x00000001;
  54. public const uint DNS_QUERY_USE_TCP_ONLY = 0x00000002;
  55. public const uint DNS_QUERY_NO_RECURSION = 0x00000004;
  56. public const uint DNS_QUERY_BYPASS_CACHE = 0x00000008;
  57. public const uint DNS_QUERY_NO_WIRE_QUERY = 0x00000010;
  58. public const uint DNS_QUERY_NO_LOCAL_NAME = 0x00000020;
  59. public const uint DNS_QUERY_NO_HOSTS_FILE = 0x00000040;
  60. public const uint DNS_QUERY_NO_NETBT = 0x00000080;
  61. public const uint DNS_QUERY_WIRE_ONLY = 0x00000100;
  62. public const uint DNS_QUERY_RETURN_MESSAGE = 0x00000200;
  63. public const uint DNS_QUERY_TREAT_AS_FQDN = 0x00001000;
  64. public const uint DNS_QUERY_DONT_RESET_TTL_VALUES = 0x00100000;
  65. public const uint DNS_QUERY_RESERVED = 0xff000000;
  66. public const int DNS_FREE_RECORD_LIST = 1;
  67. public const int DNS_RECORD_FLAGS_CHARSET_MASK = 0x18;
  68. public const int DNS_RECORD_FLAGS_CHARSET_UNICODE = 0x08;
  69. [StructLayout(LayoutKind.Sequential)]
  70. public struct DnsRecordHeader
  71. {
  72. // This structure must not contain anything that would cause
  73. // memory allocation when being marshaled and unmarshaled, like strings;
  74. // the code using it depends on this to avoid leaks.
  75. public IntPtr pNext;
  76. public IntPtr pName;
  77. public ushort wType;
  78. public ushort wDataLength;
  79. public uint flags;
  80. public uint dwTtl;
  81. public uint dwReserved;
  82. }
  83. public const uint SEC_WINNT_AUTH_IDENTITY_UNICODE = 0x02;
  84. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  85. public class SecWinNtAuthIdentity
  86. {
  87. public string User;
  88. public int UserLength;
  89. public string Domain;
  90. public int DomainLength;
  91. public string Password;
  92. public int PasswordLength;
  93. public uint Flags;
  94. public SecWinNtAuthIdentity(string user, string domain, string password)
  95. {
  96. User = user;
  97. UserLength = user != null ? user.Length : 0;
  98. Domain = domain;
  99. DomainLength = domain != null ? domain.Length : 0;
  100. Password = password;
  101. PasswordLength = password != null ? password.Length : 0;
  102. Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  103. }
  104. }
  105. [DllImport("dnsapi.dll", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
  106. public static extern int DnsQuery(string lpstrName,
  107. int wType, uint fOptions, IntPtr pExtra, out IntPtr ppQueryResultsSet,
  108. IntPtr pReserved);
  109. [DllImport("dnsapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  110. public static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
  111. [DllImport("dnsapi.dll", EntryPoint = "DnsAcquireContextHandle_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
  112. public static extern int DnsAcquireContextHandle(int credentialsFlags,
  113. [In, MarshalAs(UnmanagedType.LPStruct)] SecWinNtAuthIdentity credentials, out IntPtr handle);
  114. [DllImport("dnsapi.dll", EntryPoint = "DnsReleaseContextHandle", CharSet = CharSet.Unicode, SetLastError = false, ExactSpelling = true)]
  115. public static extern void DnsReleaseContextHandle(IntPtr handle);
  116. public const int DNS_UPDATE_SECURITY_USE_DEFAULT = 0x0;
  117. public const int DNS_UPDATE_SECURITY_OFF = 0x10;
  118. public const int DNS_UPDATE_SECURITY_ON = 0x20;
  119. public const int DNS_UPDATE_SECURITY_ONLY = 0x100;
  120. public const int DNS_UPDATE_CACHE_SECURITY_CONTEXT = 0x200;
  121. public const int DNS_UPDATE_TEST_USE_LOCAL_SYS_ACCT = 0x400;
  122. public const int DNS_UPDATE_FORCE_SECURITY_NEGO = 0x800;
  123. [DllImport("dnsapi.dll", EntryPoint = "DnsModifyRecordsInSet_W", CharSet = CharSet.Unicode, SetLastError = false, ExactSpelling = true)]
  124. public static extern int DnsModifyRecordsInSet(IntPtr pAddRecords, IntPtr pDeleteRecords, int Options,
  125. IntPtr hContext, byte[] pServerList, IntPtr pReserved);
  126. [DllImport("dnsapi.dll", EntryPoint = "DnsReplaceRecordSetW", CharSet = CharSet.Unicode, SetLastError = false, ExactSpelling = true)]
  127. public static extern int DnsReplaceRecordSet(IntPtr pNewSet, int Options,
  128. IntPtr hContext, byte[] pServerList, IntPtr pReserved);
  129. }
  130. }