Home All Groups Group Topic Archive Search About

IPAddress class in Framework 2.0

Author
2 Nov 2006 8:26 PM
Jason
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
jason.shatzka***@cexp.com

Author
2 Nov 2006 8:41 PM
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!
*************************************************
Show quote
"Jason" <Ja***@discussions.microsoft.com> wrote in message
news:CE2C5C32-1B9A-4AE5-9A76-FE86C6547F23@microsoft.com...
> 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
> jason.shatzka***@cexp.com
>
>
Author
2 Nov 2006 9:44 PM
Turkbear
On Thu, 2 Nov 2006 12:26:04 -0800, Jason <Ja***@discussions.microsoft.com> wrote:

Show quote
>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
>jason.shatzka***@cexp.com
>
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



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Author
2 Nov 2006 10:02 PM
Jason
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.~

Show quote
"Turkbear" wrote:

> On Thu, 2 Nov 2006 12:26:04 -0800, Jason <Ja***@discussions.microsoft.com> wrote:
>
> >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
> >jason.shatzka***@cexp.com
> >
> 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

>
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>
Author
3 Nov 2006 2:14 PM
Ben Voigt
Show quote
"Jason" <Ja***@discussions.microsoft.com> wrote in message
news:DC0E0974-1543-484C-94BD-87CD7C9072CD@microsoft.com...
> 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?

Show quote
>
> Thanks,...J.~
>
> "Turkbear" wrote:
>
>> On Thu, 2 Nov 2006 12:26:04 -0800, Jason
>> <Ja***@discussions.microsoft.com> wrote:
>>
>> >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
>> >jason.shatzka***@cexp.com
>> >
>> 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
>>
>>
>>
>> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>>

AddThis Social Bookmark Button