IP in network validator
Created: Last updated:
This is the documentation page for my IpInNetwork php class on github.org. This php class is part of my own framework built on top of the Zend Framework. Hence this validator extends to the Zend Framework library and set of validators. In case you are not familiar with validators see below What are validators.
In case you are looking for a webpage that converts betwween IP range and CIDR notation check out this magic-cookie webpage.
IP and network basics
Lets first answer the question what the problem is and why we need such a validator.
Basically every computer network based on TCP/IP is divided into segments or blocks usually connected by routers. The routers hold the information about the routes in a routing table. The Internet as the largest network of computers, of course, is no different.
All the server for a company (or at least a big chunk of them) are usually found within such a segment. Lets say we have a visit by a computer with IP 126.96.36.199 which belongs to Google. If you click the link you can see that this IP resides in a network with the following information
- NetRange: 188.8.131.52 - 184.108.40.206
- CIDR: 220.127.116.11/19
From the visit we also learn some referrer information in HTTP_REFERER which for this visit would look like this
- Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Now it is very easy to check the referrer and think that the visit is from Google but we cannot really trust the referrer. Of course, we can also click the website and verify manually each and ervery single time. Either way, we can only be sure the visit is indeed from a certain source when we verify the request IP address against a known network information.
Besides visits from Facebook, Google, Bing (MSN), Yahoo et al my framework also looks out for spam robots which are mostly well known addresses and even infiltrated whole networks.
For cases like these and probably some more—that's where the validator comes into play.
Validate IP in network
Unfortunately the Zend Framework does not have such a validator so I wrote my own which is now on github for your pleasure.
This class validates any given IPv4 address against a provided network notation. So the next question is: What kind of network notations can you add to the class?
Accepted network notations
The network notation can be a network range or a network address with a CIDR or 32-bit decimal subnet mask notation. For the network notation note that the main validation method always uses the CIDR notation, i.e a bitmask will be converted into a CIDR. If you don't fell comfortable with network notations or actually have a different type of use you can always use the network range which does not need to be a valid "routable" subnet and network segment. Following are some examples for network range and network notations.
Examples for network notations
- Network Range:
- true for all IP addresses inclusively in this range (i.e. from .12 to .19)
- CIDR notation:
- block with 4 hosts true for IP addresses from .8-.11 (i.e. .8, .9, .10, .11)
- Subnet mask notation:
- same as CIDR notation
Following are a few nifty things about the validator:
1) The network notations are validated, i.e. you have to pass a valid network and CIDR or subnet mask combination. For the network range the two values are validated and therefore must be valid IP addresses.
2) A CIDR notation of /32, subnet mask /255.255.255.255 or a range with two equal addresses is a valid network. In such a case it will validate for one host, i.e. the result is true if the network address or the range addresses are identical to the IP address.
3) The network notation or a range has to be set prior to calling isValid() as is custom with all Zend validators. The notation can be set when instantiating the object as an array and 'network' as the index. The setter method is setNetworkNotation($notation) and expects a string as the argument.
As mentioned above the class is available from my github account. Following is the direct link to the IpInNetwork class.
What are validators?
In case you are not familiar with validators here is a hint straight from the Zend documentation page Zend_Validate - Introduction and the Programmer's Reference Guide.
The Zend_Validate component provides a set of commonly needed validators. It also provides a simple validator chaining mechanism by which multiple validators may be applied to a single datum in a user-defined order.
In other words, a validator examines its input with respect to some requirements and produces a Boolean result, i.e. whether the input successfully validates against the requirements. If the input does not meet the requirements, a validator may additionally provide information about which requirement(s) the input does not meet.