25

I have a local IP address in dotted decimal notation in a String. I want to convert it to an InetAddress to feed it to Socket, but I need to do it without doing a DNS lookup (because this might cause lengthy timeouts).

Is there a ready method for that, or do I need to split the String and create the InetAddress from its bytes?

Update The factory methods InetAddress.getByName() and InetAddress.getByAddress() don't seem to be a good fit, as they both also accept hostnames such as java.sun.com. There is no saying if they will try to contact a DNS server in their implementation.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • possible duplicate of [Is there an easy way to convert String to Inetaddress in Java?](http://stackoverflow.com/questions/2309049/is-there-an-easy-way-to-convert-string-to-inetaddress-in-java) – Zelldon Dec 11 '13 at 12:49
  • @Zelldon, agreed. I don't want to use an entire library for just this features though. – Bart Friederichs Dec 11 '13 at 12:51
  • http://stackoverflow.com/questions/5571744/java-convert-a-string-representing-an-ip-to-inetaddress also same question – Zelldon Dec 11 '13 at 12:52
  • 1
    @Zelldon yes but different answer. I want to make 100% sure there is no network activity going on. – Bart Friederichs Dec 11 '13 at 13:00

4 Answers4

29

Do like this

InetAddress inetAddress = InetAddress.getByName("192.168.0.105");

If a literal IP address is supplied, only the validity of the address format is checked.

java source code

// if host is an IP address, we won't do further lookup    
if (Character.digit(host.charAt(0), 16) != -1 || (host.charAt(0) == ':')) {

}
Kara
  • 6,115
  • 16
  • 50
  • 57
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Is it guaranteed there will be no DNS going on (also no reverse DNS or whatever) ? – Bart Friederichs Dec 11 '13 at 12:56
  • 1
    @BartFriederichs As per java document there is no DNS verification if correct ip format is provided. Reference - http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getByName%28java.lang.String%29 – Prabhakaran Ramaswamy Dec 11 '13 at 13:01
  • 5
    Source code says the same: 'if host is an IP address, we won't do further lookup' – Alexey Odintsov Dec 11 '13 at 13:03
  • If the supplied string is in dotted quad format, it will not do a dns lookup. If you need to be sure, validate the string first, e.g. . http://stackoverflow.com/questions/5667371/validate-ip-address – nos Dec 11 '13 at 13:10
  • 1
    In my case, InetAddress address= InetAddress.getByName("www.sun.com"); runs correctly, but when I give a wrong host address as the argument such as "www.sunqqq.com" it gave exceptions. >>> java.net.UnknownHostException: www.sunqqq.com at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source) at java.net.InetAddress.getAddressesFromNameService(Unknown Source) at java.net.InetAddress.getAllByName0(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source)....(more) – Samitha Chathuranga Apr 09 '14 at 03:38
  • @SamithaChathuranga Other than IP format it will do the DNS lookup. In your case it will lookup the URL to get the IP then the url is not resolved to any IP so you are getting that exception. – Prabhakaran Ramaswamy Apr 09 '14 at 06:58
  • @Prabhakaran - But this whole answer and the comments are saying that there is no any DNS lookup here..!!! The question asks for a method to do without a DNS lookup. But if u declare now that there is a DNS lookup, so then your answer should be wrong... ??? – Samitha Chathuranga Apr 09 '14 at 07:18
  • @SamithaChathuranga you know the difference between 192.168.0.105 and www.sun.com right?. – Prabhakaran Ramaswamy Apr 09 '14 at 08:20
  • @Prabhakaran - I think now I got it. :D When there is www.sun.com (the URL) the mapping IP should be found out by a DNS lookup. Bt when there is the 192.168.0.105 (IP) there is no need of further DNS lookups as the IP is already there. Am I correct??? – Samitha Chathuranga Apr 09 '14 at 11:04
  • @Prabhakaran - Can u please answer this question. It relates to the same matter. As it is a very long I can't put it here. http://stackoverflow.com/questions/22978067/when-the-dns-lookup-occurs-in-this-code-by-getbyname-or-gethostname-or-geth – Samitha Chathuranga Apr 13 '14 at 04:04
  • We have a proxy that blocks a bunch of IP addresses. If I give it an IP that is blocked, I get a java.net.UnknownHostException: 192.0.2.235. So it looks like it still trys to do some kind of lookup on IPs, not just hostnames. – medloh Dec 07 '15 at 18:24
  • However, Guava's InetAddresses.forString(ip) works even when proxy is blocking it. – medloh Dec 07 '15 at 18:39
6

You can use Guava's InetAddresses#forString() which is specifically documented for your use case:

Returns the InetAddress having the given string representation.
This deliberately avoids all nameservice lookups (e.g. no DNS).

(emphasis added)

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

You can do this by using the getByName method. for example:

InetAddress localhost = InetAddress.getByName("127.0.0.1")

As it is described on the java docs:

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

Ali
  • 931
  • 10
  • 22
  • 1
    In my case, InetAddress address= InetAddress.getByName("www.sun.com"); runs correctly, but when I give a wrong host address as the argument such as "www.sunqqq.com" it gave exceptions. >>> java.net.UnknownHostException: www.sunqqq.com at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source) at java.net.InetAddress.getAddressesFromNameService(Unknown Source) at java.net.InetAddress.getAllByName0(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source)....(more) – – Samitha Chathuranga Apr 09 '14 at 03:44
  • So does it means that getByName() method do the DNS checkups??? As it seems the validity of the host address also have checked??? – Samitha Chathuranga Apr 09 '14 at 03:45
  • Yes. if you give a host name to getByName, it perform DNS lookup. but if an IP address is provided it only checks for the validity of the IP (string format). – Ali Apr 10 '14 at 08:07
0

The open-source IPAddress Java library will validate all standard representations of IPv6 and IPv4 and will do so without DNS lookup. Disclaimer: I am the project manager of that library.

The following code will do what you are requesting:

        String str = "fe80:0:0:0:f06c:31b8:cd17:5a44";
        try {
            IPAddressString str = new IPAddressString(str);
            IPAddress addr = str.toAddress();//throws if invalid, without a DNS lookup
            InetAddress inetAddr = addr.toInetAddress();//convert valid address
            //use address
        } catch(AddressStringException e) {
            //e.getMessage has validation error
        }
Sean F
  • 4,344
  • 16
  • 30