1

I'd like to use System.Net.IPAddress.TryParse to validate IPv6 addresses because I don't want to write my own reg exp :-)

However, this seems to allow strings such as "(validIPv6)](anythingatallhere)" - for example, "1234::5678:abcd]whargarbl".

Is there a reason for these being valid, or is this a fault?

This is further complicated by the fact that I actually want only strings of the form "[(validIPv6)]:(portnumber)" so I'm going to have to do a bit of validation myself.

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • System.Net.IPAddress.Parse("1234::5678:abcd]whargarbl") Fails for me. Remember when using try parse it doesn't do throw an except rather just leaves the out param as it's default value – Brian May 14 '10 at 16:27
  • What version of Windows? – SLaks May 14 '10 at 17:07
  • used2could: That string passes for me - TryParse returns True, and the IPAddress has e.g. AddressFamily set to InterNetworkV6 and ToString() value "1234:0000:0000:0000:0000:0000:86.120.171.205". SLaks - this is XP, which I guess doesn't actually support IPv6. – Rawling May 15 '10 at 10:25

1 Answers1

0

I was looking through reflector and it seems that if your current OS does not support IPV6 a parse routine is called that will ignore everything past the final ']' character.

This here seems to be the offending code, take notice of the second or condition as it relates to the preceding bit of code.

int length = ipString.Length;
fixed (char* str2 = ((char*) ipString))
{
    char* name = str2;
    if (IPv6AddressHelper.IsValid(name, start, ref length) || (length != ipString.Length))
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • Could you post a bit more? I can't really figure that out. Or do you not think the rest will help? Looks like IsValid either alters ipString (via str2 and name) or length... so either returns true if the whole string is valid, or alters the string/length if a substring is valid? – Rawling May 14 '10 at 17:06
  • @Rawling - Your theory is similar to what I gathered so posting more code probably wouldn't make a difference, it is really too much code to post anyway. – ChaosPandion May 14 '10 at 17:17
  • Also referenced here: http://social.msdn.microsoft.com/Forums/en/ncl/thread/52774eba-57ad-4232-bf2f-c804a0dcf8a4 – Rawling May 15 '10 at 10:30
  • So basically, it's slightly broken. Having realised that I don't actually need to use a regulr expression, I'm tempted to just write my own validation code, either handling the square brackets myself and passing the rest to the library or just use Split and so on to validate it entirely myself. – Rawling May 15 '10 at 10:32