{"id":344,"date":"2011-12-26T20:48:53","date_gmt":"2011-12-26T12:48:53","guid":{"rendered":"http:\/\/ykyi.net\/?p=344"},"modified":"2011-12-26T20:48:53","modified_gmt":"2011-12-26T12:48:53","slug":"unix%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b-%e7%ac%ac11%e7%ab%a0-name-and-address-conversions-%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=344","title":{"rendered":"Unix\u7f51\u7edc\u7f16\u7a0b \u7b2c11\u7ae0 Name and Address Conversions \u7b14\u8bb0"},"content":{"rendered":"<p># gethostbyname \u548c gethostbyaddr \u7528\u6765\u5728 IPv4 \u5730\u5740\u548c hostname \u4e4b\u95f4\u8f6c\u6362. getservbyport \u548c getservbyname \u5219\u662f\u4e0e\u670d\u52a1\u76f8\u5173\u3002gethostbyname\u51fa\u9519\u65f6\u4e0d\u8bbeerrno\u800c\u662f\u8bbeh_errno\uff0c\u5e76\u6709hstrerror()\u51fd\u6570\u3002<\/p>\n<p># FQDN\u7684\u5168\u79f0\u662f: Fully Qualified domain name. \u6280\u672f\u4e0a\u8bf4\u5fc5\u987b\u4ee5\u70b9\u53f7(period)\u7ec8\u6b62.<\/p>\n<p># AAAA \u88ab\u79f0\u4e3a &quot;quad A&quot; rcord, \u7ed9\u51fa\u4e86\u4ecehostname\u5230Ipv6\u5730\u5740\u7684\u6620\u5c04\u3002 PTR\u7528\u6765\u628aIP\u5730\u5740\u5230hostname.<\/p>\n<p># Entries in the DNS are known as Resource Records(RRs).<\/p>\n<p># \u4e00\u4e2a\u70b9\u5206\u5341\u8fdb\u5236(dotted-decimal)IPv4\u7684\u5730\u5740\u524d\u52a0 0::ffff:\u5c31\u662f IPv6\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<p># \u4e0egetpeername\u5bf9\u5e94\u7684\u51fd\u6570\u4e0d\u662fgethostname\u800c\u662fgetsockname.<\/p>\n<p># getaddrinfo\u51fd\u6570\u7684host\u53c2\u6570\u6307\u5b9a\u4e3adotted-decimal IPv4\u6216 IPv6 hex string,\u4f1a\u4f7f\u5f97\u53ea\u6709IPv4\u6216IPv6\u7684addrinfo\u8fd4\u56de\u3002<\/p>\n<p># \u4e0d\u7ed9UDP\u5957\u63a5\u5b57\u8bbe\u7f6eSO_REUSEADDR\u9009\u9879\u3002We do not set the SO_REUSEADDR socket option for the UDP socket because this socket option can allow multiple sockets to bind the same UDP port on hosts that support multicasting. Since there is nothing like TCP&#39;s TIME_WAIT state for a UDP socket, there is no need to set this socket option when the server is started.<\/p>\n<p># \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u540c\u7aef\u53e3\u7684\u4e0d\u540c\u534f\u8bae\u5bf9\u5e94\u540c\u6837\u7684\u670d\u52a1\u3002\u4f46\u4e5f\u6709\u4f8b\u5916\u3002\u5bf9\u4e8e\u7aef\u53e3514\uff0cwhich is the rsh service with TCP, but the syslog service with UDP.<\/p>\n<p># gethostbyaddr\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fchar* addr\uff0c\u800c\u5176\u5b9e\u5b83\u5e76\u975e\u6307\u5411\u4e00\u4e2achar* \u4e8b\u5b9e\u4e0a\u6307\u5411in_addr\u7ed3\u6784\u4f53\u3002<\/p>\n<p># getaddrinfo\u597d\u590d\u6742\u5440\uff01hint\u7684ai_flags\u8bbe\u7f6e\u4e86AI_CONONNAME\u6210\u5458\u5f97\u5230host\u7684canonical name.<\/p>\n<p># port 53 \u662fdomain service\u7684\u7aef\u53e3\u53f7.<\/p>\n<p># \u5982\u679c\u8bbe\u7f6e\u4e86IPV6_V6ONLY.\u90a3\u4e48\u4e00\u4e2a\u6765\u81eaipv4 client\u7684\u8fde\u63a5\u4f1a\u88ab\u62d2\u7edd\u3002<\/p>\n<p># POSIX says that specifying AF_UNSPEC will return addresses that can be used with any protocol family that can be used with the hostname and service name.<\/p>\n<p># POSIX specification also implies that if the AI_PASSIVE flag is specified without a hostname, then the IPv6 wildcard address(IN6ADDR_ANY_INIT or 0::0) should be returned as a sockaddr_in6 structure, along with the IPv4 wildcard address(INADDR_ANY or 0.0.0.0), which is returned as a sockaddr_in structure.<\/p>\n<p># An ipv6 server socket can handle both ipv4 and ipv6 on a dual-stack host. Refer to page319 in UNP for details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p># gethostbyname \u548c gethostbyaddr \u7528\u6765\u5728 IPv4 \u5730\u5740\u548c hostname \u4e4b\u95f4\u8f6c\u6362. getservbyport \u548c getservbyname \u5219\u662f\u4e0e\u670d\u52a1\u76f8\u5173\u3002gethostbyname\u51fa\u9519\u65f6\u4e0d\u8bbeerrno\u800c\u662f\u8bbeh_errno\uff0c\u5e76\u6709hstrerror()\u51fd\u6570\u3002 # FQDN\u7684\u5168\u79f0\u662f: Fully Qualified domain name. \u6280\u672f\u4e0a\u8bf4\u5fc5\u987b\u4ee5\u70b9\u53f7(period)\u7ec8\u6b62. # AAAA \u88ab\u79f0\u4e3a &quot;quad A&quot; rcord, \u7ed9\u51fa\u4e86\u4ecehostname\u5230Ipv6\u5730\u5740\u7684\u6620\u5c04\u3002 PTR\u7528\u6765\u628aIP\u5730\u5740\u5230hostname. # Entries in the DNS are known as Resource Records(RRs). # \u4e00\u4e2a\u70b9\u5206\u5341\u8fdb\u5236(dotted-decimal)IPv4\u7684\u5730\u5740\u524d\u52a0 0::ffff:\u5c31\u662f IPv6\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002 # \u4e0egetpeername\u5bf9\u5e94\u7684\u51fd\u6570\u4e0d\u662fgethostname\u800c\u662fgetsockname. # getaddrinfo\u51fd\u6570\u7684host\u53c2\u6570\u6307\u5b9a\u4e3adotted-decimal IPv4\u6216 IPv6 hex string,\u4f1a\u4f7f\u5f97\u53ea\u6709IPv4\u6216IPv6\u7684addrinfo\u8fd4\u56de\u3002 # \u4e0d\u7ed9UDP\u5957\u63a5\u5b57\u8bbe\u7f6eSO_REUSEADDR\u9009\u9879\u3002We do not set &hellip; <a href=\"https:\/\/ykyi.net\/?p=344\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Unix\u7f51\u7edc\u7f16\u7a0b \u7b2c11\u7ae0 Name and Address Conversions \u7b14\u8bb0&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6,7],"tags":[81,118],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-network","category-tech_articles","category-unix_linux","tag-unix","tag-118"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}