473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Matching IP scopes.

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?

Anyone done this?

--
Sandman[.net]
Feb 27 '06 #1
6 1767
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.
--
Oli
Feb 27 '06 #2
Oli Filth said the following on 27/02/2006 20:05:
An IP address matches a given network IP address if:

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


Oops, those should be &, not ^...

--
Oli
Feb 27 '06 #3
In article <mY*******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> 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?
--
Sandman[.net]
Feb 27 '06 #4
Sandman said the following on 27/02/2006 20:27:
In article <mY*******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> 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
Feb 27 '06 #5
On Mon, 27 Feb 2006 21:27:43 +0100, Sandman <mr@sandman.net> wrote:
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?


The user-contributed notes on http://uk.php.net/ip2long may interest you.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Feb 27 '06 #6
In article <Bu******************@newsfe2-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:
Sandman said the following on 27/02/2006 20:27:
In article <mY*******************@newsfe6-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> 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)


I get it now, I think I have it working. Thanks!
--
Sandman[.net]
Feb 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Miki Tebeka | last post by:
Hello, Can anyone explain why: >>> def make_inc(n): s = n def inc(i): s += i return s return inc
3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
4
by: kj | last post by:
I'm a Perlhead (there, I said it). Years ago I made a genuine attempt to learn Python, but my intense disappointed with the way Python deals with scopes ultimately sapped my enthusiasm. I...
0
by: Armin Wagenknecht | last post by:
Hello, i am modelling a TopicMap, and I have the following problem: I want to use two scopes in the topicmap for one occurence which has to satsify BOTH scopes!! Example: I have the...
15
by: __PPS__ | last post by:
Hello everybody, in my exam in c++ I answered that destructors may be declared private, public or protected. The question was: "What possible scopes (public or private) can a destructor have?" ...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
3
by: giblfiz | last post by:
I was hoping that someone on the group might have an idea of how to access scopes (or symbol tables) other than the one you are currently running in. (and no I don't mean global) In particular,...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.