#!/usr/bin/perl
#
# Dword.pl by Ben H <bro_evil@unbounded.com>
#
# Usage: dword.pl [ -q | --quiet ] host/ip
#
# the quiet doesnt display anything but the result, try
#
# $ telnet `dword.pl -q 127.0.0.1` 25
#
# connect to localhost's mail. not very useful, but still... 
#
# like dword.c by neeko <optik9@hotmail.com> but in perl, and adds a bit
# So full credit to him, i just made it easier to use and in perl
#
# Its crap and cludgy, but hey it works..
#
# It was written, cos my perl (as you can see) needs some work.
# and i liked neeko's version, but not the spaces in the ip..
#
# Thanks to lineman of packetstorm (http://packetstormsecurity.org) for
# spotting a bug..
#

use Socket;			# for gethostbyname()
use Math::BigInt;		# so it fits..

my $quiet, $host, @ip;		# get some vars started.

if ( $#ARGV < 0 ) {
   print "$0 by Ben H <bro_evil\@unbounded.com>\n";
   print "Usage: $0 [-q | --quiet] host \n";
   exit;
}

if ( $ARGV[0] =~ /-q|--quiet/ ) {
   $quiet=1;
   $name = $ARGV[1];
}
else {
   $name = $ARGV[0];
   print "$0 by Ben H <bro_evil\@unbounded.com>\n";
}

@host = gethostbyname( $name );    # get the ip, if a hostname is used

$foo = $host[4];

# This parses the result of the gethostbyname into numbers
# if anyone knows a better way, PLEASE tell me!

for $n (1..4) {
   $ip[$n] = ord( substr( $foo , ($n-1) , 1 ) );
}

if ($quiet != 1) {
   print "$name = $ip[1].$ip[2].$ip[3].$ip[4] = ";
}

# Values were taken from dword.c, but converted it into a nice(?!) loop

for $n (1..4) {
   $ip[$n] = ( $ip[$n] * ( 2 ** ( ( 3 - ($n-1)) * 8 ) ) );
}

print ($ip[1] + $ip[2] + $ip[3] + $ip[4]);
print "\n";

exit;
