From a .NET application, I need to query a specific DNS server for resolving a domain name (the DNS server is not defined in the Windows network configuration).
I know this is not possible using standard .NET Framework classes (See this other question). My question is what my options are. There is one open source library on CodePlex (DnDns) which does that, bu开发者_StackOverflow中文版t it hasn't been updated in a long time, and my application is mission-critical so reliability is extremely important.
Any suggestions?
You could also take a look at opendns.net and check if it fits your application
Here is some example code to get you started:
var query = new DnsQuery();
query.Servers.Add("ns1.domainname.com");
query.Servers.Add("ns2.domainname.com");
query.Servers.Add("ns3.domainname.com");
query.Domain = "domain.com";
query.QueryType = Types.TXT;
if (query.Send())
{
Console.WriteLine("TXT:");
var response = query.Response;
foreach (ResourceRecord answer in response.Answers)
{
Console.WriteLine(answer.RText);
}
}
query.QueryType = OpenDNS.Types.MX;
if (query.Send())
{
Console.WriteLine("MX:");
var response = query.Response;
foreach (MX answer in response.Answers)
{
Console.WriteLine("{0} {1}", answer.Preference, answer.Exchange);
}
}
Although this is a pretty old question. There are still new libraries being developed to do this as the .NET Framework still does not have support for this ;)
Have a look at http://dnsclient.michaco.net. It is easy to use, high performant and open source!
And it works on .NET Core cross platform, too!
You can do this with "JH Software's DNS Client for .NET". See the second code sample at http://www.simpledns.com/dns-client-lib.aspx
I had the same task and my solution to execute a command in OS with process and get the output (it works for Windows) /VB.NET/ is:
Sub Main()
Dim oProcess As New Process
Dim oStartInfo As New ProcessStartInfo("nslookup", " [name] [dns server's ip]")
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oProcess.StartInfo = oStartInfo
oProcess.Start()
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadToEnd()
End Using
Console.WriteLine(sOutput)
End Sub
I think the best option is to study the DNS protocol (which is quite easy) and implement it yourself using .net udp sockets.
DNS is really an easy protocol to understand and to implement.
Moreover, you can use the sourcode from DnDNS to see the catches in the program/protocol
DNSJava is an active project that's an open source implementation of DNS in JAVA and is pretty rich in terms of feature set. I was able to use dnsjava using ikvmc to first compile the jar to a DLL:
c:\Temp>ikvmc -target:library dnsjava-2.1.5.jar
IKVM.NET Compiler version 7.2.4630.5 Copyright (C) 2002-2012 Jeroen Frijters
http://www.ikvm.net/
note IKVMC0002: Output file is "dnsjava-2.1.5.dll"
Please note that you will have to reference IKVM.OpenJDK.Core as well as the DLL above for the code below to work. The sample below queries for an A record against a specific name server:
var lookup = new Lookup("google.com", org.xbill.DNS.Type.A, DClass.IN);
lookup.setResolver(new SimpleResolver("192.168.1.1"));
lookup.run();
lookup.getAnswers().ToList().ForEach(x => Console.WriteLine(x.rdataToString()));
Output
74.125.236.164 74.125.236.165 74.125.236.162 74.125.236.174 74.125.236.160 74.125.236.168 74.125.236.169 74.125.236.161 74.125.236.166 74.125.236.167 74.125.236.163
精彩评论