在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
转: 原文:https://blog.csdn.net/mumumuwudi/article/details/48722063 strace 命令不错,非常有用!! 使用strace命令分析一下, 系统调用过程: ________________________________________________________________
关于go dns解析的一些说明参照另一篇文章http://blog.csdn.net/mumumuwudi/article/details/48200505
其中的timeout 是 dns 超时时间 是在dnsconfig_unix.go 文件中读取 /etc/reslove.conf 的配置决定的.
总之,纯go语言的 DNS解析流程还是比较完善的~~ ——————————————————————————————
之前使用过GO语言写了一个实时图片下载程序,主要考虑到GO语言的DNS解析对协程支持友好, 即 package main import ( "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) } } 1. 编译上述程序:go build -o ss source-dns.go 2. 执行上述程序,得到进程号: 15097 root@fly:/home/fly/mm# ./ss & 3. 使用strace命令分析一下, 系统调用过程: strace -p 15097 -o aaa
使用strace命令分析一下, 系统调用过程:
上面得到的aaa 文件内如如下: restart_syscall(<... resuming interrupted futex ...>) = -1 ETIMEDOUT (Connection timed out) futex(0x60ac70, FUTEX_WAKE_PRIVATE, 1) = 1 openat(AT_FDCWD, "/etc/nsswitch.conf", O_RDONLY|O_CLOEXEC) = 3 epoll_create1(EPOLL_CLOEXEC) = 4 epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=716087048, u64=139737477062408}}) = -1 EPERM (Operation not permitted) epoll_ctl(4, EPOLL_CTL_DEL, 3, 0xc000045abc) = -1 EPERM (Operation not permitted) read(3, "# /etc/nsswitch.conf\n#\n# Example"..., 1024) = 513 read(3, "", 1024) = 0 close(3) = 0 openat(AT_FDCWD, "/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3 epoll_ctl(4, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=716087048, u64=139737477062408}}) = -1 EPERM (Operation not permitted) epoll_ctl(4, EPOLL_CTL_DEL, 3, 0xc00004599c) = -1 EPERM (Operation not permitted) fstat(3, {st_mode=S_IFREG|0644, st_size=726, ...}) = 0 read(3, "# This file is managed by man:sy"..., 65536) = 726 read(3, "", 64810) = 0 read(3, "", 65536) = 0 close(3) = 0 newfstatat(AT_FDCWD, "/etc/mdns.allow", 0xc000073078, 0) = -1 ENOENT (No such file or directory) getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=1024*1024}) = 0 socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3 connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) close(3) = 0 socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 3 connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) close(3) = 0 openat(AT_FDCWD, "/etc/nsswitch.conf", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=513, ...}) = 0 read(3, "# /etc/nsswitch.conf\n#\n# Example"..., 4096) = 513 read(3, "", 4096) = 0 close(3) = 0 stat("/etc/resolv.conf", {st_mode=S_IFREG|0644, st_size=726, ...}) = 0 openat(AT_FDCWD, "/etc/host.conf", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=92, ...}) = 0 read(3, "# The \"order\" line is only used "..., 4096) = 92 read(3, "", 4096) = 0 close(3) = 0 futex(0x7f172a74aba4, FUTEX_WAKE_PRIVATE, 2147483647) = 0 openat(AT_FDCWD, "/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=726, ...}) = 0 read(3, "# This file is managed by man:sy"..., 4096) = 726 read(3, "", 4096) = 0 close(3) = 0 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=30918, ...}) = 0 mmap(NULL, 30918, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f172ab8a000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P#\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=47568, ...}) = 0 mmap(NULL, 2168632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1725f34000 mprotect(0x7f1725f3f000, 2093056, PROT_NONE) = 0 mmap(0x7f172613e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xa000) = 0x7f172613e000 mmap(0x7f1726140000, 22328, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1726140000 close(3) = 0 mprotect(0x7f172613e000, 4096, PROT_READ) = 0 munmap(0x7f172ab8a000, 30918) = 0 openat(AT_FDCWD, "/etc/hosts", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=218, ...}) = 0 read(3, "127.0.0.1 localhost\n127.0.1.1 fl"..., 4096) = 218 read(3, "", 4096) = 0 close(3) = 0 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=30918, ...}) = 0 mmap(NULL, 30918, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f172ab8a000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libnss_dns.so.2", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\17\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=26936, ...}) = 0 mmap(NULL, 2121952, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1725d2d000 mprotect(0x7f1725d32000, 2097152, PROT_NONE) = 0 mmap(0x7f1725f32000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7f1725f32000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\00008\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=101168, ...}) = 0 mmap(NULL, 2206336, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1725b12000 mprotect(0x7f1725b29000, 2097152, PROT_NONE) = 0 mmap(0x7f1725d29000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x7f1725d29000 mmap(0x7f1725d2b000, 6784, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1725d2b000 close(3) = 0 mprotect(0x7f1725d29000, 4096, PROT_READ) = 0 mprotect(0x7f1725f32000, 4096, PROT_READ) = 0 munmap(0x7f172ab8a000, 30918) = 0 socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3 connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("127.0.0.53")}, 16) = 0 poll([{fd=3, events=POLLOUT}], 1, 0) = 1 ([{fd=3, revents=POLLOUT}]) sendmmsg(3, [{msg_hdr={msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\3t\1\0\0\1\0\0\0\0\0\1\3iot\10vidagrid\3com\0\0\1"..., iov_len=45}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, msg_len=45}, {msg_hdr={msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\315\202\1\0\0\1\0\0\0\0\0\1\3iot\10vidagrid\3com\0\0\34"..., iov_len=45}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_TRUNC|MSG_WAITALL|MSG_NOSIGNAL|MSG_MORE|MSG_WAITFORONE}, msg_len=45}], 2, MSG_NOSIGNAL) = 2 poll([{fd=3, events=POLLIN}], 1, 5000) = 1 ([{fd=3, revents=POLLIN}]) ioctl(3, FIONREAD, [61]) = 0 recvfrom(3, "\3t\201\200\0\1\0\1\0\0\0\1\3iot\10vidagrid\3com\0\0\1"..., 2048, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("127.0.0.53")}, [28->16]) = 61 poll([{fd=3, events=POLLIN}], 1, 4999) = 1 ([{fd=3, revents=POLLIN}]) ioctl(3, FIONREAD, [45]) = 0 recvfrom(3, "\315\202\201\200\0\1\0\0\0\0\0\1\3iot\10vidagrid\3com\0\0\34"..., 65536, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("127.0.0.53")}, [28->16]) = 45 close(3) = 0 futex(0x60ac70, FUTEX_WAKE_PRIVATE, 1) = 1 futex(0x60ab70, FUTEX_WAKE_PRIVATE, 1) = 1 write(1, "--120.78.15.160\n", 16) = 16 exit_group(0) = ? +++ exited with 0 +++
# coding=UTF-8 import threading import socket import socketserver class Handler(socketserver.BaseRequestHandler): def handle(self): request_data = self.request[0] # 将请求转发到 114 DNS redirect_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) redirect_socket.sendto(request_data, ('114.114.114.114', 53)) response_data, address = redirect_socket.recvfrom(1024) # 将114响应响应给客户 client_socket = self.request[1] client_socket.sendto(response_data, self.client_address) class Server(socketserver.ThreadingMixIn, socketserver.UDPServer): pass if __name__ == "__main__": # 一下ip需换成自己电脑的ip server = Server(('0.0.0.0',8088), Handler) with server: server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() print('The DNS server is running at 172.16.42.254...') server_thread.join()
|
请发表评论