分析go域名解析过程
package main
import (
"net"
"k8s.io/klog/v2"
)
func main() {
dns := "cn.bing.com"
if ip, err := net.LookupHost(dns); err != nil {
klog.Errorf("lookup %s failed, err is %v", dns, err)
} else {
klog.Infof("%s ip is %s", dns, ip)
}
}
[root@node1 use-k8s-queue]# strace ./lookup_dns
...
读取/etc/resolv.conf,保存nameserver
openat(AT_FDCWD, "/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3
epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=4247555832, u64=139693763890936}}) = -1 EPERM (Operation not permitted)
fstat(3, {st_mode=S_IFREG|0644, st_size=74, ...}) = 0
read(3, "# Generated by NetworkManager\nse"..., 65536) = 74
read(3, "", 65462) = 0
read(3, "", 65536) = 0
close(3) = 0
读取/etc/hosts,确认是否有记录匹配
newfstatat(AT_FDCWD, "/etc/hosts", {st_mode=S_IFREG|0644, st_size=199, ...}, 0) = 0
openat(AT_FDCWD, "/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=4247555832, u64=139693763890936}}) = -1 EPERM (Operation not permitted)
read(3, "127.0.0.1 localhost localhost."..., 65536) = 199
read(3, "", 65337) = 0
read(3, "", 65536) = 0
close(3) = 0
基于nameserver进行解析
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("192.168.0.1")}, 16) = 0
epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=4247555832, u64=139693763890936}}) = 0
getsockname(3, {sa_family=AF_INET, sin_port=htons(43297), sin_addr=inet_addr("192.168.0.105")}, [16]) = 0
getpeername(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("192.168.0.1")}, [16]) = 0
write(3, "\255Y\1\0\0\1\0\0\0\0\0\1\2cn\4bing\3com\0\0\34\0\1\0\0)"..., 40) = 40
read(3, 0xc000184000, 1232) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x6046a8, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
futex(0x6046a8, FUTEX_WAIT_PRIVATE, 0, NULL) = 0
epoll_pwait(4, [], 128, 0, NULL, 1) = 0
I0131 07:56:54.420358 26473 lookup_dns.go:14] cn.bing.com ip is [202.89.233.100 202.89.233.101]
epoll_pwait(4, <ptrace(SYSCALL):No such process>
+++ exited with 0 +++