Created
March 25, 2013 18:49
-
-
Save mbrownnycnyc/5239551 to your computer and use it in GitHub Desktop.
beginnings of a fully exposed dns class for c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Text; | |
using System.Runtime.InteropServices; | |
namespace MACE_DataCollector_wrapper | |
{ | |
class dns | |
{ | |
public class DnsQuerier | |
{ | |
//http://www.eggheadcafe.com/articles/20050129.asp | |
//more specifically: http://stackoverflow.com/a/5504091/843000 | |
private enum QueryOptions | |
{ | |
//http://msdn.microsoft.com/en-us/library/cc982162(v=vs.85).aspx | |
DNS_QUERY_STANDARD = 0x00000000, | |
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 0x00000001, | |
DNS_QUERY_USE_TCP_ONLY = 0x00000002, | |
DNS_QUERY_NO_RECURSION = 0x00000004, | |
DNS_QUERY_BYPASS_CACHE = 0x00000008, | |
DNS_QUERY_NO_WIRE_QUERY = 0x00000010, | |
DNS_QUERY_NO_LOCAL_NAME = 0x00000020, | |
DNS_QUERY_NO_HOSTS_FILE = 0x00000040, | |
DNS_QUERY_NO_NETBT = 0x00000080, | |
DNS_QUERY_WIRE_ONLY = 0x00000100, | |
DNS_QUERY_RETURN_MESSAGE = 0x00000200, | |
DNS_QUERY_MULTICAST_ONLY = 0x00000400, | |
DNS_QUERY_NO_MULTICAST = 0x00000800, | |
DNS_QUERY_TREAT_AS_FQDN = 0x00001000, | |
DNS_QUERY_ADDRCONFIG = 0x00002000, | |
DNS_QUERY_DUAL_ADDR = 0x00004000, | |
DNS_QUERY_MULTICAST_WAIT = 0x00020000, | |
DNS_QUERY_MULTICAST_VERIFY = 0x00040000, | |
DNS_QUERY_DONT_RESET_TTL_VALUES = 0x00100000, | |
DNS_QUERY_DISABLE_IDN_ENCODING = 0x00200000, | |
DNS_QUERY_APPEND_MULTILABEL = 0x00800000, | |
DNS_QUERY_RESERVED = 0xf0000000, | |
} | |
private enum RecordTypes | |
{ | |
//http://msdn.microsoft.com/en-us/library/cc982162(v=vs.85).aspx | |
DNS_TYPE_A = 0x0001, | |
DNS_TYPE_NS = 0x0002, | |
DNS_TYPE_MD = 0x0003, | |
DNS_TYPE_MF = 0x0004, | |
DNS_TYPE_CNAME = 0x0005, | |
DNS_TYPE_SOA = 0x0006, | |
DNS_TYPE_MB = 0x0007, | |
DNS_TYPE_MG = 0x0008, | |
DNS_TYPE_MR = 0x0009, | |
DNS_TYPE_NULL = 0x000a, | |
DNS_TYPE_WKS = 0x000b, | |
DNS_TYPE_PTR = 0x000c, | |
DNS_TYPE_HINFO = 0x000d, | |
DNS_TYPE_MINFO = 0x000e, | |
DNS_TYPE_MX = 0x000f, | |
DNS_TYPE_TEXT = 0x0010, | |
DNS_TYPE_RP = 0x0011, | |
DNS_TYPE_AFSDB = 0x0012, | |
DNS_TYPE_X25 = 0x0013, | |
DNS_TYPE_ISDN = 0x0014, | |
DNS_TYPE_RT = 0x0015, | |
DNS_TYPE_NSAP = 0x0016, | |
DNS_TYPE_NSAPPTR = 0x0017, | |
DNS_TYPE_SIG = 0x0018, | |
DNS_TYPE_KEY = 0x0019, | |
DNS_TYPE_PX = 0x001a, | |
DNS_TYPE_GPOS = 0x001b, | |
DNS_TYPE_AAAA = 0x001c, | |
DNS_TYPE_LOC = 0x001d, | |
DNS_TYPE_NXT = 0x001e, | |
DNS_TYPE_EID = 0x001f, | |
DNS_TYPE_NIMLOC = 0x0020, | |
DNS_TYPE_SRV = 0x0021, | |
DNS_TYPE_ATMA = 0x0022, | |
DNS_TYPE_NAPTR = 0x0023, | |
DNS_TYPE_KX = 0x0024, | |
DNS_TYPE_CERT = 0x0025, | |
DNS_TYPE_A6 = 0x0026, | |
DNS_TYPE_DNAME = 0x0027, | |
DNS_TYPE_SINK = 0x0028, | |
DNS_TYPE_OPT = 0x0029, | |
DNS_TYPE_DS = 0x002B, | |
DNS_TYPE_RRSIG = 0x002E, | |
DNS_TYPE_NSEC = 0x002F, | |
DNS_TYPE_DNSKEY = 0x0030, | |
DNS_TYPE_DHCID = 0x0031, | |
DNS_TYPE_UINFO = 0x0064, | |
DNS_TYPE_UID = 0x0065, | |
DNS_TYPE_GID = 0x0066, | |
DNS_TYPE_UNSPEC = 0x0067, | |
DNS_TYPE_ADDRS = 0x00f8, | |
DNS_TYPE_TKEY = 0x00f9, | |
DNS_TYPE_TSIG = 0x00fa, | |
DNS_TYPE_IXFR = 0x00fb, | |
DNS_TYPE_AXFR = 0x00fc, | |
DNS_TYPE_MAILB = 0x00fd, | |
DNS_TYPE_MAILA = 0x00fe, | |
DNS_TYPE_ALL = 0x00ff, | |
DNS_TYPE_ANY = 0x00ff, | |
DNS_TYPE_WINS = 0xff01, | |
DNS_TYPE_WINSR = 0xff02, | |
DNS_TYPE_NBSTAT = DNS_TYPE_WINSR | |
} | |
[DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)] | |
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, RecordTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved); | |
[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)] | |
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType); | |
public static string[] GetMXRecords(string domain) | |
{ | |
IntPtr ptr1 = IntPtr.Zero; | |
IntPtr ptr2 = IntPtr.Zero; | |
MXRecord recMx; | |
if (Environment.OSVersion.Platform != PlatformID.Win32NT) | |
{ | |
throw new NotSupportedException(); | |
} | |
ArrayList list1 = new ArrayList(); | |
int num1 = DnsQuerier.DnsQuery(ref domain, RecordTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0); | |
if (num1 != 0) | |
{ | |
throw new Win32Exception(num1); | |
} | |
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext) | |
{ | |
recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord)); | |
if (recMx.wType == 15) | |
{ | |
string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange); | |
list1.Add(text1); | |
} | |
} | |
DnsQuerier.DnsRecordListFree(ptr2, 0); | |
return (string[])list1.ToArray(typeof(string)); | |
} | |
/* | |
public static string[] GetARecords(string domain) | |
{ | |
IntPtr ptr1 = IntPtr.Zero; | |
IntPtr ptr2 = IntPtr.Zero; | |
ARecord recA; | |
if (Environment.OSVersion.Platform != PlatformID.Win32NT) | |
{ | |
throw new NotSupportedException(); | |
} | |
ArrayList list1 = new ArrayList(); | |
int num1 = DnsQuerier.DnsQuery(ref domain, RecordTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0); | |
if (num1 != 0) | |
{ | |
throw new Win32Exception(num1); | |
} | |
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext) | |
{ | |
recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord)); | |
if (recMx.wType == 15) | |
{ | |
string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange); | |
list1.Add(text1); | |
} | |
} | |
DnsQuerier.DnsRecordListFree(ptr2, 0); | |
return (string[])list1.ToArray(typeof(string)); | |
} | |
*/ | |
[StructLayout(LayoutKind.Sequential)] | |
//http://msdn.microsoft.com/en-us/library/ms682082(v=vs.85).aspx | |
private struct MXRecord | |
{ | |
public IntPtr pNext; | |
public string pName; | |
public short wType; | |
public short wDataLength; | |
public int flags; | |
public int dwTtl; | |
public int dwReserved; | |
//type specific stuff: | |
// http://msdn.microsoft.com/en-us/library/ms682070(v=vs.85).aspx | |
public IntPtr pNameExchange; | |
public short wPreference; | |
public short Pad; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
private struct ARecord | |
{ | |
public IntPtr pNext; | |
public string pName; | |
public short wType; | |
public short wDataLength; | |
public int flags; | |
public int dwTtl; | |
public int dwReserved; | |
//type specific stuff: | |
// http://msdn.microsoft.com/en-us/library/ms682044(v=vs.85).aspx | |
// IP4_ADDRESS data typs is only supported in windns.h Windows 8+: http://msdn.microsoft.com/en-us/library/cc982163(v=vs.85).aspx | |
// it is a dword http://stackoverflow.com/a/5490439/843000 | |
public uint IpAddress; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment