You are viewing a read-only archive of the Blogs.Harvard network. Learn more.
Skip to content

Dotless IPs

One of the fun facts people don’t generally think about is that the IP address can be represented in several ways. The most recognizable is the “dotted quad”. Dotless addresses are just converted to long words (from little endian). This technique has actually been used to bypass certain security devices. A dotted address looks like this
127.0.0.1

We’ve all seen this type of address before. The dotless looks like this
2130706433

This topic came up during my night while creating a database to hold a large amount of IP addresses. It’s MUCH easier to convert to this integer format then trying to store the address any other way. At least for me it was, at two in the morning. Anyhow I needed to create some subroutines for my perl scripts to convert addresses and thought I’d publish them. Seeing as no one else in the immediate google archive seemed to have done so.
Just as a warning i took a few shortcuts in this script and it could be a lot cleaner. Again it’s two thirty in the morning so cut me a little slack. I’ve included the URL to an online version so anyone attempting to recreate this can check their work online.

#!/usr/bin/perl
# ip conversion tool
# this script is covered under the GPL.
# if you don’t know what it is or what that means
# look it up before you use this script

use strict;

print “Enter Address:”;
my $address = ;
print “\n”;

sub DottedToLong
{
my $DottedAddress=shift;
my ($short1,$short2,$short3,$short4) = split(/\./,$DottedAddress);

my $longip = ($short1*(256*256*256)) + ($short2*(256*256)) + ($short3*(256)) + ($short4);
return $longip;
}

sub LongToDotted
{
my $LongAddress=shift;
my ($octet1,$octet2,$octet3,$octet4);
$octet1=int($LongAddress/(256*256*256));
$octet2=int(($LongAddress%(256*256*256))/(256*256));
$octet3=int((($LongAddress%(256*256*256))%(256*256))/256);
$octet4=int(((($LongAddress%(256*256*256))%(256*256))%(256))/1);
my $DottedIP=”$octet1.$octet2.$octet3.$octet4″;
return $DottedIP;
}

if ($address=~/\\./) {print DottedToLong($address);}
else {print LongToDotted($address);}

Post a Comment

You must be logged in to post a comment.