tags:

views:

147

answers:

3

Is it possible to add a record to the dns cache from java? Or will I have to use the JNI?

+1  A: 

Is it possible to add a record to the dns cache from java? Or will I have to use the JNI?

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 :-(.

Stephen C
+1  A: 

Java DNS caching is implemented in a manner that should be mostly transparent to your code. You can read the javadoc of InetAddress to learn more about how you can configure the cache. But since it serves as a JVM-wide transparent proxy, the best way you have to add a cache entry would simply be to actually issue a request directed to the domain you want to cache the info for.

Out of curiosity, what's your use case?

Julien Silland
A: 

Why don't you just configure java for using the proxy?

java -DproxySet=true -DproxyHost=proxy -DproxyPort=8080 MyApp

or

System.setProperty("proxyPort","8080");
System.setProperty("proxyHost","proxy");

will do it.

Daniel
sorry. my question was unclear. The proxy is written in java and I need the browser to use it.
silverbandit91
You have two other possibilities:1. Configure your browser to use your proxy, just be specifying your proxy.2. If you just want to use your own proxy for certain sites, add those sites to C:\Windows\system32\etc\driver\hosts.conf, and map them to 127.0.0.1 (localhost)
Daniel
this should be transparent to the user. Also, not all users have access to the hosts file. But I guess those might be my only options.
silverbandit91
I think so. If I understand correctly, you want to poison the DNS cache without the user noticing. This would be a nice malware :), and I think its a good thing that you have to do some configuration for this to work.
Daniel