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 = 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.

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.