{"id":202,"date":"2011-07-28T16:00:15","date_gmt":"2011-07-28T15:00:15","guid":{"rendered":"http:\/\/mod16.org\/hurfdurf\/?p=202"},"modified":"2011-07-28T16:09:16","modified_gmt":"2011-07-28T15:09:16","slug":"investigation-of-the-etchosts-issues-in-os-x-10-7-lion","status":"publish","type":"post","link":"https:\/\/mod16.org\/hurfdurf\/?p=202","title":{"rendered":"Investigation of the \/etc\/hosts issues in OS X 10.7 (Lion)"},"content":{"rendered":"<p>I got bored today and decided to investigate the issues people have been having with their \/etc\/hosts entries in OS X 10.7.<\/p>\n<p>A lot of people have reported <a href=\"http:\/\/gargoyle.wpm.iawhq.co.uk\/2011\/06\/16\/mac-os-x-lion-etchosts-file\/\">a ton of random issues<\/a> ranging from the hosts file not being consulted at all to odd issues with IPv6 and multiple addresses on the same line (see the comments in the linked article). I haven&#8217;t been able to reproduce much (probably because a lot of it depends on your DNS configuration), but there is one exception.<\/p>\n<p>The <tt>.local<\/tt> domain is now reserved for the Bonjour service, and lookups for names under that top domain will now take exactly five seconds to time out before the hosts file is consulted. Or, well, almost. Applications whose underlying code use <tt>getaddrinfo(3)<\/tt> will behave that way, while ancient POSIXly things that use Ye Olde <tt>gethostbyname(3)<\/tt> will resolve immediately. This is why things like <tt>ping(1)<\/tt> don&#8217;t have that problem.<\/p>\n<p>Interestingly, if you pass a hint argument to <tt>getaddrinfo<\/tt>, telling it you want an address in the <tt>PF_INET<\/tt> family, it&#8217;ll return immediately just like <tt>gethostbyname<\/tt>. I guess it&#8217;s possible that the delay is related to IPv6 funkiness (since <tt>PF_INET<\/tt> does not include <tt>PF_INET6<\/tt> addresses).<\/p>\n<p>You can test this yourself if you feel like it, assuming you have XCode installed. Compare the results of the two following programs:<\/p>\n<h3>gethostbyname<\/h3>\n<pre>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/socket.h&gt;\r\n#include &lt;netdb.h&gt;\r\n\r\nint main(int argc, char *argv[]) {\r\n    if (argc &lt;= 1) {\r\n        printf(\"not enough arguments\\n\");\r\n        return 1;\r\n    }\r\n\r\n    struct hostent *h = gethostbyname(argv[1]);\r\n    if (h) {\r\n        for (int i = 0; h-&gt;h_addr_list[i] != NULL; i++) {\r\n            printf(\"%d.%d.%d.%d\\n\", h-&gt;h_addr_list[i][0], h-&gt;h_addr_list[i][1], h-&gt;h_addr_list[i][2], h-&gt;h_addr_list[i][3]);\r\n        }\r\n    }\r\n    else {\r\n        printf(\"host lookup failed\\n\");\r\n        return 1;\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<h3>getaddrinfo<\/h3>\n<pre>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/socket.h&gt;\r\n#include &lt;netdb.h&gt;\r\n#include &lt;arpa\/inet.h&gt;\r\n\r\nint main(int argc, char *argv[]) {\r\n    if (argc &lt;= 1) {\r\n        printf(\"not enough arguments\\n\");\r\n        return 1;\r\n    }\r\n\r\n    struct addrinfo *res0, *res, hints;\r\n    memset(&amp;hints, 0, sizeof(hints));\r\n    hints.ai_family = PF_UNSPEC;\r\n\r\n    int ret = getaddrinfo(argv[1], NULL, &amp;hints, &amp;res0);\r\n    if (ret) {\r\n        printf(\"host lookup failed\\n\");\r\n        return 1;\r\n    }\r\n    for (res = res0; res; res = res-&gt;ai_next) {\r\n        struct sockaddr_in *saddr = (struct sockaddr_in*)res-&gt;ai_addr;\r\n        printf(\"%s\\n\", inet_ntoa(saddr-&gt;sin_addr));\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<h2>Workaround<\/h2>\n<p>Someone suggested using DNSMasq as a workaround, but that seems overly complex to me. I suggest <a href=\"http:\/\/rubygems.org\/gems\/ghost\">Ghost<\/a>, which adds things to the Directory Service. If you don&#8217;t want to use a Ruby gem, you can manipulate the Directory Service directly with the command line utility <a href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Darwin\/Reference\/ManPages\/man1\/dscl.1.html\">dscl(1)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got bored today and decided to investigate the issues people have been having with their \/etc\/hosts entries in OS X 10.7. A lot of people have reported a ton of random issues ranging from the hosts file not being consulted at all to odd issues with IPv6 and multiple addresses on the same line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-the-internet"],"_links":{"self":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=202"}],"version-history":[{"count":8,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=\/wp\/v2\/posts\/202\/revisions\/210"}],"wp:attachment":[{"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mod16.org\/hurfdurf\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}