Re: Routine to convert netmask to IP range

From: Maarten Wiltink (maarten_at_kittensandcats.net)
Date: 11/09/04


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



Relevant Pages

  • Re: single host netmask (255.255.255.255)
    ... The routes from three interfaces, propagate via OSPF to the rest of network.... ... One way is to remember IP addresses assigned to each interfaces, but more smart solution is to assign to this machine one EXTERNAL LOOPBACK address (single IP with mask 255.255.255.255, in other words SINGLE HOST assigned to Microsoft loopback adapter), and propagate this address ... The address 255.255.255.255 denotes a broadcast on a local hardware network, ...
    (microsoft.public.win2000.networking)
  • Re: Should I configure a firewall to allow multicast?
    ... firewall is blocking various hosts to 192.168.1.255 port 138. ... network running from 192.168.1.0 through 192.168.1.255 which would ... broadcast address - received by every host on the subnet. ...
    (comp.security.firewalls)
  • Re: internet ip addressing
    ... You need also a broadcast address making the minimum number to four. ... (some operating systems do not need the "network address" and can use ... it as a host address). ... What is normal in a point to point link is that the "client" end will ...
    (comp.os.linux.networking)
  • Re: puzzling connections
    ... There can be various reasons for a lot of broadcast ... activity depending on your network configuration. ... >> the internet and do not intend to network with other computers added to ... >> unicast traffic and is intended only for the computer with that host IP. ...
    (microsoft.public.security)
  • Re: 2 pc network - cant see host files from pc 2 on pc 1
    ... If the second card is lost on HOST PC then DSL Internet does not connect. ... Ditch the second network card in the one ...
    (microsoft.public.windowsxp.security_admin)