LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-26-2011, 06:40 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-26-2011, 06:56 AM   #2
andrewthomas
Senior Member
 
Registered: May 2010
Location: Chicago Metro
Distribution: Arch, Gentoo, Slackware
Posts: 1,690

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
It is certainly not valid.

I would report the validation of the address as a bug in the software.
 
Old 09-28-2011, 05:53 PM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 09-28-2011, 06:12 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
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?)
 
Old 09-28-2011, 06:40 PM   #5
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Wim Sturkenboom View Post
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.

Last edited by SigTerm; 09-28-2011 at 06:53 PM.
 
2 members found this post helpful.
Old 09-29-2011, 09:28 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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

PS:
thanks to everybody for the replies.
 
  


Reply

Tags
ip address, tryparse


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to get online with 169.254 address Mark_667 Linux - Wireless Networking 10 04-11-2011 08:04 AM
no valid rcpt address -smtp linuxguy08 Linux - Server 5 03-19-2009 06:57 PM
WARNING: IP: Hardware address '***' trying to be our address 172.016.104.065 sunram Solaris / OpenSolaris 1 05-30-2008 09:51 AM
Odd IP address 169.254.187.86 in ifconfig (Etch) farpoint Debian 8 12-11-2006 12:10 PM
Gateway system not getting valid IP address richcoosa19 Linux - Networking 1 11-20-2002 11:15 AM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration