summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/vici/perl/Vici-Session/lib/Vici/Message.pm
blob: d0700fa97287ca41d6e0818deff55afce5007ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package Vici::Message;

our $VERSION = '0.9';

use strict;
use Vici::Transport;

use constant {
    SECTION_START => 1,   # Begin a new section having a name
    SECTION_END   => 2,   # End a previously started section
    KEY_VALUE     => 3,   # Define a value for a named key in the section
    LIST_START    => 4,   # Begin a named list for list items
    LIST_ITEM     => 5,   # Define an unnamed item value in the current list
    LIST_END      => 6,   # End a previously started list
};

sub new {
    my $class = shift;
    my $hash = shift;
    my $self = {
        Hash => $hash
    };
    bless($self, $class);
    return $self;
}

sub from_data {
    my $class = shift;
    my $data = shift;
    my %hash = ();

    open my $data_fd, '<', \$data;
    parse($data_fd, \%hash);
    close $data_fd;

    my $self = {
        Hash => \%hash
    };
    bless($self, $class);
    return $self;
}

sub hash {
    my $self = shift;
    return $self->{Hash};
}

sub encode {
    my $self = shift;
    return encode_hash($self->{'Hash'});
}

sub raw {
    my $self = shift;
    return '{' . raw_hash($self->{'Hash'}) . '}';
}

sub result {
    my $self = shift;
    my $result = $self->{'Hash'};
    return ($result->{'success'} eq 'yes', $result->{'errmsg'});
}

# private functions

sub parse {
    my $fd = shift;
    my $hash = shift;
    my $data;

    until ( eof $fd )
    {
        my $type = unpack('C', read_data($fd, 1));

        if ( $type == SECTION_END )
        {
            return;
        }

        my $key = read_len_data($fd, 1);

        if ( $type == KEY_VALUE )
        {
            my $value = read_len_data($fd, 2);
            $hash->{$key} = $value;
        }
        elsif ( $type == SECTION_START )
        {
            my %section = ();
            parse($fd, \%section);
            $hash->{$key} = \%section;
        }
        elsif ( $type == LIST_START )
        {
            my @list = ();
            my $more = 1;

            while ( !eof($fd) and $more )
            {
                my $type = unpack('C', read_data($fd, 1));

                if ( $type == LIST_ITEM )
                {
                    my $value = read_len_data($fd, 2);
                    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"
        }
    }
}

sub read_data {
    my $fd = shift;
    my $len = shift;
    my $data;

    my $res = read $fd, $data, $len;
    unless (defined $res and $res == $len)
    {
        die "message parsing error: unable to read ", $len, " bytes\n";
    }
    return $data;
}

sub read_len_data {
    my $fd = shift;
    my $len = shift;

    $len = unpack($len == 1 ? 'C' : 'n', read_data($fd, $len));
    return read_data($fd, $len);
}

sub encode_hash {
    my $hash = shift;
    my $enc = '';

    while ( (my $key, my $value) = each %$hash )
    {
        if ( ref($value) eq 'HASH' )
        {
            $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
        {
            $enc .= pack('CC/a*n/a*', KEY_VALUE, $key, $value);
        }
    }
    return $enc;
}

sub raw_hash {
    my $hash = shift;
    my $raw = '';
    my $first = 1;

    while ( (my $key, my $value) = each %$hash )
    {
        if ($first)
        {
            $first = 0;
        }
        else
        {
            $raw .= ' ';
        }
        $raw .= $key;

        if ( ref($value) eq 'HASH' )
        {
            $raw .= '{' . raw_hash($value) . '}';
        }
        elsif ( ref($value) eq 'ARRAY' )
        {
            my $first_item = 1;
            $raw .= '[';

            foreach my $item (@$value)
            {
                if ($first_item)
                {
                    $first_item = 0;
                }
                else
                {
                    $raw .= ' ';
                }
                $raw .= $item;
            }
            $raw .= ']';
        }
        else
        {
            $raw .= '=' . $value;
        }
    }
    return $raw;
}

1;
__END__
=head1 NAME

Vici::Message - Perl extension for building and parsing strongSwan VICI messages

=head1 SYNOPSIS

  use Vici::Message;

=head1 DESCRIPTION

The Vici::Message module is needed by the Vici::Session module to build and
parse messages used in the communication with the open source strongSwan IPsec
daemon (https://www.strongswan.com) via the documented Versatile IKE
Configuration Interface (VICI). VICI allows the configuration, management and
monitoring of multiple IPsec connections.

=head2 EXPORT

None by default.

=head1 SEE ALSO

strongSwan Wiki:  https://wiki.strongswan.org/projects/strongswan/wiki/Vici

strongSwan Mailing list:  users@lists.strongswan.org

=head1 AUTHOR

Andreas Steffen, E<lt>andreas.steffen@strongswan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2015 by Andreas Steffen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

=cut