ARSoft.Tools.Net - C# DNS client/server, SPF and SenderID Library
DNS Client

The DNS Client still work and can be used for direct communication with a DNS server.

If you just want to resolve data, you should take a look on the DNS Resolver examples.

Get addresses for a domain name (IPv4)
Copy Code
DnsMessage dnsMessage = DnsClient.Default.Resolve(DomainName.Parse(www.example.com), RecordType.A);
if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
{
  throw new Exception("DNS request failed");
}
else
{
  foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
  {
    ARecord aRecord = dnsRecord as ARecord;
    if (aRecord != null)
    {
      Console.WriteLine(aRecord.Address.ToString());
    }
  }
}

 

Get mail exchangers for a domain name
Copy Code
DnsMessage dnsMessage = DnsClient.Default.Resolve(DomainName.Parse("example.com"), RecordType.Mx);
if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
{
  throw new Exception("DNS request failed");
}
else
{
  foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
  {
    MxRecord mxRecord = dnsRecord as MxRecord;
    if (mxRecord != null)
    {
      Console.WriteLine(mxRecord.ExchangeDomainName);
    }
  }
}

 

Get reverse lookup adress for an ip address
Copy Code
DnsMessage dnsMessage = DnsClient.Default.Resolve(IPAddress.Parse("192.0.2.1").GetReverseLookupDomain(), RecordType.Ptr);
if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
{
  throw new Exception("DNS request failed");
}
else
{
  foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
  {
    PtrRecord ptrRecord = dnsRecord as PtrRecord;
    if (ptrRecord != null)
    {
      Console.WriteLine(ptrRecord.PointerDomainName);
    }
  }
}

 

Send dynamic update
Copy Code
DnsUpdateMessage msg = new DnsUpdateMessage
{
  ZoneName = "example.com"
}

msg.Updates.Add(new DeleteRecordUpdate(DomainName.Parse("dyn.example.com"), RecordType.A));
msg.Updates.Add(new AddRecordUpdate(new ARecord(DomainName.Parse("dyn.example.com"), 300, IPAddress.Parse("192.0.2.42"))));
msg.TSigOptions = new TSigRecord(DomainName.Parse("my-key"), TSigAlgorithm.Md5, DateTime.Now, new TimeSpan(0, 5, 0), msg.TransactionID, ReturnCode.NoError, null, Convert.FromBase64String("0jnu3SdsMvzzlmTDPYRceA=="));

DnsUpdateMessage dnsResult = new DnsClient(IPAddress.Parse("192.0.2.1"), 5000).SendUpdate(msg);

 

 


© 2010-2015 Alexander Reinert. All Rights Reserved.

Send Feedback


This documentation was created using Document! X