by Kevin Schroeder | 12:00 am

I am stupid busy right now.  Between normal work, adoption (moreso the stupid state requirements), preparing for 3 user group meetings and 6 hours of speaking at OSI Days in India, working on the back yard, writing Flash/Flex articles and recording an album I really don't have much time to get a whole lot done.  I've got some really cool stuff as part of my normal work for the cloud that's coming out, but getting that done has proven to be a challenge.

I'm also woefully behind on blogging.  So, to alleviate that I am going to post an excerpt from my upcoming book "You want to do WHAT with PHP?"  Today we are going to take a look at Chapter 1.

   Chapter 1: Networking and Sockets
   Chapter 2: Binary Protocols
   Chapter 3: Character Encoding
   Chapter 4: Streams
   Chapter 5: SPL
   Chapter 6: Asynchronous Operations with Some Encryption Thrown In
   Chapter 7: Structured File Access
   Chapter 8: Daemons
   Chapter 9: Debugging, Profiling, and Good Development
   Chapter 10: Preparing for Success

 

Because you are limited to working with bits you cannot be specific as to how many IP addresses have been assigned to a given subnet. This can be calculated simply by switching one bit at a time from the end of the 32 bit IP address to a zero. On a class C network that means that our only possible netmask options are

 

Class C Netmasks
255.255.255.252
255.255.255.248
255.255.255.240
255.255.255.224
255.255.255.192
255.255.255.128

Figure 1.5 Class C netmasks

Subnetmask 255.255.255.254 is also available. But since each subnet needs to have one IP address as the network endpoint and one IP address for broadcasting packets to the subnet, the 255.255.255.254 subnetmask is mostly useless to you.

Let’s take our original program for calculating netmasks and now add support for subnets as well as netmasks other than for class A, B or C networks.

Figure 1.6 Calculating host/netmask relationship, example 2

$host = ip2long('192.168.0.35');

$networks = array(
    '192.168.0.0' => '255.255.255.248',
    '192.168.0.16' => '255.255.255.192',
    '192.168.1.0' => '255.255.255.0'
);

echo "Host: " . long2ip($host) . "nn";

foreach ($networks as $network => $netmask) {
    $printNetwork = $network;
    $printNetmask = $netmask;
    $network = ip2long($network);
    $netmask = ip2long($netmask);

    echo "Network: {$printNetwork} Netmask: {$printNetmask}n";
    echo decbin($host & $netmask)
    . " Host netmasked: {$printNetmask}n";
    echo decbin($network) . " Networkn";
    echo decbin($network & $netmask)
    . " Network netmaskedn";

    if (($host & $netmask) == ($network & $netmask)) {
        echo 'n' . long2ip($host) . " is on network "
        . long2ip($network);

    exit;
    }

    echo "n";
}

This code prints out

Host: 192.168.0.35

Network: 192.168.0.0 Netmask: 255.255.255.248
11000000101010000000000000100000 Host netmasked: 255.255.255.248
11000000101010000000000000000000 Network
11000000101010000000000000000000 Network netmasked

Network: 192.168.0.16 Netmask: 255.255.255.192
11000000101010000000000000000000 Host netmasked: 255.255.255.192
11000000101010000000000000010000 Network
11000000101010000000000000000000 Network netmasked

192.168.0.35 is on network 192.168.0.16

Using that calculation you can now determine the sub-network that an IP address is on. At this point you might be asking yourself “as a web developer, why am I going through this in the first place.” There are several reasons.

  1. You should. Many PHP developers just assume that the network is there without knowing the underlying concepts. While it may not always be necessary to know that, it will make you more aware of network related issues that you may run into.

  2. The security concept of Defense in Depth posits that a security implementation should have multiple layers. This allows you to add a networking component to your overall security implementation if you need it.

  3. You could provide different content for people who are on a specific network. For example, a competitor.

  4. By understanding some of the basics of networking you can build better, faster, more secure, more interesting network services. From web services to your own binary protocols, an understanding of basic networking helps you to write better programs.

Tags:

Comments

onezero

Thanks for the preview! Looks like a interesting, and educational book to read. Will the book also be available at O’Reilly’s Safari Books Online?

Aug 25.2010 | 12:44 pm

Kevin

Most likely not, but the publisher is looking at e-publishing as well as dead-tree.

Aug 25.2010 | 04:30 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.