IPAddress class in Framework 2.0

G

Guest

All,

Please note the following:

IPAddress ip = IPAddress.Parse(“10.1.176.10â€);

Works fine, as expected, as does:

bool good = IPAddress.TryParse(“10.1.176.10â€);

Both return the proper result. HOWEVER…

IPAddress ip = IPAddress.Parse(“10.1.176â€);

Also works fine? It creates an IP address object, which when examined became
“10.1.0.176� WTF?

Note that this also returned true:

bool good = IPAddress.TryParse(“10.1.176â€);

Am I simply out of luck until version 3? Is this a known issue? Am I the one
who’s wrong, but just don’t see it? (wouldn't surprise me:))

For the life of me I can’t understand how TryParse() could return true on an
IP address with only 3 octets?

Please advise….

Thanks,…Jason

Jason Scott Shatzkamer
Director of IT / Ecommerce
Corporate Express
954.379.5415
(e-mail address removed)
 
C

Cowboy \(Gregory A. Beamer\)

Looks like they missed the "never trust user input" argument. :)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
T

Turkbear

All,

Please note the following:

IPAddress ip = IPAddress.Parse(“10.1.176.10”);

Works fine, as expected, as does:

bool good = IPAddress.TryParse(“10.1.176.10”);

Both return the proper result. HOWEVER…

IPAddress ip = IPAddress.Parse(“10.1.176”);

Also works fine? It creates an IP address object, which when examined became
“10.1.0.176”? WTF?

Note that this also returned true:

bool good = IPAddress.TryParse(“10.1.176”);

Am I simply out of luck until version 3? Is this a known issue? Am I the one
who’s wrong, but just don’t see it? (wouldn't surprise me:))

For the life of me I can’t understand how TryParse() could return true on an
IP address with only 3 octets?

Please advise….

Thanks,…Jason

Jason Scott Shatzkamer
Director of IT / Ecommerce
Corporate Express
954.379.5415
(e-mail address removed)
Perhaps the expanation is here:( http://msdn2.microsoft.com/en-gb/library/system.net.ipaddress.parse.aspx )
--------------------------------------------------------
The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and
in colon-hexadecimal notation for IPv6.

The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. A one
part address is stored directly in the network address. A two part address, convenient for specifying a class A address,
puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. A
three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part
in the second byte, and the final part in the right-most two bytes of the network address. For example:


Number of parts and example ipString
IPv4 address for IPAddress

1 -- "65536"
0.0.255.255

2 -- "20.2"
20.0.0.2

2 -- "20.65535"
20.0.255.255

3 -- "128.1.2"
128.1.0.2



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
G

Guest

Nice find...that DOES look a whole lot like my pain :)

What I do know, however, is that if someone ever called me and I asked them
for their IP address, and they told me "10.1.176", I would say "That is not a
valid IP address."

More so if someone said "65536"...

I guess the real answer, then, is to do the following logic:

string ipString = "10.1.176";
if (IPAddress.TryParse(ipString))
{
IPAddress ip = IPAddress.Parse(ipString);
if (ip.ToString() != ipString)
// Not Valid
else
// Valid
}

That look about right? Seems silly, but works...

Thanks,...J.~
 
B

Ben Voigt

Jason said:
Nice find...that DOES look a whole lot like my pain :)

What I do know, however, is that if someone ever called me and I asked
them
for their IP address, and they told me "10.1.176", I would say "That is
not a
valid IP address."

More so if someone said "65536"...

I guess the real answer, then, is to do the following logic:

string ipString = "10.1.176";
if (IPAddress.TryParse(ipString))
{
IPAddress ip = IPAddress.Parse(ipString);
if (ip.ToString() != ipString)
// Not Valid
else
// Valid
}

That look about right? Seems silly, but works...

It'll reject leading zeros which aren't actually invalid.

Maybe IPAddress.TryParse(ipString) && ipString.Split(new Char[] {
'.' }).Length == 4?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top