Directly Mapping MAC addresses to IP addresses for mesh networks

In mesh networks or rather on a wide basis in ad hoc networks assigning IP addresses can be a bit of a drag. The MIT Roofnet Project came up with this neat hack where they thought of taking the MAC address and extracting the last three bytes and assigning these bytes to the IP Address. The reason why it works is that a class A IP address can be assigned the last 24 bits which is equal to the number of bits from the last 3 bytes ( 8 * 3 ) of a MAC address.This perl script implements this. The input given to the script is the network interface :

#!/usr/local/bin/perl -w

# MAC to IP address conversion script for mesh networks
# Version 0.1
# Copyright (c) 2007, Wiser Group , http://wireless.cs.uh.edu/
# Released under the GPL license
# http://www.gnu.org/copyleft/gpl.html
# usage : perl ipconfig.pl [nw interface]

use strict ;

#Extract the line which contains the MAC Address
my $cmd = ‘ifconfig ‘.$ARGV[0].’ | grep HWaddr’ ;
my $mac_addr_line = (`$cmd`) ;
my @mac_addr_split = split (‘ ‘ , $mac_addr_line ) ;

#Split the MAC address to get the last three bytes
my @mac_addr = split (/:/,$mac_addr_split[4]) ;
my @ip ;
my $count = 0 ;

#Convert from Hex to Decimal
foreach (@mac_addr)
{
$ip[$count] = hex ($_) ;
$count++ ;
}

# Construct IP Address
my $ip_addr = ’10.’.$ip[3].’.’.$ip[4].’.’.$ip[5] ;
$cmd = ‘ifconfig ‘.$ARGV[0].’ ‘.$ip_addr ;

# Assign IP Address
system ($cmd) ;

Advertisements

One thought on “Directly Mapping MAC addresses to IP addresses for mesh networks

  1. Or a one-liner:

    ifconfig | grep HWaddr | awk ‘{ print $5 }’ | perl -ne ‘chomp; printf( “%s => 10.%d.%d.%d\n”, $_, map { hex( $_ ) } (split/:/,$_)[3..5] )’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s