NSNMP/Agent.pm

filter: Statementstmt 89.3% 84 / 94 Branchbran 65.3% 17 / 26 Conditioncond 64.7% 11 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 9 totaltotal 75.5% 130 / 172 CC 24 SCAR 15.2
CC
24
Cov
82%
run
12.0
next_oid_after
6.3
handle_set_request
5.1
CRAP 4.6
« NSNMP/Simple.pm ↑ Summary NSNMP/Mapper.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package NSNMP::Agent;
3 # Copyright (c) 2003-2004 AirWave Wireless, Inc.
4  
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8  
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
11 # disclaimer.
12 # 2. Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # 3. The name of the author may not be used to endorse or
17 # promote products derived from this software without specific
18 # prior written permission.
19  
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 2 ▶use NSNMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use NSNMP::Mapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # on my 500MHz machine, it handled 2800 requests per second when it
36 # peremptorily returned an error message
37  
38 # adding the ability to actually return values slowed the error
39 # messages to 2300 per second; returning messages with values in them
40 # was roughly as fast
41  
42 # adding SNMP SET handling cut it down to 2200 messages per second or
43 # so.
44  
45 # Adding community string handling cut it down to 2100 messages per
46 # second or so.
47  
48 # Adding error handling and get-next handling cut it down to 2000
49 # get-requests or 1900 get-next requests per second. On a very small
50 # dataset, though; it was down around 1300 for a slightly larger one.
51  
52 22 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
53 22 my ($class, %args) = @_;
54 22 my $self = bless \%args, $class;
55 22 $self->{typemapper} = NSNMP::Mapper->new(%{$self->{types}});
56 $self->{_values} = {
57 22 map { NSNMP->encode_oid($_) => $self->{values}{$_} } keys %{$self->{values}}
58 };
59 22 $self->{_oids} = [sort keys %{$self->{_values}}]; # yay BER oids
60 22 $self->{lastoid_idx} = 0;
61 22 ▶ $self->{community} ||= 'public';
Condition100%2/2
$self->{"community"} ||= "public"
l!l
121
MC/DC100%1/1
$self->{"community"} ||= "public"
$self->{"community"}
Truth table100%2/2
A
A= $self->{"community"}
Aresult
11
00
62 22 return $self;
63 }
64  
65 5 ▶sub _noSuchName {
Subroutine _noSuchName: called CC=1 SCAR=0.0
66 5 my ($dr) = @_;
67 5 return NSNMP->encode(
68 request_id => $dr->request_id,
69 error_status => NSNMP::noSuchName,
70 error_index => 1, # XXX
71 type => NSNMP::GET_RESPONSE,
72 varbindlist => [$dr->varbindlist], # XXX?
73 );
74 }
75  
76 1 ▶sub _badValue {
Subroutine _badValue: called CC=1 SCAR=0.0
77 1 my ($dr) = @_;
78 1 return NSNMP->encode(
79 request_id => $dr->request_id,
80 error_status => NSNMP::badValue,
81 error_index => 1, # XXX
82 type => NSNMP::GET_RESPONSE,
83 varbindlist => [$dr->varbindlist], # XXX??
84 );
85 }
86  
87 5 ▶sub next_oid_after {
Subroutine next_oid_after: called CC=6 SCAR=18.4
Pod undocumented
88 5 my ($self, $oid) = @_;
89 5 my $oids = $self->{_oids};
90 5 my $lastindex = $self->{lastoid_idx};
91 5 my $lastoid = $oids->[$lastindex];
92 5 ▶ return $oids->[++$self->{lastoid_idx}] || NSNMP->encode_oid('.1.3')
Branch: 1 branch
BranchTrueFalseTotal
if $lastoid and $oid eq $lastoid 4 1 5
Condition66%4/6
$lastoid and $oid eq $lastoid
!ll&&!rl&&r
014
$$oids[++$self->{'lastoid_idx'}] || 'NSNMP'->encode_oid('.1.3')
l!l&&r!l&&!r
310
MC/DC50%1/2
$lastoid and $oid eq $lastoid
$lastoid $oid eq $lastoid
MC/DC0%0/2
$$oids[++$self->{'lastoid_idx'}] || 'NSNMP'->encode_oid('.1.3')
$$oids[++$self->{'lastoid_idx'}] 'NSNMP'->encode_oid('.1.3')
Truth table66%2/3
A and B
A= $lastoidB= $oid eq $lastoid
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= $$oids[++$self->{'lastoid_idx'}]B= 'NSNMP'->encode_oid('.1.3')
ABresult
1X1
011
000
93 if $lastoid and $oid eq $lastoid;
94 1 for my $ii (0..$#$oids) {
95 1 ▶ if ($oids->[$ii] gt $oid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oids->[$ii] gt $oid) 1 0 1
96 1 $self->{lastoid_idx} = $ii;
97 1 return $oids->[$ii];
98 }
99 }
100 0 return NSNMP->encode_oid('.1.3'); # hope nobody tries to attach a value here
101 }
102  
103 15 ▶sub handle_get_request {
Subroutine handle_get_request: called CC=5 SCAR=16.1
Pod undocumented
104 15 my ($self, $dr, $reqtype) = @_;
105 15 my @rvbl;
106 15 for my $varbind ($dr->varbindlist) {
107 15 my ($oid, $type, $value) = @{$varbind};
108 15 ▶ $oid = $self->next_oid_after($oid) if $reqtype eq NSNMP::GET_NEXT_REQUEST;
Branch: 1 branch
BranchTrueFalseTotal
if $reqtype eq "\241" 5 10 15
109 # XXX damn, I thought I could avoid decoding this OID:
110 15 my ($otype, $instance) = $self->{typemapper}->map(NSNMP->decode_oid($oid));
111 15 my $ovalue = $self->{_values}{$oid};
112 15 ▶ return _noSuchName($dr) if not defined $otype or not defined $ovalue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $otype and defined $ovalue 5 10 15
Condition66%2/3
defined $otype and defined $ovalue
!ll&&!rl&&r
5010
MC/DC50%1/2
defined $otype and defined $ovalue
defined $otype defined $ovalue
Truth table66%2/3
A and B
A= defined $otypeB= defined $ovalue
ABresult
0X0
100
111
113 10 push @rvbl, [$oid, $otype, $ovalue];
114 }
115 10 return NSNMP->encode(
116 request_id => $dr->request_id,
117 type => NSNMP::GET_RESPONSE,
118 varbindlist => \@rvbl,
119 );
120 }
121  
122 4 ▶sub handle_set_request {
Subroutine handle_set_request: called CC=5 SCAR=16.4
Pod undocumented
123 4 my ($self, $dr) = @_;
124 4 for my $varbind ($dr->varbindlist) {
125 4 my ($oid, $type, $value) = @{$varbind};
126 4 my ($otype, $instance) = $self->{typemapper}->map(NSNMP->decode_oid($oid));
127 4 my $ovalue = $self->{_values}{$oid};
128 4 ▶ return _noSuchName($dr) if not defined $otype or not defined $ovalue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $otype and defined $ovalue 0 4 4
Condition33%1/3
defined $otype and defined $ovalue
!ll&&!rl&&r
004
MC/DC0%0/2
defined $otype and defined $ovalue
defined $otype defined $ovalue
Truth table33%1/3
A and B
A= defined $otypeB= defined $ovalue
ABresult
0X0
100
111
129 4 ▶ return _badValue($dr) if $type ne $otype;
Branch: 1 branch
BranchTrueFalseTotal
if $type ne $otype 1 3 4
130 3 $self->{_values}{$oid} = $value;
131 }
132 3 return NSNMP->encode(
133 request_id => $dr->request_id,
134 type => NSNMP::GET_RESPONSE,
135 varbindlist => [$dr->varbindlist],
136 );
137 }
138  
139 21 ▶sub handle_request {
Subroutine handle_request: called CC=4 SCAR=13.9
Pod undocumented
140 21 my ($self, $request) = @_;
141 21 my $dr = NSNMP->decode($request);
142 21 ▶ return undef unless $dr and $dr->community eq $self->{community};
Branch: 1 branch
BranchTrueFalseTotal
unless $dr and $dr->community eq $self->{"community"} 2 19 21
Condition66%2/3
$dr and $dr->community eq $self->{"community"}
!ll&&!rl&&r
0219
MC/DC50%1/2
$dr and $dr->community eq $self->{"community"}
$dr $dr->community eq $self->{"community"}
Truth table66%2/3
A and B
A= $drB= $dr->community eq $self->{"community"}
ABresult
0X0
100
111
143 19 my $type = $dr->type;
144 19 ▶ return(($type eq NSNMP::SET_REQUEST) ? handle_set_request($self, $dr) :
Branch: 1 branch
BranchTrueFalseTotal
$type eq "\243" ? : 4 15 19
145 handle_get_request($self, $dr, $type));
146 }
147  
148 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
149 0 my ($self, $socket) = @_;
150 0 my ($request, $requestor);
151 0 for (;;) {
152 0 ▶ if ($requestor = recv $socket, $request, 65536, 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requestor = recv($socket, $request, 65536, 0)) { } 0 0 0
153 0 my $response = $self->handle_request($request, $requestor);
154 0 ▶ send $socket, $response, 0, $requestor if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 0 0 0
155 } else {
156 0 warn "Error on receive: $!";
157 }
158 }
159 }
160  
161 # for testing purposes only
162 # non-testing would require specifying host and returning errors sensibly
163 15 ▶sub spawn {
Subroutine spawn: called CC=4 SCAR=15.0
Pod undocumented
164 15 my ($self, $port) = @_;
165 # note we bind socket before forking, which has two advantages:
166 # - packets never get lost because they got sent before the child
167 # binds the port
168 # - errors kill the main process, not the child.
169 15 my $listensocket = IO::Socket::INET->new(
170 Proto => 'udp',
171 LocalAddr => "127.0.0.1:$port",
172 ReuseAddr => 1,
173 );
174 15 ▶ die "Can't bind port $port: $!" unless $listensocket;
Branch: 1 branch
BranchTrueFalseTotal
unless $listensocket 0 15 15
175 15 my $pid = fork();
176 15 ▶ die "Can't fork: $!" if not defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 15 15
177 15 ▶ if (not $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 15 15
178 0 $self->run($listensocket);
179 0 POSIX::_exit(0);
180 }
181 15 $listensocket->close(); # in parent
182 15 return $pid;
183 }
184  
185 # temp_agent --- test utility function for reaping agents later
186 {
187 2 my $port = 16165;
188 2 my @pids;
189 13 ▶ sub temp_agent {
Subroutine temp_agent: called CC=1 SCAR=0.0
Pod undocumented
190 13 my ($self) = @_;
191 13 $port++;
192 13 my $pid = $self->spawn($port);
193 13 push @pids, $pid;
194 13 return "127.0.0.1:$port";
195 }
196 2 ▶ sub kill_temp_agents {
Subroutine kill_temp_agents: called CC=2 SCAR=6.9
Pod undocumented
197 2 for (@pids) { kill 9, $_; wait() }
198 2 @pids = ();
199 }
200 }
201  
202 2 1;
« NSNMP/Simple.pm ↑ Summary NSNMP/Mapper.pm »