Diy dns queryer: Program Implementation

Source: Internet
Author: User
Tags domain name server mx record

 

In the previous article, "diy dns queryer: Understanding the DNS protocol" describes the principle and data structure of the DNS query protocol. After two weeks of development, the queryserver was compiled. Some problems were encountered during the period, such:

1. Format of the RDData content in a Resource Record.

2. About the pointer to compression encoding.

3. The Code redundancy structure is unclear.

In particular, the problem of compression encoding has plagued me for a long time. I found a lot of Chinese materials and all said that when the length value is 192, It is a pointer, the content of the next byte is the offset position. However, this value is found to be "193" during the process and has been puzzled for a long time. Here I will explain:

 

Assume that the 13th bytes are 05-6c-69-78-69-6e-02-6d-6e.

Translated as "5-l-i-x-i-n-2-m-e"

And somewhere later, also appeared the lixin. me string, then will use pointer encoding: "c0-0c ".

C0 decimal is 192, which indicates that the following content is a pointer, 0c is an address, pointing to the location of data [12.

Why is there 193? You can understand it. Because one byte content is used to indicate the offset, and at most 256 bytes can be offset. Assume that the udp packet has a length of 500 bytes, however, it is useless to point to 300th bytes. So the actual offset is not determined by the oc byte content, the correct offset is: (Cn-C0) * 256 + 0c.

If the value is 193 and the next one is 1, the specific offset is (193-192) * 256 + 1.

Another thing to note is what the variable length Name field ends. There are 3 ending methods:

1. Length + content + ~ + Length 0

2. offset ID + offset

3. Length + content + ~ + Offset ID + offset

This problem can be solved successfully by referring to materials. Although I know that there must be content in rfc1034 and rfc1035, it is a pity that foreign languages are hard to understand and cannot be viewed, the Chinese and a small amount of foreign materials obtained by searching are not clear. Finally, I have to thank the TCP-IP detailed Volume 1: agreement chapter 14th Dns Protocol Introduction, although only a short 17 pages, but still help me solve the problem. So if you are engaged in the same agreement, you may wish to get your notebook first, or if you have any problems, you can go and check it out first.

 

Now let's talk about this program.

Based on the dns protocol structure, I divide the project into three major structures: MyDnsHeader. cs, MyDnsQuestion. cs, and MyDnsRecord. cs.

When sending a dns request, you only need to construct the MyDnsHeader and MyDnsQuestion structures, then use the GetBytes () function to obtain the constructed byte array, and then send it out through udp. Then accept the response from the server and Parse the received byte array using the Parse (byte [] recvData) method, finally, the query information is obtained through the attribute fields of these structures.

The resource records include A record, SOA record, TXT record, CNAME record, MX record, and NS record.

Sample Code:

MyDns mydns = new MyDns ();//

// Query the record of the lixin. me domain name on the 8.8.8.8 Domain Name Server,

If (! Mydns. Search ("lixin. me" QueryType. A, "8.8.8.8", null ))

{

// If the server returns an error message, the error message is displayed.

MessageBox. Show (mydns. header. RCODE. ToString ());

Return;

}

TxtInfo. Clear ();

TxtInfo. AppendText (string. Format ("Number of reply records: {0} \ n", mydns. header. ANCOUNT ));

TxtInfo. AppendText (string. Format ("number of additional records replied: {0} \ n", mydns. header. ARCOUNT ));

TxtInfo. AppendText (string. Format ("replies to authoritative records: {0}", mydns. header. NSCOUNT ));

TxtContent. Clear ();

Foreach (MyDnsRecord item in mydns. record. Records)

{

// Record the cyclic resources and print them out.

TxtContent. AppendText (item. QType. ToString () + "" + item. RDDate. ToString () + "\ n ");

}

Interface:

 

Code download and browsing:

I put the code on CodePlex.com. Address: http://mydnspackage.codeplex.com/

Welcome to the test. If an error is found, please let me know

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.