C # implement DNS resolution service

Source: Internet
Author: User

This DNS resolution service is required in the open-source control library ARSoft. Tools. Net.

First, let's briefly introduce ARSoft. tools. net, ARSoft. tools. net is a very powerful open-source DNS control library, including. net SPF validation, SenderID validation, and DNS Client and DNS Server interfaces. You can use this interface to easily implement DNS client and server resolution. It is http://arsofttoolsnet.codeplex.com /.

DNS Client implementation

The Code is as follows:

Using ARSoft. Tools. Net. Dns; // Add reference

// Initialize DnsClient. The first parameter is the IP address of the DNS server, and the second parameter is the timeout time DnsClient dnsClient = new DnsClient (IPAddress. parse ("127.0.0.1"), QUERY_TIMEOUT); // resolve the domain name. Send the domain name request to the DNS server for resolution. The first parameter is the domain name to be resolved, the second parameter is the resolution type, RecordType. A Is IPV4 type DnsMessage dnsMessage = dnsClient. resolve ("www.sina.com", RecordType. a); // If the returned result is null or an error exists, the request fails. If (dnsMessage = null | (dnsMessage. ReturnCode! = ReturnCode. NoError & dnsMessage. ReturnCode! = ReturnCode. NxDomain) {return null;} else {// cyclically traverses the returned results and adds the returned IPV4 Record to the result set List. Foreach (DnsRecordBase dnsRecord in dnsMessage. AnswerRecords) {ARecord aRecord = dnsRecord as ARecord; if (aRecord! = Null) resultIpList. Add (aRecord. Address. ToString (); else continue ;}}

So far, a simple client has been completed, and we can continuously send resolution requests to DNS through this program. If the amount of data sent is large enough, the DNS server may be paralyzed, this is what we often call DOS attacks.

DNS Server implementation

The Code is as follows:

// Initialize DnsServer. The first parameter ipAddress is the local IP address of the listener, the second parameter is the number of concurrent UDP processing, and the third parameter is the number of concurrent TCP processing, the fourth parameter is a delegate, through which we can customize the resolution returned result DnsServer dnsServer = new DnsServer (ipAddress, maxConnection, maxConnection, this. processQuery); dnsServer. start (); // delegate implementation method. You can customize the parsing rule private DnsMessageBase ProcessQuery (DnsMessageBase message, IPAddress clientAddress, ProtocolType protocol) {message. isQuery = false; DnsMessage query = message as DnsMessage; if (query = null | query. questions. count <= 0) message. returnCode = ReturnCode. serverFailure; else {if (query. questions [0]. recordType = RecordType. a) {// custom resolution rule. clientAddress is the IP address of the client, dnsQuestion. name is the domain Name requested by the client. Resolve is the custom method code and will not be pasted out). Return the resolved ip address and add it to the foreach (DnsQuestion dnsQuestion in query in AnswerRecords. questions) {string resolvedIp = Resolve (clientAddress. toString (), dnsQuestion. name); ARecord aRecord = new ARecord (query. questions [0]. name, 36000, ipAddress); query. answerRecords. add (aRecord) ;}} else // if it is an IPV6 request, it will be sent to the superior DNS server for processing, and the code will not be pasted} return message ;}

After the DnsServer is complete, we can expand its functions on this basis, such as not parsing the IP addresses in the blacklist, adding the ip addresses that may be attacked to the blacklist, and creating the cache, accelerate DNS resolution and Intelligent Resolution.

Summary

ARSoft. Tools. Net is a powerful open-source tool library. Its functions are not only in the preceding two aspects, but also include other functions. You can view the source code for yourself.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.