#!/bin/perl
use Socket;

if (@ARGV != 1) {
	die "$0: argument must be a dotted IPv4 address";
}

$ip = inet_aton($ARGV[0])
	or die "$0: argument $ARGV[0] must be a dotted IPv4 address";

$i = unpack('N', $ip);

if (($i & 1 << 31) == 0) {
	$class = 'A';   #Bit 31 is 0 (includes loopback address).
} elsif (($i & 1 << 30) == 0) {
	$class = 'B';   #Bit 31 is 1, bit 30 is 0.
} elsif (($i & 1 << 29) == 0) {
	$class = 'C';   #Bit 31 is 1, bit 30 is 1, bit 29 is 0.
} elsif (($i & 1 << 28) == 0) {
	$class = 'D';   #Bit 31 is 1, bit 30 is 1, bit 29 is 1, bit 28 is 0.
} else {
	$class = 'E';   #Bit 31 is 1, bit 30 is 1, bit 29 is 1, bit 28 is 1.
}

print "$ARGV[0] is a class $class IPv4 address.\n";
exit 0;
