blib/lib/Encoding/BER/SNMP.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 67 CC 4 SCAR 17.6
CC
4
Cov
0%
encode_ip
12.0
decode_ip
6.0
subclass_tag_data_byname
2.0
CRAP 5.8
1 # -*- perl -*-
2  
3 # Copyright (c) 2007 by Jeff Weisberg
4 # Author: Jeff Weisberg <jaw+pause @ tcp4me.com>
5 # Created: 2007-Feb-04 15:51 (EST)
6 # Function: BER Encoding/Decoding for SNMP
7 #
8 # $Id: SNMP.pm,v 1.7 2007/03/06 02:50:11 jaw Exp $
9  
10 package Encoding::BER::SNMP;
11 use Encoding::BER 'init_tag_lookups';
12 use vars qw($VERSION @ISA);
13 $VERSION = '1.00';
14 @ISA = qw(Encoding::BER);
15 use strict;
16  
17
18 my %TAG =
19 (
20 application => {
21 ip_address => { v => 0, type => ['application'], e => \&encode_ip, d => \&decode_ip },
22 counter32 => { v => 1, type => ['application'], implicit => 'uint32' },
23 gauge32 => { v => 2, type => ['application'], implicit => 'uint32' },
24 timeticks => { v => 3, type => ['application'], implicit => 'int32' },
25 opaque => { v => 4, type => ['application'], implicit => 'octet_string' },
26 counter64 => { v => 6, type => ['application'], implicit => 'unsigned_integer' },
27 },
28 context => {
29 get_request => { v => 0, type => ['context', 'constructed'], implicit => 'sequence' },
30 get_next_request => { v => 1, type => ['context', 'constructed'], implicit => 'sequence' },
31 get_response => { v => 2, type => ['context', 'constructed'], implicit => 'sequence' },
32 set_request => { v => 3, type => ['context', 'constructed'], implicit => 'sequence' },
33 trap => { v => 4, type => ['context', 'constructed'], implicit => 'sequence' },
34 get_bulk_request => { v => 5, type => ['context', 'constructed'], implicit => 'sequence' },
35 inform_request => { v => 6, type => ['context', 'constructed'], implicit => 'sequence' },
36 snmpv2_trap => { v => 7, type => ['context', 'constructed'], implicit => 'sequence' },
37 report => { v => 8, type => ['context', 'constructed'], implicit => 'sequence' },
38 },
39 );
40  
41 my %ALLTAG;
42 my %REVTAG;
43  
44 init_tag_lookups( \%TAG, \%ALLTAG, \%REVTAG );
45  
46  
47 sub subclass_tag_data_byname {
48 my $me = shift;
49 my $name = shift;
50  
51 $ALLTAG{$name};
52 }
53  
54 sub subclass_tag_data_bynumber {
55 my $me = shift;
56 my $class = shift;
57 my $tnum = shift;
58  
59 $TAG{$class}{ $REVTAG{$class}{$tnum} };
60 }
61  
62 sub encode_ip {
63 my $me = shift;
64 my $data = shift;
65 my $val = $data->{value};
66 # either dotted-quad or 4B-packed
67  
68 if( length($val) == 4){
69 $val;
70 }else{
71 my @o = split /\./, $val;
72 $me->error("invalid IP address: $val") unless @o == 4;
73 pack('C4', @o);
74 }
75 }
76  
77 sub decode_ip {
78 my $me = shift;
79 my $data = shift;
80 my $type = shift;
81  
82 $me->warn("received invalid IP address") unless length($data) == 4;
83  
84 {
85 value => join('.', unpack('C4', $data)),
86 };
87 }
88  
89 =head1 NAME
90  
91 Encoding::BER::SNMP - adds SNMP specific tags to Encoding::BER
92  
93 =head1 SYNOPSIS
94  
95 use Encoding::BER::SNMP;
96 my $enc = Encoding::BER::SNMP->new();
97 my $snmp = $enc->encode( [ 1, 'public',
98 { type => 'get_request',
99 value => [ $reqid, 0, 0,
100 [ [ { type => 'oid',
101 value => '1.3.6.1.2.1.2.2.1.10.4' },
102 undef ] ] ] };
103 send(UDP, $snmp, 0);
104
105 =head1 DESCRIPTION
106  
107 This is a subclass of Encoding::BER, and the following tags are made available:
108  
109 ip_address counter32 gauge32 timeticks opaque counter64
110 get_request get_next_request get_response set_request
111 trap get_bulk_request inform_request snmpv2_trap report
112  
113 =head1 BUGS
114  
115 There are no known bugs in this module.
116
117 =head1 SEE ALSO
118  
119 Encoding::BER
120  
121 =head1 AUTHOR
122  
123 Jeff Weisberg - http://www.tcp4me.com/
124  
125 =cut
126 ;
127  
128  
129  
130  
131 1;