BGPmon-Filter-2
view release on metacpan or search on metacpan
bin/bgpmon_filter view on Meta::CPAN
############################PROGRAM START SUBROUTINES##########################
sub logFilterSize{
my $numPrefs = BGPmon::Filter::get_num_ASes();
log_info("Total ASes parsed: $numPrefs");
$numPrefs = BGPmon::Filter::get_num_ip_addrs();
log_info("Total addresses parsed: $numPrefs");
$numPrefs = BGPmon::Filter::get_num_IPv4_prefs();
log_info("total IPv4 prefixes after condensing: $numPrefs");
$numPrefs = BGPmon::Filter::get_num_IPv6_prefs();
log_info("total IPv6 prefixes after condensing: $numPrefs");
$numPrefs= BGPmon::Filter::get_total_num_filters();
log_info("Total number of active filters:$numPrefs");
}
sub parseAndCheck{
my @params = (
{
Name => BGPmon::Configure::CONFIG_FILE_PARAMETER_NAME,
lib/BGPmon/Filter.pm view on Meta::CPAN
use threads;
use threads::shared;
BEGIN{
require Exporter;
our $VERSION = '2.00';
our $AUTOLOAD;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(init parse_xml_msg parse_config_file
parse_database_config toString filterReset get_error_msg get_error_code
matches printFilters get_num_IPv4_prefs get_num_IPv6_prefs
get_num_ASes get_num_ip_addrs get_tot_num_filters);
}
my $progName = $0;
# Variables to hold error codes and messages
my %error_code;
my %error_msg;
lib/BGPmon/Filter.pm view on Meta::CPAN
$error_msg{$fname} = UNKNOWN_CONFIG_MSG;
return 1;
}
}
#closing the file
close($file);
condense_prefs(); #aggregates where possible
optimize_prefs(); #puts them in the multilayer hash for faster lookups
$error_code{$fname} = NO_ERROR_CODE;
$error_msg{$fname} = NO_ERROR_MSG;
return 0;
}
sub parse_database_config{
my $listName = shift;
my $fname = 'parse_database_config';
lock($lock);
#getting a list of prefixes
my @prefs = BGPmon::CPM::PList::Manager::export2CSV('',$listName);
my $size = scalar(@prefs);
if($size == 0){
#TODO create an error message that has the list size at zero
}
#resetting the module
filterReset();
#creating new prefixes
foreach(@prefs){
my $newPref = $_->prefix();
my $moreSpec = $_->watch_more_specifics();
if(is_IPv6($newPref)){
my $temp = new BGPmon::Filter::Prefix(6, $newPref, $moreSpec);
push(@v6prefixes, $temp);
}
else{
my $temp = new BGPmon::Filter::Prefix(4, $newPref, $moreSpec);
push(@v4prefixes, $temp);
}
}
#optimizing
condense_prefs(); #aggregates where possible
optimize_prefs(); #puts them in the multilayer hash for faster lookups
$error_code{$fname} = NO_ERROR_CODE;
$error_msg{$fname} = NO_ERROR_MSG;
return 0;
}
#puts the prefixes for IPv4 into a multi-layer hash lookup
sub optimize_prefs{
foreach(@v4prefixes){
my $temp = $_;
addV4prefixToHash($temp->prefix(), $temp);
}
#TODO put in error codes
return 0;
}
=head2 get_num_IPv4_prefs
Will count the number of IPv4 prefixes it has parsed from the configuration
file and return the integer
Input: None
Output : Integer
=cut
sub get_num_IPv4_prefs{
my $toReturn = scalar(@v4prefixes);
return $toReturn;
}
=head2 get_num_IPv6_prefs
Will count the number of IPv6 prefixes it has parsed from the configuration
file and return the integer
Input: None
Output : Integer
=cut
sub get_num_IPv6_prefs{
my $toReturn = scalar(@v6prefixes);
return $toReturn;
}
=head2 get_num_ASes
Will count the number of ASes it has parsed from the configuration
file and return the integer
lib/BGPmon/Filter.pm view on Meta::CPAN
lock($lock);
my $toReturn = 0;
$toReturn += scalar(@v4prefixes);
$toReturn += scalar(@v6prefixes);
$toReturn += scalar(%asNumbers);
$toReturn += scalar(@addresses);
return $toReturn;
}
#condense__prefs
#
#Will try to aggregate IPv4 and IPv6 prefixes where possible. This is used to reduce
#overhead that the filter script may experience later. Please note that the
#parse_config_file must be ran beforehand.
#
#Input: None
#
#Output: 0 if there is no error
# 1 if an error occured
#
#
sub condense_prefs{
##Starting with IPv4
#Will continuously agg. addresses where it can until none are left
my $found = TRUE;
while($found){
$found = FALSE;
for( my $i = 0; $i < scalar(@v4prefixes); $i++){
my $currPref = $v4prefixes[$i];
for(my $k = $i+1; $k < scalar(@v4prefixes); $k++){
my $thisPref = $v4prefixes[$k];
t/00-bgpmon-filter.t view on Meta::CPAN
##############################################################################
##############################################################################
# XML Parsing Tests
##############################################################################
##############################################################################
#Testing XML Message Parsing
BGPmon::Filter::init();
BGPmon::Filter::parse_config_file($location."bgpmon-filter-config.txt");
BGPmon::Filter::condense_prefs();
BGPmon::Filter::optimize_prefs();
#--testing that the code checks for a message
BGPmon::Filter::parse_xml_msg();
$errCode = BGPmon::Filter::get_error_code('parse_xml_msg');
is($errCode, BGPmon::Filter::NO_MSG_GIVEN, "No XML Message Given");
#--testing for correct <WITHDRAWN> filtering w/ IPv4
my $xml4msg = '<BGP_MONITOR_MESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns="urn:ietf:params:xml:ns:bgp_monitor" xmlns:bgp="urn:ietf:params:xml:ns:xfb" xmlns:ne="urn:ietf:params:xml:ns:network_elements"><SOURCE><ADDRESS afi="1">129.250.1.2...
my $res = BGPmon::Filter::matches($xml4msg);
is($res, TRUE, "XML IPv4 WITHDRAWN tag");
t/00-bgpmon-filter.t view on Meta::CPAN
#--testing for as number in as path - last as number
$xml6msg = '<BGP_MONITOR_MESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns="urn:ietf:params:xml:ns:bgp_monitor" xmlns:bgp="urn:ietf:params:xml:ns:xfb" xmlns:ne="urn:ietf:params:xml:ns:network_elements"><SOURCE><ADDRESS afi="2">2a03:2f00:ffff...
my $res33 = BGPmon::Filter::matches($xml6msg);
is($res33, TRUE, "XML ASN IN AS PATH");
#--testing more specific prefix matching works correctly
BGPmon::Filter::filterReset();
BGPmon::Filter::parse_config_file($location.'bgpmon-filter-config-ms.txt');
BGPmon::Filter::condense_prefs();
BGPmon::Filter::optimize_prefs();
$xml4msg = '<BGP_MONITOR_MESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns="urn:ietf:params:xml:ns:bgp_monitor" xmlns:bgp="urn:ietf:params:xml:ns:xfb" xmlns:ne="urn:ietf:params:xml:ns:network_elements"><SOURCE><ADDRESS afi="1">213.248.80.244...
my $resa = BGPmon::Filter::matches($xml4msg);
is($resa, TRUE, "More Specific Prefix Matching");
#--testing less specific prefix matching works correctly
BGPmon::Filter::filterReset();
BGPmon::Filter::parse_config_file($location.'bgpmon-filter-config-ls.txt');
BGPmon::Filter::condense_prefs();
BGPmon::Filter::optimize_prefs();
$xml4msg = '<BGP_MONITOR_MESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns="urn:ietf:params:xml:ns:bgp_monitor" xmlns:bgp="urn:ietf:params:xml:ns:xfb" xmlns:ne="urn:ietf:params:xml:ns:network_elements"><SOURCE><ADDRESS afi="1">213.248.80.244...
my $resb = BGPmon::Filter::matches($xml4msg);
is($resb, TRUE, "Less Specific Prefix Matching");
#--tEsting that IPv4 address matching works correctly
BGPmon::Filter::filterReset();
BGPmon::Filter::parse_config_file($location.'bgpmon-filter-config-ip.txt');
BGPmon::Filter::condense_prefs();
BGPmon::Filter::optimize_prefs();
my $rescc = BGPmon::Filter::matches($xml4msg);
is($rescc, FALSE, "IPv4 Address No Matching");
$xml4msg = '<BGP_MONITOR_MESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns="urn:ietf:params:xml:ns:bgp_monitor" xmlns:bgp="urn:ietf:params:xml:ns:xfb" xmlns:ne="urn:ietf:params:xml:ns:network_elements"><SOURCE><ADDRESS afi="1">213.248.80.244...
$rescc = BGPmon::Filter::matches($xml4msg);
is($rescc, TRUE, "IPv4 Address Matching");
#--Testing that the filter will deliever true if 0.0.0.0/0 is given (accept all)
BGPmon::Filter::filterReset();
BGPmon::Filter::parse_config_file($location.'bgpmon-filter-config-all-ms.txt');
BGPmon::Filter::condense_prefs();
BGPmon::Filter::optimize_prefs();
my $num = BGPmon::Filter::get_total_num_filters();
is($num, 1, "Number of Filters");
$rescc = BGPmon::Filter::matches($xml4msg);
is($rescc, TRUE, "Filter All");
( run in 1.803 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )