#!/usr/bin/perl use warnings; use strict; package main; my $suspect = '72.31.79.5'; my $suspect2 = '255.255.255.0'; &is_hostaddress($suspect, $suspect2); sub dec2bin { my $str = unpack("B32", pack("N", shift)); return $str; } sub bin2dec { return unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub is_hostaddress { my $ipaddr = shift; my $netmask = shift; return unless defined($ipaddr) && defined($netmask); my $binipaddr = unpack('B32', pack('C4C4C4C4', split(/\./, $ipaddr))); my $binnetmask = unpack('B32', pack('C4C4C4C4', split(/\./, $netmask))); print "ip addr = $binipaddr\n"; print "netmask = $binnetmask\n"; # # Convert the mask back to a decimal integer before # attempting to negate. Negating directly gives # Interesting results. # my $ugly = ~ $binnetmask; my $not = dec2bin( ~ bin2dec($binnetmask)); print "badmask = $ugly\n"; print "notmask = $not\n"; my $result = $binipaddr & $binnetmask; my $result2 = $binipaddr | $not; print $result; print "\n"; print $result2; print "\n"; }