Re: Routine to convert netmask to IP range
From: Maarten Wiltink (maarten_at_kittensandcats.net)
Date: 11/09/04
- Next message: Asd: "Re: ZIP component that does direct file I/O to a file in archive"
- Previous message: _at_: "Re: ZIP component that does direct file I/O to a file in archive"
- In reply to: Peter Piper: "Routine to convert netmask to IP range"
- Next in thread: Martin Harvey (Demon account): "Re: Routine to convert netmask to IP range"
- Reply: Martin Harvey (Demon account): "Re: Routine to convert netmask to IP range"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 9 Nov 2004 15:58:51 +0100
"Peter Piper" <NoEmail@Email.com> wrote in message
news:cmqdpr$n9c$1@titan.btinternet.com...
> Does anyone know a way, in delphi, to convert a netmask or IPbits in
> to an IP range?
>
> Eg. 192.168.0.1\24 or 192.168.255.255 or similar
An IP address is basically a 32-bit number. The dotted-quad notation is
used only to make them more easy to remember for humans. The IP address
192.168.0.1 is strictly equal to $c0a80001 or 3,232,235,521 (unsigned)
decimal.
The slash is a _forward_ slash, thank you very much, and indicates the
number of high bits that make up the network part of the address. The
remaining bits are the host part and should be zero because when you're
including prefix lengths, you're expected to be describing the network
part of the IP address - for some reason, using the notation to give
an IP address and what it thinks is its netmask isn't done.
The netmask is an older way of expressing how the IP address is
partitioned. The ones in it mark the bit in that position as belonging
to the network part, and the zeroes mark the host part. The ones should
form a single block on the left and the zeroes a single block on the
right.
So now you know everything you need to write your own code. Decode the
dotted quad into a Cardinal (best done by overlaying it with a record
or array with four bytes in it), test that the host part is zero if you
want to be strict, and the range of host parts is 0 to 2^n-1, where n
is the prefix length.
Host zero denotes the network range as a whole, host (-1) (2^n-1) is
the broadcast address. Neither is allowed to be used as a host IP
address. A broadcast address ends in all ones, but since the network
part may also end with ones, you can't reliably deduce the prefix length
from a broadcast address alone. The 192.168.255.255 above may be the
broadcast address for network 192.168/16, or 192.168.255/24, or
192.168.255.254/31 (this range contains two addresses, the network
address and the broadcast addresses, and no useable host addresses).
Groetjes,
Maarten Wiltink
- Next message: Asd: "Re: ZIP component that does direct file I/O to a file in archive"
- Previous message: _at_: "Re: ZIP component that does direct file I/O to a file in archive"
- In reply to: Peter Piper: "Routine to convert netmask to IP range"
- Next in thread: Martin Harvey (Demon account): "Re: Routine to convert netmask to IP range"
- Reply: Martin Harvey (Demon account): "Re: Routine to convert netmask to IP range"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|