Adrian World Design
  • Services
    • Website Consulting
    • Startup Consulting
    • Zend Development
  • Portfolio
    • My Framework
  • Knowledge
    • Web Standards
    • PHP bare-bones
    • Zend Framework
    • Zend Framework 2
    • Git - Github
    • Search Engine Optimization
    • Web Hosting
    • Linux
    • Microsoft Windows
    • Web Browsers
    • Mobile Devices
  • About
    • Business
    • Professionally
    • Personally
  • Contact
    • Contact Form
    • Phone
    • Email
    • Messaging

Knowledge Base Overview

IP in network validator

Knowledge ⇒ Git - Github ⇒ Code ⇒ IP in network validator
Tweet
Share on Tumblr

Created: Jan 17, 2012, 4:36:33 PM CSTLast updated: Oct 20, 2012, 8:52:16 AM CST

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 66.249.68.183 which belongs to Google. If you click the link you can see that this IP resides in a network with the following information

NetRange: 66.249.64.0 - 66.249.95.255
CIDR: 66.249.64.0/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:
128.0.0.12-128.0.0.19
true for all IP addresses inclusively in this range (i.e. from .12 to .19)
CIDR notation:
128.0.0.8/30
block with 4 hosts true for IP addresses from .8-.11 (i.e. .8, .9, .10, .11)
Subnet mask notation:
128.0.0.8/255.255.255.252
same as CIDR notation
Special Notes:

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.

Download Link

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.

blog comments powered by Disqus
Prev

Powered by FeedBurner Load our "Knowledge Base" feed in your RSS feeder

Follow us on Twitter
Follow us on Facebook
Follow us on LinkedIn
Follow us on Google+

All rights reserved, Adrian World Design ©2009–2023 Powered by Wejas Framework

Jump to Top