SNMP/Message.pm

filter: Statementstmt 37.2% 269 / 722 Branchbran 23.3% 106 / 454 Conditioncond 33.3% 28 / 84 MC/DCmcdc 6.3% 3 / 47 Subroutinesub 42.6% 73 / 171 Podpod 0.0% 0 / 108 totaltotal 30.2% 479 / 1586 CC 259 SCAR 29.3
CC
259
Cov
32%
msg_security_model
110.0
asn1_ticks_to_time
82.2
msg_id
56.0
CRAP 18.7
« SNMP/Dispatcher.pm ↑ Summary SNMP/Security/Community.pm »
1 # -*- mode: perl -*-
2  
3 # ============================================================================
4  
5 package Net::SNMP::Message;
6  
7 # $Id: Message.pm,v 3.1 2010/09/10 00:01:22 dtown Rel $
8  
9 # Object used to represent a SNMP message.
10  
11 # Copyright (c) 2001-2010 David M. Town <dtown@cpan.org>
12 # All rights reserved.
13  
14 # This program is free software; you may redistribute it and/or modify it
15 # under the same terms as the Perl 5 programming language system itself.
16  
17 # ============================================================================
18  
19 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use Math::BigInt();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ## Version of the Net::SNMP::Message module
25  
26 4 our $VERSION = v3.0.1;
27  
28 ## Handle importing/exporting of symbols
29  
30 4 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our @EXPORT_OK = qw( TRUE FALSE DEBUG_INFO );
33  
34 4 our %EXPORT_TAGS = (
35 generictrap => [
36 qw( COLD_START WARM_START LINK_DOWN LINK_UP AUTHENTICATION_FAILURE
37 EGP_NEIGHBOR_LOSS ENTERPRISE_SPECIFIC )
38 ],
39 msgFlags => [
40 qw( MSG_FLAGS_NOAUTHNOPRIV MSG_FLAGS_AUTH MSG_FLAGS_PRIV
41 MSG_FLAGS_REPORTABLE MSG_FLAGS_MASK )
42 ],
43 securityLevels => [
44 qw( SECURITY_LEVEL_NOAUTHNOPRIV SECURITY_LEVEL_AUTHNOPRIV
45 SECURITY_LEVEL_AUTHPRIV )
46 ],
47 securityModels => [
48 qw( SECURITY_MODEL_ANY SECURITY_MODEL_SNMPV1 SECURITY_MODEL_SNMPV2C
49 SECURITY_MODEL_USM )
50 ],
51 translate => [
52 qw( TRANSLATE_NONE TRANSLATE_OCTET_STRING TRANSLATE_NULL
53 TRANSLATE_TIMETICKS TRANSLATE_OPAQUE TRANSLATE_NOSUCHOBJECT
54 TRANSLATE_NOSUCHINSTANCE TRANSLATE_ENDOFMIBVIEW TRANSLATE_UNSIGNED
55 TRANSLATE_ALL )
56 ],
57 types => [
58 qw( INTEGER INTEGER32 OCTET_STRING NULL OBJECT_IDENTIFIER SEQUENCE
59 IPADDRESS COUNTER COUNTER32 GAUGE GAUGE32 UNSIGNED32 TIMETICKS
60 OPAQUE COUNTER64 NOSUCHOBJECT NOSUCHINSTANCE ENDOFMIBVIEW
61 GET_REQUEST GET_NEXT_REQUEST GET_RESPONSE SET_REQUEST TRAP
62 GET_BULK_REQUEST INFORM_REQUEST SNMPV2_TRAP REPORT )
63 ],
64 utilities => [ qw( asn1_ticks_to_time asn1_itoa ) ],
65 versions => [ qw( SNMP_VERSION_1 SNMP_VERSION_2C SNMP_VERSION_3 ) ],
66 );
67  
68 4 Exporter::export_ok_tags(
69 qw( generictrap msgFlags securityLevels securityModels translate types
70 utilities versions )
71 );
72  
73 4 $EXPORT_TAGS{ALL} = [ @EXPORT_OK ];
74  
75 ## ASN.1 Basic Encoding Rules type definitions
76  
77 17 ▶sub INTEGER { 0x02 } # INTEGER
Subroutine INTEGER: called CC=1 SCAR=0.0
Pod undocumented
78 0 ▶sub INTEGER32 { 0x02 } # Integer32 - SNMPv2c
Subroutine INTEGER32: not called CC=1 SCAR=6.9
Pod undocumented
79 23 ▶sub OCTET_STRING { 0x04 } # OCTET STRING
Subroutine OCTET_STRING: called CC=1 SCAR=0.0
Pod undocumented
80 12 ▶sub NULL { 0x05 } # NULL
Subroutine NULL: called CC=1 SCAR=0.0
Pod undocumented
81 14 ▶sub OBJECT_IDENTIFIER { 0x06 } # OBJECT IDENTIFIER
Subroutine OBJECT_IDENTIFIER: called CC=1 SCAR=0.0
Pod undocumented
82 18 ▶sub SEQUENCE { 0x30 } # SEQUENCE
Subroutine SEQUENCE: called CC=1 SCAR=0.0
Pod undocumented
83  
84 12 ▶sub IPADDRESS { 0x40 } # IpAddress
Subroutine IPADDRESS: called CC=1 SCAR=0.0
Pod undocumented
85 12 ▶sub COUNTER { 0x41 } # Counter
Subroutine COUNTER: called CC=1 SCAR=0.0
Pod undocumented
86 0 ▶sub COUNTER32 { 0x41 } # Counter32 - SNMPv2c
Subroutine COUNTER32: not called CC=1 SCAR=6.9
Pod undocumented
87 12 ▶sub GAUGE { 0x42 } # Gauge
Subroutine GAUGE: called CC=1 SCAR=0.0
Pod undocumented
88 0 ▶sub GAUGE32 { 0x42 } # Gauge32 - SNMPv2c
Subroutine GAUGE32: not called CC=1 SCAR=6.9
Pod undocumented
89 0 ▶sub UNSIGNED32 { 0x42 } # Unsigned32 - SNMPv2c
Subroutine UNSIGNED32: not called CC=1 SCAR=6.9
Pod undocumented
90 12 ▶sub TIMETICKS { 0x43 } # TimeTicks
Subroutine TIMETICKS: called CC=1 SCAR=0.0
Pod undocumented
91 16 ▶sub OPAQUE { 0x44 } # Opaque
Subroutine OPAQUE: called CC=1 SCAR=0.0
Pod undocumented
92 14 ▶sub COUNTER64 { 0x46 } # Counter64 - SNMPv2c
Subroutine COUNTER64: called CC=1 SCAR=0.0
Pod undocumented
93  
94 12 ▶sub NOSUCHOBJECT { 0x80 } # noSuchObject - SNMPv2c
Subroutine NOSUCHOBJECT: called CC=1 SCAR=0.0
Pod undocumented
95 12 ▶sub NOSUCHINSTANCE { 0x81 } # noSuchInstance - SNMPv2c
Subroutine NOSUCHINSTANCE: called CC=1 SCAR=0.0
Pod undocumented
96 12 ▶sub ENDOFMIBVIEW { 0x82 } # endOfMibView - SNMPv2c
Subroutine ENDOFMIBVIEW: called CC=1 SCAR=0.0
Pod undocumented
97  
98 12 ▶sub GET_REQUEST { 0xa0 } # GetRequest-PDU
Subroutine GET_REQUEST: called CC=1 SCAR=0.0
Pod undocumented
99 12 ▶sub GET_NEXT_REQUEST { 0xa1 } # GetNextRequest-PDU
Subroutine GET_NEXT_REQUEST: called CC=1 SCAR=0.0
Pod undocumented
100 12 ▶sub GET_RESPONSE { 0xa2 } # GetResponse-PDU
Subroutine GET_RESPONSE: called CC=1 SCAR=0.0
Pod undocumented
101 12 ▶sub SET_REQUEST { 0xa3 } # SetRequest-PDU
Subroutine SET_REQUEST: called CC=1 SCAR=0.0
Pod undocumented
102 12 ▶sub TRAP { 0xa4 } # Trap-PDU
Subroutine TRAP: called CC=1 SCAR=0.0
Pod undocumented
103 12 ▶sub GET_BULK_REQUEST { 0xa5 } # GetBulkRequest-PDU - SNMPv2c
Subroutine GET_BULK_REQUEST: called CC=1 SCAR=0.0
Pod undocumented
104 12 ▶sub INFORM_REQUEST { 0xa6 } # InformRequest-PDU - SNMPv2c
Subroutine INFORM_REQUEST: called CC=1 SCAR=0.0
Pod undocumented
105 12 ▶sub SNMPV2_TRAP { 0xa7 } # SNMPv2-Trap-PDU - SNMPv2c
Subroutine SNMPV2_TRAP: called CC=1 SCAR=0.0
Pod undocumented
106 12 ▶sub REPORT { 0xa8 } # Report-PDU - SNMPv3
Subroutine REPORT: called CC=1 SCAR=0.0
Pod undocumented
107  
108 ## SNMP RFC version definitions
109  
110 12 ▶sub SNMP_VERSION_1 { 0x00 } # RFC 1157 SNMPv1
Subroutine SNMP_VERSION_1: called CC=1 SCAR=0.0
Pod undocumented
111 7 ▶sub SNMP_VERSION_2C { 0x01 } # RFC 1901 Community-based SNMPv2
Subroutine SNMP_VERSION_2C: called CC=1 SCAR=0.0
Pod undocumented
112 3 ▶sub SNMP_VERSION_3 { 0x03 } # RFC 3411 SNMPv3
Subroutine SNMP_VERSION_3: called CC=1 SCAR=0.0
Pod undocumented
113  
114 ## RFC 1157 - generic-trap definitions
115  
116 0 ▶sub COLD_START { 0 } # coldStart(0)
Subroutine COLD_START: not called CC=1 SCAR=6.9
Pod undocumented
117 0 ▶sub WARM_START { 1 } # warmStart(1)
Subroutine WARM_START: not called CC=1 SCAR=6.9
Pod undocumented
118 0 ▶sub LINK_DOWN { 2 } # linkDown(2)
Subroutine LINK_DOWN: not called CC=1 SCAR=6.9
Pod undocumented
119 0 ▶sub LINK_UP { 3 } # linkUp(3)
Subroutine LINK_UP: not called CC=1 SCAR=6.9
Pod undocumented
120 0 ▶sub AUTHENTICATION_FAILURE { 4 } # authenticationFailure(4)
Subroutine AUTHENTICATION_FAILURE: not called CC=1 SCAR=6.9
Pod undocumented
121 0 ▶sub EGP_NEIGHBOR_LOSS { 5 } # egpNeighborLoss(5)
Subroutine EGP_NEIGHBOR_LOSS: not called CC=1 SCAR=6.9
Pod undocumented
122 0 ▶sub ENTERPRISE_SPECIFIC { 6 } # enterpriseSpecific(6)
Subroutine ENTERPRISE_SPECIFIC: not called CC=1 SCAR=6.9
Pod undocumented
123  
124 ## RFC 3412 - msgFlags::=OCTET STRING
125  
126 0 ▶sub MSG_FLAGS_NOAUTHNOPRIV { 0x00 } # Means noAuthNoPriv
Subroutine MSG_FLAGS_NOAUTHNOPRIV: not called CC=1 SCAR=6.9
Pod undocumented
127 0 ▶sub MSG_FLAGS_AUTH { 0x01 } # authFlag
Subroutine MSG_FLAGS_AUTH: not called CC=1 SCAR=6.9
Pod undocumented
128 0 ▶sub MSG_FLAGS_PRIV { 0x02 } # privFlag
Subroutine MSG_FLAGS_PRIV: not called CC=1 SCAR=6.9
Pod undocumented
129 0 ▶sub MSG_FLAGS_REPORTABLE { 0x04 } # reportableFlag
Subroutine MSG_FLAGS_REPORTABLE: not called CC=1 SCAR=6.9
Pod undocumented
130 0 ▶sub MSG_FLAGS_MASK { 0x07 }
Subroutine MSG_FLAGS_MASK: not called CC=1 SCAR=6.9
Pod undocumented
131  
132 ## RFC 3411 - SnmpSecurityLevel::=TEXTUAL-CONVENTION
133  
134 2 ▶sub SECURITY_LEVEL_NOAUTHNOPRIV { 1 } # noAuthNoPriv
Subroutine SECURITY_LEVEL_NOAUTHNOPRIV: called CC=1 SCAR=0.0
Pod undocumented
135 6 ▶sub SECURITY_LEVEL_AUTHNOPRIV { 2 } # authNoPriv
Subroutine SECURITY_LEVEL_AUTHNOPRIV: called CC=1 SCAR=0.0
Pod undocumented
136 4 ▶sub SECURITY_LEVEL_AUTHPRIV { 3 } # authPriv
Subroutine SECURITY_LEVEL_AUTHPRIV: called CC=1 SCAR=0.0
Pod undocumented
137  
138 ## RFC 3411 - SnmpSecurityModel::=TEXTUAL-CONVENTION
139  
140 0 ▶sub SECURITY_MODEL_ANY { 0 } # Reserved for 'any'
Subroutine SECURITY_MODEL_ANY: not called CC=1 SCAR=6.9
Pod undocumented
141 0 ▶sub SECURITY_MODEL_SNMPV1 { 1 } # Reserved for SNMPv1
Subroutine SECURITY_MODEL_SNMPV1: not called CC=1 SCAR=6.9
Pod undocumented
142 0 ▶sub SECURITY_MODEL_SNMPV2C { 2 } # Reserved for SNMPv2c
Subroutine SECURITY_MODEL_SNMPV2C: not called CC=1 SCAR=6.9
Pod undocumented
143 0 ▶sub SECURITY_MODEL_USM { 3 } # User-Based Security Model (USM)
Subroutine SECURITY_MODEL_USM: not called CC=1 SCAR=6.9
Pod undocumented
144  
145 ## Translation masks
146  
147 4 ▶sub TRANSLATE_NONE { 0x00 } # Bit masks used to determine
Subroutine TRANSLATE_NONE: called CC=1 SCAR=0.0
Pod undocumented
148 5 ▶sub TRANSLATE_OCTET_STRING { 0x01 } # if a specific ASN.1 type is
Subroutine TRANSLATE_OCTET_STRING: called CC=1 SCAR=0.0
Pod undocumented
149 0 ▶sub TRANSLATE_NULL { 0x02 } # translated into a "human
Subroutine TRANSLATE_NULL: not called CC=1 SCAR=6.9
Pod undocumented
150 0 ▶sub TRANSLATE_TIMETICKS { 0x04 } # readable" form.
Subroutine TRANSLATE_TIMETICKS: not called CC=1 SCAR=6.9
Pod undocumented
151 0 ▶sub TRANSLATE_OPAQUE { 0x08 }
Subroutine TRANSLATE_OPAQUE: not called CC=1 SCAR=6.9
Pod undocumented
152 0 ▶sub TRANSLATE_NOSUCHOBJECT { 0x10 }
Subroutine TRANSLATE_NOSUCHOBJECT: not called CC=1 SCAR=6.9
Pod undocumented
153 0 ▶sub TRANSLATE_NOSUCHINSTANCE { 0x20 }
Subroutine TRANSLATE_NOSUCHINSTANCE: not called CC=1 SCAR=6.9
Pod undocumented
154 0 ▶sub TRANSLATE_ENDOFMIBVIEW { 0x40 }
Subroutine TRANSLATE_ENDOFMIBVIEW: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub TRANSLATE_UNSIGNED { 0x80 }
Subroutine TRANSLATE_UNSIGNED: not called CC=1 SCAR=6.9
Pod undocumented
156 0 ▶sub TRANSLATE_ALL { 0xff }
Subroutine TRANSLATE_ALL: not called CC=1 SCAR=6.9
Pod undocumented
157  
158 ## Truth values
159  
160 53 ▶sub TRUE { 0x01 }
Subroutine TRUE: called CC=1 SCAR=0.0
Pod undocumented
161 29 ▶sub FALSE { 0x00 }
Subroutine FALSE: called CC=1 SCAR=0.0
Pod undocumented
162  
163 ## Package variables
164  
165 4 our $DEBUG = FALSE; # Debug flag
166  
167 4 our $AUTOLOAD; # Used by the AUTOLOAD method
168  
169 ## Initialize the request-id/msgID.
170  
171 4 our $ID = int rand((2**16) - 1) + ($^T & 0xff);
172  
173 # [public methods] -----------------------------------------------------------
174  
175 sub new
176 4 ▶{
Subroutine new: called CC=14 SCAR=37.9
Pod undocumented
177 4 my ($class, %argv) = @_;
178  
179 # Create a new data structure for the object
180 4 my $this = bless {
181 '_buffer' => q{}, # Serialized message buffer
182 '_error' => undef, # Error message
183 '_index' => 0, # Buffer index
184 '_leading_dot' => FALSE, # Prepend leading dot on OIDs
185 '_length' => 0, # Buffer length
186 '_security' => undef, # Security Model object
187 '_translate' => TRANSLATE_NONE, # Translation mode
188 '_transport' => undef, # Transport Layer object
189 '_version' => SNMP_VERSION_1, # SNMP version
190 }, $class;
191  
192 # Validate the passed arguments
193  
194 4 for (keys %argv) {
195  
196 1 ▶ if (/^-?callback$/i) {
Branch: 9 branches
BranchTrueFalseTotal
if (/^-?callback$/i) { } 0 1 1
elsif (/^-?debug$/i) { } 0 1 1
elsif (/^-?leadingdot$/i) { } 0 1 1
elsif (/^-?msgid$/i) { } 0 1 1
elsif (/^-?requestid$/i) { } 0 1 1
elsif (/^-?security$/i) { } 0 1 1
elsif (/^-?translate$/i) { } 0 1 1
elsif (/^-?transport$/i) { } 0 1 1
elsif (/^-?version$/i) { } 1 0 1
197 0 $this->callback($argv{$_});
198 } elsif (/^-?debug$/i) {
199 0 $this->debug($argv{$_});
200 } elsif (/^-?leadingdot$/i) {
201 0 $this->leading_dot($argv{$_});
202 } elsif (/^-?msgid$/i) {
203 0 $this->msg_id($argv{$_});
204 } elsif (/^-?requestid$/i) {
205 0 $this->request_id($argv{$_});
206 } elsif (/^-?security$/i) {
207 0 $this->security($argv{$_});
208 } elsif (/^-?translate$/i) {
209 0 $this->translate($argv{$_});
210 } elsif (/^-?transport$/i) {
211 0 $this->transport($argv{$_});
212 } elsif (/^-?version$/i) {
213 1 $this->version($argv{$_});
214 } else {
215 0 $this->_error('The argument "%s" is unknown', $_);
216 }
217  
218 1 ▶ if (defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_error"}) 0 1 1
219 0 ▶ return wantarray ? (undef, $this->{_error}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
220 }
221  
222 }
223  
224 4 ▶ return wantarray ? ($this, q{}) : $this;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
225 }
226  
227 {
228 4 my $prepare_methods = {
229 INTEGER, \&_prepare_integer,
230 OCTET_STRING, \&_prepare_octet_string,
231 NULL, \&_prepare_null,
232 OBJECT_IDENTIFIER, \&_prepare_object_identifier,
233 SEQUENCE, \&_prepare_sequence,
234 IPADDRESS, \&_prepare_ipaddress,
235 COUNTER, \&_prepare_counter,
236 GAUGE, \&_prepare_gauge,
237 TIMETICKS, \&_prepare_timeticks,
238 OPAQUE, \&_prepare_opaque,
239 COUNTER64, \&_prepare_counter64,
240 NOSUCHOBJECT, \&_prepare_nosuchobject,
241 NOSUCHINSTANCE, \&_prepare_nosuchinstance,
242 ENDOFMIBVIEW, \&_prepare_endofmibview,
243 GET_REQUEST, \&_prepare_get_request,
244 GET_NEXT_REQUEST, \&_prepare_get_next_request,
245 GET_RESPONSE, \&_prepare_get_response,
246 SET_REQUEST, \&_prepare_set_request,
247 TRAP, \&_prepare_trap,
248 GET_BULK_REQUEST, \&_prepare_get_bulk_request,
249 INFORM_REQUEST, \&_prepare_inform_request,
250 SNMPV2_TRAP, \&_prepare_v2_trap,
251 REPORT, \&_prepare_report
252 };
253  
254 sub prepare
255 10 ▶ {
Subroutine prepare: called CC=4 SCAR=15.9
Pod undocumented
256 # my ($this, $type, $value) = @_;
257  
258 10 ▶ return $_[0]->_error() if defined $_[0]->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"_error"} 0 10 10
259  
260 10 ▶ if (!defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1]) 0 10 10
261 0 return $_[0]->_error('The ASN.1 type is not defined');
262 }
263  
264 10 ▶ if (!exists $prepare_methods->{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$prepare_methods{$_[1]}) 0 10 10
265 0 return $_[0]->_error('The ASN.1 type "%s" is unknown', $_[1]);
266 }
267  
268 10 return $_[0]->${\$prepare_methods->{$_[1]}}($_[2]);
269 }
270 }
271  
272 {
273 4 my $process_methods = {
274 INTEGER, \&_process_integer32,
275 OCTET_STRING, \&_process_octet_string,
276 NULL, \&_process_null,
277 OBJECT_IDENTIFIER, \&_process_object_identifier,
278 SEQUENCE, \&_process_sequence,
279 IPADDRESS, \&_process_ipaddress,
280 COUNTER, \&_process_counter,
281 GAUGE, \&_process_gauge,
282 TIMETICKS, \&_process_timeticks,
283 OPAQUE, \&_process_opaque,
284 COUNTER64, \&_process_counter64,
285 NOSUCHOBJECT, \&_process_nosuchobject,
286 NOSUCHINSTANCE, \&_process_nosuchinstance,
287 ENDOFMIBVIEW, \&_process_endofmibview,
288 GET_REQUEST, \&_process_get_request,
289 GET_NEXT_REQUEST, \&_process_get_next_request,
290 GET_RESPONSE, \&_process_get_response,
291 SET_REQUEST, \&_process_set_request,
292 TRAP, \&_process_trap,
293 GET_BULK_REQUEST, \&_process_get_bulk_request,
294 INFORM_REQUEST, \&_process_inform_request,
295 SNMPV2_TRAP, \&_process_v2_trap,
296 REPORT, \&_process_report
297 };
298  
299 sub process
300 10 ▶ {
Subroutine process: called CC=8 SCAR=24.2
Pod undocumented
301 # my ($this, $expected, $found) = @_;
302  
303 # XXX: If present, $found is updated as a side effect.
304  
305 10 ▶ return $_[0]->_error() if defined $_[0]->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"_error"} 0 10 10
306  
307 10 ▶ return $_[0]->_error() if !defined (my $type = $_[0]->_buffer_get(1));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $type = $_[0]->_buffer_get(1)) 0 10 10
308  
309 10 $type = unpack 'C', $type;
310  
311 10 ▶ if (!exists $process_methods->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $process_methods->{$type}) 0 10 10
312 0 return $_[0]->_error('The ASN.1 type 0x%02x is unknown', $type);
313 }
314  
315 # Check to see if a specific ASN.1 type was expected.
316 10 ▶ if ((@_ > 1) && (defined $_[1]) && ($type != $_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1 and defined $_[1] and $type != $_[1]) 0 10 10
Condition66%4/6
@_ > 1 and defined $_[1] and $type != $_[1]
!ll&&!rl&&r
640
@_ > 1 and defined $_[1]
!ll&&!rl&&r
604
MC/DC0%0/3
@_ > 1 and defined $_[1] and $type != $_[1]
@_ > 1 defined $_[1] $type != $_[1]
Truth table50%2/4
A and B and C
A= @_ > 1B= defined $_[1]C= $type != $_[1]
ABCresult
0XX0
10X0
1100
1111
317 0 return $_[0]->_error(
318 'Expected %s, but found %s', asn1_itoa($_[1]), asn1_itoa($type)
319 );
320 }
321  
322 # Update the found ASN.1 type, if the argument is present.
323 10 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 0 10 10
324 0 $_[2] = $type;
325 }
326  
327 10 return $_[0]->${\$process_methods->{$type}}($type);
328 }
329 }
330  
331 sub context_engine_id
332 4 ▶{
Subroutine context_engine_id: not called CC=7 SCAR=39.0
Pod undocumented
333 0 my ($this, $engine_id) = @_;
334  
335 # RFC 3412 - contextEngineID::=OCTET STRING
336  
337 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
338 0 ▶ if (!defined $engine_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $engine_id) 0 0 0
339 0 return $this->_error('The contextEngineID value is not defined');
340 }
341 0 $this->{_context_engine_id} = $engine_id;
342 }
343  
344 0 ▶ if (exists $this->{_context_engine_id}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->{'_context_engine_id'}) { } 0 0 0
elsif (defined $this->{'_security'}) { } 0 0 0
345 0 ▶ return $this->{_context_engine_id} || q{};
Condition0%0/2
$this->{'_context_engine_id'} || ''
l!l
00
MC/DC0%0/1
$this->{'_context_engine_id'} || ''
$this->{'_context_engine_id'}
Truth table0%0/2
A
A= $this->{'_context_engine_id'}
Aresult
11
00
346 } elsif (defined $this->{_security}) {
347 0 ▶ return $this->{_security}->engine_id() || q{};
Condition0%0/2
$this->{'_security'}->engine_id || ''
l!l
00
MC/DC0%0/1
$this->{'_security'}->engine_id || ''
$this->{'_security'}->engine_id
Truth table0%0/2
A
A= $this->{'_security'}->engine_id
Aresult
11
00
348 }
349  
350 0 return q{};
351 }
352  
353 sub context_name
354 0 ▶{
Subroutine context_name: not called CC=4 SCAR=30.0
Pod undocumented
355 0 my ($this, $name) = @_;
356  
357 # RFC 3412 - contextName::=OCTET STRING
358  
359 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
360 0 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
361 0 return $this->_error('The contextName value is not defined');
362 }
363 0 $this->{_context_name} = $name;
364 }
365  
366 0 ▶ return exists($this->{_context_name}) ? $this->{_context_name} : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'_context_name'} ? : 0 0 0
367 }
368  
369 sub msg_flags
370 0 ▶{
Subroutine msg_flags: not called CC=4 SCAR=30.0
Pod undocumented
371 0 my ($this, $flags) = @_;
372  
373 # RFC 3412 - msgFlags::=OCTET STRING (SIZE(1))
374  
375 # NOTE: The stored value is not an OCTET STRING.
376  
377 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
378 0 ▶ if (!defined $flags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flags) 0 0 0
379 0 return $this->_error('The msgFlags value is not defined');
380 }
381 0 $this->{_msg_flags} = $flags;
382 }
383  
384 0 ▶ if (exists $this->{_msg_flags}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"_msg_flags"}) 0 0 0
385 0 return $this->{_msg_flags};
386 }
387  
388 0 return MSG_FLAGS_NOAUTHNOPRIV;
389 }
390  
391 sub msg_id
392 0 ▶{
Subroutine msg_id: not called CC=7 SCAR=40.3
Pod undocumented
393 0 my ($this, $msg_id) = @_;
394  
395 # RFC 3412 - msgID::=INTEGER (0..2147483647)
396  
397 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
398 0 ▶ if (!defined $msg_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg_id) 0 0 0
399 0 return $this->_error('The msgID value is not defined');
400 }
401 0 ▶ if (($msg_id < 0) || ($msg_id > 2147483647)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_id < 0 or $msg_id > 2147483647) 0 0 0
Condition0%0/3
$msg_id < 0 or $msg_id > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg_id < 0 or $msg_id > 2147483647
$msg_id < 0 $msg_id > 2147483647
Truth table0%0/3
A or B
A= $msg_id < 0B= $msg_id > 2147483647
ABresult
1X1
011
000
402 0 return $this->_error(
403 'The msgId %d is out of range (0..2147483647)', $msg_id
404 );
405 }
406 0 $this->{_msg_id} = $msg_id;
407 }
408  
409 0 ▶ if (exists $this->{_msg_id}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->{'_msg_id'}) { } 0 0 0
elsif (exists $this->{'_request_id'}) { } 0 0 0
410 0 return $this->{_msg_id};
411 } elsif (exists $this->{_request_id}) {
412 0 return $this->{_request_id};
413 }
414  
415 0 return 0;
416 }
417  
418 sub msg_max_size
419 0 ▶{
Subroutine msg_max_size: not called CC=6 SCAR=37.4
Pod undocumented
420 0 my ($this, $size) = @_;
421  
422 # RFC 3412 - msgMaxSize::=INTEGER (484..2147483647)
423  
424 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
425 0 ▶ if (!defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 0 0
426 0 return $this->_error('The msgMaxSize value is not defined');
427 }
428 0 ▶ if (($size < 484) || ($size > 2147483647)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < 484 or $size > 2147483647) 0 0 0
Condition0%0/3
$size < 484 or $size > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$size < 484 or $size > 2147483647
$size < 484 $size > 2147483647
Truth table0%0/3
A or B
A= $size < 484B= $size > 2147483647
ABresult
1X1
011
000
429 0 return $this->_error(
430 'The msgMaxSize %d is out of range (484..2147483647)', $size
431 );
432 }
433 0 $this->{_msg_max_size} = $size;
434 }
435  
436 0 ▶ return $this->{_msg_max_size} || 484;
Condition0%0/2
$this->{'_msg_max_size'} || 484
l!l
00
MC/DC0%0/1
$this->{'_msg_max_size'} || 484
$this->{'_msg_max_size'}
Truth table0%0/2
A
A= $this->{'_msg_max_size'}
Aresult
11
00
437 }
438  
439 sub msg_security_model
440 0 ▶{
Subroutine msg_security_model: not called CC=10 SCAR=47.0
Pod undocumented
441 0 my ($this, $model) = @_;
442  
443 # RFC 3412 - msgSecurityModel::=INTEGER (1..2147483647)
444  
445 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
446 0 ▶ if (!defined $model) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $model) 0 0 0
447 0 return $this->_error('The msgSecurityModel value is not defined');
448 }
449 0 ▶ if (($model < 1) || ($model > 2147483647)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model < 1 or $model > 2147483647) 0 0 0
Condition0%0/3
$model < 1 or $model > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$model < 1 or $model > 2147483647
$model < 1 $model > 2147483647
Truth table0%0/3
A or B
A= $model < 1B= $model > 2147483647
ABresult
1X1
011
000
450 0 return $this->_error(
451 'The msgSecurityModel %d is out of range (1..2147483647)', $model
452 );
453 }
454 0 $this->{_security_model} = $model;
455 }
456  
457 0 ▶ if (exists $this->{_security_model}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->{'_security_model'}) { } 0 0 0
elsif (defined $this->{'_security'}) { } 0 0 0
458 0 return $this->{_security_model};
459 } elsif (defined $this->{_security}) {
460 0 return $this->{_security}->security_model();
461 } else {
462 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($this->{'_version'} == SNMP_VERSION_1()) { } 0 0 0
elsif ($this->{'_version'} == SNMP_VERSION_2C()) { } 0 0 0
elsif ($this->{'_version'} == SNMP_VERSION_3()) { } 0 0 0
463 0 return SECURITY_MODEL_SNMPV1;
464 } elsif ($this->{_version} == SNMP_VERSION_2C) {
465 0 return SECURITY_MODEL_SNMPV2C;
466 } elsif ($this->{_version} == SNMP_VERSION_3) {
467 0 return SECURITY_MODEL_USM;
468 }
469 }
470  
471 0 return SECURITY_MODEL_ANY;
472 }
473  
474 sub request_id
475 0 ▶{
Subroutine request_id: not called CC=4 SCAR=30.0
Pod undocumented
476 0 my ($this, $request_id) = @_;
477  
478 # request-id::=INTEGER
479  
480 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
481 0 ▶ if (!defined $request_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $request_id) 0 0 0
482 0 return $this->_error('The request-id value is not defined');
483 }
484 0 $this->{_request_id} = $request_id;
485 }
486  
487 0 ▶ return exists($this->{_request_id}) ? $this->{_request_id} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'_request_id'} ? : 0 0 0
488 }
489  
490 sub security_level
491 0 ▶{
Subroutine security_level: not called CC=7 SCAR=40.3
Pod undocumented
492 0 my ($this, $level) = @_;
493  
494 # RFC 3411 - SnmpSecurityLevel::=INTEGER { noAuthNoPriv(1),
495 # authNoPriv(2),
496 # authPriv(3) }
497  
498 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
499 0 ▶ if (!defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 0 0 0
500 0 return $this->_error('The securityLevel value is not defined');
501 }
502 0 ▶ if (($level < SECURITY_LEVEL_NOAUTHNOPRIV) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($level < SECURITY_LEVEL_NOAUTHNOPRIV() or $level > SECURITY_LEVEL_AUTHPRIV()) 0 0 0
Condition0%0/3
$level < SECURITY_LEVEL_NOAUTHNOPRIV() or $level > SECURITY_LEVEL_AUTHPRIV()
l!l&&r!l&&!r
000
MC/DC0%0/2
$level < SECURITY_LEVEL_NOAUTHNOPRIV() or $level > SECURITY_LEVEL_AUTHPRIV()
$level < SECURITY_LEVEL_NOAUTHNOPRIV() $level > SECURITY_LEVEL_AUTHPRIV()
Truth table0%0/3
A or B
A= $level < SECURITY_LEVEL_NOAUTHNOPRIV()B= $level > SECURITY_LEVEL_AUTHPRIV()
ABresult
1X1
011
000
503 ($level > SECURITY_LEVEL_AUTHPRIV))
504 {
505 0 return $this->_error(
506 'The securityLevel %d is out of range (%d..%d)', $level,
507 SECURITY_LEVEL_NOAUTHNOPRIV, SECURITY_LEVEL_AUTHPRIV
508 );
509 }
510 0 $this->{_security_level} = $level;
511 }
512  
513 0 ▶ if (exists $this->{_security_level}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->{'_security_level'}) { } 0 0 0
elsif (defined $this->{'_security'}) { } 0 0 0
514 0 return $this->{_security_level};
515 } elsif (defined $this->{_security}) {
516 0 return $this->{_security}->security_level();
517 }
518  
519 0 return SECURITY_LEVEL_NOAUTHNOPRIV;
520 }
521  
522 sub security_name
523 0 ▶{
Subroutine security_name: not called CC=5 SCAR=34.0
Pod undocumented
524 0 my ($this, $name) = @_;
525  
526 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
527 0 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
528 0 return $this->_error('The securityName value is not defined');
529 }
530 # No length checks due to no limits by RFC 1157 for community name.
531 0 $this->{_security_name} = $name;
532 }
533  
534 0 ▶ if (exists $this->{_security_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $this->{'_security_name'}) { } 0 0 0
elsif (defined $this->{'_security'}) { } 0 0 0
535 0 return $this->{_security_name};
536 } elsif (defined $this->{_security}) {
537 0 return $this->{_security}->security_name();
538 }
539  
540 0 return q{};
541 }
542  
543 sub version
544 2 ▶{
Subroutine version: called CC=5 SCAR=19.6
Pod undocumented
545 2 my ($this, $version) = @_;
546  
547 2 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2 0 2
548 2 ▶ if (($version == SNMP_VERSION_1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($version == SNMP_VERSION_1() or $version == SNMP_VERSION_2C() or $version == SNMP_VERSION_3()) { } 2 0 2
Condition33%2/6
$version == SNMP_VERSION_1() or $version == SNMP_VERSION_2C() or $version == SNMP_VERSION_3()
l!l&&r!l&&!r
200
$version == SNMP_VERSION_1() or $version == SNMP_VERSION_2C()
l!l&&r!l&&!r
020
MC/DC0%0/3
$version == SNMP_VERSION_1() or $version == SNMP_VERSION_2C() or $version == SNMP_VERSION_3()
$version == SNMP_VERSION_1() $version == SNMP_VERSION_2C() $version == SNMP_VERSION_3()
Truth table25%1/4
A or B or C
A= $version == SNMP_VERSION_1()B= $version == SNMP_VERSION_2C()C= $version == SNMP_VERSION_3()
ABCresult
1XX1
01X1
0011
0000
549 ($version == SNMP_VERSION_2C) ||
550 ($version == SNMP_VERSION_3))
551 {
552 2 $this->{_version} = $version;
553 } else {
554 0 return $this->_error('The SNMP version %d is not supported', $version);
555 }
556 }
557  
558 2 return $this->{_version};
559 }
560  
561 sub error_status
562 0 ▶{
Subroutine error_status: not called CC=1 SCAR=6.9
Pod undocumented
563 0 return 0; # noError(0)
564 }
565  
566 sub error_index
567 0 ▶{
Subroutine error_index: not called CC=1 SCAR=6.9
Pod undocumented
568 0 return 0;
569 }
570  
571 sub var_bind_list
572 0 ▶{
Subroutine var_bind_list: not called CC=1 SCAR=6.9
Pod undocumented
573 0 return undef;
574 }
575  
576 sub var_bind_names
577 0 ▶{
Subroutine var_bind_names: not called CC=1 SCAR=6.9
Pod undocumented
578 0 return [];
579 }
580  
581 sub var_bind_types
582 0 ▶{
Subroutine var_bind_types: not called CC=1 SCAR=6.9
Pod undocumented
583 0 return undef;
584 }
585  
586 #
587 # Security Model accessor methods
588 #
589  
590 sub security
591 0 ▶{
Subroutine security: not called CC=3 SCAR=24.8
Pod undocumented
592 0 my ($this, $security) = @_;
593  
594 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
595 0 ▶ if (defined $security) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $security) { } 0 0 0
596 0 $this->{_security} = $security;
597 } else {
598 0 $this->_error_clear();
599 0 return $this->_error('The Security Model object is not defined');
600 }
601 }
602  
603 0 return $this->{_security};
604 }
605  
606 #
607 # Transport Domain accessor methods
608 #
609  
610 sub transport
611 1 ▶{
Subroutine transport: called CC=3 SCAR=12.3
Pod undocumented
612 1 my ($this, $transport) = @_;
613  
614 1 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 1 0 1
615 1 ▶ if (defined $transport) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $transport) { } 0 1 1
616 0 $this->{_transport} = $transport;
617 } else {
618 1 $this->_error_clear();
619 1 return $this->_error('The Transport Domain object is not defined');
620 }
621 }
622  
623 0 return $this->{_transport};
624 }
625  
626 sub hostname
627 0 ▶{
Subroutine hostname: not called CC=2 SCAR=17.9
Pod undocumented
628 0 my ($this) = @_;
629  
630 0 ▶ if (defined $this->{_transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"_transport"}) 0 0 0
631 0 return $this->{_transport}->dest_hostname();
632 }
633  
634 0 return q{};
635 }
636  
637 sub dstname
638 0 ▶{
Subroutine dstname: not called CC=1 SCAR=6.9
Pod undocumented
639 0 require Carp;
640 0 Carp::croak(
641 sprintf '%s::dstname() is obsolete, use hostname() instead', ref $_[0]
642 );
643  
644 # Never get here.
645 0 return shift->hostname(@_);
646 }
647  
648 sub max_msg_size
649 0 ▶{
Subroutine max_msg_size: not called CC=4 SCAR=30.0
Pod undocumented
650 0 my ($this, $size) = @_;
651  
652 0 ▶ if (!defined $this->{_transport}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_transport"}) 0 0 0
653 0 return 0;
654 }
655  
656 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
657 0 $this->_error_clear();
658 0 ▶ if (defined ($size = $this->{_transport}->max_msg_size($size))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($size = $this->{"_transport"}->max_msg_size($size))) 0 0 0
659 0 return $size;
660 }
661 0 return $this->_error($this->{_transport}->error());
662 }
663  
664 0 return $this->{_transport}->max_msg_size();
665 }
666  
667 sub retries
668 0 ▶{
Subroutine retries: not called CC=2 SCAR=17.9
Pod undocumented
669 0 ▶ return defined($_[0]->{_transport}) ? $_[0]->{_transport}->retries() : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'_transport'} ? : 0 0 0
670 }
671  
672 sub timeout
673 0 ▶{
Subroutine timeout: not called CC=2 SCAR=17.9
Pod undocumented
674 0 ▶ return defined($_[0]->{_transport}) ? $_[0]->{_transport}->timeout() : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'_transport'} ? : 0 0 0
675 }
676  
677 sub send
678 0 ▶{
Subroutine send: not called CC=3 SCAR=24.8
Pod undocumented
679 0 my ($this) = @_;
680  
681 0 $this->_error_clear();
682  
683 0 ▶ if (!defined $this->{_transport}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_transport"}) 0 0 0
684 0 return $this->_error('The Transport Domain object is not defined');
685 }
686  
687 0 DEBUG_INFO('transport address %s', $this->{_transport}->dest_taddress());
688 0 $this->_buffer_dump();
689  
690 0 ▶ if (defined (my $bytes = $this->{_transport}->send($this->{_buffer}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $bytes = $this->{"_transport"}->send($this->{"_buffer"}))) 0 0 0
691 0 return $bytes;
692 }
693  
694 0 return $this->_error($this->{_transport}->error());
695 }
696  
697 sub recv
698 0 ▶{
Subroutine recv: not called CC=3 SCAR=24.8
Pod undocumented
699 0 my ($this) = @_;
700  
701 0 $this->_error_clear();
702  
703 0 ▶ if (!defined $this->{_transport}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_transport"}) 0 0 0
704 0 return $this->_error('The Transport Domain object is not defined');
705 }
706  
707 0 my $name = $this->{_transport}->recv($this->{_buffer});
708  
709 0 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
710 0 $this->{_length} = CORE::length($this->{_buffer});
711 0 DEBUG_INFO('transport address %s', $this->{_transport}->peer_taddress());
712 0 $this->_buffer_dump();
713 0 return $name;
714 }
715  
716 0 return $this->_error($this->{_transport}->error());
717 }
718  
719 #
720 # Data representation methods
721 #
722  
723 sub translate
724 1 ▶{
Subroutine translate: called CC=2 SCAR=7.6
Pod undocumented
725 1 ▶ return (@_ == 2) ? $_[0]->{_translate} = $_[1] : $_[0]->{_translate};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 1 0 1
726 }
727  
728 sub leading_dot
729 0 ▶{
Subroutine leading_dot: not called CC=2 SCAR=17.9
Pod undocumented
730 0 ▶ return (@_ == 2) ? $_[0]->{_leading_dot} = $_[1] : $_[0]->{_leading_dot};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
731 }
732  
733 #
734 # Callback handler methods
735 #
736  
737 sub callback
738 0 ▶{
Subroutine callback: not called CC=4 SCAR=30.0
Pod undocumented
739 0 my ($this, $callback) = @_;
740  
741 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
742 0 ▶ if (ref($callback) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $callback eq 'CODE') { } 0 0 0
elsif (not defined $callback) { } 0 0 0
743 0 $this->{_callback} = $callback;
744 } elsif (!defined $callback) {
745 0 $this->{_callback} = undef;
746 } else {
747 0 DEBUG_INFO('unexpected callback format');
748 }
749 }
750  
751 0 return $this->{_callback};
752 }
753  
754 sub callback_execute
755 0 ▶{
Subroutine callback_execute: not called CC=3 SCAR=24.8
Pod undocumented
756 0 my ($this) = @_;
757  
758 0 ▶ if (!defined $this->{_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_callback"}) 0 0 0
759 0 DEBUG_INFO('no callback');
760 0 return TRUE;
761 }
762  
763 # Protect ourselves from user error.
764 0 eval { $this->{_callback}->($this); };
765  
766 # We clear the callback in case it was a closure which might hold
767 # up the reference count of the calling object.
768  
769 0 $this->{_callback} = undef;
770  
771 0 ▶ return ($@) ? $this->_error($@) : TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
772 }
773  
774 sub status_information
775 0 ▶{
Subroutine status_information: not called CC=5 SCAR=34.0
Pod undocumented
776 0 my $this = shift;
777  
778 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
779 0 ▶ $this->{_error} = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
780 0 ▶ if ($this->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->debug) 0 0 0
781 printf "error: [%d] %s(): %s\n",
782 0 (caller 0)[2], (caller 1)[3], $this->{_error};
783 }
784 0 $this->callback_execute();
785 }
786  
787 0 ▶ return $this->{_error} || q{};
Condition0%0/2
$this->{'_error'} || ''
l!l
00
MC/DC0%0/1
$this->{'_error'} || ''
$this->{'_error'}
Truth table0%0/2
A
A= $this->{'_error'}
Aresult
11
00
788 }
789  
790 sub process_response_pdu
791 0 ▶{
Subroutine process_response_pdu: not called CC=1 SCAR=6.9
Pod undocumented
792 0 goto &callback_execute;
793 }
794  
795 sub timeout_id
796 0 ▶{
Subroutine timeout_id: not called CC=2 SCAR=17.9
Pod undocumented
797 0 ▶ return (@_ == 2) ? $_[0]->{_timeout_id} = $_[1] : $_[0]->{_timeout_id};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
798 }
799  
800 #
801 # Buffer manipulation methods
802 #
803  
804 sub index
805 2 ▶{
Subroutine index: called CC=4 SCAR=16.4
Pod undocumented
806 2 my ($this, $index) = @_;
807  
808 2 ▶ if ((@_ == 2) && ($index >= 0) && ($index <= $this->{_length})) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and $index >= 0 and $index <= $this->{"_length"}) 2 0 2
Condition33%2/6
@_ == 2 and $index >= 0 and $index <= $this->{"_length"}
!ll&&!rl&&r
002
@_ == 2 and $index >= 0
!ll&&!rl&&r
002
MC/DC0%0/3
@_ == 2 and $index >= 0 and $index <= $this->{"_length"}
@_ == 2 $index >= 0 $index <= $this->{"_length"}
Truth table25%1/4
A and B and C
A= @_ == 2B= $index >= 0C= $index <= $this->{"_length"}
ABCresult
0XX0
10X0
1100
1111
809 2 $this->{_index} = $index;
810 }
811  
812 2 return $this->{_index};
813 }
814  
815 sub length
816 8 ▶{
Subroutine length: called CC=1 SCAR=0.0
Pod undocumented
817 8 return $_[0]->{_length};
818 }
819  
820 sub prepend
821 2 ▶{
Subroutine prepend: called CC=1 SCAR=0.0
Pod undocumented
822 2 goto &_buffer_put;
823 }
824  
825 sub append
826 2 ▶{
Subroutine append: called CC=1 SCAR=0.0
Pod undocumented
827 2 goto &_buffer_append;
828 }
829  
830 sub copy
831 0 ▶{
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
832 0 return $_[0]->{_buffer};
833 }
834  
835 sub reference
836 6 ▶{
Subroutine reference: called CC=1 SCAR=0.0
Pod undocumented
837 6 return \$_[0]->{_buffer};
838 }
839  
840 sub clear
841 11 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
842 11 my ($this) = @_;
843  
844 11 $this->{_index} = 0;
845 11 $this->{_length} = 0;
846  
847 11 return substr $this->{_buffer}, 0, CORE::length($this->{_buffer}), q{};
848 }
849  
850 sub dump
851 0 ▶{
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
852 0 goto &_buffer_dump;
853 }
854  
855 #
856 # Debug/error handling methods
857 #
858  
859 sub error
860 2 ▶{
Subroutine error: called CC=6 SCAR=29.7
Pod undocumented
861 2 my $this = shift;
862  
863 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
864 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
865 0 ▶ $this->{_error} = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
866 0 ▶ if ($this->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->debug) 0 0 0
867 printf "error: [%d] %s(): %s\n",
868 0 (caller 0)[2], (caller 1)[3], $this->{_error};
869 }
870 } else {
871 0 $this->{_error} = undef;
872 }
873 }
874  
875 2 ▶ return $this->{_error} || q{};
Condition50%1/2
$this->{'_error'} || ''
l!l
02
MC/DC0%0/1
$this->{'_error'} || ''
$this->{'_error'}
Truth table50%1/2
A
A= $this->{'_error'}
Aresult
11
00
876 }
877  
878 sub debug
879 1 ▶{
Subroutine debug: called CC=3 SCAR=16.0
Pod undocumented
880 1 ▶ return (@_ == 2) ? $DEBUG = ($_[1]) ? TRUE : FALSE : $DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
$_[1] ? : 0 0 0
881 }
882  
883 sub AUTOLOAD
884 4 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=19.6
885 4 my ($this) = @_;
886  
887 4 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::SNMP::Message::AUTOLOAD =~ /::DESTROY$/ 4 0 4
888  
889 0 $AUTOLOAD =~ s/.*://;
890  
891 0 ▶ if (ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this) { } 0 0 0
892 0 $this->_error_clear();
893 0 return $this->_error('The method "%s" is not supported', $AUTOLOAD);
894 } else {
895 0 require Carp;
896 0 Carp::croak(sprintf 'The function "%s" is not supported', $AUTOLOAD);
897 }
898  
899 # Never get here.
900 0 return;
901 }
902  
903 # [private methods] ----------------------------------------------------------
904  
905 #
906 # Basic Encoding Rules (BER) prepare methods
907 #
908  
909 sub _prepare_type_length
910 10 ▶{
Subroutine _prepare_type_length: called CC=5 SCAR=24.1
911 # my ($this, $type, $value) = @_;
912  
913 10 ▶ if (!defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[1]) 0 10 10
914 0 return $_[0]->_error('The ASN.1 type is not defined');
915 }
916  
917 10 my $length = CORE::length($_[2]);
918  
919 10 ▶ if ($length < 0x80) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length < 128) { } 10 0 10
elsif ($length <= 255) { } 0 0 0
elsif ($length <= 65535) { } 0 0 0
920 10 return $_[0]->_buffer_put(pack('C2', $_[1], $length) . $_[2]);
921 } elsif ($length <= 0xff) {
922 0 return $_[0]->_buffer_put(pack('C3', $_[1], 0x81, $length) . $_[2]);
923 } elsif ($length <= 0xffff) {
924 0 return $_[0]->_buffer_put(pack('CCn', $_[1], 0x82, $length) . $_[2]);
925 }
926  
927 0 return $_[0]->_error('Unable to prepare the ASN.1 length');
928 }
929  
930 sub _prepare_integer
931 2 ▶{
Subroutine _prepare_integer: called CC=5 SCAR=20.3
932 2 my ($this, $value) = @_;
933  
934 2 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
935 0 return $this->_error('The INTEGER value is not defined');
936 }
937  
938 2 ▶ if ($value !~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^-?\d+$/) 0 2 2
939 0 return $this->_error(
940 'The INTEGER value "%s" is expected in numeric format', $value
941 );
942 }
943  
944 2 ▶ if ($value < -2147483648 || $value > 4294967295) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < '-2147483648' or $value > 4294967295) 0 2 2
Condition33%1/3
$value < '-2147483648' or $value > 4294967295
l!l&&r!l&&!r
002
MC/DC0%0/2
$value < '-2147483648' or $value > 4294967295
$value < '-2147483648' $value > 4294967295
Truth table33%1/3
A or B
A= $value < '-2147483648'B= $value > 4294967295
ABresult
1X1
011
000
945 0 return $this->_error(
946 'The INTEGER value "%s" is out of range (-2147483648..4294967295)',
947 $value
948 );
949 }
950  
951 2 return $this->_prepare_integer32(INTEGER, $value);
952 }
953  
954 sub _prepare_unsigned32
955 0 ▶{
Subroutine _prepare_unsigned32: not called CC=5 SCAR=34.0
956 0 my ($this, $type, $value) = @_;
957  
958 0 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
959 0 return $this->_error('The %s value is not defined', asn1_itoa($type));
960 }
961  
962 0 ▶ if ($value !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+$/) 0 0 0
963 0 return $this->_error(
964 'The %s value "%s" is expected in positive numeric format',
965 asn1_itoa($type), $value
966 );
967 }
968  
969 0 ▶ if ($value < 0 || $value > 4294967295) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0 or $value > 4294967295) 0 0 0
Condition0%0/3
$value < 0 or $value > 4294967295
l!l&&r!l&&!r
000
MC/DC0%0/2
$value < 0 or $value > 4294967295
$value < 0 $value > 4294967295
Truth table0%0/3
A or B
A= $value < 0B= $value > 4294967295
ABresult
1X1
011
000
970 0 return $this->_error(
971 'The %s value "%s" is out of range (0..4294967295)',
972 asn1_itoa($type), $value
973 );
974 }
975  
976 0 return $this->_prepare_integer32($type, $value);
977 }
978  
979 sub _prepare_integer32
980 2 ▶{
Subroutine _prepare_integer32: called CC=9 SCAR=22.0
981 2 my ($this, $type, $value) = @_;
982  
983 # Determine if the value is positive or negative
984 2 my $negative = ($value < 0);
985  
986 # Check to see if the most significant bit is set, if it is we
987 # need to prefix the encoding with a zero byte.
988  
989 2 my $size = 4; # Assuming 4 byte integers
990 2 my $prefix = FALSE;
991 2 my $bytes = q{};
992  
993 2 ▶ if ((($value & 0xff000000) & 0x80000000) && (!$negative)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value & 4278190080 & 2147483648 and not $negative) 1 1 2
Condition66%2/3
$value & 4278190080 & 2147483648 and not $negative
!ll&&!rl&&r
011
MC/DC50%1/2
$value & 4278190080 & 2147483648 and not $negative
$value & 4278190080 & 2147483648 not $negative
Truth table66%2/3
A and not(B)
A= $value & 4278190080 & 2147483648B= not $negative
ABresult
0X0
100
111
994 1 $size++;
995 1 $prefix = TRUE;
996 }
997  
998 # Remove occurances of nine consecutive ones (if negative) or zeros
999 # from the most significant end of the two's complement integer.
1000  
1001 2 ▶ while ((((!($value & 0xff800000))) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!($value & 4286578688) || ($value & 4286578688) == 4286578688 && $negative and $size > 1) 3 2 5
Condition77%7/9
!($value & 4286578688) || ($value & 4286578688) == 4286578688 && $negative and $size > 1
!ll&&!rl&&r
203
not $value & 4286578688 or ($value & 4286578688) == 4286578688 and $negative
l!l&&r!l&&!r
032
($value & 4286578688) == 4286578688 and $negative
!ll&&!rl&&r
113
MC/DC50%2/4
!($value & 4286578688) || ($value & 4286578688) == 4286578688 && $negative and $size > 1
not $value & 4286578688 ($value & 4286578688) == 4286578688 $negative $size > 1
Truth table50%3/6
not(A) or B and C and D
A= not $value & 4286578688B= ($value & 4286578688) == 4286578688C= $negativeD= $size > 1
ABCDresult
00XX0
010X0
1XX00
01100
1XX11
01111
1002 ((($value & 0xff800000) == 0xff800000) && ($negative))) &&
1003 ($size > 1))
1004 {
1005 3 $size--;
1006 3 $value <<= 8;
1007 }
1008  
1009 # Add a zero byte so the integer is decoded as a positive value
1010 2 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 1 1 2
1011 1 $bytes = pack 'x';
1012 1 $size--;
1013 }
1014  
1015 # Build the integer
1016 2 ▶ while ($size-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size-- > 0) 5 2 7
1017 5 $bytes .= pack 'C*', (($value & 0xff000000) >> 24);
1018 5 $value <<= 8;
1019 }
1020  
1021 # Encode ASN.1 header
1022 2 return $this->_prepare_type_length($type, $bytes);
1023 }
1024  
1025 sub _prepare_octet_string
1026 4 ▶{
Subroutine _prepare_octet_string: called CC=2 SCAR=7.6
1027 4 my ($this, $value) = @_;
1028  
1029 4 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 4 4
1030 0 return $this->_error('The OCTET STRING value is not defined');
1031 }
1032  
1033 4 return $this->_prepare_type_length(OCTET_STRING, $value);
1034 }
1035  
1036 sub _prepare_null
1037 0 ▶{
Subroutine _prepare_null: not called CC=1 SCAR=6.9
1038 0 return $_[0]->_prepare_type_length(NULL, q{});
1039 }
1040  
1041 sub _prepare_object_identifier
1042 1 ▶{
Subroutine _prepare_object_identifier: called CC=12 SCAR=31.8
1043 1 my ($this, $value) = @_;
1044  
1045 1 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 1 1
1046 0 return $this->_error('The OBJECT IDENTIFIER value not defined');
1047 }
1048  
1049 # The OBJECT IDENTIFIER is expected in dotted notation.
1050 1 ▶ if ($value !~ m/^\.?\d+(?:\.\d+)* *$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\.?\d+(?:\.\d+)* *$/) 0 1 1
1051 0 return $this->_error(
1052 'The OBJECT IDENTIFIER value "%s" is expected in dotted decimal ' .
1053 'notation', $value
1054 );
1055 }
1056  
1057 # Break it up into sub-identifiers.
1058 1 my @subids = split /\./, $value;
1059  
1060 # If there was a leading dot on _any_ OBJECT IDENTIFIER passed to
1061 # a prepare method, return a leading dot on _all_ of the OBJECT
1062 # IDENTIFIERs in the process methods.
1063  
1064 1 ▶ if ($subids[0] eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subids[0] eq "") 1 0 1
1065 1 DEBUG_INFO('leading dot present');
1066 1 $this->{_leading_dot} = TRUE;
1067 1 shift @subids;
1068 }
1069  
1070 # RFC 2578 Section 3.5 - "...there are at most 128 sub-identifiers in
1071 # a value, and each sub-identifier has a maximum value of 2^32-1..."
1072  
1073 1 ▶ if (@subids > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subids > 128) 0 1 1
1074 0 return $this->_error(
1075 'The OBJECT IDENTIFIER value "%s" contains more than the maximum ' .
1076 'of 128 sub-identifiers allowed', $value
1077 );
1078 }
1079  
1080 1 ▶ if (grep { $_ < 0 || $_ > 4294967295; } @subids) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ < 0 or $_ > 4294967295;} @subids) 0 1 1
Condition33%1/3
$_ < 0 || $_ > 4294967295
l!l&&r!l&&!r
009
MC/DC0%0/2
$_ < 0 || $_ > 4294967295
$_ < 0 $_ > 4294967295
Truth table33%1/3
A || B
A= $_ < 0B= $_ > 4294967295
ABresult
1X1
011
000
1081 0 return $this->_error(
1082 'The OBJECT IDENTIFIER value "%s" contains a sub-identifier which ' .
1083 'is out of range (0..4294967295)', $value
1084 );
1085 }
1086  
1087 # ISO/IEC 8825 - Specification of Basic Encoding Rules for Abstract
1088 # Syntax Notation One (ASN.1) dictates that the first two sub-identifiers
1089 # are encoded into the first identifier using the the equation:
1090 # subid = ((first * 40) + second). Pad the OBJECT IDENTIFIER to at
1091 # least two sub-identifiers.
1092  
1093 1 ▶ while (@subids < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subids < 2) 0 1 1
1094 0 push @subids, 0;
1095 }
1096  
1097 # The first sub-identifiers are limited to ccitt(0), iso(1), and
1098 # joint-iso-ccitt(2) as defined by RFC 2578.
1099  
1100 1 ▶ if ($subids[0] > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subids[0] > 2) 0 1 1
1101 0 return $this->_error(
1102 'The OBJECT IDENTIFIER value "%s" must begin with either 0 ' .
1103 '(ccitt), 1 (iso), or 2 (joint-iso-ccitt)', $value
1104 );
1105 }
1106  
1107 # If the first sub-identifier is 0 or 1, the second is limited to 0 - 39.
1108  
1109 1 ▶ if (($subids[0] < 2) && ($subids[1] >= 40)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subids[0] < 2 and $subids[1] >= 40) { } 0 1 1
elsif ($subids[1] >= 4294967215) { } 0 1 1
Condition33%1/3
$subids[0] < 2 and $subids[1] >= 40
!ll&&!rl&&r
010
MC/DC0%0/2
$subids[0] < 2 and $subids[1] >= 40
$subids[0] < 2 $subids[1] >= 40
Truth table33%1/3
A and B
A= $subids[0] < 2B= $subids[1] >= 40
ABresult
0X0
100
111
1110 0 return $this->_error(
1111 'The second sub-identifier in the OBJECT IDENTIFIER value "%s" ' .
1112 'must be less than 40', $value
1113 );
1114 } elsif ($subids[1] >= (4294967295 - 80)) {
1115 0 return $this->_error(
1116 'The second sub-identifier in the OBJECT IDENTIFIER value "%s" ' .
1117 'must be less than %u', $value, (4294967295 - 80)
1118 );
1119 }
1120  
1121 # Now apply: subid = ((first * 40) + second)
1122  
1123 1 $subids[1] += (shift(@subids) * 40);
1124  
1125 # Encode each sub-identifier in base 128, most significant digit first,
1126 # with as few digits as possible. Bit eight (the high bit) is set on
1127 # each byte except the last.
1128  
1129 # Encode the ASN.1 header
1130 1 return $this->_prepare_type_length(OBJECT_IDENTIFIER, pack 'w*', @subids);
1131 }
1132  
1133 sub _prepare_sequence
1134 2 ▶{
Subroutine _prepare_sequence: called CC=1 SCAR=0.0
1135 2 return $_[0]->_prepare_implicit_sequence(SEQUENCE, $_[1]);
1136 }
1137  
1138 sub _prepare_implicit_sequence
1139 2 ▶{
Subroutine _prepare_implicit_sequence: called CC=5 SCAR=27.4
1140 2 my ($this, $type, $value) = @_;
1141  
1142 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 0 2
1143 2 return $this->_prepare_type_length($type, $value);
1144 }
1145  
1146 # If the passed value is undefined, we assume that the value of
1147 # the IMPLICIT SEQUENCE is the data currently in the serial buffer.
1148  
1149 0 ▶ if ($this->{_length} < 0x80) {
Branch: 3 branches
BranchTrueFalseTotal
if ($this->{'_length'} < 128) { } 0 0 0
elsif ($this->{'_length'} <= 255) { } 0 0 0
elsif ($this->{'_length'} <= 65535) { } 0 0 0
1150 0 return $this->_buffer_put(pack 'C2', $type, $this->{_length});
1151 } elsif ($this->{_length} <= 0xff) {
1152 0 return $this->_buffer_put(pack 'C3', $type, 0x81, $this->{_length});
1153 } elsif ($this->{_length} <= 0xffff) {
1154 0 return $this->_buffer_put(pack 'CCn', $type, 0x82, $this->{_length});
1155 }
1156  
1157 0 return $this->_error('Unable to prepare the ASN.1 SEQUENCE length');
1158 }
1159  
1160 sub _prepare_ipaddress
1161 0 ▶{
Subroutine _prepare_ipaddress: not called CC=4 SCAR=30.0
1162 0 my ($this, $value) = @_;
1163  
1164 0 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
1165 0 return $this->_error('IpAddress is not defined');
1166 }
1167  
1168 0 ▶ if ($value !~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+\.\d+\.\d+\.\d+$/) 0 0 0
1169 0 return $this->_error(
1170 'The IpAddress value "%s" is expected in dotted decimal notation',
1171 $value
1172 );
1173 }
1174  
1175 0 my @octets = split /\./, $value;
1176  
1177 0 ▶ if (grep { $_ > 255; } @octets) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ > 255;} @octets) 0 0 0
1178 0 return $this->_error('The IpAddress value "%s" is invalid', $value);
1179 }
1180  
1181 0 return $this->_prepare_type_length(IPADDRESS, pack 'C4', @octets);
1182 }
1183  
1184 sub _prepare_counter
1185 0 ▶{
Subroutine _prepare_counter: not called CC=1 SCAR=6.9
1186 0 return $_[0]->_prepare_unsigned32(COUNTER, $_[1]);
1187 }
1188  
1189 sub _prepare_gauge
1190 0 ▶{
Subroutine _prepare_gauge: not called CC=1 SCAR=6.9
1191 0 return $_[0]->_prepare_unsigned32(GAUGE, $_[1]);
1192 }
1193  
1194 sub _prepare_timeticks
1195 0 ▶{
Subroutine _prepare_timeticks: not called CC=1 SCAR=6.9
1196 0 return $_[0]->_prepare_unsigned32(TIMETICKS, $_[1]);
1197 }
1198  
1199 sub _prepare_opaque
1200 0 ▶{
Subroutine _prepare_opaque: not called CC=2 SCAR=17.9
1201 0 my ($this, $value) = @_;
1202  
1203 0 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
1204 0 return $this->_error('The Opaque value is not defined');
1205 }
1206  
1207 0 return $this->_prepare_type_length(OPAQUE, $value);
1208 }
1209  
1210 sub _prepare_counter64
1211 1 ▶{
Subroutine _prepare_counter64: called CC=9 SCAR=25.0
1212 1 my ($this, $value) = @_;
1213  
1214 # Validate the SNMP version
1215 1 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 1 1
1216 0 return $this->_error('The Counter64 type is not supported in SNMPv1');
1217 }
1218  
1219 # Validate the passed value
1220 1 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 1 1
1221 0 return $this->_error('The Counter64 value is not defined');
1222 }
1223  
1224 1 ▶ if ($value !~ /^\+?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\+?\d+$/) 0 1 1
1225 0 return $this->_error(
1226 'The Counter64 value "%s" is expected in positive numeric format',
1227 $value
1228 );
1229 }
1230  
1231 1 $value = Math::BigInt->new($value);
1232  
1233 1 ▶ if ($value eq 'NaN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "NaN") 0 1 1
1234 0 return $this->_error('The Counter64 value "%s" is invalid', $value);
1235 }
1236  
1237 # Make sure the value is no more than 8 bytes long
1238 1 ▶ if ($value->bcmp('18446744073709551615') > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->bcmp("18446744073709551615") > 0) 0 1 1
1239 0 return $this->_error(
1240 'The Counter64 value "%s" is out of range (0..18446744073709551615)',
1241 $value
1242 );
1243 }
1244  
1245 1 my ($quotient, $remainder, @bytes);
1246  
1247 # Handle a value of zero
1248 1 ▶ if ($value == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 0 1 1
1249 0 unshift @bytes, 0x00;
1250 }
1251  
1252 1 ▶ while ($value > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > 0) 8 1 9
1253 8 ($quotient, $remainder) = $value->bdiv(256);
1254 8 $value = Math::BigInt->new($quotient);
1255 8 unshift @bytes, $remainder;
1256 }
1257  
1258 # Make sure that the value is encoded as a positive value
1259 1 ▶ if ($bytes[0] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] & 128) 1 0 1
1260 1 unshift @bytes, 0x00;
1261 }
1262  
1263 1 return $this->_prepare_type_length(COUNTER64, pack 'C*', @bytes);
1264 }
1265  
1266 sub _prepare_nosuchobject
1267 0 ▶{
Subroutine _prepare_nosuchobject: not called CC=2 SCAR=17.9
1268 0 my ($this) = @_;
1269  
1270 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1271 0 return $this->_error('The noSuchObject type is not supported in SNMPv1');
1272 }
1273  
1274 0 return $this->_prepare_type_length(NOSUCHOBJECT, q{});
1275 }
1276  
1277 sub _prepare_nosuchinstance
1278 0 ▶{
Subroutine _prepare_nosuchinstance: not called CC=2 SCAR=17.9
1279 0 my ($this) = @_;
1280  
1281 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1282 0 return $this->_error(
1283 'The noSuchInstance type is not supported in SNMPv1'
1284 );
1285 }
1286  
1287 0 return $this->_prepare_type_length(NOSUCHINSTANCE, q{});
1288 }
1289  
1290 sub _prepare_endofmibview
1291 0 ▶{
Subroutine _prepare_endofmibview: not called CC=2 SCAR=17.9
1292 0 my ($this) = @_;
1293  
1294 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1295 0 return $this->_error('The endOfMibView type is not supported in SNMPv1');
1296 }
1297  
1298 0 return $this->_prepare_type_length(ENDOFMIBVIEW, q{});
1299 }
1300  
1301 sub _prepare_get_request
1302 0 ▶{
Subroutine _prepare_get_request: not called CC=1 SCAR=6.9
1303 0 return $_[0]->_prepare_implicit_sequence(GET_REQUEST, $_[1]);
1304 }
1305  
1306 sub _prepare_get_next_request
1307 0 ▶{
Subroutine _prepare_get_next_request: not called CC=1 SCAR=6.9
1308 0 return $_[0]->_prepare_implicit_sequence(GET_NEXT_REQUEST, $_[1]);
1309 }
1310  
1311 sub _prepare_get_response
1312 0 ▶{
Subroutine _prepare_get_response: not called CC=1 SCAR=6.9
1313 0 return $_[0]->_prepare_implicit_sequence(GET_RESPONSE, $_[1]);
1314 }
1315  
1316 sub _prepare_set_request
1317 0 ▶{
Subroutine _prepare_set_request: not called CC=1 SCAR=6.9
1318 0 return $_[0]->_prepare_implicit_sequence(SET_REQUEST, $_[1]);
1319 }
1320  
1321 sub _prepare_trap
1322 0 ▶{
Subroutine _prepare_trap: not called CC=2 SCAR=17.9
1323 0 my ($this, $value) = @_;
1324  
1325 0 ▶ if ($this->{_version} != SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} != SNMP_VERSION_1()) 0 0 0
1326 0 return $this->_error('The Trap-PDU is only supported in SNMPv1');
1327 }
1328  
1329 0 return $this->_prepare_implicit_sequence(TRAP, $value);
1330 }
1331  
1332 sub _prepare_get_bulk_request
1333 0 ▶{
Subroutine _prepare_get_bulk_request: not called CC=2 SCAR=17.9
1334 0 my ($this, $value) = @_;
1335  
1336 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1337 0 return $this->_error(
1338 'The GetBulkRequest-PDU is not supported in SNMPv1'
1339 );
1340 }
1341  
1342 0 return $this->_prepare_implicit_sequence(GET_BULK_REQUEST, $value);
1343 }
1344  
1345 sub _prepare_inform_request
1346 0 ▶{
Subroutine _prepare_inform_request: not called CC=2 SCAR=17.9
1347 0 my ($this, $value) = @_;
1348  
1349 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1350 0 return $this->_error('The InformRequest-PDU is not supported in SNMPv1');
1351 }
1352  
1353 0 return $this->_prepare_implicit_sequence(INFORM_REQUEST, $value);
1354 }
1355  
1356 sub _prepare_v2_trap
1357 0 ▶{
Subroutine _prepare_v2_trap: not called CC=2 SCAR=17.9
1358 0 my ($this, $value) = @_;
1359  
1360 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1361 0 return $this->_error('The SNMPv2-Trap-PDU is not supported in SNMPv1');
1362 }
1363  
1364 0 return $this->_prepare_implicit_sequence(SNMPV2_TRAP, $value);
1365 }
1366  
1367 sub _prepare_report
1368 0 ▶{
Subroutine _prepare_report: not called CC=2 SCAR=17.9
1369 0 my ($this, $value) = @_;
1370  
1371 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1372 0 return $this->_error('The Report-PDU is not supported in SNMPv1');
1373 }
1374  
1375 0 return $this->_prepare_implicit_sequence(REPORT, $value);
1376 }
1377  
1378 #
1379 # Basic Encoding Rules (BER) process methods
1380 #
1381  
1382 sub _process_length
1383 10 ▶{
Subroutine _process_length: called CC=7 SCAR=29.6
1384 10 my ($this) = @_;
1385  
1386 10 ▶ return $this->_error() if defined $this->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_error"} 0 10 10
1387  
1388 10 my $length = $this->_buffer_get(1);
1389  
1390 10 ▶ if (!defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 0 10 10
1391 0 return $this->_error();
1392 }
1393  
1394 10 $length = unpack 'C', $length;
1395  
1396 10 ▶ if (!($length & 0x80)) { # "Short" length
Branch: 1 branch
BranchTrueFalseTotal
unless ($length & 128) 10 0 10
1397 10 return $length;
1398 }
1399  
1400 0 my $byte_cnt = $length & 0x7f;
1401  
1402 0 ▶ if ($byte_cnt == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($byte_cnt == 0) { } 0 0 0
elsif ($byte_cnt > 4) { } 0 0 0
1403 0 return $this->_error('Indefinite ASN.1 lengths are not supported');
1404 } elsif ($byte_cnt > 4) {
1405 0 return $this->_error(
1406 'The ASN.1 length is too long (%u bytes)', $byte_cnt
1407 );
1408 }
1409  
1410 0 ▶ if (!defined($length = $this->_buffer_get($byte_cnt))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($length = $this->_buffer_get($byte_cnt))) 0 0 0
1411 0 return $this->_error();
1412 }
1413  
1414 0 return unpack 'N', ("\000" x (4 - $byte_cnt) . $length);
1415 }
1416  
1417 sub _process_integer32
1418 2 ▶{
Subroutine _process_integer32: called CC=11 SCAR=27.0
1419 2 my ($this, $type) = @_;
1420  
1421 # Decode the length
1422 2 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 2 2
1423  
1424 # Return an error if the object length is zero?
1425 2 ▶ if ($length < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 1) 0 2 2
1426 0 return $this->_error('The %s length is equal to zero', asn1_itoa($type));
1427 }
1428  
1429 # Retrieve the whole byte stream outside of the loop.
1430 2 ▶ return $this->_error() if !defined(my $bytes = $this->_buffer_get($length));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $bytes = $this->_buffer_get($length)) 0 2 2
1431  
1432 2 my @bytes = unpack 'C*', $bytes;
1433 2 my $negative = FALSE;
1434 2 my $int32 = 0;
1435  
1436 # Validate the length of the Integer32
1437 2 ▶ if (($length > 5) || (($length > 4) && ($bytes[0] != 0x00))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 5 or $length > 4 and $bytes[0] != 0) 0 2 2
Condition50%3/6
$length > 5 or $length > 4 and $bytes[0] != 0
l!l&&r!l&&!r
002
$length > 4 and $bytes[0] != 0
!ll&&!rl&&r
110
MC/DC0%0/3
$length > 5 or $length > 4 and $bytes[0] != 0
$length > 5 $length > 4 $bytes[0] != 0
Truth table50%2/4
A or B and C
A= $length > 5B= $length > 4C= $bytes[0] != 0
ABCresult
1XX1
0111
00X0
0100
1438 0 return $this->_error(
1439 'The %s length is too long (%u bytes)', asn1_itoa($type), $length
1440 );
1441 }
1442  
1443 # If the first bit is set, the Integer32 is negative
1444 2 ▶ if ($bytes[0] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] & 128) 1 1 2
1445 1 $int32 = -1;
1446 1 $negative = TRUE;
1447 }
1448  
1449 # Build the Integer32
1450 2 map { $int32 = (($int32 << 8) | $_) } @bytes;
1451  
1452 2 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 1 1 2
1453 1 ▶ if (($type == INTEGER) || (!($this->{_translate} & TRANSLATE_UNSIGNED))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == INTEGER() or not $this->{'_translate'} & TRANSLATE_UNSIGNED()) { } 1 0 1
Condition33%1/3
$type == INTEGER() or not $this->{"_translate"} & TRANSLATE_UNSIGNED()
l!l&&r!l&&!r
100
MC/DC0%0/2
$type == INTEGER() or not $this->{"_translate"} & TRANSLATE_UNSIGNED()
$type == INTEGER() not $this->{"_translate"} & TRANSLATE_UNSIGNED()
Truth table33%1/3
A or not(B)
A= $type == INTEGER()B= not $this->{"_translate"} & TRANSLATE_UNSIGNED()
ABresult
1X1
011
000
1454 1 return unpack 'l', pack 'l', $int32;
1455 } else {
1456 0 DEBUG_INFO('translating negative %s value', asn1_itoa($type));
1457 0 return unpack 'L', pack 'l', $int32;
1458 }
1459 }
1460  
1461 1 return unpack 'L', pack 'L', $int32;
1462 }
1463  
1464 sub _process_octet_string
1465 4 ▶{
Subroutine _process_octet_string: called CC=6 SCAR=18.7
1466 4 my ($this, $type) = @_;
1467  
1468 # Decode the length
1469 4 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 4 4
1470  
1471 # Get the string
1472 4 ▶ return $this->_error() if !defined(my $s = $this->_buffer_get($length));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $s = $this->_buffer_get($length)) 0 4 4
1473  
1474 # Set the translation mask
1475 4 ▶ my $mask = ($type == OPAQUE) ? TRANSLATE_OPAQUE : TRANSLATE_OCTET_STRING;
Branch: 1 branch
BranchTrueFalseTotal
$type == OPAQUE() ? : 0 4 4
1476  
1477 #
1478 # Translate based on the definition of a DisplayString in RFC 2579.
1479 #
1480 # DisplayString ::= TEXTUAL-CONVENTION
1481 #
1482 # - the graphics characters (32-126) are interpreted as
1483 # US ASCII
1484 # - NUL, LF, CR, BEL, BS, HT, VT and FF have the special
1485 # meanings specified in RFC 854
1486 # - the sequence 'CR x' for any x other than LF or NUL is
1487 # illegal.
1488 #
1489  
1490 4 ▶ if ($this->{_translate} & $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & $mask) 1 3 4
1491 1 $type = asn1_itoa($type);
1492 1 ▶ if ($s =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ / # The values other than NUL, LF, CR, BEL, BS, HT, VT, FF, # and the graphic characters (32-126) trigger translation. [\x01-\x06\x0e-\x1f\x7f-\xff]| # The sequence 'CR x' for any x other than LF or NUL # also triggers translation. \x0d(?![\x00\x0a]) /x) { } 1 0 1
1493 # The values other than NUL, LF, CR, BEL, BS, HT, VT, FF,
1494 # and the graphic characters (32-126) trigger translation.
1495 [\x01-\x06\x0e-\x1f\x7f-\xff]|
1496 # The sequence 'CR x' for any x other than LF or NUL
1497 # also triggers translation.
1498 \x0d(?![\x00\x0a])
1499 }x)
1500 {
1501 1 DEBUG_INFO(
1502 'translating %s to hexadecimal formatted DisplayString', $type
1503 );
1504 1 return sprintf '0x%s', unpack 'H*', $s;
1505 } else {
1506 0 DEBUG_INFO(
1507 'not translating %s, all octets are allowed in a DisplayString',
1508 $type
1509 );
1510 }
1511 }
1512  
1513 3 return $s;
1514 }
1515  
1516 sub _process_null
1517 0 ▶{
Subroutine _process_null: not called CC=4 SCAR=30.0
1518 0 my ($this) = @_;
1519  
1520 # Decode the length
1521 0 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 0 0
1522  
1523 0 ▶ return $this->_error('NULL length is not equal to zero') if ($length != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $length != 0 0 0 0
1524  
1525 0 ▶ if ($this->{_translate} & TRANSLATE_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & TRANSLATE_NULL()) 0 0 0
1526 0 DEBUG_INFO(q{translating NULL to 'NULL' string});
1527 0 return 'NULL';
1528 }
1529  
1530 0 return q{};
1531 }
1532  
1533 sub _process_object_identifier
1534 1 ▶{
Subroutine _process_object_identifier: called CC=11 SCAR=31.5
1535 1 my ($this) = @_;
1536  
1537 # Decode the length
1538 1 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 1 1
1539  
1540 # Return an error if the length is equal to zero?
1541 1 ▶ if ($length < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 1) 0 1 1
1542 0 return $this->_error('The OBJECT IDENTIFIER length is equal to zero');
1543 }
1544  
1545 # Retrieve the whole byte stream (by Niilo Neuvo).
1546  
1547 1 ▶ return $this->_error() if !defined(my $bytes = $this->_buffer_get($length));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $bytes = $this->_buffer_get($length)) 0 1 1
1548  
1549 1 my @oid = ( 0, eval { unpack 'w129', $bytes } );
1550  
1551 # RFC 2578 Section 3.5 - "...there are at most 128 sub-identifiers in
1552 # a value, and each sub-identifier has a maximum value of 2^32-1..."
1553  
1554 1 ▶ if ($@ || (grep { $_ > 4294967295; } @oid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or grep {$_ > 4294967295;} @oid) 0 1 1
Condition33%1/3
$@ or grep {$_ > 4294967295;} @oid
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or grep {$_ > 4294967295;} @oid
$@ grep {$_ > 4294967295;} @oid
Truth table33%1/3
A or B
A= $@B= grep {$_ > 4294967295;} @oid
ABresult
1X1
011
000
1555 0 return $this->_error(
1556 'The OBJECT IDENTIFIER contains a sub-identifier which is out of ' .
1557 'range (0..4294967295)'
1558 );
1559 }
1560  
1561 1 ▶ if (@oid > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oid > 128) 0 1 1
1562 0 return $this->_error(
1563 'The OBJECT IDENTIFIER contains more than the maximum of 128 ' .
1564 'sub-identifiers allowed'
1565 );
1566 }
1567  
1568 # The first two sub-identifiers are encoded into the first identifier
1569 # using the the equation: subid = ((first * 40) + second).
1570  
1571 1 ▶ if ($oid[1] == 0x2b) { # Handle the most common case
Branch: 3 branches
BranchTrueFalseTotal
if ($oid[1] == 43) { } 1 0 1
elsif ($oid[1] < 40) { } 0 0 0
elsif ($oid[1] < 80) { } 0 0 0
1572 1 $oid[0] = 1; # first [iso(1).org(3)]
1573 1 $oid[1] = 3;
1574 } elsif ($oid[1] < 40) {
1575 0 $oid[0] = 0;
1576 } elsif ($oid[1] < 80) {
1577 0 $oid[0] = 1;
1578 0 $oid[1] -= 40;
1579 } else {
1580 0 $oid[0] = 2;
1581 0 $oid[1] -= 80;
1582 }
1583  
1584 # Return the OID in dotted notation (optionally with a
1585 # leading dot if one was passed to the prepare routine).
1586  
1587 1 ▶ if ($this->{_leading_dot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_leading_dot"}) 1 0 1
1588 1 DEBUG_INFO('adding leading dot');
1589 1 unshift @oid, q{};
1590 }
1591  
1592 1 return join q{.}, @oid;
1593 }
1594  
1595 sub _process_sequence
1596 2 ▶{
Subroutine _process_sequence: called CC=1 SCAR=0.0
1597 # Return the length, instead of the value
1598 2 goto &_process_length;
1599 }
1600  
1601 sub _process_ipaddress
1602 0 ▶{
Subroutine _process_ipaddress: not called CC=4 SCAR=30.0
1603 0 my ($this) = @_;
1604  
1605 # Decode the length
1606 0 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 0 0
1607  
1608 0 ▶ if ($length != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != 4) 0 0 0
1609 0 return $this->_error('The IpAddress length of %d is invalid', $length);
1610 }
1611  
1612 0 ▶ if (defined(my $ipaddress = $this->_buffer_get(4))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ipaddress = $this->_buffer_get(4))) 0 0 0
1613 0 return sprintf '%vd', $ipaddress;
1614 }
1615  
1616 0 return $this->_error();
1617 }
1618  
1619 sub _process_counter
1620 0 ▶{
Subroutine _process_counter: not called CC=1 SCAR=6.9
1621 0 goto &_process_integer32;
1622 }
1623  
1624 sub _process_gauge
1625 0 ▶{
Subroutine _process_gauge: not called CC=1 SCAR=6.9
1626 0 goto &_process_integer32;
1627 }
1628  
1629 sub _process_timeticks
1630 0 ▶{
Subroutine _process_timeticks: not called CC=3 SCAR=24.8
1631 0 my ($this) = @_;
1632  
1633 0 ▶ if (defined(my $ticks = $this->_process_integer32(TIMETICKS))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ticks = $this->_process_integer32(TIMETICKS()))) 0 0 0
1634 0 ▶ if ($this->{_translate} & TRANSLATE_TIMETICKS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'_translate'} & TRANSLATE_TIMETICKS()) { } 0 0 0
1635 0 DEBUG_INFO('translating %u TimeTicks to time', $ticks);
1636 0 return asn1_ticks_to_time($ticks);
1637 } else {
1638 0 return $ticks;
1639 }
1640 }
1641  
1642 0 return $this->_error();
1643 }
1644  
1645 sub _process_opaque
1646 0 ▶{
Subroutine _process_opaque: not called CC=1 SCAR=6.9
1647 0 goto &_process_octet_string;
1648 }
1649  
1650 sub _process_counter64
1651 1 ▶{
Subroutine _process_counter64: called CC=12 SCAR=32.6
1652 1 my ($this, $type) = @_;
1653  
1654 # Verify the SNMP version
1655 1 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 1 1
1656 0 return $this->_error('The Counter64 type is not supported in SNMPv1');
1657 }
1658  
1659 # Decode the length
1660 1 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 1 1
1661  
1662 # Return an error if the object length is zero?
1663 1 ▶ if ($length < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 1) 0 1 1
1664 0 return $this->_error('The Counter64 length is equal to zero');
1665 }
1666  
1667 # Retrieve the whole byte stream outside of the loop.
1668 1 ▶ return $this->_error() if !defined(my $bytes = $this->_buffer_get($length));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $bytes = $this->_buffer_get($length)) 0 1 1
1669  
1670 1 my @bytes = unpack 'C*', $bytes;
1671 1 my $negative = FALSE;
1672  
1673 # Validate the length of the Counter64
1674 1 ▶ if (($length > 9) || (($length > 8) && ($bytes[0] != 0x00))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 9 or $length > 8 and $bytes[0] != 0) 0 1 1
Condition33%2/6
$length > 9 or $length > 8 and $bytes[0] != 0
l!l&&r!l&&!r
001
$length > 8 and $bytes[0] != 0
!ll&&!rl&&r
010
MC/DC0%0/3
$length > 9 or $length > 8 and $bytes[0] != 0
$length > 9 $length > 8 $bytes[0] != 0
Truth table25%1/4
A or B and C
A= $length > 9B= $length > 8C= $bytes[0] != 0
ABCresult
1XX1
0111
00X0
0100
1675 0 return $_[0]->_error(
1676 'The Counter64 length is too long (%u bytes)', $length
1677 );
1678 }
1679  
1680 # If the first bit is set, the integer is negative
1681 1 ▶ if ($bytes[0] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] & 128) 0 1 1
1682 0 $bytes[0] ^= 0xff;
1683 0 $negative = TRUE;
1684 }
1685  
1686 # Build the Counter64
1687 1 my $int64 = Math::BigInt->new(shift @bytes);
1688 map {
1689 0 ▶ if ($negative) { $_ ^= 0xff; }
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 0 8 8
1690 8 $int64 *= 256;
1691 8 $int64 += $_;
1692 } @bytes;
1693  
1694 # If the value is negative the other end incorrectly encoded
1695 # the Counter64 since it should always be a positive value.
1696  
1697 1 ▶ if ($negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 0 1 1
1698 0 $int64 = Math::BigInt->new('-1') - $int64;
1699 0 ▶ if ($this->{_translate} & TRANSLATE_UNSIGNED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & TRANSLATE_UNSIGNED()) 0 0 0
1700 0 DEBUG_INFO('translating negative Counter64 value');
1701 0 $int64 += Math::BigInt->new('18446744073709551616');
1702 }
1703 }
1704  
1705 # Perl 5.6.0 (force to string or substitution does not work).
1706 1 $int64 .= q{};
1707  
1708 # Remove the plus sign (or should we leave it to imply Math::BigInt?)
1709 1 $int64 =~ s/^\+//;
1710  
1711 1 return $int64;
1712 }
1713  
1714 sub _process_nosuchobject
1715 0 ▶{
Subroutine _process_nosuchobject: not called CC=5 SCAR=34.0
1716 0 my ($this) = @_;
1717  
1718 # Verify the SNMP version
1719 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1720 0 return $this->_error('The noSuchObject type is not supported in SNMPv1');
1721 }
1722  
1723 # Decode the length
1724 0 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 0 0
1725  
1726 0 ▶ if ($length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != 0) 0 0 0
1727 0 return $this->_error('The noSuchObject length is not equal to zero');
1728 }
1729  
1730 0 ▶ if ($this->{_translate} & TRANSLATE_NOSUCHOBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & TRANSLATE_NOSUCHOBJECT()) 0 0 0
1731 0 DEBUG_INFO(q{translating noSuchObject to 'noSuchObject' string});
1732 0 return 'noSuchObject';
1733 }
1734  
1735 # XXX: Releases greater than v5.2.0 longer set the error-status.
1736 # $this->{_error_status} = NOSUCHOBJECT;
1737  
1738 0 return q{};
1739 }
1740  
1741 sub _process_nosuchinstance
1742 0 ▶{
Subroutine _process_nosuchinstance: not called CC=5 SCAR=34.0
1743 0 my ($this) = @_;
1744  
1745 # Verify the SNMP version
1746 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1747 0 return $this->_error(
1748 'The noSuchInstance type is not supported in SNMPv1'
1749 );
1750 }
1751  
1752 # Decode the length
1753 0 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 0 0
1754  
1755 0 ▶ if ($length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != 0) 0 0 0
1756 0 return $this->_error('The noSuchInstance length is not equal to zero');
1757 }
1758  
1759 0 ▶ if ($this->{_translate} & TRANSLATE_NOSUCHINSTANCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & TRANSLATE_NOSUCHINSTANCE()) 0 0 0
1760 0 DEBUG_INFO(q{translating noSuchInstance to 'noSuchInstance' string});
1761 0 return 'noSuchInstance';
1762 }
1763  
1764 # XXX: Releases greater than v5.2.0 longer set the error-status.
1765 # $this->{_error_status} = NOSUCHINSTANCE;
1766  
1767 0 return q{};
1768 }
1769  
1770 sub _process_endofmibview
1771 0 ▶{
Subroutine _process_endofmibview: not called CC=5 SCAR=34.0
1772 0 my ($this) = @_;
1773  
1774 # Verify the SNMP version
1775 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1776 0 return $this->_error('The endOfMibView type is not supported in SNMPv1');
1777 }
1778  
1779 # Decode the length
1780 0 ▶ return $this->_error() if !defined(my $length = $this->_process_length());
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $length = $this->_process_length) 0 0 0
1781  
1782 0 ▶ if ($length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != 0) 0 0 0
1783 0 return $this->_error('The endOfMibView length is not equal to zero');
1784 }
1785  
1786 0 ▶ if ($this->{_translate} & TRANSLATE_ENDOFMIBVIEW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_translate"} & TRANSLATE_ENDOFMIBVIEW()) 0 0 0
1787 0 DEBUG_INFO(q{translating endOfMibView to 'endOfMibView' string});
1788 0 return 'endOfMibView';
1789 }
1790  
1791 # XXX: Releases greater than v5.2.0 longer set the error-status.
1792 # $this->{_error_status} = ENDOFMIBVIEW;
1793  
1794 0 return q{};
1795 }
1796  
1797 sub _process_pdu_type
1798 0 ▶{
Subroutine _process_pdu_type: not called CC=2 SCAR=17.9
1799 0 my ($this, $type) = @_;
1800  
1801 # Generic methods used to process the PDU type. The ASN.1 type is
1802 # returned by the method as passed by the generic process routine.
1803  
1804 0 ▶ return defined($this->_process_length()) ? $type : $this->_error();
Branch: 1 branch
BranchTrueFalseTotal
defined $this->_process_length ? : 0 0 0
1805 }
1806  
1807 sub _process_get_request
1808 0 ▶{
Subroutine _process_get_request: not called CC=1 SCAR=6.9
1809 0 goto &_process_pdu_type;
1810 }
1811  
1812 sub _process_get_next_request
1813 0 ▶{
Subroutine _process_get_next_request: not called CC=1 SCAR=6.9
1814 0 goto &_process_pdu_type;
1815 }
1816  
1817 sub _process_get_response
1818 0 ▶{
Subroutine _process_get_response: not called CC=1 SCAR=6.9
1819 0 goto &_process_pdu_type;
1820 }
1821  
1822 sub _process_set_request
1823 0 ▶{
Subroutine _process_set_request: not called CC=1 SCAR=6.9
1824 0 goto &_process_pdu_type;
1825 }
1826  
1827 sub _process_trap
1828 0 ▶{
Subroutine _process_trap: not called CC=2 SCAR=17.9
1829 0 my ($this) = @_;
1830  
1831 0 ▶ if ($this->{_version} != SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} != SNMP_VERSION_1()) 0 0 0
1832 0 return $this->_error('The Trap-PDU is only supported in SNMPv1');
1833 }
1834  
1835 0 goto &_process_pdu_type;
1836 }
1837  
1838 sub _process_get_bulk_request
1839 0 ▶{
Subroutine _process_get_bulk_request: not called CC=2 SCAR=17.9
1840 0 my ($this) = @_;
1841  
1842 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1843 0 return $this->_error('The GetBulkRequest-PDU is not supported in SNMPv1');
1844 }
1845  
1846 0 goto &_process_pdu_type;
1847 }
1848  
1849 sub _process_inform_request
1850 0 ▶{
Subroutine _process_inform_request: not called CC=2 SCAR=17.9
1851 0 my ($this) = @_;
1852  
1853 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1854 0 return $this->_error('The InformRequest-PDU is not supported in SNMPv1');
1855 }
1856  
1857 0 goto &_process_pdu_type;
1858 }
1859  
1860 sub _process_v2_trap
1861 0 ▶{
Subroutine _process_v2_trap: not called CC=2 SCAR=17.9
1862 0 my ($this) = @_;
1863  
1864 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1865 0 return $this->_error('The SNMPv2-Trap-PDU is not supported in SNMPv1');
1866 }
1867  
1868 0 goto &_process_pdu_type;
1869 }
1870  
1871 sub _process_report
1872 0 ▶{
Subroutine _process_report: not called CC=2 SCAR=17.9
1873 0 my ($this) = @_;
1874  
1875 0 ▶ if ($this->{_version} == SNMP_VERSION_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_version"} == SNMP_VERSION_1()) 0 0 0
1876 0 return $this->_error('The Report-PDU is not supported in SNMPv1');
1877 }
1878  
1879 0 goto &_process_pdu_type;
1880 }
1881  
1882 #
1883 # Abstract Syntax Notation One (ASN.1) utility functions
1884 #
1885  
1886 {
1887 4 my $types = {
1888 INTEGER, 'INTEGER',
1889 OCTET_STRING, 'OCTET STRING',
1890 NULL, 'NULL',
1891 OBJECT_IDENTIFIER, 'OBJECT IDENTIFIER',
1892 SEQUENCE, 'SEQUENCE',
1893 IPADDRESS, 'IpAddress',
1894 COUNTER, 'Counter',
1895 GAUGE, 'Gauge',
1896 TIMETICKS, 'TimeTicks',
1897 OPAQUE, 'Opaque',
1898 COUNTER64, 'Counter64',
1899 NOSUCHOBJECT, 'noSuchObject',
1900 NOSUCHINSTANCE, 'noSuchInstance',
1901 ENDOFMIBVIEW, 'endOfMibView',
1902 GET_REQUEST, 'GetRequest-PDU',
1903 GET_NEXT_REQUEST, 'GetNextRequest-PDU',
1904 GET_RESPONSE, 'GetResponse-PDU',
1905 SET_REQUEST, 'SetRequest-PDU',
1906 TRAP, 'Trap-PDU',
1907 GET_BULK_REQUEST, 'GetBulkRequest-PDU',
1908 INFORM_REQUEST, 'InformRequest-PDU',
1909 SNMPV2_TRAP, 'SNMPv2-Trap-PDU',
1910 REPORT, 'Report-PDU'
1911 };
1912  
1913 sub asn1_itoa
1914 1 ▶ {
Subroutine asn1_itoa: called CC=3 SCAR=12.0
Pod undocumented
1915 1 my ($type) = @_;
1916  
1917 1 ▶ return q{??} if (@_ != 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
1918  
1919 1 ▶ if (!exists $types->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $types->{$type}) 0 1 1
1920 0 return sprintf '?? [0x%02x]', $type;
1921 }
1922  
1923 1 return $types->{$type};
1924 }
1925 }
1926  
1927 sub asn1_ticks_to_time
1928 4 ▶{
Subroutine asn1_ticks_to_time: not called CC=9 SCAR=44.1
Pod undocumented
1929 0 ▶ my $ticks = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1930  
1931 0 my $days = int($ticks / (24 * 60 * 60 * 100));
1932 0 $ticks %= (24 * 60 * 60 * 100);
1933  
1934 0 my $hours = int($ticks / (60 * 60 * 100));
1935 0 $ticks %= (60 * 60 * 100);
1936  
1937 0 my $minutes = int($ticks / (60 * 100));
1938 0 $ticks %= (60 * 100);
1939  
1940 0 my $seconds = ($ticks / 100);
1941  
1942 0 ▶ if ($days != 0){
Branch: 3 branches
BranchTrueFalseTotal
if ($days != 0) { } 0 0 0
elsif ($hours != 0) { } 0 0 0
elsif ($minutes != 0) { } 0 0 0
1943 0 ▶ return sprintf '%d day%s, %02d:%02d:%05.02f', $days,
Branch: 1 branch
BranchTrueFalseTotal
$days == 1 ? : 0 0 0
1944 ($days == 1 ? q{} : 's'), $hours, $minutes, $seconds;
1945 } elsif ($hours != 0) {
1946 0 ▶ return sprintf '%d hour%s, %02d:%05.02f', $hours,
Branch: 1 branch
BranchTrueFalseTotal
$hours == 1 ? : 0 0 0
1947 ($hours == 1 ? q{} : 's'), $minutes, $seconds;
1948 } elsif ($minutes != 0) {
1949 0 ▶ return sprintf '%d minute%s, %05.02f', $minutes,
Branch: 1 branch
BranchTrueFalseTotal
$minutes == 1 ? : 0 0 0
1950 ($minutes == 1 ? q{} : 's'), $seconds;
1951 } else {
1952 0 ▶ return sprintf '%04.02f second%s', $seconds, ($seconds == 1 ? q{} : 's');
Branch: 1 branch
BranchTrueFalseTotal
$seconds == 1 ? : 0 0 0
1953 }
1954 }
1955  
1956 #
1957 # Error handlers
1958 #
1959  
1960 sub _error
1961 1 ▶{
Subroutine _error: called CC=4 SCAR=15.2
1962 1 my $this = shift;
1963  
1964 1 ▶ if (!defined $this->{_error}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_error"}) 1 0 1
1965 1 ▶ $this->{_error} = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
1966 1 ▶ if ($this->debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->debug) 0 1 1
1967 printf "error: [%d] %s(): %s\n",
1968 0 (caller 0)[2], (caller 1)[3], $this->{_error};
1969 }
1970 }
1971  
1972 1 return;
1973 }
1974  
1975 sub _error_clear
1976 1 ▶{
Subroutine _error_clear: called CC=1 SCAR=0.0
1977 1 return $_[0]->{_error} = undef;
1978 }
1979  
1980 #
1981 # Buffer manipulation methods
1982 #
1983  
1984 sub _buffer_append
1985 2 ▶{
Subroutine _buffer_append: called CC=2 SCAR=7.0
1986 # my ($this, $value) = @_;
1987  
1988 2 ▶ return $_[0]->_error() if defined $_[0]->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"_error"} 0 2 2
1989  
1990 # Always reset the index when the buffer is modified
1991 2 $_[0]->{_index} = 0;
1992  
1993 # Update our length
1994 2 $_[0]->{_length} += CORE::length($_[1]);
1995  
1996 # Append to the current buffer
1997 2 return $_[0]->{_buffer} .= $_[1];
1998 }
1999  
2000 sub _buffer_get
2001 28 ▶{
Subroutine _buffer_get: called CC=5 SCAR=23.4
2002 28 my ($this, $requested) = @_;
2003  
2004 28 ▶ return $this->_error() if defined $this->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_error"} 0 28 28
2005  
2006 # Return the number of bytes requested at the current index or
2007 # clear and return the whole buffer if no argument is passed.
2008  
2009 28 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 28 0 28
2010  
2011 28 ▶ if (($this->{_index} += $requested) > $this->{_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($this->{"_index"} += $requested) > $this->{"_length"}) 0 28 28
2012 0 $this->{_index} -= $requested;
2013 0 ▶ if ($this->{_length} >= $this->max_msg_size()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_length"} >= $this->max_msg_size) 0 0 0
2014 0 return $this->_error(
2015 'The message size exceeded the buffer maxMsgSize of %d',
2016 $this->max_msg_size()
2017 );
2018 }
2019 0 return $this->_error('Unexpected end of message buffer');
2020 }
2021  
2022 28 return substr $this->{_buffer}, $this->{_index} - $requested, $requested;
2023 }
2024  
2025 # Always reset the index when the buffer is modified
2026 0 $this->{_index} = 0;
2027  
2028 # Update our length to 0, the whole buffer is about to be cleared.
2029 0 $this->{_length} = 0;
2030  
2031 0 return substr $this->{_buffer}, 0, CORE::length($this->{_buffer}), q{};
2032 }
2033  
2034 sub _buffer_put
2035 12 ▶{
Subroutine _buffer_put: called CC=2 SCAR=7.0
2036 # my ($this, $value) = @_;
2037  
2038 12 ▶ return $_[0]->_error() if defined $_[0]->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"_error"} 0 12 12
2039  
2040 # Always reset the index when the buffer is modified
2041 12 $_[0]->{_index} = 0;
2042  
2043 # Update our length
2044 12 $_[0]->{_length} += CORE::length($_[1]);
2045  
2046 # Add the prefix to the current buffer
2047 12 substr $_[0]->{_buffer}, 0, 0, $_[1];
2048  
2049 12 return $_[0]->{_buffer};
2050 }
2051  
2052 sub _buffer_dump
2053 0 ▶{
Subroutine _buffer_dump: not called CC=4 SCAR=30.0
2054 0 my ($this) = @_;
2055  
2056 0 ▶ return $DEBUG if (!$DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::SNMP::Message::DEBUG 0 0 0
2057  
2058 0 ▶ DEBUG_INFO('%d byte%s', $this->{_length}, $this->{_length} != 1 ? 's' : q{});
Branch: 1 branch
BranchTrueFalseTotal
$this->{'_length'} != 1 ? : 0 0 0
2059  
2060 0 my ($offset, $hex, $text) = (0, q{}, q{});
2061  
2062 0 ▶ while ($this->{_buffer} =~ /(.{1,16})/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_buffer"} =~ /(.{1,16})/gs) 0 0 0
2063 0 $hex = unpack 'H*', ($text = $1);
2064 0 $hex .= q{ } x (32 - CORE::length($hex));
2065 0 $hex = sprintf '%s %s %s %s ' x 4, unpack 'a2' x 16, $hex;
2066 0 $text =~ s/[\x00-\x1f\x7f-\xff]/./g;
2067 0 printf "[%04d] %s %s\n", $offset, uc($hex), $text;
2068 0 $offset += 16;
2069 }
2070  
2071 0 return $DEBUG;
2072 }
2073  
2074 sub DEBUG_INFO
2075 3 ▶{
Subroutine DEBUG_INFO: called CC=3 SCAR=17.3
Pod undocumented
2076 3 ▶ return $DEBUG if (!$DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::SNMP::Message::DEBUG 3 0 3
2077  
2078 0 ▶ return printf
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
2079 sprintf('debug: [%d] %s(): ', (caller 0)[2], (caller 1)[3]) .
2080 ((@_ > 1) ? shift(@_) : '%s') .
2081 "\n",
2082 @_;
2083 }
2084  
2085 # ============================================================================
2086 4 1; # [end Net::SNMP::Message]
« SNMP/Dispatcher.pm ↑ Summary SNMP/Security/Community.pm »