use strict; open TXT, ") { chomp; my @words = split(/ +/); print "Checking mask $words[2]\n"; my $maskvalid = ValidateMasks($words[2]); print "Mask is $maskvalid\n\n"; } sub ValidateMasks() { return 0 unless defined $_[0]; my @octets = split (/\./, $_[0]); print "\toctets array is @octets\n"; my $valid = 1; my $index = 0; my $flagvalue = 0; while($index < 4) { $valid = ValidateOctet($octets[$index]); $index++; } return $valid; sub ValidateOctet() { return 0 unless defined $_[0]; # bad if no argument passed return 0 unless $valid; # return bad if any previous argument is bad my $oc = $_[0]; return 0 if $oc > 255; # bad if value is above 255 print "\tchecking octet $oc\n"; return 1 if ($oc eq $flagvalue); # good if equal to flag value print "\t\tUnequal: oc is $oc, flagvalue is $flagvalue\n"; return 0 if $flagvalue; # bad if this is a second non-zero and it's not equal to 255. $flagvalue = 255; # All further octets after this one must be 255 # This is the first non-zero octet. # Should be equal to a power of 2 minus 1 # X is a power of 2 if (X & X-1) = 0 # $oc + 0 forces value to number vice string my $bitand = ($oc + 0) & ($oc + 1); return ( $bitand ? 0 : 1); } }