]> git.ipfire.org Git - people/stevee/perl-ipset.git/commitdiff
Add pure perl get_set_data function().
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 11 Sep 2022 13:52:48 +0000 (15:52 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 11 Sep 2022 13:52:48 +0000 (15:52 +0200)
This function will create an object with the set data of a given set
and allows easy access to the single parts by using different methods.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
lib/IPSet.pm

index 649e267a5ee48ac55f21072739949e455a99087f..cc13ba0fa2759f8cf37dc4223357bd74436469b3 100644 (file)
@@ -27,6 +27,146 @@ require XSLoader;
 XSLoader::load('IPSet', $VERSION);
 
 # Preloaded methods go here.
+sub get_set_data($$) {
+       my ($session, $setname) = @_;
+
+       # Hash to store and access the set data.
+       my %data = ();
+
+       # Array to store and access the set members.
+       my @members = ();
+
+       # Call XS function to get the set data.
+       my $data_raw = &get_set_data_raw($session, $setname);
+
+       # Split the data from the members part.
+       my ($data, $members) = split("Members:", $data_raw);
+
+       # Cut the data part into single pieces for easy parsing.
+       my @tmp = split("\n", $data);
+       my $header;
+
+       # Extract and assign values to the data hash.
+       foreach (@tmp) {
+               $data{'name'} = $1 if($_ =~ /Name: (.*)/);
+               $data{'type'} = $1 if($_ =~ /Type: (.*)/);
+               $data{'revision'} = $1 if ($_ =~ /Revision: (.*)/);
+               $data{'memsize'} = $1 if ($_ =~ /memory: (.*)/);
+               $data{'references'} = $1 if ($_ =~ /References: (.*)/);
+               $data{'entries'} = $1 if ($_ =~ /entries: (.*)/);
+
+               # Grab and store the header.
+               $header = $1 if ($_ =~ /Header: (.*)/);
+       }
+
+       # Cut the heade string into single pieces.
+       @tmp = split(/ /, $header);
+
+       # Add the header values to the data hash.
+       $data{'family'} = $tmp[1];
+       $data{'hashsize'} = $tmp[3];
+       $data{'maxelem'} = $tmp[5];
+
+       # Check if the members part contains data.
+       if($members) {
+               # Cut the list of members into sigle ones and
+               # assign them to the members array.
+               @tmp = split("\n", $members);
+
+               foreach (@tmp) {
+                       # Remove any remain newlines.
+                       chomp($_);
+
+                       # Skip empty elements.
+                       next unless ($_);
+
+                       # Add member to the members array.
+                       push(@members, $_);
+               }
+
+               # Add the members array to the data hash.
+               $data{'members'} = \@members;
+       }
+
+       # Create object.
+       my $self = bless \%data, $setname;
+}
+
+#
+# Methods to get single data elements.
+# (In alphabetical order)
+#
+
+# Amount of entries a set has.
+sub entries {
+       my $self = shift;
+
+       return $self->{entries};
+}
+
+# The family of the set.
+sub family {
+       my $self = shift;
+
+       return $self->{family};
+}
+
+# The configured hashsize.
+sub hashsize {
+       my $self = shift;
+
+       return $self->{hashsize};
+}
+
+# A list of current members of the set.
+# Stored as array.
+sub members {
+       my $self = shift;
+
+       return $self->{members};
+}
+
+# The amount of maximum elements a set can have.
+sub maxelem {
+       my $self = shift;
+
+       return $self->{maxelem};
+}
+
+# The size of a set in memory.
+sub memsize {
+       my $self = shift;
+
+       return $self->{memsize};
+}
+
+# The name of a set.
+sub name {
+       my $self = shift;
+
+       return $self->{name};
+}
+
+# The amount how often the set is referenced.
+sub references {
+       my $self = shift;
+
+       return $self->{references};
+}
+
+# The revison number of a given set.
+sub revision {
+       my $self = shift;
+
+       return $self->{revision};
+}
+
+# The type of the set.
+sub type {
+       my $self = shift;
+
+       return $self->{type};
+}
 
 1;
 __END__