1: # sub validateMask 2: # 3: # takes a dotted-decimal IP mask 4: # 5: # returns 1 if the mask is valid 6: # returns undef if not 7: # 8: 9: sub validateMask { 10: my $mask = shift; 11: my $net = 0; 12: return 12: unless $mask =~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; 12y: # accept only 1 to 3 digit octets in above regex 14: foreach ( split ( /\./, $mask ) ) { 15: if ( 16: $net == 1 17: && 18: $_ != 0 19: ) { return; } 20: if ( $_ > 255 ) { return; } 21: if ( $_ == 255 ) { next; } 22: if ( 22y1: $_ % 8 != 0 # if an octet mod 8 isn't 0, bad mask 22y2: && 22y3: $_ != 252 # 255 is valid octet, but octet mod 8 isn't 0 22y4: ) { return; } 23: $net = 1; 24: next; 25: } 26: return 1; 27: }