{"id":361842,"date":"2024-05-21T01:22:16","date_gmt":"2024-05-21T01:22:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=361842"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=361842","title":{"rendered":"<span>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 Go, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e RFC<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0432\u043e\u0435\u043c \u043e\u043f\u044b\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b \u044f \u0435\u0433\u043e &#171;\u0447\u0438\u0441\u0442\u043e \u043f\u043e\u0432\u0435\u0441\u0435\u043b\u0438\u0442\u044c\u0441\u044f&#187;, \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 RFC.<\/p>\n<h3>DNS \u0441\u0435\u0440\u0432\u0435\u0440<\/h3>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e-\u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u0432 \u0447\u0435\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b DNS \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432. \u0427\u0442\u043e\u0431\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u0432\u044b \u0437\u0430\u0448\u043b\u0438 \u043d\u0430 \u0425\u0430\u0431\u0440, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0432\u044b \u0432\u0432\u0435\u043b\u0438 <a href=\"http:\/\/www.habr.com\" rel=\"noopener noreferrer nofollow\">www.habr.com<\/a>, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0436\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0434\u043e\u043c\u0435\u043d \u0432 ip \u0430\u0434\u0440\u0435\u0441, \u043f\u043e \u0442\u0438\u043f\u0443 178.248.237.68:443, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441. \u0414\u043e\u043c\u0435\u043d\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442, \u0447\u0442\u043e\u0431\u044b \u043b\u044e\u0434\u0438 \u043d\u0435 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438 \u044d\u0442\u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0447\u0438\u0441\u0435\u043b, \u0430 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043d\u0430\u043c \u0441\u043b\u043e\u0432\u0430. DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0436\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u044f\u0442 \u044d\u0442\u0438 \u0434\u043e\u043c\u0435\u043d\u044b \u0432 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0432\u0438\u0434.<br \/> \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044f, \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043d\u0430\u044f \u043a\u043d\u0438\u0436\u043a\u0430. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u043d\u043e\u043c\u0435\u0440\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0430\u043a\u0442 \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0430\u043c \u0432 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043d\u043e\u0439 \u043a\u043d\u0438\u0436\u043a\u0435.<\/p>\n<h3>DNS \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b<\/h3>\n<p>DNS \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 UDP. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 \u0441\u0443\u0449\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.1\" rel=\"noopener noreferrer nofollow\">\u0444\u043e\u0440\u043c\u0430\u0442<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f &#171;\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435&#187;.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e7a\/afe\/3b7\/e7aafe3b74d61af8adfe8b5a1313c798.png\" alt=\"\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 DNS \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\" title=\"\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 DNS \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\" width=\"683\" height=\"237\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e7a\/afe\/3b7\/e7aafe3b74d61af8adfe8b5a1313c798.png\"\/><\/p>\n<div><figcaption>\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 DNS \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c DNS-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 DNS-\u043e\u0442\u0432\u0435\u0442 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f &#8212; 512 \u0431\u0430\u0439\u0442, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.2.1\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/a>. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0437\u0436\u0435 \u0438 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<h3>\u041d\u0430\u0447\u0430\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438<\/h3>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0434\u043d\u0438\u043c\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 UDP \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u043e\u0442\u0434\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u044b, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445.<\/p>\n<p>\u041a\u043e\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<pre><code class=\"go\">package main      import (       \"fmt\"       \"log\"         \"net\" )      const Address = \"127.0.0.1:2053\"      func main() {       udpAddr, err := net.ResolveUDPAddr(\"udp\", Address)       if err != nil {          log.Fatal(\"failed to resolve udp address\", err)       }          udpConn, err := net.ListenUDP(\"udp\", udpAddr)       if err != nil {          log.Fatal(\"failed to to bind to address\", err)       }       defer udpConn.Close()          log.Printf(\"started server on %s\", Address)    \/\/ \u0440\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0444\u0435\u043d\u0440\u0430 512 \u0431\u0430\u0439\u0442 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     buf := make([]byte, 512)       for {          size, source, err := udpConn.ReadFromUDP(buf)          if err != nil {             log.Println(\"failed to receive data\", err)             break          }             data := string(buf[:size])             log.Printf(\"received %d bytes from %s: %s\", size, source.String(), data)             response := []byte{} \/\/ \u043f\u0443\u0441\u0442\u043e\u0439 \u043e\u0442\u0432\u0435\u0442          _, err = udpConn.WriteToUDP(response, source)          if err != nil {             fmt.Println(\"Failed to send response:\", err)          }       }   } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u043e\u0434\u0430<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/mr\/ku\/9x\/mrku9xhgphgalp8xugb8uiit5n4.gif\" width=\"953\" height=\"304\" data-src=\"https:\/\/habrastorage.org\/webt\/mr\/ku\/9x\/mrku9xhgphgalp8xugb8uiit5n4.gif\"\/><\/figure>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u0442\u0438\u043b\u0438\u0442\u044b nc \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u043a UDP \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441. \u041f\u0440\u043e \u0443\u0442\u0438\u043b\u0438\u0442\u0443 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/netcat.sourceforge.net\/\" rel=\"noopener noreferrer nofollow\">https:\/\/netcat.sourceforge.net\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/657613\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/657613\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/678968\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/678968\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/336596\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/336596\/<\/a><\/p>\n<\/li>\n<\/ul>\n<h3>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f<\/h3>\n<p>\u041a\u0430\u043a \u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b \u0432\u044b\u0448\u0435, \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 \u0435\u0441\u0442\u044c 5 \u0441\u0435\u043a\u0446\u0438\u0439, \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.1.1\" rel=\"noopener noreferrer nofollow\">Header<\/a> (\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a)<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/e2\/lk\/ew\/e2lkewfxiyqhuv1xiabbzcrjh7e.png\" alt=\"\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\" title=\"\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\" width=\"499\" height=\"326\" data-src=\"https:\/\/habrastorage.org\/webt\/e2\/lk\/ew\/e2lkewfxiyqhuv1xiabbzcrjh7e.png\"\/><\/p>\n<div><figcaption>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 <strong>\u0412\u0421\u0415\u0413\u0414\u0410<\/strong> 12 \u0431\u0430\u0439\u0442, \u0430 \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 Big-Endian. \u042d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0435\u0439 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435, \u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0432\u0430\u0436\u043d\u044b\u0435, \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e:<\/p>\n<ul>\n<li>\n<p>ID, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, ID \u043e\u0442\u0432\u0435\u0442\u0430 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0432\u0435\u043d ID \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>QR, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0438 0 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>RCODE, \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430, 0 (no error)<\/p>\n<\/li>\n<li>\n<p>QDCOUNT, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432\/\u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0446\u0438\u0438 Questions \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>ANCOUNT, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0441\u0435\u043a\u0446\u0438\u0438 Answers \u0432 \u043e\u0442\u0432\u0435\u0442\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 Go \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"go\">type Header struct {       PacketID uint16       QR       uint16       OPCODE   uint16       AA       uint16       TC       uint16       RD       uint16       RA       uint16       Z        uint16       RCode    uint16       QDCount  uint16       ANCount  uint16       NSCount  uint16       ARCount  uint16   }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043a \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043e\u0442\u0432\u0435\u0442\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 12 \u0431\u0430\u0439\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"go\">func ReadHeader(buf []byte) Header {       h := Header{          ID:      uint16(buf[0])&lt;&lt;8 | uint16(buf[1]),          QR:      1,  \/\/ \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 1, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0442        OPCODE:  uint16((buf[2] &lt;&lt; 1) >> 4),          AA:      uint16((buf[2] &lt;&lt; 5) >> 7),          TC:      uint16((buf[2] &lt;&lt; 6) >> 7),          RD:      uint16((buf[2] &lt;&lt; 7) >> 7),          RA:      uint16(buf[3] >> 7),          Z:       uint16((buf[3] &lt;&lt; 1) >> 5),          QDCOUNT: uint16(buf[4])&lt;&lt;8 | uint16(buf[5]),          ANCOUNT: uint16(buf[5])&lt;&lt;8 | uint16(buf[7]),          NSCOUNT: uint16(buf[8])&lt;&lt;8 | uint16(buf[9]),          ARCOUNT: uint16(buf[10])&lt;&lt;8 | uint16(buf[11]),       }      \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 OPCODE \u043d\u0435 \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e, \u0442\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u0441 \u043a\u043e\u0434\u043e\u043c \u043e\u0448\u0438\u0431\u043a\u0438 4     if h.OPCODE == 0 {          h.RCODE = 0       } else {          h.RCODE = 4       }          return h   }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u043f\u043e \u043a\u043e\u0434\u0443, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0435 \u0441\u0434\u0432\u0438\u0433\u0438. \u0412\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u0444\u0435\u0442\u0447\u0438\u043c \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. <\/p>\n<p>\u041d\u043e \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u043e \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0431\u0430\u0439\u0442\u044b. \u041e\u0442\u0432\u0435\u0442\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043f\u043e\u0442\u043e\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438. \u0412\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430:<\/p>\n<pre><code class=\"go\">func (h Header) Encode() []byte { dnsHeader := make([]byte, 12)  var flags uint16 = 0 flags = h.QR&lt;&lt;15 | h.OPCODE&lt;&lt;11 | h.AA&lt;&lt;10 | h.TC&lt;&lt;9 | h.RD&lt;&lt;8 | h.RA&lt;&lt;7 | h.Z&lt;&lt;4 | h.RCode  binary.BigEndian.PutUint16(dnsHeader[0:2], h.PacketID) binary.BigEndian.PutUint16(dnsHeader[2:4], flags) binary.BigEndian.PutUint16(dnsHeader[4:6], h.QDCount) binary.BigEndian.PutUint16(dnsHeader[6:8], h.ANCount) binary.BigEndian.PutUint16(dnsHeader[8:10], h.NSCount) binary.BigEndian.PutUint16(dnsHeader[10:12], h.ARCount)  return dnsHeader }<\/code><\/pre>\n<p>\u0411\u0438\u0442\u043e\u0432\u044b\u0435 \u0441\u0434\u0432\u0438\u0433\u0438 \u043d\u0430\u0448\u0435 \u0432\u0441\u0435!<br \/>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435, \u0433\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0437\u0430\u0433\u0430\u043b\u043e\u0432\u043a\u0435<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/6l\/c2\/dh\/6lc2dh233betyqcbaoscwydtw3y.png\" alt=\"\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\" title=\"\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\" width=\"500\" height=\"321\" data-src=\"https:\/\/habrastorage.org\/webt\/6l\/c2\/dh\/6lc2dh233betyqcbaoscwydtw3y.png\"\/><\/p>\n<div><figcaption>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"go\">header := ReadHeader(buf[:12]) log.Printf(\"ID: %d; QR: %d; QDCount: %d\\n\", header.PacketID, header.QR, header.QDCount)  response := header.Encode() _, err = udpConn.WriteToUDP(response, source)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430, \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 DNS \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0430 Python<\/p>\n<pre><code class=\"python\">import socket   def build_dns_query():     header = bytearray([         0x00, 0x01,  # Transaction ID         0x00, 0x00,  # Flags: Standard query         0x00, 0x01,  # Questions         0x00, 0x00,  # Answer RRs         0x00, 0x00,  # Authority RRs         0x00, 0x00   # Additional RRs     ])      return header   def send_dns_query(query, server, port=2053):     with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:         s.sendto(query, (server, port))         response, _ = s.recvfrom(1024)     return response   if __name__ == \"__main__\":     dns_server = \"127.0.0.1\"     dns_query = build_dns_query()     dns_response = send_dns_query(dns_query, dns_server)<\/code><\/pre>\n<figure class=\"float\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/cb\/jc\/ii\/cbjcii2ypghntrahpeuveqeayli.png\" width=\"244\" height=\"305\" data-src=\"https:\/\/habrastorage.org\/webt\/cb\/jc\/ii\/cbjcii2ypghntrahpeuveqeayli.png\"\/><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 \u043b\u043e\u0433\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435.<br \/>\u0414\u0435\u0431\u0430\u0433\u0435\u0440, \u043d\u0443 \u0447\u0442\u043e\u0431\u044b \u0442\u043e\u0447\u043d\u043e \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f)<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/92\/l0\/yd\/92l0ydlal03hddifo4k7-__rixi.png\" alt=\"\" title=\"\" width=\"537\" height=\"86\" data-src=\"https:\/\/habrastorage.org\/webt\/92\/l0\/yd\/92l0ydlal03hddifo4k7-__rixi.png\"\/><\/figure>\n<p>\u0411\u0430\u043b\u0434\u0435\u0436<\/p>\n<h2>Questions<\/h2>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#autoid-42\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043f\u0440\u043e\u0441\u044b<\/a> (\u0432\u043e\u043f\u0440\u043e\u0441\u044b), \u043a\u0430\u043a \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u043e, \u0432\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c DNS \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0440\u0430\u0432\u043d\u043e 1, \u043d\u043e \u0431\u044b\u0432\u0430\u0435\u0442 \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432\/\u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043c\u0435\u0435\u0442 \u043a\u0443\u0434\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u043e\u043b\u0435\u0439.  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/xb\/ka\/a0\/xbkaa0p6jltpxvo9dx4idxbwh-w.png\" width=\"533\" height=\"253\" data-src=\"https:\/\/habrastorage.org\/webt\/xb\/ka\/a0\/xbkaa0p6jltpxvo9dx4idxbwh-w.png\"\/><\/figure>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0437\u043c\u0435\u0431\u0435\u0440\u0435\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0432 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438<\/p>\n<ul>\n<li>\n<p>QName, \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043b\u0435\u0439\u0431\u043b\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f habr.com \u0431\u0443\u0434\u0435\u0442 \u0434\u0432\u0430 \u043b\u0435\u0439\u0431\u043b\u0430: habr \u0438 com.  <\/p>\n<\/li>\n<li>\n<p>QType, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c. \u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0410, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0410 &#8212; \u0430\u0434\u0440\u0435\u0441 \u0445\u043e\u0441\u0442\u0430, \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0438\u043f\u043e\u0432 <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1035#section-3.2.2\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<\/li>\n<li>\n<p>QClass, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 IN, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e IN &#8212; the Internet \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043b\u0430\u0441\u0441\u043e\u0432 <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1035#section-3.2.4\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u043f\u043b\u043e\u0448\u043d\u044b\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u043c, \u0430 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u0435\u0439\u0431\u043b\u043e\u0432 &lt;length>&lt;label>, \u0433\u0434\u0435 <\/p>\n<ul>\n<li>\n<p>&lt;length> &#8212; \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0431\u0430\u0439\u0442,  \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0434\u043b\u0438\u043d\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043b\u0435\u0439\u0431\u043b\u0430<\/p>\n<\/li>\n<li>\n<p>&lt;label> &#8212; \u0441\u0430\u043c \u043b\u0435\u0439\u0431\u043b<\/p>\n<\/li>\n<li>\n<p>\\x00 &#8212; \u0431\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u0435\u0439\u0431\u043b\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440, habr.com \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442\u044c \u0442\u0430\u043a<\/p>\n<pre><code>\\x04habr\\x03com\\x00<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f \u0434\u043b\u044f QClass \u0438 QType. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0435 \u0435\u0434\u0438\u043d\u0438\u0446\u044b, \u043d\u043e \u043c\u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0431\u043b\u0438\u0436\u0435 <\/p>\n<pre><code class=\"go\">type Class uint16  const ( _ Class = iota IN CS CH HS )  type Type uint16  const ( _ Type = iota A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX TXT )  type Question struct { QName  string QType  Type QClass Class }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<pre><code class=\"go\">func ReadQuestion(buf []byte) Question { start := 0 var nameParts []string  for len := buf[start]; len != 0; len = buf[start] { start++ nameParts = append(nameParts, string(buf[start:start+int(len)])) start += int(len) } questionName := strings.Join(nameParts, \".\") start++  questionType := binary.BigEndian.Uint16(buf[start : start+2]) questionClass := binary.BigEndian.Uint16(buf[start+2 : start+4])  q := Question{ QName:  questionName, QType:  Type(questionType), QClass: Class(questionClass), }  return q }  func (q Question) Encode() []byte { domain := q.QName parts := strings.Split(domain, \".\")  var buf bytes.Buffer  for _, label := range parts { if len(label) > 0 { buf.WriteByte(byte(len(label))) buf.WriteString(label) } } buf.WriteByte(0x00) buf.Write(intToBytes(uint16(q.QType))) buf.Write(intToBytes(uint16(q.QClass)))  return buf.Bytes() }<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 main \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"go\">header := ReadHeader(buf[:12]) log.Printf(\"ID: %d; QR: %d; QDCount: %d\\n\", header.PacketID, header.QR, header.QDCount)  question := ReadQuestion(buf[12:])  var res bytes.Buffer res.Write(header.Encode()) res.Write(question.Encode())  _, err = udpConn.WriteToUDP(res.Bytes(), source)<\/code><\/pre>\n<p>\u0418 \u0447\u0443\u0442\u043e\u043a \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043c python \u043a\u043b\u0438\u0435\u043d\u0442<\/p>\n<pre><code class=\"python\">import socket   def build_dns_query(domain: str):     header = bytearray([         0x00, 0x01,  # Transaction ID         0x00, 0x00,  # Flags: Standard query         0x00, 0x01,  # Questions         0x00, 0x00,  # Answer RRs         0x00, 0x00,  # Authority RRs         0x00, 0x00   # Additional RRs     ])      question = bytearray()     labels = domain.split('.')     for label in labels:         question.append(len(label))         question.extend(label.encode('utf-8'))     question.extend([0x00, 0x00, 0x01, 0x00, 0x01])  # QTYPE and QCLASS (A record, Internet)      return header + question   def send_dns_query(query, server, port=2053):     with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:         s.sendto(query, (server, port))         response, _ = s.recvfrom(1024)     return response   def parse_dns_response(response):     print(response)     print(response.hex())   if __name__ == \"__main__\":     dns_server = \"127.0.0.1\"     domain = \"habr.com\"     dns_query = build_dns_query(domain)     dns_response = send_dns_query(dns_query, dns_server)     parse_dns_response(dns_response) <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u043e\u0432\u0430 \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u0431\u043e\u0442\u0435<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/lz\/vy\/do\/lzvydol6m2jxe9lqquwhlwwqwfw.png\" alt=\"\u0420\u0430\u0441\u043f\u0440\u0430\u0441\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\" title=\"\u0420\u0430\u0441\u043f\u0440\u0430\u0441\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\" width=\"260\" height=\"96\" data-src=\"https:\/\/habrastorage.org\/webt\/lz\/vy\/do\/lzvydol6m2jxe9lqquwhlwwqwfw.png\"\/><\/p>\n<div><figcaption>\u0420\u0430\u0441\u043f\u0440\u0430\u0441\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441<\/figcaption><\/div>\n<\/figure>\n<h2>Answers<\/h2>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.1.3\" rel=\"noopener noreferrer nofollow\">\u041e\u0442\u0432\u0435\u0442<\/a> &#8212; \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043f\u043e\u043b\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u0438 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f IP \u0430\u0434\u0440\u0435\u0441 \u0445\u043e\u0441\u0442\u0430.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/js\/nu\/oe\/jsnuoe7q-t9nggu0gypv7rknpni.png\" alt=\"\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0442\u0432\u0435\u0442\u0430\" title=\"\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0442\u0432\u0435\u0442\u0430\" width=\"506\" height=\"412\" data-src=\"https:\/\/habrastorage.org\/webt\/js\/nu\/oe\/jsnuoe7q-t9nggu0gypv7rknpni.png\"\/><\/p>\n<div><figcaption>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0442\u0432\u0435\u0442\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442\u0435 \u043c\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 \u043f\u043e\u043b\u044f, \u043d\u043e \u0438\u0437 \u043d\u043e\u0432\u044b\u0445 \u0442\u0443\u0442<\/p>\n<ul>\n<li>\n<p>TTL &#8212; time-to-live, \u043f\u0435\u0440\u0438\u043e\u0434 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u0440\u0430\u0437\u043c\u0435\u0440 32 \u0431\u0438\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>RDLENGHT &#8212; \u0434\u043b\u0438\u043d\u0430 RDATA, \u0442\u0430\u043a \u043a\u0430\u043a IP \u0430\u0434\u0440\u0435\u0441 \u044d\u0442\u043e 4 \u0431\u0430\u0439\u0442\u0430, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u043e 4, \u0440\u0430\u0437\u043c\u0435\u0440 32 \u0431\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>RDATA &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u043c \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430 IP \u0430\u0434\u0440\u0435\u0441, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 8.8.8.8<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u0438, \u0441\u0430\u043c\u043e \u0441\u043e\u0431\u043e\u0439, \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 <\/p>\n<pre><code class=\"go\">type Answer struct { Name string Type Type Class Class TTL uint32 Length uint32 Data [4]uint8 }  func (a Answer) Encode() []byte { var rrBytes []byte  domain := a.Name parts := strings.Split(domain, \".\")  for _, label := range parts { if len(label) > 0 { rrBytes = append(rrBytes, byte(len(label))) rrBytes = append(rrBytes, []byte(label)...) } } rrBytes = append(rrBytes, 0x00)  rrBytes = append(rrBytes, intToBytes(uint16(a.Type))...) rrBytes = append(rrBytes, intToBytes(uint16(a.Class))...)  time := make([]byte, 4) binary.BigEndian.PutUint32(time, a.TTL)  rrBytes = append(rrBytes, time...) rrBytes = append(rrBytes, intToBytes(a.Length)...)  ipBytes, err := net.IPv4(a.Data[0], a.Data[1], a.Data[2], a.Data[3]).MarshalText() if err != nil { return nil }  rrBytes = append(rrBytes, ipBytes...)  return rrBytes } <\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u043a\u0438\u043d\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0430\u043f\u0443, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u043e\u043c\u0435\u043d\u0430 \u043a \u0435\u0433\u043e IP<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/-l\/j4\/gc\/-lj4gc98qv9eoupwx4tv9e4fq0m.png\" alt=\"\u0430\u0439\u043f\u0438\u0448\u043d\u0438\u043a\u0438\" title=\"\u0430\u0439\u043f\u0438\u0448\u043d\u0438\u043a\u0438\" width=\"386\" height=\"142\" data-src=\"https:\/\/habrastorage.org\/webt\/-l\/j4\/gc\/-lj4gc98qv9eoupwx4tv9e4fq0m.png\"\/><\/p>\n<div><figcaption>\u0430\u0439\u043f\u0438\u0448\u043d\u0438\u043a\u0438<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"go\">answer := Answer{     Name:   question.QName,     Type:   A,     Class:  IN,     TTL:    0,     Length: net.IPv4len,     Data:   nameToIP[question.QName], }  var res bytes.Buffer res.Write(header.Encode()) res.Write(question.Encode()) res.Write(answer.Encode())  _, err = udpConn.WriteToUDP(res.Bytes(), source)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 Python \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430\u0448 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 IP \u0430\u0434\u0440\u0435\u0441<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/iw\/zu\/ga\/iwzugaziwq7igutyrmef5xew8to.png\" alt=\"ip\" title=\"ip\" width=\"259\" height=\"84\" data-src=\"https:\/\/habrastorage.org\/webt\/iw\/zu\/ga\/iwzugaziwq7igutyrmef5xew8to.png\"\/><\/p>\n<div><figcaption>ip<\/figcaption><\/div>\n<\/figure>\n<h2>\u0420\u0435\u0437\u044e\u043c\u0435<\/h2>\n<p>\u041d\u0443 \u043f\u043e\u0434\u0432\u043e\u0434\u044f \u0438\u0442\u043e\u0433\u0438, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e \u0443\u043c\u0435\u043d\u0438\u044f\u043c \u0440\u0430\u0431\u043e\u0447\u0438\u0439 DNS \u0441\u0435\u0440\u0432\u0435\u0440.<br \/>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0432\u0430\u043c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f!<\/p>\n<p>P.S. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043e\u043f\u0435\u0447\u0430\u0442\u043e\u043a, \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/777996\/\"> https:\/\/habr.com\/ru\/articles\/777996\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0432\u043e\u0435\u043c \u043e\u043f\u044b\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0420\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b \u044f \u0435\u0433\u043e &#171;\u0447\u0438\u0441\u0442\u043e \u043f\u043e\u0432\u0435\u0441\u0435\u043b\u0438\u0442\u044c\u0441\u044f&#187;, \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 RFC.<\/p>\n<h3>DNS \u0441\u0435\u0440\u0432\u0435\u0440<\/h3>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u043f\u043e-\u0431\u044b\u0441\u0442\u0440\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u0432 \u0447\u0435\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b DNS \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432. \u0427\u0442\u043e\u0431\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u0432\u044b \u0437\u0430\u0448\u043b\u0438 \u043d\u0430 \u0425\u0430\u0431\u0440, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0432\u044b \u0432\u0432\u0435\u043b\u0438 <a href=\"http:\/\/www.habr.com\" rel=\"noopener noreferrer nofollow\">www.habr.com<\/a>, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0436\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0434\u043e\u043c\u0435\u043d \u0432 ip \u0430\u0434\u0440\u0435\u0441, \u043f\u043e \u0442\u0438\u043f\u0443 178.248.237.68:443, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441. \u0414\u043e\u043c\u0435\u043d\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442, \u0447\u0442\u043e\u0431\u044b \u043b\u044e\u0434\u0438 \u043d\u0435 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438 \u044d\u0442\u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0447\u0438\u0441\u0435\u043b, \u0430 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043d\u0430\u043c \u0441\u043b\u043e\u0432\u0430. DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0436\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u044f\u0442 \u044d\u0442\u0438 \u0434\u043e\u043c\u0435\u043d\u044b \u0432 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0432\u0438\u0434.<br \/> \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044f, \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043d\u0430\u044f \u043a\u043d\u0438\u0436\u043a\u0430. \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0435 \u043d\u043e\u043c\u0435\u0440\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0430\u043a\u0442 \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0430\u043c \u0432 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u043d\u043e\u0439 \u043a\u043d\u0438\u0436\u043a\u0435.<\/p>\n<h3>DNS \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b<\/h3>\n<p>DNS \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 UDP. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 \u0441\u0443\u0449\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.1\" rel=\"noopener noreferrer nofollow\">\u0444\u043e\u0440\u043c\u0430\u0442<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f &#171;\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435&#187;.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 DNS \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f<\/figcaption><\/div>\n<\/figure>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c DNS-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 DNS-\u043e\u0442\u0432\u0435\u0442 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f &#8212; 512 \u0431\u0430\u0439\u0442, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.2.1\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/a>. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0437\u0436\u0435 \u0438 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<h3>\u041d\u0430\u0447\u0430\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438<\/h3>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0434\u043d\u0438\u043c\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 UDP \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u043e\u0442\u0434\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u044b, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445.<\/p>\n<p>\u041a\u043e\u0434 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<pre><code class=\"go\">package main      import (       \"fmt\"       \"log\"         \"net\" )      const Address = \"127.0.0.1:2053\"      func main() {       udpAddr, err := net.ResolveUDPAddr(\"udp\", Address)       if err != nil {          log.Fatal(\"failed to resolve udp address\", err)       }          udpConn, err := net.ListenUDP(\"udp\", udpAddr)       if err != nil {          log.Fatal(\"failed to to bind to address\", err)       }       defer udpConn.Close()          log.Printf(\"started server on %s\", Address)    \/\/ \u0440\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0444\u0435\u043d\u0440\u0430 512 \u0431\u0430\u0439\u0442 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438     buf := make([]byte, 512)       for {          size, source, err := udpConn.ReadFromUDP(buf)          if err != nil {             log.Println(\"failed to receive data\", err)             break          }             data := string(buf[:size])             log.Printf(\"received %d bytes from %s: %s\", size, source.String(), data)             response := []byte{} \/\/ \u043f\u0443\u0441\u0442\u043e\u0439 \u043e\u0442\u0432\u0435\u0442          _, err = udpConn.WriteToUDP(response, source)          if err != nil {             fmt.Println(\"Failed to send response:\", err)          }       }   } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u043e\u0434\u0430<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u0442\u0438\u043b\u0438\u0442\u044b nc \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u043a UDP \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441. \u041f\u0440\u043e \u0443\u0442\u0438\u043b\u0438\u0442\u0443 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/netcat.sourceforge.net\/\" rel=\"noopener noreferrer nofollow\">https:\/\/netcat.sourceforge.net\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/657613\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/657613\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/678968\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/678968\/<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/336596\/\" rel=\"noopener noreferrer nofollow\">https:\/\/habr.com\/ru\/articles\/336596\/<\/a><\/p>\n<\/li>\n<\/ul>\n<h3>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f<\/h3>\n<p>\u041a\u0430\u043a \u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b \u0432\u044b\u0448\u0435, \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 \u0435\u0441\u0442\u044c 5 \u0441\u0435\u043a\u0446\u0438\u0439, \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#section-4.1.1\" rel=\"noopener noreferrer nofollow\">Header<\/a> (\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a)<\/p>\n<figure class=\"\">\n<div><figcaption>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438 <strong>\u0412\u0421\u0415\u0413\u0414\u0410<\/strong> 12 \u0431\u0430\u0439\u0442, \u0430 \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 Big-Endian. \u042d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u0435\u0439 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435, \u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0432\u0430\u0436\u043d\u044b\u0435, \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e:<\/p>\n<ul>\n<li>\n<p>ID, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, ID \u043e\u0442\u0432\u0435\u0442\u0430 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0432\u0435\u043d ID \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>QR, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u0438 0 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>RCODE, \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430, 0 (no error)<\/p>\n<\/li>\n<li>\n<p>QDCOUNT, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432\/\u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0446\u0438\u0438 Questions \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>ANCOUNT, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0441\u0435\u043a\u0446\u0438\u0438 Answers \u0432 \u043e\u0442\u0432\u0435\u0442\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 Go \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"go\">type Header struct {       PacketID uint16       QR       uint16       OPCODE   uint16       AA       uint16       TC       uint16       RD       uint16       RA       uint16       Z        uint16       RCode    uint16       QDCount  uint16       ANCount  uint16       NSCount  uint16       ARCount  uint16   }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043a \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043e\u0442\u0432\u0435\u0442\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 12 \u0431\u0430\u0439\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"go\">func ReadHeader(buf []byte) Header {       h := Header{          ID:      uint16(buf[0])&lt;&lt;8 | uint16(buf[1]),          QR:      1,  \/\/ \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 1, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0442        OPCODE:  uint16((buf[2] &lt;&lt; 1) >> 4),          AA:      uint16((buf[2] &lt;&lt; 5) >> 7),          TC:      uint16((buf[2] &lt;&lt; 6) >> 7),          RD:      uint16((buf[2] &lt;&lt; 7) >> 7),          RA:      uint16(buf[3] >> 7),          Z:       uint16((buf[3] &lt;&lt; 1) >> 5),          QDCOUNT: uint16(buf[4])&lt;&lt;8 | uint16(buf[5]),          ANCOUNT: uint16(buf[5])&lt;&lt;8 | uint16(buf[7]),          NSCOUNT: uint16(buf[8])&lt;&lt;8 | uint16(buf[9]),          ARCOUNT: uint16(buf[10])&lt;&lt;8 | uint16(buf[11]),       }      \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 OPCODE \u043d\u0435 \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e, \u0442\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u0441 \u043a\u043e\u0434\u043e\u043c \u043e\u0448\u0438\u0431\u043a\u0438 4     if h.OPCODE == 0 {          h.RCODE = 0       } else {          h.RCODE = 4       }          return h   }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u043f\u043e \u043a\u043e\u0434\u0443, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0435 \u0441\u0434\u0432\u0438\u0433\u0438. \u0412\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u0444\u0435\u0442\u0447\u0438\u043c \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. <\/p>\n<p>\u041d\u043e \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u043e \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0431\u0430\u0439\u0442\u044b. \u041e\u0442\u0432\u0435\u0442\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043f\u043e\u0442\u043e\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0435\u043a\u0446\u0438\u0438. \u0412\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430:<\/p>\n<pre><code class=\"go\">func (h Header) Encode() []byte { dnsHeader := make([]byte, 12)  var flags uint16 = 0 flags = h.QR&lt;&lt;15 | h.OPCODE&lt;&lt;11 | h.AA&lt;&lt;10 | h.TC&lt;&lt;9 | h.RD&lt;&lt;8 | h.RA&lt;&lt;7 | h.Z&lt;&lt;4 | h.RCode  binary.BigEndian.PutUint16(dnsHeader[0:2], h.PacketID) binary.BigEndian.PutUint16(dnsHeader[2:4], flags) binary.BigEndian.PutUint16(dnsHeader[4:6], h.QDCount) binary.BigEndian.PutUint16(dnsHeader[6:8], h.ANCount) binary.BigEndian.PutUint16(dnsHeader[8:10], h.NSCount) binary.BigEndian.PutUint16(dnsHeader[10:12], h.ARCount)  return dnsHeader }<\/code><\/pre>\n<p>\u0411\u0438\u0442\u043e\u0432\u044b\u0435 \u0441\u0434\u0432\u0438\u0433\u0438 \u043d\u0430\u0448\u0435 \u0432\u0441\u0435!<br \/>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435, \u0433\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0437\u0430\u0433\u0430\u043b\u043e\u0432\u043a\u0435<\/p>\n<figure class=\"\">\n<div><figcaption>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"go\">header := ReadHeader(buf[:12]) log.Printf(\"ID: %d; QR: %d; QDCount: %d\\n\", header.PacketID, header.QR, header.QDCount)  response := header.Encode() _, err = udpConn.WriteToUDP(response, source)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430, \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 DNS \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0430 Python<\/p>\n<pre><code class=\"python\">import socket   def build_dns_query():     header = bytearray([         0x00, 0x01,  # Transaction ID         0x00, 0x00,  # Flags: Standard query         0x00, 0x01,  # Questions         0x00, 0x00,  # Answer RRs         0x00, 0x00,  # Authority RRs         0x00, 0x00   # Additional RRs     ])      return header   def send_dns_query(query, server, port=2053):     with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:         s.sendto(query, (server, port))         response, _ = s.recvfrom(1024)     return response   if __name__ == \"__main__\":     dns_server = \"127.0.0.1\"     dns_query = build_dns_query()     dns_response = send_dns_query(dns_query, dns_server)<\/code><\/pre>\n<figure class=\"float\"><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 \u043b\u043e\u0433\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435.<br \/>\u0414\u0435\u0431\u0430\u0433\u0435\u0440, \u043d\u0443 \u0447\u0442\u043e\u0431\u044b \u0442\u043e\u0447\u043d\u043e \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f)<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0411\u0430\u043b\u0434\u0435\u0436<\/p>\n<h2>Questions<\/h2>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc1035#autoid-42\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043f\u0440\u043e\u0441\u044b<\/a> (\u0432\u043e\u043f\u0440\u043e\u0441\u044b), \u043a\u0430\u043a \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u043e, \u0432\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c DNS \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0440\u0430\u0432\u043d\u043e 1, \u043d\u043e \u0431\u044b\u0432\u0430\u0435\u0442 \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432\/\u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432. \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043c\u0435\u0435\u0442 \u043a\u0443\u0434\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u043e\u043b\u0435\u0439.  <\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0437\u043c\u0435\u0431\u0435\u0440\u0435\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0432 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438<\/p>\n<ul>\n<li>\n<p>QName, \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043b\u0435\u0439\u0431\u043b\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f habr.com \u0431\u0443\u0434\u0435\u0442 \u0434\u0432\u0430 \u043b\u0435\u0439\u0431\u043b\u0430: habr \u0438 com.  <\/p>\n<\/li>\n<li>\n<p>QType, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c. \u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0410, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0410 &#8212; \u0430\u0434\u0440\u0435\u0441 \u0445\u043e\u0441\u0442\u0430, \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0438\u043f\u043e\u0432 <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1035#section-3.2.2\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<\/li>\n<li>\n<p>QClass, 16 \u0431\u0438\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u0435\u0444\u043e\u043b\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 IN, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e IN &#8212; the Internet \u043f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043b\u0430\u0441\u0441\u043e\u0432 <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc1035#section-3.2.4\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u043f\u043b\u043e\u0448\u043d\u044b\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u043c, \u0430 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u0435\u0439\u0431\u043b\u043e\u0432 &lt;length>&lt;label>, \u0433\u0434\u0435 <\/p>\n<ul>\n<li>\n<p>&lt;length> &#8212; \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0431\u0430\u0439\u0442,  \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0434\u043b\u0438\u043d\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043b\u0435\u0439\u0431\u043b\u0430<\/p>\n<\/li>\n<li>\n<p>&lt;label> &#8212; \u0441\u0430\u043c \u043b\u0435\u0439\u0431\u043b<\/p>\n<\/li>\n<li>\n<p>\\x00 &#8212; \u0431\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u0435\u0439\u0431\u043b\u043e\u0432<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440, habr.com \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442\u044c \u0442\u0430\u043a<\/p>\n<pre><code>\\x04habr\\x03com\\x00<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0438\u043f \u0434\u043b\u044f QClass \u0438 QType. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0435 \u0435\u0434\u0438\u043d\u0438\u0446\u044b, \u043d\u043e \u043c\u043d\u0435 \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0431\u043b\u0438\u0436\u0435 <\/p>\n<pre><code class=\"go\">type Class uint16  const ( _ Class = iota IN CS CH HS )  type Type uint16  const ( _ Type = iota A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX TXT )  type Question struct { QName  string QType  Type QClass Class }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<pre><code class=\"go\">func ReadQuestion(buf []byte) Question { start := 0 var nameParts []string  for len := buf[start]; len != 0; len = buf[start] { start++ nameParts = append(nameParts, string(buf[start:start+int(len)])) start += int(len) } questionName := strings.Join(nameParts, \".\") start++  questionType := binary.BigEndian.Uint16(buf[start : start+2]) questionClass := binary.BigEndian.Uint16(buf[start+2 : start+4])  q := Question{ QName:  questionName, QType:  Type(questionType), QClass: Class(questionClass), }  return q }  func (q Question) Encode() []byte { domain := q.QName parts := strings.Split(domain, \".\")  var buf bytes.Buffer  for _, label := range parts { if len(label) > 0 { buf.WriteByte(byte(len(label))) buf.WriteString(label) } } buf.WriteByte(0x00) buf.Write(intToBytes(uint16(q.QType))) buf.Write(intToBytes(uint16(q.QClass)))  return buf.Bytes() }<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 main \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"go\">header := ReadHeader(buf[:12]) log.Printf(\"ID: %d; QR: %d; QDCount: %d\\n\", header.PacketID, header.QR, header.QDCount)  question := ReadQuestion(buf[12:])  var res bytes.Buffer res.Write(header.Encode()) res.Write(question.Encode())  _, err = udpConn.WriteToUDP(res.Bytes(), source)<\/code><\/pre>\n<p>\u0418 \u0447\u0443\u0442\u043e\u043a \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043c python \u043a\u043b\u0438\u0435\u043d\u0442<\/p>\n<pre><code class=\"python\">import socket   def build_dns_query(domain: str):     header = bytearray([         0x00, 0x01,  # Transaction ID         0x00, 0x00,  # Flags: Standard query         0x00, 0x01,  # Questions         0x00, 0x00,  # Answer RRs         0x00, 0x00,  # Authority RRs         0x00, 0x00   # Additional RRs     ])      question = bytearray()     labels = domain.split('.')     for label in labels:         question.append(len(label))         question.extend(label.encode('utf-8'))<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-361842","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=361842"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361842\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}