networking

Determining if an IP is within a specific range: redux

Submarine NetI was reading Paul Gregg’s very clear explanation of “classless” ranges comparison when I realized that his code was not as “bare metal” as could be.

So, here is the code I’ve been using in nextBBS.
It only accepts ranges in the form “x.x.x.x/b” but it’s short and all I needed, really.

function isSubnet($subnet, $ip)
{
    // Classless (in more than one way) comparison
    $cursubnet = explode('/', $subnet);
    $longsubnet = ip2long($cursubnet[0]);
    $longip = ip2long($ip);
 
    if(count($cursubnet)<2)
    {
        // Compare IP itself
        return ($longip==$longsubnet);
    }
 
    // IPv4 only!
    $subnetmask = 0xffffffff << (32-$cursubnet[1]);
    return (($longip & $subnetmask) == ($longsubnet & $subnetmask));
}

If you enjoyed this post, make sure you subscribe to my RSS feed!


How I setup networking for VirtualBox on Ubuntu

Just a quick note: I use VirtualBox daily at work. It provides excellent emulation for my original XP install (the one that came pre-installed on my Dell box!) while my main OS is Ubuntu. Setting up networking is not as straightforward as with VMWare, so here is what I had to do:

First, edit /etc/udev/rules.d/20-names.rules and make sure this line exists:

KERNEL="tun", NAME="net/%k"

Then create this script – you will run it whenever you wish to reconfigure your network:

#!/bin/bash
echo "Erasing old configuration"
sudo route del default
sudo ifconfig tap0 down
sudo ifconfig eth0 down
sudo ifconfig br0 down
sudo brctl delbr br0
sleep 1
 
echo "Creating virtual interface"
sudo chown chris /dev/sdc1
sudo tunctl -t tap0 -u chris
sleep 1
echo "Creating bridge interface"
sudo brctl addbr br0
sleep 1
echo "Making physical interface promiscuous"
sudo ifconfig eth0 0.0.0.0 promisc
sleep 1
echo "Binding bridge to physical interface"
sudo brctl addif br0 eth0
sleep 1
#sudo ifconfig br0 10.255.203.34 netmask 255.255.255.0
echo "Configuring IP address"
sudo ifconfig br0 198.206.186.210 netmask 255.255.255.0
# sudo dhclient br0 # The new way!
sleep 1
echo "Binding bridge to virtual interface"
sudo brctl addif br0 tap0
sleep 1
echo "Enabling virtual interface"
sudo ifconfig tap0 up
sleep 1
#sudo route add default gw 10.255.203.254
echo "Adding default route"
sudo route add default gw 198.206.186.254
echo "All done!"

What’s that, then?
We start by getting rid of any existing configuration; then I change my virtual interface device’s owner to my own user, since I am logged in as a non-root user. Obviously you need to replace ‘chris’ with your own user name. From now on, tap0 will be the virtual interface seen by VirtualBox.
I make sure that my real interface is in promiscuous mode, which then allows me to bind it with a bridge interface.
All that is left to do it configure my bridge interface so that it can take over communicating with the rest of the world!

If you enjoyed this post, make sure you subscribe to my RSS feed!


How To Setup An Agile Networking Lab – Part 1

Caution: boring tech porn blather ahead!

It recently became obvious to me that the lab I had setup for our development team was not aging very well. When I first configured it, I was under the impression that creating a boatload of VLANs and using routing everywhere was the way to go. Of course that belief held true for about 2 days, ’til I realized that what we really need is an agile network, of which subsets can be reconfigured in no time, to mimic particular real-life configurations.

Thus, my new setup is going to be as follows:

  1. An edge switch, that will connect the lab to the outside world
  2. A core of routers that will be immutable
  3. Everything else will vary daily

(more…)

If you enjoyed this post, make sure you subscribe to my RSS feed!