Go (golang) DNS domain name resolution implementation

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

previously used the go language to write a real-time picture downloader, mainly considering that the go language of the DNS resolution of the support is friendly to the process, that

DNS parsing does not block the execution thread, will only block the current process, and by the way the net.lookuphost/resolveipaddr of Go

Implement Way. the following section describes the official documents translated from the Go language https://golang.org/pkg/net/ domain name resolution:

domain name parsing functions, dial functions are called indirectly, while Lokuphost and lookupaddr call the domain name resolution function directly, not with

operating system implementation is different, There are two methods for domain name resolution in UNIX systems:

1) Domain name resolution for pure Go language, remove local DNS server address list from/etc/resolv.conf, send DNS request (UDP

Messages) and get results

2) using the CGO method, will eventually be called to the C standard library of the getaddrinfo or Getnameinfo function (not recommended for the go process unfriendly )

For CGO DNS resolution pit refer to the following link

https://jira.mongodb.org/browse/MGO-41

https://github.com/golang/go/issues/8602#issuecomment-66098142

The Go language uses a pure go domain name resolution by default, because such a blocked DNS request consumes only a single thread, making in a CGO way

you will block a system thread, only some The system-provided CGO method is used under certain conditions, for example: 1) The program is not allowed in OS X system

Straight  To send a DNS request;  2) Localdomainh environment variable exists, even if it is empty; 3) Es_options or hostaliases or ASR

_config environment variable non-NULL, 4)/etc/resolv.conf or/etc/nsswitch.conf specified usage Go parser is not implemented; 5) When you want to solve

the domain name of the analysis end With. Local, or a MDNs domain name

the default DNS resolution for the go language can be set by Godebug environment variables pure go or CGO,
Export Godebug=netdns=go # force pure Go Resolver Pure go mode
Export GODEBUG=NETDNS=CGO # force CGO Resolver CGO Way
You can also specify the compile tag of Netgo or NETCGO at compile time to set
In Plan 9, domain name resolution can only be done through/net/cs and/net/dns

Domain name resolution in Windows is available only through the C standard library functions provided by Windows Getaddrinfo or DnsQuery

OK official description Read it, let's write an example and try it.

Package Mainimport ("NET" "FMT" "OS") Func main () {ns, err: = Net. Lookuphost ("www.baidu.com") if err! = Nil {fmt. fprintf (OS. Stderr, "Err:%s", err. Error ()) Return}for _, N: = range ns {FMT. fprintf (OS. Stdout, "--%s\n", N)}}


Using the Strace command to analyze the system call procedure

Openat (AT_FDCWD, "/etc/hosts", o_rdonly| o_cloexec) = 3read (3, "127.0.0.1 localhost localhost.")                                ..., 4096) = 158read (3, "", 3938) = 0read (3, "", 4096) = 0close (3) = 0//read local DNS server configuration stat ("/etc/resolv.conf", {st_mode=s_ifreg|0644, st_size=104, ...}) = 0//Create UDP so Cket send ready to send DNS request socket (pf_inet, sock_dgram| sock_cloexec| Sock_nonblock, ipproto_ip) = 3setsockopt (3, Sol_socket, So_broadcast, [1], 4) = 0connect (3, {sa_family=af_inet, sin_port= Htons (+), sin_addr=inet_addr ("172.16.1.3")}, +) = 0epoll_create1 (epoll_cloexec) = 4//add UDP socket to Epoll EP Oll_ctl (4, Epoll_ctl_add, 3, {epollin| Epollout| epollrdhup| Epollet, {u32=2130514816, u64=140679489328000}}) = 0getsockname (3, {sa_family=af_inet, sin_port=htons (57587), Sin_ ADDR=INET_ADDR ("10.0.2.15")}, [+]) = 0getpeername (3, {sa_family=af_inet, sin_port=htons (+), sin_addr=inet_addr (" 172.16.1.3 ")}, [16]) = 0//Send DNS request write (3," \363}\1\0\0\1\0\0\0\0\0\0\3www\5Baidu\3com\0\0\34\0\1 "," = 31futex (0X645C10, futex_wait, 0, NULL) = 0read (3, 0xc82007c000, up to) = 1 EA GAIN (Resource temporarily unavailable) epoll_wait (4, {{epollout, {u32=2130514816, u64=140679489328000}}, {epollout, { u32=2130514624, u64=140679489327808}}, 0) = 2//epoll waiting for Socket event Epoll_wait (4, {{epollin| Epollout, {u32=2130514624, u64=140679489327808}}, 1,-1) = 1futex (0x645680, futex_wake,) = 1read (5, "g\217\2 01\200\0\1\0\2\0\r\0\v\3www\5baidu\3com\0\0\1\0\1\300 "..., +) = 474epoll_ctl (4, Epoll_ctl_del, 5, {0, {u32=0, u64=0} }) = 0close (5) = 0epoll_wait (4, {}, +, 0) = 0epoll_wait (4, {epollin| Epollout, {u32=2130514816, u64=140679489328000}}, 1,-1) = 1futex (0x645680, futex_wake,) = 1//Get DNS parse result read (3, "\363}\201\200\0\1\0\1\0\1\0\0\3www\5baidu\3com\0\0\34\0\1\300" ..., +) = 115epoll_ctl (4, Epoll_ctl_del, 3, {0, { U32=0, u64=0}}) = 0close (3) = 0

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.