Java dynamic replacement for DNS in inetaddress simple analysis 1

Source: Internet
Author: User

In the java.net package description, a brief description of some of the key interfaces. Which is responsible for networking identifiers is addresses. The concrete implementation class of this class is inetaddress, the bottom encapsulates the similarities and differences between inet4address and inet6address, can be regarded as a facade tool class.

    1. A low Level API, which deals with the following abstractions:
    2. Addresses, which is networking identifiers, like IP Addresses.
    3. Sockets, which is basic bidirectional data communication mechanisms.
    4. Interfaces, which describe network Interfaces.

Copy Code

In OpenJDK's InetAddress source code section, the IP is parsed according to DNS or hostname:

  1. private static inetaddress[] GETALLBYNAME0 (String host, InetAddress reqaddr, Boolean check)
  2. Throws Unknownhostexception {
  3. /* If It gets here it's presumed to be a hostname */
  4. /* Cache.get can return:null, unknownaddress, or inetaddress[] */
  5. /* Make sure the connection to the host is allowed, before we
  6. * Give out a hostname
  7. */
  8. if (check) {
  9. SecurityManager security = System.getsecuritymanager ();
  10. if (Security! = null) {
  11. Security.checkconnect (host,-1);
  12. }
  13. }
  14. Inetaddress[] addresses = getcachedaddresses (host);
  15. /* If No entry in cache, then do the host lookup */
  16. if (addresses = = null) {
  17. Addresses = Getaddressesfromnameservice (host, reqaddr);
  18. }
  19. if (addresses = = Unknown_array)
  20. throw new Unknownhostexception (host);
  21. return Addresses.clone ();
  22. }

Copy Code

The

Key two methods are:
Getcachedaddresses (host);
Getaddressesfromnameservice (host, reqaddr); The
parses the cached IP from Addresscache, or Negativecache according to Dns/hostname. The
then iterates through the nameservices, calls each Nameservice's LOOKUPALLHOSTADDR (host) to find the IP, and then caches the HOST:IP to the previous cache.
based on the above, there are two ways to dynamically resolve DNS:
1. Reflect Addresscache, or negativecache, and put Host:ip through the cache's put () method.
2. Reflect the nameservices and put the Nameservice instance of the agent into.
Two ways to do this:
1. Addresscache, or Negativecache, are java.net.InetAddress.Cache, and their internal cacheentry are affected by the two sets of JVM options:
Networkaddress.cache.ttl
Networkaddress.cache.negative.ttl
After the TTL, CacheEntry get () returns only null.
2. Nameservices is only a OPENJDK implementation. In other words, just the sun family. The other JDK does not use this property name.
Write the snippet code to see the properties inside the JRockit and IBM JVM inetaddress:

    1. class<inetaddress> type = Inetaddress.class;
    2. field[] fields = Type.getdeclaredfields ();
    3. for (Field f:fields) {
    4. System.out.println (F.getname () + ":" + F.gettype ());
    5. }

Copy Code

OpenJDK:

    1. IPv4
    2. IPv6
    3. Preferipv6address
    4. Holder
    5. Nameservices
    6. Canonicalhostname
    7. Serialversionuid
    8. Addresscache
    9. Negativecache
    10. Addresscacheinit
    11. Unknown_array
    12. Impl
    13. LookupTable
    14. Cachedlocalhost
    15. CacheTime
    16. Maxcachetime
    17. Cachelock
    18. Fields_offset
    19. UNSAFE
    20. Serialpersistentfields
    21. $assertionsDisabled

Copy Code

JRockit:

    1. IPv4
    2. IPv6
    3. Preferipv6address
    4. HostName
    5. Address
    6. Family
    7. Nameservice
    8. Canonicalhostname
    9. Serialversionuid
    10. Addresscache
    11. Negativecache
    12. Addresscacheinit
    13. Unknown_array
    14. Impl
    15. LookupTable
    16. $assertionsDisabled

Copy Code

IBM JDK

    1. Ipv4:int
    2. Ipv6:int
    3. Preferipv6address
    4. Hostname:class java.lang.String
    5. Address:int
    6. Family:int
    7. Nameservice:interface Sun.net.spi.nameservice.NameService
    8. Canonicalhostname:class java.lang.String
    9. Serialversionuid:long
    10. Addresscache:class Java.net.inetaddress$cache
    11. Negativecache:class Java.net.inetaddress$cache
    12. Localhostname:class java.lang.String
    13. Localhostnamelock:class Java.lang.Object
    14. Cachelocalhost:boolean
    15. Addresscacheinit:boolean
    16. Unknown_array:class [Ljava.net.InetAddress;
    17. Impl:interface Java.net.InetAddressImpl
    18. Lookuptable:class Java.util.HashMap
    19. $assertionsDisabled: Boolean

Copy Code

See here, know the egg hurts. Three types of JDK,
OpenJDK is Nameservices is a list<nameservice>
JRockit and the IBM JVM are Nameservice, just a separate nameservice instance.
So with a 2nd approach, you should at least meet the needs of these three mainstream JDK types.
Simple implementation of two practices:
Procedure 1, dynamically replaces the Addresscache.
Procedure 2, dynamic proxy nameservice.
The source code is as follows

    1. Length limit, source view Huitie.

Copy Code

Temporarily test to this bar, interested students can be perfect together. Strive to meet OpenJDK, Jrockit, IBM JDK Three mainstream environment DNS dynamic resolution class.

Java dynamic replacement for DNS in inetaddress simple analysis 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.