]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
Improvements to the VICI Perl bindings by Andreas Hofmeister
authorAndreas Hofmeister <andi@collax.com>
Fri, 18 Dec 2015 13:17:57 +0000 (14:17 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 18 Dec 2015 13:25:59 +0000 (14:25 +0100)
- Switch.pm, which was implemented as a source filter, has been deprecated in
  Perl 5.10 and was later removed from the core modules in Perl 5.14 or so.

  Unfortunately, its replacement, the given/when/default construct, has since
  been downgraded to "experimental" status because of problems with the underlying
  "smart-match" operator.

  Thus, as of Perl 5.22, Perl still has no actually usable "switch"-like construct.

  So just use boring, old and ugly "if/elsif/else" constructs instead, which are
  compatible with almost any Perl version.

- None of the Perl modules here does anything that would require "AutoLoader".

- "Exporter" can be used to export plain functions into another modules name
  space. But the things that were exported here are meant to be called as
  methods.  In this case, it is neither necessary nor advisable to export those
  symbols.

  Just export nothing (the POD documentation already said so).

- It is usually the calling script that enables (or does not enable) warnings
  globally. When a module says "use warnings;" however, the caller looses control
  over what warnings should be enabled in that module.

src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm
src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm
src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Session.pm
src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Transport.pm

index 228431c903282e0233208a22ef4b4834c59f2d7a..b0a942c04f8a23282cb23f3e1ca7d71879ae97b8 100644 (file)
@@ -1,17 +1,8 @@
 package Vici::Message;
 
-require Exporter;
-use AutoLoader qw(AUTOLOAD);
-
-our @ISA = qw(Exporter);
-our @EXPORT = qw(
-    new, from_data, hash, encode, raw, result
-);
 our $VERSION = '0.9';
 
 use strict;
-use warnings;
-use Switch;
 use Vici::Transport;
 
 use constant {
@@ -85,52 +76,46 @@ sub parse {
 
         (my $key, $data) = unpack('C/a*a*', $data);
 
-        switch ($type)
-        {       
-            case KEY_VALUE
-            {
-                (my $value, $data) = unpack('n/a*a*', $data);
-                $hash->{$key} = $value;
-            }
-            case SECTION_START
-            {
-                my %section = ();
-                $data = parse($data, \%section);
-                $hash->{$key} = \%section;
-            }
-            case LIST_START
-            {
-                my @list = ();
-                my $more = 1;
+        if ( $type == KEY_VALUE )
+        {
+            (my $value, $data) = unpack('n/a*a*', $data);
+            $hash->{$key} = $value;
+        }
+        elsif ( $type == SECTION_START )
+        {
+            my %section = ();
+            $data = parse($data, \%section);
+            $hash->{$key} = \%section;
+        }
+        elsif ( $type == LIST_START )
+        {
+            my @list = ();
+            my $more = 1;
 
-                while (length($data) > 0 and $more)
+            while (length($data) > 0 and $more)
+            {
+                (my $type, $data) = unpack('Ca*', $data);
+                if ( $type == LIST_ITEM )
                 {
-                    (my $type, $data) = unpack('Ca*', $data);
-                    switch ($type)
-                    {
-                        case LIST_ITEM
-                        {
-                            (my $value, $data) = unpack('n/a*a*', $data);
-                            push(@list, $value);
-                        }
-                        case LIST_END
-                        {
-                            $more = 0;
-                            $hash->{$key} = \@list;
-                         }
-                        else
-                        {
-                            die "message parsing error: ", $type, "\n"
-                        }
-                    }
+                    (my $value, $data) = unpack('n/a*a*', $data);
+                    push(@list, $value);
+                }
+                elsif ( $type == LIST_END )
+                {
+                    $more = 0;
+                    $hash->{$key} = \@list;
+                 }
+                else
+                {
+                    die "message parsing error: ", $type, "\n"
                 }
             }
-            else
-            {
-                die "message parsing error: ", $type, "\n"
-            }
-        } 
-       }
+        }
+        else
+        {
+            die "message parsing error: ", $type, "\n"
+        }
+    }
     return $data;
 }
 
@@ -141,31 +126,28 @@ sub encode_hash {
 
     while ( (my $key, my $value) = each %$hash )
     {
-        switch (ref($value))
+        if ( ref($value) eq 'HASH' )
         {
-            case 'HASH'
-            {
-                $enc .= pack('CC/a*', SECTION_START, $key);
-                $enc .= encode_hash($value);
-                $enc .= pack('C', SECTION_END);
-            }
-            case 'ARRAY'
-            {
-                $enc .= pack('CC/a*', LIST_START, $key);
+            $enc .= pack('CC/a*', SECTION_START, $key);
+            $enc .= encode_hash($value);
+            $enc .= pack('C', SECTION_END);
+        }
+        elsif ( ref($value) eq 'ARRAY' )
+        {
+            $enc .= pack('CC/a*', LIST_START, $key);
 
-                foreach my $item (@$value)
-                {
-                    $enc .= pack('Cn/a*', LIST_ITEM, $item);
-                }
-                $enc .= pack('C', LIST_END);
-            }
-            else
+            foreach my $item (@$value)
             {
-                $enc .= pack('CC/a*n/a*', KEY_VALUE, $key, $value);
+                $enc .= pack('Cn/a*', LIST_ITEM, $item);
             }
+            $enc .= pack('C', LIST_END);
+        }
+        else
+        {
+            $enc .= pack('CC/a*n/a*', KEY_VALUE, $key, $value);
         }
     }
-    return $enc;        
+    return $enc;
 }
 
 sub raw_hash {
@@ -185,38 +167,35 @@ sub raw_hash {
         }
         $raw .= $key;
 
-        switch (ref($value))
+        if ( ref($value) eq 'HASH' )
         {
-            case 'HASH'
-            {
-                $raw .= '{' . raw_hash($value) . '}';
-            }
-            case 'ARRAY'
-            {
-                my $first_item = 1;
-                $raw .= '[';
+            $raw .= '{' . raw_hash($value) . '}';
+        }
+        elsif ( ref($value) eq 'ARRAY' )
+        {
+            my $first_item = 1;
+            $raw .= '[';
 
-                foreach my $item (@$value)
+            foreach my $item (@$value)
+            {
+                if ($first_item)
                 {
-                    if ($first_item)
-                    {
-                        $first_item = 0;
-                    }
-                    else
-                    {
-                        $raw .= ' ';
-                    }
-                    $raw .= $item;
+                    $first_item = 0;
                 }
-                $raw .= ']';
-            }
-            else
-            {
-                $raw .= '=' . $value;
+                else
+                {
+                    $raw .= ' ';
+                }
+                $raw .= $item;
             }
+            $raw .= ']';
+        }
+        else
+        {
+            $raw .= '=' . $value;
         }
     }
-    return $raw;        
+    return $raw;
 }
 
 1;
index 62dac0a3772708f8d9d14fcb0099e3652f7e1afb..9e2b77fa53825592c4237ceeeef9ef4162dbb5de 100644 (file)
@@ -1,17 +1,8 @@
 package Vici::Packet;
 
-require Exporter;
-use AutoLoader qw(AUTOLOAD);
-
-our @ISA = qw(Exporter);
-our @EXPORT = qw(
-    new, request, register, unregister, streamed_request
-);
 our $VERSION = '0.9';
 
 use strict;
-use warnings;
-use Switch;
 use Vici::Message;
 use Vici::Transport;
 
@@ -45,21 +36,18 @@ sub request {
     my $response = $self->{'Transport'}->receive();
     my ($type, $data) = unpack('Ca*', $response);
 
-       switch ($type)
+    if ( $type == CMD_RESPONSE )
     {
-        case CMD_RESPONSE
-        {
-            return Vici::Message->from_data($data);
-        }
-        case CMD_UNKNOWN
-        {
-            die "unknown command '", $command, "'\n"
-        }
-        else
-        {
-            die "invalid response type\n"
-        }
-    }; 
+        return Vici::Message->from_data($data);
+    }
+    elsif ( $type == CMD_UNKNOWN )
+    {
+        die "unknown command '", $command, "'\n"
+    }
+    else
+    {
+        die "invalid response type\n"
+    }
 }
 
 sub register {
@@ -70,21 +58,18 @@ sub register {
     my $response = $self->{'Transport'}->receive();
     my ($type, $data) = unpack('Ca*', $response);
 
-       switch ($type)
+    if ( $type == EVENT_CONFIRM )
     {
-        case EVENT_CONFIRM
-        {
-            return
-        }
-        case EVENT_UNKNOWN
-        {
-            die "unknown event '", $event, "'\n"
-        }
-        else
-        {
-            die "invalid response type\n"
-        }
-    }; 
+        return
+    }
+    elsif ( $type == EVENT_UNKNOWN )
+    {
+        die "unknown event '", $event, "'\n"
+    }
+    else
+    {
+        die "invalid response type\n"
+    }
 }
 
 sub unregister {
@@ -95,21 +80,18 @@ sub unregister {
     my $response = $self->{'Transport'}->receive();
     my ($type, $data) = unpack('Ca*', $response);
 
-       switch ($type)
+    if ( $type == EVENT_CONFIRM )
     {
-        case EVENT_CONFIRM
-        {
-            return
-        }
-        case EVENT_UNKNOWN
-        {
-            die "unknown event '", $event, "'\n"
-        }
-        else
-        {
-            die "invalid response type\n"
-        }
-    }; 
+        return
+    }
+    elsif ( $type == EVENT_UNKNOWN )
+    {
+        die "unknown event '", $event, "'\n"
+    }
+    else
+    {
+        die "invalid response type\n"
+    }
 }
 
 sub streamed_request {
@@ -123,33 +105,30 @@ sub streamed_request {
     my $more = 1;
     my @list = ();
 
-       while ($more)
-       {
+    while ($more)
+    {
         my $response = $self->{'Transport'}->receive();
         my ($type, $data) = unpack('Ca*', $response);
 
-        switch ($type)
+        if ( $type == EVENT )
+        {
+           (my $event_name, $data) = unpack('C/a*a*', $data);
+
+           if ($event_name eq $event)
+           {
+               my $msg = Vici::Message->from_data($data);
+               push(@list, $msg);
+           }
+        }
+        elsif ( $type == CMD_RESPONSE )
+        {
+            $self->unregister($event);
+            $more = 0;
+        }
+        else
         {
-            case EVENT
-            {
-               (my $event_name, $data) = unpack('C/a*a*', $data);
-
-               if ($event_name eq $event)
-               {
-                   my $msg = Vici::Message->from_data($data);
-                   push(@list, $msg);
-               }
-            }
-            case CMD_RESPONSE
-            {
-                $self->unregister($event);
-                $more = 0;
-            }
-            else
-            {
-                $self->unregister($event);
-                die "invalid response type\n";
-            }
+            $self->unregister($event);
+            die "invalid response type\n";
         }
     }
     return \@list;
index 02ed269f80dc4cd07358437baded1be6b97f1561..5252296cfe496049851ded2411b4d2fa3432b2d2 100644 (file)
@@ -1,20 +1,8 @@
 package Vici::Session;
 
-require Exporter;
-use AutoLoader qw(AUTOLOAD);
-
-our @ISA = qw(Exporter);
-our @EXPORT = qw(
-    new, version, stats, reload_settings, initiate, terminate, install,
-    uninstall, list_sas, list_policies, list_conns, get_conns, list_certs,
-    list_authorities, get_authorities, load_conn, unload_conn, load_cert,
-    load_key, load_shared, clear_creds, load_authority, unload_authority,
-    load_pool, unload_pool, get_pools, get_algorithms
-);
 our $VERSION = '0.9';
 
 use strict;
-use warnings;
 use Vici::Packet;
 use Vici::Message;
 
index 88a09fb0cc7a1d60163dd34329f12040120c32f0..6524bf76df1f04b7b22c317531b1738d972fc164 100644 (file)
@@ -1,16 +1,8 @@
 package Vici::Transport;
 
-require Exporter;
-use AutoLoader qw(AUTOLOAD);
-
-our @ISA = qw(Exporter);
-our @EXPORT = qw(
-    new, send, receive
-);
 our $VERSION = '0.9';
 
 use strict;
-use warnings;
 
 sub new {
     my $class = shift;