0

I am trying to determine whether a hard-coded string is a valid IPv4 or IPv6 address. I realized way too late that there's an IPAddress class, so I am trying to continue on with my unnecessarily difficult way. I have a findIPv4 method that works perfectly fine, but when I attempted to copy it and modify it slightly for the IPv6 method, it didn't work (because IPv6 uses hexadecimal and can have chars a-f). I think the problem lies in the !section.ToLowerInvariant().Contains('d') part of the if-statement, but I'm not sure.

public string findIPv6(string ipv6)
    {
        // This will split the user inputted string at every instance of a ':'
        var bytes = ipv6.Split(':');

        // If we do not have 8 bytes, it is not an IPv6 therefore return neither
        if (bytes.Length != 8)
        {
            return "Neither";
        }

        // This will check if there is an integer between 1 and 50
        int value;
        if (Int32.TryParse(ipv6, out value) && (value <= 50 && value >= 1))
        {
            return "Neither";
        }

        foreach (var section in bytes)
        {
            // If the length of the parsed int is not equal to the length
            // of the byte string or  0 > int < 9 or abcdef , return false
            int s;
            if (!Int32.TryParse(section, out s) || !s.ToString().Length.Equals(section.Length) || s < 0 || s > 9 || !section.ToLowerInvariant().Contains('a') || !section.ToLowerInvariant().Contains('b') || !section.ToLowerInvariant().Contains('c') || !section.ToLowerInvariant().Contains('d') || !section.ToLowerInvariant().Contains('e') || !section.ToLowerInvariant().Contains('f'))
            {
                return "Neither";
            }

        }

        return "IPv6";
    }
crin
  • 53
  • 1
  • 11
  • 3
    Don't be afraid to throw code away for tested core classes. There's a lot to be said about having tested, proven code and you may be wasting more time trying to "continue on" than it takes to swap it out. – Ron Beyer Nov 09 '15 at 19:51
  • 2
    I'd suggest to use [`System.Net.IPAddress.TryParse`](https://msdn.microsoft.com/en-us/library/system.net.ipaddress.tryparse.aspx) method, available in .NET. – Styxxy Nov 09 '15 at 19:52
  • Why not convert the Hex portions to integer and check that they are <=255? Look at `Convert.ToInt32("FF", 16)`. Or do the comparison between Hex values directly. I would go with @RonBeyer on this one though. Why reinvent the wheel in a potentially broken way? – gmiley Nov 09 '15 at 19:53
  • Here is a similar question: [How to parse ipv6 address into “octets”?](http://stackoverflow.com/questions/21027735/how-to-parse-ipv6-address-into-octets). And @Styxxy is right, use IPAddress.TryParse. – KiwiPiet Nov 09 '15 at 19:54
  • Your code will fail when parsing `::1`, which is a valid IPv6 address. It will also fail when parsing IPv4-mapped IPv6 addresses. Also, for the `if` statement in your `foreach`, the `Int32.TryParse` will for any `section` with a hex character in it. I suggest (like others here) that you use the library function instead of trying to write your own. – E. Moffat Nov 09 '15 at 20:01
  • Even though it may be a little confusing, if you would prefer, you could just make your existing functions wrappers to the `IPAddress` class functions instead of replacing every one of your function calls. – gmiley Nov 09 '15 at 20:04
  • you can use the regex to check if input string is valid IPv6 or not. – Deepak Sharma Nov 09 '15 at 20:05
  • @RonBeyer and everyone else, thanks. I'll look at thee IPAddress class. – crin Nov 09 '15 at 20:15

0 Answers0