2

In my application, I use InetAddress.getByName() quite a bit to convert strings like "192.168.1.56" to InetAddress objects -- mainly because it seems to me to be a good idea to store IP addresses as IP addresses rather than as strings. Up til now, I would have sworn it was pretty foolproof too, but today I discovered a bug. This does not work:

InetAddress ia = InetAddress.getByName ("192.168.1.056");

It would appear that my Android thinks THAT string is a host name, and so it's trying to look it up (which isn't possible, because it's not on a "real" network). Is this something I can work around -- meaning is there a way to insist to getByName that this is an IP address, not a hostname? Or do I need to build method to purge leading zeroes out of IP address strings?? Or is there an Apache utility buried somewhere that might do a better job of this??

Rich
  • 4,157
  • 5
  • 33
  • 45
  • What's the error you get? Additionally, some implementations will handle leading zeros as an octal value, which won't be what you want. Not sure about this one. – FatalError Nov 07 '14 at 15:45
  • Check this question and answers: http://stackoverflow.com/questions/9556978/how-come-inetaddress-getbyname1-2-is-valid-ip-address – Anas Alkhatib Nov 07 '14 at 15:49
  • The error I'm getting is NetworkOnMainThread -- which leads me to believe the software is treating the string as a hostname and going out the network to try to resolve it -- which is both impossible and not the right thing to do. Sorry about the incomplete post. – Rich Nov 07 '14 at 15:52
  • Anas: I did, but that doesn't seem to be germane to this issue. My address string has 4 numbers in it, and is separately verified to actually BE and IP address (using a java.util.regex.Pattern). – Rich Nov 07 '14 at 15:55
  • Can you post the stack trace? – Sotirios Delimanolis Nov 07 '14 at 16:13

2 Answers2

0

Why not try getByAddress (byte[] ipAddress)? This method seems more suitable for your needs.

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35
  • I'm using my code to parse an IP address string provided by the user in a Dialog EditText. Apparently, one of my users found a new and interesting way to screw things up. I'm trying to trap the error without resorting to toasting an Invalid Address Error and doing nothing. – Rich Nov 10 '14 at 15:27
0

It may be that a Regex-wizard could have found a way to fix the IP address string directly, but I don't know how to do that. However, there is something called the Apache SubnetUtils library which is native to Android. SubnetUtils doesn't have a cow over an IP address string with a leading zero in it. My code looks like this:

public static InetAddress addrFromStr (String addr)
{
    InetAddress ia = null;
    SubnetUtils su = new SubnetUtils (addr + "/8");

    try
    {
        ia = InetAddress.getByName (su.getInfo().getAddress());
    }
    catch (UnknownHostException e)
    { }

    return ia;
}

Basically, I create a SubnetUtils object and then ask it for the IP address -- which is now stripped of any numerical strangeness that InetAddress.getByName() apparently can't handle. (The "/8" business is the CIDR notation subnet mask, and the only thing I can say about it is don't use "/0").

Rich
  • 4,157
  • 5
  • 33
  • 45