For an app I'm writing I want to completely avoid DNS host name resolution: I'm using numeric IPv4 and IPv6 addresses and I have no use for lookups. I've googled a bit but I can't find a class that allows me to open a connection without causing a DNS request to occur. Any hints?
TIA
If you invoke InetAddress.getByName() with a name that's not really a name, but is an IP address, Java won't perform the DNS lookup.
Another option is to use InetAddress.getByAddress() like so:
byte[] numericAddress = new byte[]{127,0,0,1};
InetAddress.getByAddress(numericAddress); //Will return an InetAddress that points to 127.0.0.1
Related
I have found that resolution of a hostname that does NOT exist always returns an address belonging to "akamaitechnologies.com" (e.g. 23.221.222.250). It does work correctly for hosts that do exist.
Code:
InetAddress addr = InetAddress.getByName( "NON-EXISTING.com" );
The documentation for InetAddress or a Google search provide little help. It is claimed that an UnknownHostException should occur but that does not happen for me.
Why is this happening?
This is not an Android/Java defect. It turns out it was a DNS issue. Namely, my AT&T phone uses AT&T's default DNS server (sbcglobal.net). This P.O.S. server returns a valid IP address even for non-existent domains. After I changed to "dns.google" (8.8.8.8), everything worked as expected.
This DNS spoofing is a Bad Thing because many apps depened on a Unknown-Host-Exception to detect an incorrectly entered domain, e.g. in an email address.
I am deplyong a web application in the Internet and I am checking whether the user's login is valid by checking its client IP address (e.g. 192.168.2.XXX). Actually, I have found a working code (below). This code was completely working before, but after some time, its output is not the same anymore. Now, this code only gives me 1 IP address which is the server's IP address. What is wrong with my code? How can I get the client's static IP address rather than the server's IP address? I have also tried other solutions such as getRemoteAddr() and request.getHeader("PROXY-CLIENT-IP") but it is not working.
Java:
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if(ipAddress == null)
ipAddress = InetAddress.getLocalHost().getHostAddress();
Your are mixing two levels of abstractions and that is rarely a good thing.
The header X-FORWARDED_FOR is inserted by a load balancer or proxy. If the client reaches the server directly, then this header isn't present and you are executing this code InetAddress.getLocalHost().getHostAddress();. Which does exactly what you say: It is retrieving the IP address of the host where this piece of code is running, i.e. the web server.
See also here: Getting the client IP address: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, what else could be useful?
The following always prints the first returned IPv4 address and not the first IPv6 address
although I explicitly used Inet6Adress and not InetAddress. Is this a bug in the OpenJDK 1.7.0 I use?
InetAddress ipv6 = Inet6Address.getByName("www.google.com");
How am I supposed to get one IPv6 adresse for a given host name (or NULL if none exists)?
I do not want to change the resolver preference between IPv4 and IPv6, in this case I really want to see if at least one IPv6 address exists.
It's probably doable by iterating all results of InetAddress.getAllByName() and check for any "instanceof Inet6Address" but that does not look like the supposed way to do it.
Just found this answer .
I am copying textually the answer of the user Pr0gr4mm3r
java.net.Inet6Address does not override getByName()
so it will always return the specific IPv4-Address,
unless your parameter itself is in the form of an valid IPv6-Address, in this case this method will return an Inet6Address-Object.
For example:
getByName("stackoverflow.com") --> Inet4Address
getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344") --> Inet6Address
I'm using PMD to check java code, and I've run into the problem that states, "Do not hard code IPv4 or IPv6 addresses, even 127.0.0.1!" The IPv4 address I'm using is in fact just 127.0.0.1, and is only for testing purposes, but nonetheless I must convert the hard-coded version to some sort of encrypted version. I'm not sure what would be the easiest way to do this.
Any help would be greatly appreciated!
You are not trying to encrypt anything.
What you wan to do is pass in a host name and do the proper host lookup to get the Internet address. Look at the standard JDK's InetAddress and the getAllByName(String host) and getByName(String host)
I've resolved this PMD´s warning in this way:
// Old code
`if (!"127.0.0.1".equals(serverIP)) { ... }`
// New code
`if (!InetAddress.getLoopbackAddress().getHostAddress().equals(serverIP)) { ... }`
I'm making a peer-to-peer instant messaging application.
Currently, if UserA.pool.net says "hello" to UserB.pool.net, User A sees "You: hello" and User B sees "UserA.pool.net: hello".
Rather than User A seeing "You", I want them to see the hostname of their own machine so that User A sees the same text as User B.
See these functions of java.net.InetAddress - getLocalHost and getHostName :
String localhostname = java.net.InetAddress.getLocalHost().getHostName();
Note that this gets you the hostname as the machine sees itself; others may see it with a different one (e.g. the local hosts file says something different than DNS). In other words, it is not guaranteed that machine A will be seen with the same hostname from machine A, machine B, or machine C.
As #biniam_Ethiopia points out, it is not even guaranteed that you'll get the same result from different programs on the same machine, as they may be using network-based name resolution (seen e.g. here).
It may be more useful to send the whole identifier: piskvor#lachtan.my.network.example.com , instead of just piskvor.
The short answer is that if you really want User A and User B to see the same text, you can't rely on finding out your hostname yourself. You need User B to transmit their view of User A's hostname to User A and vice versa. Due to NAT, you won't be able to just check your own computer's hostname.
Alternatively, (Jonathon beat me to this in the question comments) you can have each user send their own private hostname as part of the connection handshake and use that to print messages on the remote end.
I've gotten the hostname of the local machine in the past using something like this:
InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
You could refer to: InetAddress.getHostName()
You may need to use getCanonicalHostName() to get full qualified host name which include domain name also.
Code -
String fullHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName();