31

I am trying to convert strings into Inetaddress. I am not trying to resolve hostnames: the strings are ipv4 addresses. Does InetAddress.getByName(String host) work? Or do I have to manually parse it?

user207421
  • 305,947
  • 44
  • 307
  • 483
TiansHUo
  • 8,509
  • 7
  • 45
  • 57

5 Answers5

26

com.google.common.net.InetAddresses.forString(String ipString) is better for this as it will not do a DNS lookup regardless of what string is passed to it.

Flow
  • 23,572
  • 15
  • 99
  • 156
  • 1
    This is part [of Guava](http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/net/InetAddresses.html). – Matthew Flaschen Jun 19 '12 at 16:37
  • 2
    It should be `com.google.common.net.InetAddresses.forString` (with `InetAddresses` with uppercase A). SO won’t allow me to correct that in your answer, as the edit has to be at least 6 characters. ;-) Anyway, thanks, was looking for it, and yet again Guava saves the day. – silmeth Oct 20 '16 at 14:10
14

Yes, that will work. The API is very clear on this ("The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address."), and of course you could easily check yourself.

FujiRoyale
  • 762
  • 10
  • 26
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    Thanks, I looked up the api, and missed that line. – TiansHUo Feb 22 '10 at 05:49
  • Whats when host is a pattern like 192.168.0.*? Will that work too? Regarding to Inet4Address doc there seems support for this. – Sebastian Hoffmann Feb 24 '12 at 13:58
  • @Paranaix, no, it will throw a `IllegalArgumentException` with the message "invalid host wildcard specification" – Matthew Flaschen Feb 24 '12 at 16:24
  • 7
    in particular, the documentation says: `If a literal IP address is supplied, only the validity of the address format is checked.` which I read as: 'if you specify a (dotted quad notation) IP address, no DNS lookup is performed'. – Andre Holzner Apr 03 '12 at 07:33
  • 1
    The OP said "I am not trying to resolve hostnames"; if the input to `getByName()` is not a valid numeric IP address, but is a valid resolvable DNS name, the name will be resolved. That does not seem to be what the OP wants. – Raedwald Mar 28 '13 at 16:57
  • @Raedwald, he also said "the strings are ipv4 addresses". If the input is known to be just IP addresses, this should work fine. – Matthew Flaschen Mar 28 '13 at 20:00
3

Beware: it seems that parsing an invalid address such as InetAddress.getByName("999.999.999.999") will not result in an exception as one might expect from the documentation's phrase:

the validity of the address format is checked

Empirically, I find myself getting an InetAddress instance with the local machine's raw IP address and the invalid IP address as the host name. Certainly this was not what I expected!

wu-lee
  • 749
  • 4
  • 17
  • 4
    As of Java 7, this will throw an UnknownHostException; haven't checked earlier Javas, but I would expect an exception there as well. – Craig Trader Mar 12 '15 at 12:38
  • @CraigTrader, but if you look at the stack trace you will see that this is not actually a parsing exception, but Java tries to look this up as host name, which is somewhat worrying. – Marcono1234 Aug 10 '21 at 03:06
  • Because `999.999.999.999` is NOT a valid IPv4 address, Java asks the system's name server to resolve it as a hostname. Since there is no valid DNS entry for that IP address, you'll get an `UnknownHostException`. If you try this with a valid IPv4 address, you'll get a valid `InetAddress` object. – Craig Trader Aug 10 '21 at 15:14
1

You could try using a regular expression to filter-out non-numeric IP addresses before passing the String to getByName(). Then getByName() will not try name resolution.

Community
  • 1
  • 1
Raedwald
  • 46,613
  • 43
  • 151
  • 237
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 s = "1.2.3.4";
     try {
            IPAddressString str = new IPAddressString(s);
            IPAddress addr = str.toAddress();
            InetAddress inetAddress = addr.toInetAddress(); //IPv4 or IPv6
            if(addr.isIPv4() || addr.isIPv4Convertible()) {//IPv4 specific
                IPv4Address ipv4Addr = addr.toIPv4();
                Inet4Address inetAddr = ipv4Addr.toInetAddress();
                //use address
            }
      } catch(AddressStringException e) {
            //e.getMessage has validation error
      }
Sean F
  • 4,344
  • 16
  • 30