{"id":4734,"date":"2017-05-16T08:55:24","date_gmt":"2017-05-16T13:55:24","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=4734"},"modified":"2017-05-16T08:55:24","modified_gmt":"2017-05-16T13:55:24","slug":"dns-server-address-cached-in-java","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2017\/05\/16\/dns-server-address-cached-in-java\/","title":{"rendered":"DNS server address cached in java?"},"content":{"rendered":"<p>We found that an old DNS address was used until we restarted the JVM.  I need to review the <a href=\"https:\/\/raw.githubusercontent.com\/openjdk-mirror\/jdk7u-jdk\/master\/src\/solaris\/classes\/sun\/net\/dns\/ResolverConfigurationImpl.java\" target=_blank>class used to perform this activity.<\/a>.<\/p>\n<p>Test this with a simple class.<\/p>\n<pre>\r\nimport java.net.*;\r\n\r\npublic class test {\r\n  static {\r\n    java.security.Security.setProperty (\"networkaddress.cache.ttl\" , \"10\");\r\n  }\r\n  public static void main (String args[]) throws Exception {\r\n    \/\/System.setProperty(\"sun.net.spi.nameservice.nameservers\", \"192.168.56.50\");\r\n    \/\/System.setProperty(\"sun.net.spi.nameservice.provider.1\", \"dns,sun\");\r\n    System.out.println(\"DEFAULT DNS TTL: \"+sun.net.InetAddressCachePolicy.get());\r\n\r\n    while (true) {\r\n      try {\r\n        System.out.println(InetAddress.getByName(args[0]).getHostName());\r\n      }\r\n      catch (Exception e) {\r\n        e.printStackTrace();\r\n      }\r\n      Thread.sleep(10000);\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>This is done mainly for security.  If someone hacks the name resolution configuration, your application can&#8217;t be erroneously pointed to malicious hostnames.  Of course, this is only relevant if you check the name resolution configuration before a restart.<\/p>\n<p>It should be noted that as shown below, setting the cache timeout on the JVM also won&#8217;t impact the DNS servers used for resolution of names to IP addresses.  While this test ran, we changed the name server in \/etc\/resolv.conf from 192.168.56.50 to something else, which had no impact on our running class.  Once started, the DNS server used by the JVM remains the same&#8230;<\/p>\n<pre>\r\n[root@hdp ~]# strace -f -e trace=connect java test hdp\r\nProcess 18123 attached\r\n[pid 18123] connect(3, {sa_family=AF_FILE, path=\"\/var\/run\/nscd\/socket\"}, 110) = -1 ENOENT (No such file or directory)\r\n[pid 18123] connect(3, {sa_family=AF_FILE, path=\"\/var\/run\/nscd\/socket\"}, 110) = -1 ENOENT (No such file or directory)\r\nProcess 18124 attached\r\nProcess 18125 attached\r\nProcess 18126 attached\r\nProcess 18129 attached\r\nProcess 18130 attached\r\nProcess 18131 attached\r\nProcess 18132 attached\r\nProcess 18133 attached\r\nDEFAULT DNS TTL: 10\r\n[pid 18123] connect(4, {sa_family=AF_FILE, path=\"\/var\/run\/nscd\/socket\"}, 110) = -1 ENOENT (No such file or directory)\r\n[pid 18123] connect(4, {sa_family=AF_FILE, path=\"\/var\/run\/nscd\/socket\"}, 110) = -1 ENOENT (No such file or directory)\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\nhdp\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\nhdp\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\nhdp\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\nhdp\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\nhdp\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n[pid 18123] connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr(\"192.168.56.50\")}, 16) = 0\r\n<\/pre>\n<p>Regardless, beware of this unlikely issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We found that an old DNS address was used until we restarted the JVM. I need to review the class used to perform this activity.. Test this with a simple class. import java.net.*; public class test { static { java.security.Security.setProperty&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2017\/05\/16\/dns-server-address-cached-in-java\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[24,25,56],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4734"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=4734"}],"version-history":[{"count":12,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4734\/revisions"}],"predecessor-version":[{"id":4746,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/4734\/revisions\/4746"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=4734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=4734"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=4734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}