C # Programming implements DNS client and server (GO)

Source: Internet
Author: User
Tags dotnet

Most of us use DNS primarily for domain name resolution, and there is a special requirement recently: passing special data through the DNS protocol. Turn over the Internet and finally find a powerful C # DNS ToolPak ARSoft.Tools.Net Library, thanks to CodePlex, thanks Alexreinert!

For the DNS protocol, refer to the Microsoft Online Documentation DNS architecture or RFC series documentation.

My requirements are simple to send a specific DNS request packet to the specified server and get the results returned. In order to complete this verification work, you need to have the following conditions as I do:

1. Have a server with an external network address, such as: 54.243.209.209;

2. Set the firewall policy, release the 53/UDP port communication; (very important, I took a detour during the test-_-)

3. Set a record for domain subA.mooo.com to 54.243.209.209

4. Set the nameserver of the domain name subB.mooo.com to subA.mooo.com

5. According to the above settings, but when we request to resolve xxx.subB.mooo.com domain name, 54.243.209.209 will receive the DNS Request packet from the DNS server;

6. In response to this packet, a standard DNS request response can be completed;

7. The special domain name can be constructed to realize the transmission of characteristic data;

Next, write the client and server code separately. You can also refer to the sample in the official documentation: http://arsofttoolsnet.codeplex.com/with Wireshark packet analysis, which can help you debug your program well.

C # Programming implementation implements DNS request client

Create a new C # project that uses the dotnet Framework 3.5 as the running environment. Add a reference to the library file "ARSoft.Tools.Net.dll". Write the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingARSoft.Tools.Net.Dns;namespacemydnsclient{/// <summary>    ///@auth shadu{at}foxmail.com///@desc C # DNS Client/// </summary>    classProgram {Static voidMain (string[] args) {            //clientquery ("www.baidu.com");Clientquery ("xxx.subB.mooo.com");        Console.readkey (); }          Public Static voidClientquery (stringdomain) {Dnsmessage Dnsmessage=DnsClient.Default.Resolve (domain, RECORDTYPE.A); if(Dnsmessage = =NULL) || ((Dnsmessage.returncode! = returncode.noerror) && (Dnsmessage.returncode! =Returncode.nxdomain)) {Console.WriteLine ("DNS request failed"); }            Else            {                foreach(Dnsrecordbase Dnsrecordinchdnsmessage.answerrecords) {Arecord Arecord= Dnsrecord asArecord; if(Arecord! =NULL) {Console.WriteLine ("DNS Request successfully: {0}", aRecord.Address.ToString ()); }                }            }        }    }}

C # Programming implementation to implement DNS response service side

Create a new C # project that uses the dotnet Framework 3.5 as the running environment. Add a reference to the library file "ARSoft.Tools.Net.dll". Write the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingARSoft.Tools.Net.Dns;usingSystem.Net; namespacednsserver{/// <summary>    ///@auth shadu{at}foxmail.com///@desc C # DNS Server/// </summary>    classProgram {Static voidMain (string[] args) {            using(DNSServer server =NewDNSServer (Ipaddress.any,Ten,Ten, Processquery)) {Server.                Start (); Console.WriteLine ("Press any key to stop server");            Console.ReadLine (); }        }         Staticdnsmessagebase processquery (dnsmessagebase message, IPAddress clientaddress, System.Net.Sockets.ProtocolType Protocol) {message. Isquery=false; Dnsmessage Query= Message asDnsmessage; //The official sample document does not reflect the IP of the DNS request initiator, which I have added here. Console.WriteLine ("Client address:{0}", clientaddress.tostring ()); Console.WriteLine ("query. QUESTIONS.COUNT:{0}", query.            Questions.count); Console.WriteLine ("query. Questions.tostring (): {0}", query.            Questions.tostring ()); Console.WriteLine ("query. Questions[0]. ToString (): {0}", query. questions[0].            ToString ()); Console.WriteLine ("query. Questions[0]. GetType (): {0}", query. questions[0]. GetType ().             ToString ()); Console.WriteLine ("query. Questions[0]. NAME:{0}", query. questions[0].            Name.tostring ()); Console.WriteLine ("query. Questions[0]. RECORDTYPE:{0}", query. questions[0].            Recordtype.tostring ()); Console.WriteLine ("query. Questions[0]. Recordclass {0}", query. questions[0].             Recordclass.tostring ()); if(Query. questions[0]. Name.contains ("subB.mooo.com") {Console.WriteLine ("Contains subB.mooo.com"); Query. Answerrecords.add (NewArecord ("xxx.subB.mooo.com",3600, Ipaddress.parse ("9.9.9.9"))); Message. ReturnCode=Returncode.noerror; returnmessage; }            Else{Console.WriteLine ("!!! Not Contains mooo"); Message. ReturnCode=returncode.serverfailure; Console.WriteLine (message.                 Returncode.tostring ()); returnmessage; }        }    }}

Service-Side effects


C # Programming implements DNS client and server (GO)

Related Article

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.