Is it possible to add a record to the dns cache from java? Or will I have to use the JNI?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming that you are talking about the DNS cache that Java applications use, the answer is No in both cases.
The cache is implemented in the java.net.InetAddress class; refer here for the source code. As you can see, the cache is implemented using private static attributes and all of the classes and methods involved are private or package private. In short, the only way you could get at the cache would be by using nasty reflection tricks to subvert the Java access rules.
Since this is implemented in pure Java, JNI won’t help.
EDIT
Unfortunately, the link above no longer points to the OpenJDK code :-(.
FOLLOWUP
Re: these comments.
There is no way to directly plant things in the system DNS cache from Java. Indeed, I don’t even think that Java uses the system DNS cache.
But there are better alternatives to what you are trying to do:
Get your users to configure their browsers to use an ‘autoproxy.pac’ file to determine which proxies to use. IMO, this is the best option.
Put entries for the hosts that you want to selectively proxy into “/etc/hosts” and configure (using “/etc/host.conf”) your local resolver to look in “/etc/hosts” before talking to the DNS server. Unfortunately (like cache poisoning) this “pollutes” your DNS with bogus entries, can cause problems when using services other than HTTP / HTTPS.
Finally, you probably should rethink your goal of doing this totally transparently to your users:
If you are doing this to implement some company security or internet access rules, people can “route around” any measures you implement at this level. (You’d be better off firewalling your network and forcing to use a proxy for external access … or something like that.)
If you are just trying to implement a useful service, you should use the autoproxy.pac approach which gives the users 1) visibility of what is going on (if they care to look), and 2) the option of opting in or out.
If you are trying to do something else …