IP address to hostname - java

I am trying to convert ip addresses to hostnames. I tried the answer given here. While the linux command "host" works for all the ip addresess I have, this code only works for some of them. Why would this be?
InetAddress addr = InetAddress.getByName("192.168.190.62");
String host = addr.getHostName();
System.out.println(host);

when looking at http://download.java.net/jdk7/archive/b123/docs/api/index.html?java/net/InetAddress.html you find...
getHostName
Gets the host name for this IP address.
If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName().

Related

Skip DNS lookup in Java

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

Is there any way to get client's Computer Name at the server side using client Socket connection?(For Example :-Windows-PC)

Socket clientsocket = serverSock.accept();
String clientIP = clientsocket.getInetAddress().getHostAddress();
String clientName = clientsocket.getInetAddress().getHostName();
But clientIP and clientName both returns same value (like 192.168.173.46).
According to the documentation on the Oracle website I assume that you have a bad DNS setup. The following documentation is for the getHostName() method:
public String getHostName() Gets the host name for this IP address. If
this InetAddress was created with a host name, this host name will be
remembered and returned; otherwise, a reverse name lookup will be
performed and the result will be returned based on the system
configured name lookup service. If a lookup of the name service is
required, call getCanonicalHostName.
If there is a security manager, its checkConnect method is first
called with the hostname and -1 as its arguments to see if the
operation is allowed. If the operation is not allowed, it will return
the textual representation of the IP address.
Returns: the host name for this IP address, or if the operation is not
allowed by the security check, the textual representation of the IP
address. See Also: getCanonicalHostName(),
SecurityManager.checkConnect(java.lang.String, int)
Source
I do not know how you could fix this, but you could try adding a hostname manually in your hosts file.

InetAddress.getByName returning wrong IP address

I have been using the following code on a server.
public SocketServer(int port,String inetAddress) throws IOException {
this.port = port;
this.ia = InetAddress.getByName(inetAddress);
log.info(String.format("Internet Address %s using port %d for provided IP Address %s", this.ia.toString() ,this.port ,inetAddress.toString()));
s = new ServerSocket(port,50,this.ia );
}
This works fine on my local server but on production it is providing wrong address.
Production server do contain following type of IPs:-
Private IP
VPN IP
Public IP
I am providing private IP and expecting the server to connect using that private IP but instead it is connecting using the VPN IP.
One more thing i though to do was to use InetAddress.getByAddress() but i am unable to convert my IP in string to a byte array.
Can anyone suggest me any solution in this regard?
If I am not mistaken this might be a problem related to DNS. InetAddress.getByName(String host) will return the first IP address assigned to a certain domain name.
I.e. if in your /etc/hosts file you have something like this
192.168.1.1 sandbox1
192.168.1.2 sandbox1
The code
InetAddress.getByName("sandbox1")
will always give you 192.168.1.1
Hope this helps!

Java's ServerSocketChannel.getLocalAddress() differs from InetSocketAddress passed to bind()

If I bind to a specific interface using a specific hostname and then use ServerSocketChannel.getLocalAddress() to retreive the bound address, the hostname is longer there.
Is this by design or just undefined behavior? Is there any way to fix that?
InetSocketAddress bindTo = new InetSocketAddress("my-hostname", 9999);
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(bindTo);
InetSocketAddress localAddr = (InetSocketAddress) serverSocketChannel.getLocalAddress();
System.out.println(bindTo);
System.out.println(localAddr);
> my-hostname/10.20.200.201:9999
> /10.20.200.201:9999
"my-hostname" in the above example is one of several hostnames that will resolve to the local IP. It is, however, not the hostname to which the IP resolves to when doing a reverse lookup.
The reason I ask is that I use a framework which will bind and then publish the service information to a central registry. However, it retrieves the bound address using .getLocalAddress() which ultimately ends up publishing the wrong hostname.
the hostname is longer there
You mean "the hostname is longer there" in the String returned by InetAddress.toString(). There's nothing that obliges that behaviour. The local address to which a socket is bound is primarily an IP address. If you want to map it to a hostname, that's up to you.

How to get multiple domains for the single IP?

Here's the method-
public static String getHostByAddr(byte[] addr) throws UnknownHostException {
Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
final String[] servers = new String[] {"208.67.220.220", "208.67.222.222"};
final Resolver res = new ExtendedResolver(servers);
final Lookup lookUp = new Lookup(name, Type.PTR);
lookUp.setResolver(res);
Record[] records = lookUp.run();
if (records == null) {
throw new UnknownHostException();
}
return ((PTRRecord) records[0]).getTarget().toString();
}
And here's the call to the above method-
final InetAddress ip = InetAddress.getByName("198.154.218.168");
final byte[] bytes = ip.getAddress();
final String host = getHostByAddr(bytes);
System.out.println("Host - " + host);
Works fine for most of the cases, but fails when the IP is mapped to multiple domains (???)
Here's the example-
Get IP of securonix.com from here, it is 198.154.218.168
If I pass this IP to the above method it gives error
But if I try the same IP here, it lists down the 4 domains
Is it possible to do this with DNSJava?
In general, you can't. Just as the owner of the domain securonix.com created an entry on their DNS server that translates securonix.com to 198.154.218.168, the owner of the IP address 198.154.218.168 maintains a "reverse" DNS record (usually on a separate server) that maps the IP address to a default DNS name. See the page for "Reverse DNS lookup" on wikipedia for more information on reverse DNS lookups, and note that the reverse DNS lookup -- from IP address to name -- is often maintained by a different owner than the domain name owner and it is NOT just created "automatically" by swapping the name and IP address.
Furthermore, the site you mention doesn't seem to have reverse DNS entries for that IP address, so you won't ever get an answer for 198.154.218.168 from a standard DNS query; there simply is no (reverse) DNS entry for 198.154.218.168 and the code above fails.
As an example of the difference between the forward and reverse DNS lookups, when I was first allocated my static IP address, my provider (Comcast) mapped the IP address to some generic name like 75-148-###-###-Houston.hfc.comcastbusiness.net (random IP address example) and I had ask them to modify the reverse DNS entry to map to the name of my domain instead so that a forward and reverse lookup of the IP address and domain name matched. They maintained the lookup of the IP address on their DNS servers, and I maintained the lookup of the domain name on my DNS servers.
The page at yougetsignal.com must have been doing DNS (forward) lookups of names and storing them in the huge database available for purchase, which allows the web page to find all the names with the same IP. But there is no easy way to do that for an arbitrary IP address by querying DNS servers, unless you have considerable additional information or have already done the millions of lookups like that site.
I found that there are two websites providing this functionality. However I don't know how they implement that functionality.
http://reverseip.domaintools.com/
http://ipaddress.com/reverse_ip/
update:
domaintools provide api calls. maybe you can call domaintools api to get multiple domain names for the single IP.

Categories