Net::Silk::Bag - SiLK bag file interface
use Net::Silk::Bag;
use Net::Silk::IPSet;
use Net::Silk::IPWildcard;
my $bag1 = Net::Silk::Bag->new_ipaddr;
$bag1->add('1.1.1.1', '2.2.2.2', '3.3.3.3');
$bag1->incr('3.3.3.3');
my $bag2 = Net::Silk::Bag->new_ipaddr;
$bag2->add('3.3.3.3', '4.4.4.4', '5.5.5.5');
# intersection
my $bag3 = $bag1 & $bag2;
# IO handle
while (my $kv = <$bag3>) {
print "$kv->[0] -> $kv->[1]\n";
}
# hash interface
foreach my $key (keys %$bag3) {
print "$key -> $bag3->{$key}\n";
}
# iterator
my $iter = $bag3->iter;
while (my $kv = $iter->()) {
print "$kv->[0] -> $kv->[1]\n";
}
# interact with other object types
my $set = Net::Silk::IPSet->new('3.3.3.3');
my $wc = Net::Silk::IPWildcard->new('3.3.3.3');
my %hash = ('3.3.3.3' => 1);
my @array = qw( 3.3.3.3 );
my $bag4 = $bag1 & $set;
my $bag5 = $bag1 & $wc;
my $bag6 = $bag1 & \%hash;
my $bag7 = $bag1 & \@array;
Net::Silk::Bag
is a representation of a multiset. Each key represents a potential element in the set, and the key's value represents the number of times that key is in the set. As such, it is also a reasonable representation of a mapping from keys to integers. Despite its set-like properties, a bag object is not nearly as efficient as a <Net::Silk::IPSet> when representing large contiguous ranges of key data.
The following are methods are available for bags:
Returns a new bag object. Accepts the following keyword arguments:
Type of key to use. Valid types are listed below. Defaults to 'any-IPv6' if SILK_IPV6_ENABLED is true, otherwise 'any-IPv4'.
Length of key. If not specified, defaults to the default number of bytes for the given key type.
Type of counter to use. Valid types are listed below. Defaults to 'custom'.
Length of the counter type. Currently this defaults to 8, which is the only valid value so far.
Values with which to initialize the bag. This can be a reference to a hash, array, or Net::Silk::IPSet. In the latter two cases, the count values are set to 1.
Valid key and counter types are:
sIPv4 class
dIPv4 type
sPort icmpTypeCode
dPort sIPv6
protocol dIPv6
packets nhIPv6
bytes records
flags sum-packets
sTime sum-bytes
duration sum-duration
eTime any-port
sensor any-snmp
input any-time
output custom
nhIPv4 any-IPv4
initialFlags any-IPv6
sessionFlags application
attributes
Creates a bag using 'any-ipv6' if SILK_IPV6_ENABLED is true, otherwise using 'any-ipv4'.
Creates a bag using 'any-ipv4', regardless of SILK_IPV6_ENABLED.
Returns a bag using the 'custom' key type (integer bag).
Returns a copy of the bag object.
Returns a bag object loaded from the given filename.
Saves the bag object into th given filename.
Return a list of valid key/counter type values.
Return a hash of key type, counter type, and their respective lengths for this bag.
Modify the key and counter characteristics of the bag. Accepts the following keyword arguments:
key_type
key_len
counter_type
counter_len
Return the field type that would be given (by default) to a bag that is a result of co-mingling two bags of the given types. For example, 'sport' and 'dport' would merge to 'any-port'.
Given another bag, return the key merge type that results from co-mingling with that bag.
Add values to the bag. Items can be keys, arrays of keys, hashes, a Net::Silk::IPSet
, or other bags. For items having key/value pairs, the given value is added to the current value for that key. For keys and lists of keys, the value is incremented by 1.
Remove values from the bag. Items can be keys, arrays of keys, hashes, a Net::Silk::IPSet
, or other bags. For items having key/value pairs, the given value is subtracted from the current value for that key. For keys and lists of keys, the value is deccremented by 1.
Return the value for the given key.
Set the given key to the given value.
Delete the given key.
Update the bag with the key/values in the given items. Items can be other bags, hashes, arrays of keys, single keys, etc. In the latter two cases, the value for the keys is 1.
Delete all keys which do not fall into the given range. Either min or max can be undef, but not both.
Delete all keys having values which do not fall into the given range. Either min or max can be undef, but not both.
Increment the number of the given key in the bag by the given value, which defaults to 1.
Decrement the number of the given key in the bag by the given value, which defaults to 1. If the given value is greater than the current value, the key is deleted.
Divide the bag by the given items. Items can be other bags or hashes. Returns a new bag for which values in the original bag are divided by their corresponding values in the given bag (non-zero), rounded to the nearest integer.
Multiply the bag by the given items. Items can be other bags or hashes. Returns a new bag for which values in the original bag are multiplied by their corresponding values in the given bag.
Return a new bag where all values are divided by the given value, rounded to the nearest integer.
Return a new bag where all values are multiplied by the given value.
Empty the bag.
Return a count of how many keys are present in the bag.
Return whether or not the bag contains the given key.
Return the maximum value contained in the bag.
Return the minimum value contained in the bag.
Return a Net::Silk::IPSet containing the keys of the bag in cases where the keys are IP addresses.
Return a new integer bag for which all values from the original bag are inserted as keys with values representing how many times that value was present in the bag. Hence, if two keys in the bag have a value of 5, the newbag would have a key of 5 with a value of 2.
Return a new bag with keys present in the given items. Items can be other bags, hashes, arrays of keys, single keys, Net::Silk::IPSet, or Net::Silk::IPWildcard.
Return a new bag with keys not present in the given items. Items can be other bags, hashes, arrays of keys, single keys, Net::Silk::IPSet, or Net::Silk::IPWildcard.
Return a sub ref iterator that returns key/value pairs as an array ref. In list context, returns all key/value pairs in a flattened list suitable for sending to a hash. Takes an optional parameter which, if present and true, causes results to be returned in key-sorted order. This is the type of iterator used when the bag is placed in the IO operator.
Return a sub ref iterator that returns all keys of the bag that have non-zero values.
Return a sub ref iterator that returns all non-zero values in the bag.
Return a sub ref iterator that returns keys and values from this bag and the given bag. For each key which is in either bag, the iterator returns a triple (key, value1, value2) where the first value is from this bag and the second from the given bag. The keys are returned in sorted order.
Bag objects can be treated as though they are hash references. All corresponding hash functions work as expected.
The following operators are overloaded and work with bag objects:
- &
-= &=
+ ==
+= !=
/ eq
/= ne
* ""
*= <>
Net::Silk, Net::Silk::RWRec, Net::Silk::IPSet, Net::Silk::Pmap, Net::Silk::IPWildcard, Net::Silk::Range, Net::Silk::CIDR, Net::Silk::IPAddr, Net::Silk::TCPFlags, Net::Silk::ProtoPort, Net::Silk::File, Net::Silk::Site, silk(7)
Copyright (C) 2011-2016 by Carnegie Mellon University
Use of the Net-Silk library and related source code is subject to the terms of the following licenses:
GNU General Public License (GPL) Rights pursuant to Version 2, June 1991 Government Purpose License Rights (GPLR) pursuant to DFARS 252.227.7013
NO WARRANTY
See GPL.txt and LICENSE.txt for more details.