View on
Gregory Jones > Net-CIDR-Compare-0.03 > Net::CIDR::Compare



Annotate this POD


New  1
Open  1
View/Report Bugs
Module Version: 0.03   Source  


Net::CIDR::Compare - Find intersections across multiple lists of CIDR ranges, fast.


  use Net::CIDR::Compare;

  my $collection = Net::CIDR::Compare->new(print_errors => 1);

  my $first_list = $collection->new_list();
  $collection->add_range($first_list, "", 1);

  my $second_list = $collection->new_list();
  $collection->add_range($second_list, "", 1);

  $collection->process_intersection(expand_cidr => 8);
  while (my $cidr_range = $collection->get_next_intersection_range()) {
    print "$cidr_range\n"; # prints


  $collection->process_intersection(expand_cidr => 8);
  while (my $cidr_range = $collection->get_next_intersection_range()) {
    print "$cidr_range\n"; # prints


  This module accepts various formats of IPv4 ranges, converts non-CIDR
  ranges to CIDR, and produces the intersection of CIDR ranges across
  all the lists in the collection.

  Net::CIDR::Compare was designed to handle large IPv4 lists and compute
  the intersection of these lists in a memory-efficient and speedy way.
  The intersection code is C code and Perl-wrapped using XS.  You will
  need a C compiler to install this code.

  Although the main driver for this module's creation is to find the
  intersection across several lists, this module can also be used with just
  a single list to convert non-CIDR range formats to CIDR and merge ranges

  Net::CIDR::Compare also requires Net::CIDR and Net::Netmask for some
  of the range format conversions (e.g. converting 10.0.0.* to


  Net::CIDR::Compare objects are created with one optional parameter,
  print_errors.  Errors are always stored in $collection->{error} (from
  above example).  If print_errors is set (default yes) then errors
  are also printed to STDERR.

  $collection = Net::CIDR::Compare->new(print_errors => 1)


  Creates a new list (C binary tree).  Returns a list pointer.
->add_range($list, $iprange, $skip_check)
  Adds an IP range to the list specified. If the $skip_check parameter is
  set (default no), the range is assumed to be CIDR and no validation checks
  are run.  This greatly improves performance, but if an invalid range is
  passed the result is unknown.

  Non-CIDR formats are silently converted to CIDR unless $skip_check is set.
  Accepted IP range formats:
  Wildcard: 1.1.1.*
  Start-End Pair:
  Bracket Octet Range: 1.1.1.[0-255]
->process_intersection(expand_cidr => 8)
  Finds the intersection across all lists in the collection.  Returns nothing.

  An optional parameter, expand_cidr, sets the minimum CIDR value.  For example,
  setting this parameter to 32 would produce single IP results.  The default is
  0, which produces the smallest CIDR value (largest network size) results.
  Returns the next CIDR range in the intersection.
  Removes a list from the collection.


  Sections of this code were obtained from a C program called cidr-convert
  which can be found on numerous sites.  The sourcecode claimed to be of 
  "the public domain."  This code is also part of the public domain.

  Please send feedback to
syntax highlighting: