Go (Golang) DNS parsing source Go/src/net/dnsclient_unix.go analysis

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Some instructions on Go DNS parsing refer to another article http://blog.csdn.net/mumumuwudi/article/details/48200505

Go DNS resolves source code in Go/src/net/dnsclient_unix.go, Lookuphost () obtains IP and domain name by sending a request to the local DNS server

The corresponding relationship is then returned, and the function call relationship is as follows:

Lookuphost ()
->golookuphostorder ()
-->golookupiporder ()
--->tryonename ()
---->exchange ()

Func Exchange (server, name string, Qtype uint16, timeout time. Duration) (*dnsmsg, error) {d: = dialer{timeout:timeout}out: = Dnsmsg{dnsmsghdr:dnsmsghdr{recursion_desired:true,}, Question: []dnsquestion{{name, Qtype, Dnsclassinet},},}for _, Network: = range []string{"UDP", "TCP"} {c, err: = D.dialdns (Network, server)    Create UDPIF Err! = Nil {return nil, Err}defer c.close () If timeout > 0 {c.setdeadline (time. Now (). ADD (timeout))}out.id = UInt16 (rand. Int ()) ^ UInt16 (time. Now (). Unixnano ()) If err: = C.writednsquery (&out); Err! = Nil {   //Send DNS request return nil, err}in, err: = C.readdnsresponse ()   //Parse DNS request get IPIF Err! = Nil {return nil, err}i F in.id! = Out.id {return nil, errors. New ("DNS message ID mismatch")}if in.truncated {//See RFCs 5966continue}return in, Nil}return Nil, errors. New ("No answer from DNS server")}

Where timeout is the DNS time-out is determined by the configuration of the read/etc/reslove.conf in the Dnsconfig_unix.go file

The Dialtimeout function in Net.go will also go to the DNS resolution process, which will eventually call to Lookupipdeadline to enable a new co-

Process to parse DNS, the specific call stack is as follows:

Dialtimeout ()
->resolveaddrlist ()
-->internetaddrlist ()
--->lookupipdeadline ()
---->lookupgroup.dochan () to do DNS resolution in the new process
----->LOOKUPIP ()
------>golookupiporder ()

In short, the Pure Go language DNS parsing process is still relatively perfect ~ ~

Copyright NOTICE: Reprint Please indicate the source, thank Http://blog.csdn.net/mumumuwudi

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.