LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Is 172.32.254 a valid ip address ? TryParse thinks it is. (http://www.linuxquestions.org/questions/programming-9/is-172-32-254-a-valid-ip-address-tryparse-thinks-it-is-905039/)

Wim Sturkenboom 09-26-2011 05:40 AM

Is 172.32.254 a valid ip address ? TryParse thinks it is.
 
Not sure where to post this. This might be networking or coding.

I have an ipv4 ip address that I need to validate in C#. I'm using IpAddress.Tryparse

Code:

            IPAddress ip = null;
            if (rc == true && IPAddress.TryParse(address, out ip) == false)
            {
                errmsg = "Invalid IP address '" + address + "'";
                rc = false;
            }

If I pass 172.18.254 as the address, parsing succeeds and the resulting bytes are 172, 18, 0 and 254.

Can somebody explain this to me?

andrewthomas 09-26-2011 05:56 AM

It is certainly not valid.

I would report the validation of the address as a bug in the software.

Wim Sturkenboom 09-28-2011 04:53 PM

Hm, that would mean filing a bug report with Microsoft. Need to so further research first before I take that step.

Current workaround is to first split the string with the IP address and check if I get four octets back.

anomie 09-28-2011 05:12 PM

I don't know anything about C# or TryParse(), but: that seems like an absurdly blatant validation failure. Not only that, it's inserting a value for you. (Maybe the documentation speaks to what happens when you pass it three octets?)

SigTerm 09-28-2011 05:40 PM

Quote:

Originally Posted by Wim Sturkenboom (Post 4482443)
Not sure where to post this. This might be networking or coding.

I have an ipv4 ip address that I need to validate in C#. I'm using IpAddress.Tryparse

Code:

            IPAddress ip = null;
            if (rc == true && IPAddress.TryParse(address, out ip) == false)
            {
                errmsg = "Invalid IP address '" + address + "'";
                rc = false;
            }

If I pass 172.18.254 as the address, parsing succeeds and the resulting bytes are 172, 18, 0 and 254.

Can somebody explain this to me?

IPAddress.TryParse
Quote:

IPAddress.TryParse uses inet_addr under the hood to parse an IPv4 address. This makes the following rules valid:

1) Each part of the address can be either specified in decimal, octal or hexadecimal notation. A leading zero will indicate an octal number, a "0x" prefix will indicate an hexadecimal one. The following strings will all parse to the same address:

"16.16.16.16" // decimal
"020.020.020.020" // octal
"0x10.0x10.0x10.0x10" // hexadecimal
"16.020.0x10.16" // mixed base


2) Compatibility with Berkley software:

When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most 2 bytes of the network address.

Example: "12.12.258" is valid and maps to 12.12.1.2

When a two-part address is specified, the last part is interpreted as a 24-bit quantity and placed in the right-most 3 bytes of the network address.

Example: "12.258" is valid and maps to 12.0.1.2

For further information, read the MSDN article on inet_addr
Solution: use regexp for validating ip address.

Please note, that in situations like this (unexpected behavior of system function) following approach is recommended:
  1. Read offline (installed with SDK or compiler) documentation for the function.
  2. Read online documentation (may be newer than offline documentation).
  3. Google with function name + keywords describing problem.
  4. Read source code or attempt to reverse-engineer if it is not time-consuming and does not violate library license.
  5. Ask question on the forum.

Wim Sturkenboom 09-29-2011 08:28 AM

Thanks SigTerm.

I found that link, but I never read the user comments in it (till somebody on another forum pointed me to it about 8 hours ago). In my opinion it should not be there but under remarks in the 'official' section.

And this is the inet_addr link that I found on msdn.

OK, solved :D

PS:
thanks to everybody for the replies.


All times are GMT -5. The time now is 05:31 AM.