2

When I am entering the string "192" and it parses the string, it continues to return IPv4 even when it is not a valid IPv4 address. I tried adding an else if (someIP.GetAddressBytes().Length == 1) but it still returns IPv4.

IPAddress someIP = IPAddress.Parse("192");

if (someIP.GetAddressBytes().Length == 4)
{
    Console.WriteLine("IPv4");
}
else if (someIP.GetAddressBytes().Length == 16)
{
    Console.WriteLine("IPv6");
}
else
{
    Console.WriteLine("Neither");
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
crin
  • 53
  • 1
  • 11
  • 1
    What makes you think that `192` is not a valid IP address? – DavidG Nov 11 '15 at 00:14
  • `IPAddress.Parse` seems to parse the address in a similar fashion to `ping.exe` - see this SuperUser answer about other valid IP address representations: http://superuser.com/a/486936/270560 – cbr Nov 11 '15 at 00:20
  • @cubrr Most likely because it uses the same underlying code which also probably conforms to a (IETF?) standard. – DavidG Nov 11 '15 at 00:22
  • @DavidG You're right about them using the same underlying code. `WSAStringToAddress` is what they use. Regarding the standards: [RFC 791](https://tools.ietf.org/html/rfc791#section-2.3) states: _"Addresses are fixed length of four octets (32 bits)."_. I didn't find any other standard regarding "valid" addresses, so I can only assume that the parser behavior is specific to Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738563.aspx#Internet_Addresses – cbr Nov 11 '15 at 00:31
  • That's why it is called dotted "notation". 192.168.1.1 is just one representation (that is pretty easy for humans to remember) of a 32-bit number. Any 32-bit number can be represented like this, not just IP addresses. – Chet Nov 11 '15 at 01:02

3 Answers3

3

You can use the following code to validate IPv6 and IPv4 addresses:

public static bool IsValidIP(string address)
{
    IPAddress ip;
    if (!IPAddress.TryParse(address, out ip)) return false;

    switch (ip.AddressFamily)
    {
        case AddressFamily.InterNetwork:
            if (address.Length > 6 && address.Contains("."))
            {
                string[] s = address.Split('.');
                if (s.Length == 4 && s[0].Length > 0 && s[1].Length > 0 && s[2].Length > 0 && s[3].Length > 0)
                    return true;
            }
            break;
        case AddressFamily.InterNetworkV6:
            if (address.Contains(":") && address.Length > 15)
                return true;
            break;
    }
    return false;
}

According to documentation, IPAddress.AddressFamily will return either InterNetwork for IPv4 or InterNetworkV6 for IPv6.

  • I had to modify this a bit because it doesn't work with input like 12.9 (as cubrr mentioned), but thank you for this :) – crin Nov 11 '15 at 00:43
  • It doesn't actually return the IP, and it breaks on the input the asker is providing. – Chet Nov 11 '15 at 01:00
  • @Chet Oh, I didn't mention it in my question but I just need it to print "IPv4", "IPv6", or "Neither" so I changed the method to void. When I enter 192 my program doesn't break, it returns false (as seen in kraden's response) or prints "Neither". – crin Nov 11 '15 at 01:39
0

The way that MS parses the string you're entering makes it a valid IP address. They've added a kind of shorthand for dealing with parts of an IP and then they fill in the blanks.

If you look into the remarks section on this page you'll see what I'm talking about.

Josh
  • 1,724
  • 13
  • 15
0

There are many completely valid representations of an IP Address than just 0.0.0.0 format. "192" probably parses to 0.0.0.192, which is why the program isn't crashing AND why it's length is 4.

If you must only accepted dotted notation, use string.Split combined with int.Parse and create an IPAddress instance yourself.

Chet
  • 3,461
  • 1
  • 19
  • 24
  • Thank you for your suggestion. I will use string.Contains to see if the string has a "." or ":". I think that will work – crin Nov 11 '15 at 00:19
  • @corinne If you just use `string.Contains`, you will also allow an address like `192.0` . If you mean to validate whether the address has four octets or not, you probably want to do more than just a `string.Contains`. – cbr Nov 11 '15 at 00:22
  • Or use [regex](http://stackoverflow.com/questions/10006459/regular-expression-for-ip-address-validation) – DavidG Nov 11 '15 at 00:23
  • Ah, yes.. that's true @cubrr. Thanks for informing me about that lol – crin Nov 11 '15 at 00:26