Re: Matching IP scopes.



Sandman said the following on 27/02/2006 20:27:
In article <mYIMf.60881$mf2.26167@xxxxxxxxxxxxxxxxxxxx>,
Oli Filth <catch@xxxxxxxxxxxxxx> wrote:

Sandman said the following on 27/02/2006 19:59:
So, I have this list of valid IP scopes, in the form below. How do I match if $_SERVER[REMOTE_ADDR] is covered in any of these scopes?

193.11.120.0/21
193.11.128.0/24
193.11.129.0/24
193.11.130.0/24
193.11.131.0/24


First, I don't really know how to interprete the "/24" ending. I am guessing that "193.11.131.0/24" means "193.11.131.X to 193.11.131.Y" or something, but what? When that's translated to something useful, how do I match IP numbers reliably?
The /XX represents the length of the subnet mask in bits, so your hunch is pretty much correct.

An IP address matches a given network IP address if:

(Address ^ Mask) == (NetAddress ^ Mask)

where Mask = 11111...0000, the number of ones given by the /XX.

Ok, but how do I calculate it? Bits you say, but how do I translate 24 bits to addresses? For instance, the first line above, what addresses does it cover?


An IP address is a 32-bit quantity, which can be represented as A.B.C.D, where the actual 32-bit value is given by:

Y = (A * 2^24) + (B * 2^16) + (C * 2^8) + D

[I'm using ^ to represent "to the power of" in this case.]

So convert your test address and network address to this form, either directly or by using the ip2long() function.

Then form your subnet mask as a 32-bit value.
[HINT: (2^X - 1) = (1000....000 - 1) = 111....111]

Then test the equality of the expression I originally posted, noting that I got it wrong, and it should be:

(Address & Mask) == (NetAddress & Mask)


--
Oli
.



Relevant Pages

  • Re: Matching IP scopes.
    ... Oli Filth wrote: ... I don't really know how to interprete the "/24" ending. ... The /XX represents the length of the subnet mask in bits, ... but how do I translate 24 ...
    (comp.lang.php)
  • Re: Matching IP scopes.
    ... Oli Filth wrote: ... but how do I translate 24 ... So convert your test address and network address to this form, ... Then form your subnet mask as a 32-bit value. ...
    (comp.lang.php)
  • Changing the subnet mask of an existing DHCP Scope
    ... This holds true for any version of Windows Server Software I can ... that I do - the subnet mask is nothing more but DHCP option number 1 ... I see no reason for inhibiting a change of just a simple network ...
    (microsoft.public.windows.server.networking)
  • Re: TCPIP Profile (GATEWAY vs BEGIN/ENDROUTES)
    ... GATEWAY is the old, old way of doing it, the understanding of which was some ... Thus the network is 10, ... says and 1500 is good for one of the Ethernet flavours, Subnet Mask is ... TCPIP Profile ...
    (bit.listserv.ibm-main)
  • Re: Home Net Probs - AGAIN!!
    ... > the subnet mask was 255.0.0.0 as per the previous problem, ... > standard reboot and default back to factory settings - no change. ... rebooted the first two computers, and the network came back to normal. ...
    (microsoft.public.windowsxp.network_web)