Annotation of embedaddon/strongswan/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Packet.pm, revision 1.1.1.1

1.1       misho       1: package Vici::Packet;
                      2: 
                      3: our $VERSION = '0.9';
                      4: 
                      5: use strict;
                      6: use Vici::Message;
                      7: use Vici::Transport;
                      8: 
                      9: use constant {
                     10:     CMD_REQUEST      => 0,  # Named request message
                     11:     CMD_RESPONSE     => 1,  # Unnamed response message for a request
                     12:     CMD_UNKNOWN      => 2,  # Unnamed response if requested command is unknown
                     13:     EVENT_REGISTER   => 3,  # Named event registration request
                     14:     EVENT_UNREGISTER => 4,  # Named event de-registration request
                     15:     EVENT_CONFIRM    => 5,  # Unnamed confirmation for event (de-)registration
                     16:     EVENT_UNKNOWN    => 6,  # Unnamed response if event (de-)registration failed
                     17:     EVENT            => 7,  # Named event message
                     18: };
                     19: 
                     20: sub new {
                     21:     my $class = shift;
                     22:     my $socket = shift;
                     23:     my $self = {
                     24:         Transport => Vici::Transport->new($socket),
                     25:     };
                     26:     bless($self, $class);
                     27:     return $self;
                     28: }
                     29: 
                     30: sub request {
                     31:     my ($self, $command, $vars) = @_;
                     32:     my $out = defined $vars ? $vars->encode() : '';
                     33:     my $request = pack('CC/a*a*', CMD_REQUEST, $command, $out);
                     34:     $self->{'Transport'}->send($request);
                     35: 
                     36:     my $response = $self->{'Transport'}->receive();
                     37:     my ($type, $data) = unpack('Ca*', $response);
                     38: 
                     39:     if ( $type == CMD_RESPONSE )
                     40:     {
                     41:         return Vici::Message->from_data($data);
                     42:     }
                     43:     elsif ( $type == CMD_UNKNOWN )
                     44:     {
                     45:         die "unknown command '", $command, "'\n"
                     46:     }
                     47:     else
                     48:     {
                     49:         die "invalid response type\n"
                     50:     }
                     51: }
                     52: 
                     53: sub register {
                     54:     my ($self, $event) = @_;
                     55:     my $request = pack('CC/a*a*', EVENT_REGISTER, $event);
                     56:     $self->{'Transport'}->send($request);
                     57: 
                     58:     my $response = $self->{'Transport'}->receive();
                     59:     my ($type, $data) = unpack('Ca*', $response);
                     60: 
                     61:     if ( $type == EVENT_CONFIRM )
                     62:     {
                     63:         return
                     64:     }
                     65:     elsif ( $type == EVENT_UNKNOWN )
                     66:     {
                     67:         die "unknown event '", $event, "'\n"
                     68:     }
                     69:     else
                     70:     {
                     71:         die "invalid response type\n"
                     72:     }
                     73: }
                     74: 
                     75: sub unregister {
                     76:     my ($self, $event) = @_;
                     77:     my $request = pack('CC/a*a*', EVENT_UNREGISTER, $event);
                     78:     $self->{'Transport'}->send($request);
                     79: 
                     80:     my $response = $self->{'Transport'}->receive();
                     81:     my ($type, $data) = unpack('Ca*', $response);
                     82: 
                     83:     if ( $type == EVENT_CONFIRM )
                     84:     {
                     85:         return
                     86:     }
                     87:     elsif ( $type == EVENT_UNKNOWN )
                     88:     {
                     89:         die "unknown event '", $event, "'\n"
                     90:     }
                     91:     else
                     92:     {
                     93:         die "invalid response type\n"
                     94:     }
                     95: }
                     96: 
                     97: sub streamed_request {
                     98:     my ($self, $command, $event, $vars) = @_;
                     99:     my $out = defined $vars ? $vars->encode() : '';
                    100: 
                    101:     $self->register($event);
                    102: 
                    103:     my $request = pack('CC/a*a*', CMD_REQUEST, $command, $out);
                    104:     $self->{'Transport'}->send($request);
                    105:     my $more = 1;
                    106:     my @list = ();
                    107: 
                    108:     while ($more)
                    109:     {
                    110:         my $response = $self->{'Transport'}->receive();
                    111:         my ($type, $data) = unpack('Ca*', $response);
                    112: 
                    113:         if ( $type == EVENT )
                    114:         {
                    115:             (my $event_name, $data) = unpack('C/a*a*', $data);
                    116: 
                    117:             if ($event_name eq $event)
                    118:             {
                    119:                 my $msg = Vici::Message->from_data($data);
                    120:                 push(@list, $msg);
                    121:             }
                    122:         }
                    123:         elsif ( $type == CMD_RESPONSE )
                    124:         {
                    125:             $self->unregister($event);
                    126:             $more = 0;
                    127:         }
                    128:         else
                    129:         {
                    130:             $self->unregister($event);
                    131:             die "invalid response type\n";
                    132:         }
                    133:     }
                    134:     return \@list;
                    135: }
                    136: 
                    137: 1;
                    138: __END__
                    139: =head1 NAME
                    140: 
                    141: Vici::Packet - Perl extension for sending and receiving strongSwan VICI packets
                    142: 
                    143: =head1 SYNOPSIS
                    144: 
                    145: use Vici::Packet;
                    146: 
                    147: =head1 DESCRIPTION
                    148: 
                    149: The Vici::Packet module is needed by the Vici::Session module to send and
                    150: receive packets used in the communication with the open source strongSwan IPsec
                    151: daemon (https://www.strongswan.com) via the documented Versatile IKE
                    152: Configuration Interface (VICI). VICI allows the configuration, management and
                    153: monitoring of multiple IPsec connections.
                    154: 
                    155: =head2 EXPORT
                    156: 
                    157: None by default.
                    158: 
                    159: =head1 SEE ALSO
                    160: 
                    161: strongSwan Wiki:  https://wiki.strongswan.org/projects/strongswan/wiki/Vici
                    162: 
                    163: strongSwan Mailing list:  users@lists.strongswan.org
                    164: 
                    165: =head1 AUTHOR
                    166: 
                    167: Andreas Steffen, E<lt>andreas.steffen@strongswan.orgE<gt>
                    168: 
                    169: =head1 COPYRIGHT AND LICENSE
                    170: 
                    171: Copyright (C) 2015 by Andreas Steffen
                    172: 
                    173: Permission is hereby granted, free of charge, to any person obtaining a copy
                    174: of this software and associated documentation files (the "Software"), to deal
                    175: in the Software without restriction, including without limitation the rights
                    176: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                    177: copies of the Software, and to permit persons to whom the Software is
                    178: furnished to do so, subject to the following conditions:
                    179: 
                    180: The above copyright notice and this permission notice shall be included in
                    181: all copies or substantial portions of the Software.
                    182: 
                    183: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                    184: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                    185: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
                    186: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                    187: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                    188: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
                    189: THE SOFTWARE.
                    190: 
                    191: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>