package CIDR_Lite; use strict; use vars qw($VERSION); $VERSION= '0.03'; my $cache; sub _setCache { my $nBytes= shift; my $nBits= 8*$nBytes; my $h= $cache->[$nBytes]; if( ! $h ) { $h= {}; $h->{zero}= my $zero= "\0" x $nBytes; @{ $h->{mask} }= ( map { pack( "B*", "0" x $_ . "1" x ($nBits-$_) ) } 0..$nBits, ); @{ $h->{width} }= ( $zero, map { pack( "B*", "0" x ($_-1) . "1" . "0" x ($nBits-$_-2) ) } 1..$nBits, ); $cache->[$nBytes]= $h; } return $h; } sub new { my $proto = shift; my $class = ref($proto) || $proto; my $nBytes= shift || 4; my $self= {}; @{$self}{qw( mask width zero )}= @{_setCache($nBytes)}{qw( mask width zero )}; $self->{nBytes}= $nBytes; $self->{nBits}= 8*$nBytes; bless $self, $class; } sub _plus { my $self= shift; my $base= shift; my $bits= shift; my $res= $base | $self->{width}[$bits]; while( $res le $base ) { $res &= ~$self->{width}[$bits--]; return "\xff"x(1+length($base)) if $bits < 0; $res |= $self->{width}[$bits]; } return $res; } sub add { my $self= shift; my $input = shift; my( $ip, $bits )= split "/", $input; my $start= pack( "C*", split /\./, $ip ) & ~$self->{mask}[$bits]; my $end= $self->_plus( $start, $bits ); ++($self->{ranges}{$start}) || delete $self->{ranges}{$start}; --($self->{ranges}{$end}) || delete $self->{ranges}{$end}; } sub clean { my $self = shift; my $ranges= $self->{ranges}; my $total= 0; for my $key ( sort keys %$ranges ) { if( 0 == $total ) { die "Impossible" if $ranges->{$key} <= 0; $total += $ranges->{$key}; $ranges->{$key}= 1; } elsif( 0 == ( $total += $ranges->{$key} ) ) { die "Impossible" if 0 <= $ranges->{$key}; $ranges->{$key}= -1; } else { delete $ranges->{$key}; } } } sub list { my $self= shift; my( $start, $total ); my @results; my $zero= $self->{zero}; for my $ip ( sort keys %{ $self->{ranges} } ) { $start= $ip unless $total; $total += $self->{ranges}{$ip}; if( 0 == $total ) { my $end= $ip; my $bits= $self->{nBits}; while( 1 ) { $bits-- while $zero eq ( $start & $self->{mask}[$bits] ); my $next= $self->_plus( $start, ++$bits ); last if $end lt $next; push @results, join(".",unpack"C*",$start)."/".$bits; $start= $next; } my @temp; $bits= $self->{nBits}; while( 1 ) { $bits-- while $zero eq ( $end & $self->{mask}[$bits] ); my $next= ~ $self->_plus( ~$end, ++$bits ); last if $next lt $start; push @temp, join(".",unpack"C*",$next)."/".$bits; $end= $next; } push @results, reverse @temp; } } wantarray ? @results : \@results; } 1;