ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-SNMP-XS/XS.pm
(Generate patch)

Comparing Net-SNMP-XS/XS.pm (file contents):
Revision 1.1 by root, Wed Apr 8 10:30:46 2009 UTC vs.
Revision 1.10 by root, Wed May 5 20:46:09 2010 UTC

13This module tries to speed up Net::SNMP response packet decoding. 13This module tries to speed up Net::SNMP response packet decoding.
14 14
15It does this by overriding a few selected internal method by (almost) 15It does this by overriding a few selected internal method by (almost)
16equivalent XS methods. 16equivalent XS methods.
17 17
18This currently reduces decode time by some 70%. 18This currently reduces decode time by a factor of ten for typical bulk
19responses.
19 20
20There are currently the following limitations when using this module: 21There are currently the following limitations when using this module:
21 22
22=over 4 23=over 4
23 24
24=item leading dots are required for iods 25=item overriding internal functions might cause the module to
26malfunction with future versions of Net::SNMP
25 27
26=item error handling is currently broken (but parse errors will be detected) 28=item error messages will be simpler/different
27
28=item oid components are limited to unsigned 32 bit integers
29 29
30=item translation will be ignored (all values will be delivered "raw") 30=item translation will be ignored (all values will be delivered "raw")
31
32=item a moderately modern (>= C99) C compiler is required
33
34=item only tested with 5.10, no intentions to port to older perls
35
36=item duplicate OIDs are not supported
37
38=item REPORT PDUs are not supported
31 39
32=back 40=back
33 41
34=cut 42=cut
35 43
38use strict qw(vars subs); 46use strict qw(vars subs);
39no warnings; 47no warnings;
40 48
41use Guard; 49use Guard;
42 50
51use Net::SNMP ();
43use Net::SNMP::PDU (); 52use Net::SNMP::PDU ();
44use Net::SNMP::Message (); 53use Net::SNMP::Message ();
45use Net::SNMP::MessageProcessing (); 54use Net::SNMP::MessageProcessing ();
46 55
47our $VERSION; 56our $VERSION;
48our $old_prepare;
49 57
50BEGIN { 58BEGIN {
51 $VERSION = '0.01'; 59 $VERSION = '1.0';
52 60
53 $old_prepare = \&Net::SNMP::MessageProcessing::prepare_data_elements; 61 # this overrides many methods inside Net::SNMP and it's submodules
54
55 # this overrides many methods inside
56 require XSLoader; 62 require XSLoader;
57 XSLoader::load Net::SNMP::XS, $VERSION; 63 XSLoader::load Net::SNMP::XS, $VERSION;
58} 64}
59 65
60sub Net::SNMP::MessageProcessing::prepare_data_elements { 66package Net::SNMP::Message;
61 my ($self, $msg) = @_;
62 67
63 set_msg $msg, $msg->{_buffer}; 68Net::SNMP::XS::set_type INTEGER , \&_process_integer32;
64 scope_guard \&clr_msg; 69Net::SNMP::XS::set_type OCTET_STRING , \&_process_octet_string;
65 &$old_prepare 70Net::SNMP::XS::set_type NULL , \&_process_null;
66} 71Net::SNMP::XS::set_type OBJECT_IDENTIFIER, \&_process_object_identifier;
72Net::SNMP::XS::set_type SEQUENCE , \&_process_sequence;
73Net::SNMP::XS::set_type IPADDRESS , \&_process_ipaddress;
74Net::SNMP::XS::set_type COUNTER , \&_process_counter;
75Net::SNMP::XS::set_type GAUGE , \&_process_gauge;
76Net::SNMP::XS::set_type TIMETICKS , \&_process_timeticks;
77Net::SNMP::XS::set_type OPAQUE , \&_process_opaque;
78Net::SNMP::XS::set_type COUNTER64 , \&_process_counter64;
79Net::SNMP::XS::set_type NOSUCHOBJECT , \&_process_nosuchobject;
80Net::SNMP::XS::set_type NOSUCHINSTANCE , \&_process_nosuchinstance;
81Net::SNMP::XS::set_type ENDOFMIBVIEW , \&_process_endofmibview;
82Net::SNMP::XS::set_type GET_REQUEST , \&_process_get_request;
83Net::SNMP::XS::set_type GET_NEXT_REQUEST , \&_process_get_next_request;
84Net::SNMP::XS::set_type GET_RESPONSE , \&_process_get_response;
85Net::SNMP::XS::set_type SET_REQUEST , \&_process_set_request;
86Net::SNMP::XS::set_type TRAP , \&_process_trap;
87Net::SNMP::XS::set_type GET_BULK_REQUEST , \&_process_get_bulk_request;
88Net::SNMP::XS::set_type INFORM_REQUEST , \&_process_inform_request;
89Net::SNMP::XS::set_type SNMPV2_TRAP , \&_process_v2_trap;
90Net::SNMP::XS::set_type REPORT , \&_process_report;
67 91
68# almost direct copy from Net::SNMP::Message, as we cannot access $process_methods 92package Net::SNMP::PDU;
93
94# var_bind_list hardcodes oid_lex_sort. *sigh*
95# we copy it 1:1, except for using oid_lex_sort.
96
97sub var_bind_list
69{ 98{
70 package Net::SNMP::Message; 99 my ($this, $vbl, $types) = @_;
71 100
72 my @process_methods; 101 return if defined($this->{_error});
73 102
74 $process_methods [INTEGER ] = \&_process_integer32; 103 if (@_ > 1) {
75 $process_methods [OCTET_STRING ] = \&_process_octet_string; 104 # The VarBindList HASH is being updated from an external
76 $process_methods [NULL ] = \&_process_null; 105 # source. We need to update the VarBind names ARRAY to
77 $process_methods [OBJECT_IDENTIFIER] = \&_process_object_identifier; 106 # correspond to the new keys of the HASH. If the updated
78 $process_methods [SEQUENCE ] = \&_process_sequence; 107 # information is valid, we will use lexicographical ordering
79 $process_methods [IPADDRESS ] = \&_process_ipaddress; 108 # for the ARRAY entries since we do not have a PDU to use
80 $process_methods [COUNTER ] = \&_process_counter; 109 # to determine the ordering. The ASN.1 types HASH is also
81 $process_methods [GAUGE ] = \&_process_gauge; 110 # updated here if a cooresponding HASH is passed. We double
82 $process_methods [TIMETICKS ] = \&_process_timeticks; 111 # check the mapping by populating the hash with the keys of
83 $process_methods [OPAQUE ] = \&_process_opaque; 112 # the VarBindList HASH.
84 $process_methods [COUNTER64 ] = \&_process_counter64;
85 $process_methods [NOSUCHOBJECT ] = \&_process_nosuchobject;
86 $process_methods [NOSUCHINSTANCE ] = \&_process_nosuchinstance;
87 $process_methods [ENDOFMIBVIEW ] = \&_process_endofmibview;
88 $process_methods [GET_REQUEST ] = \&_process_get_request;
89 $process_methods [GET_NEXT_REQUEST ] = \&_process_get_next_request;
90 $process_methods [GET_RESPONSE ] = \&_process_get_response;
91 $process_methods [SET_REQUEST ] = \&_process_set_request;
92 $process_methods [TRAP ] = \&_process_trap;
93 $process_methods [GET_BULK_REQUEST ] = \&_process_get_bulk_request;
94 $process_methods [INFORM_REQUEST ] = \&_process_inform_request;
95 $process_methods [SNMPV2_TRAP ] = \&_process_v2_trap;
96 $process_methods [REPORT ] = \&_process_report;
97 113
98 # should be done in XS 114 if (!defined($vbl) || (ref($vbl) ne 'HASH')) {
99 sub process
100 {
101# my ($this, $expected, $found) = @_;
102 115
103 return $_[0]->_error if defined($_[0]->{_error}); 116 $this->{_var_bind_list} = undef;
104 return $_[0]->_error unless defined(my $type = _buffer_get($_[0], 1)); 117 $this->{_var_bind_names} = [];
118 $this->{_var_bind_types} = undef;
105 119
106 $type = unpack 'C', $type; 120 } else {
107 121
108 if ($process_methods[$type]) { 122 $this->{_var_bind_list} = $vbl;
109 if (@_ >= 2 && (defined $_[1]) && $type != $_[1]) { 123
110 return $_[0]->_error( 124 @{$this->{_var_bind_names}} = Net::SNMP::oid_lex_sort keys %$vbl;
111 'Expected %s, but found %s', asn1_itoa($_[1]), asn1_itoa($type) 125
112 ); 126 if (!defined($types) || (ref($types) ne 'HASH')) {
127 $types = {};
113 } 128 }
114 $_[2] = $type if (@_ == 3); 129
115 $process_methods[$type]->($_[0], $type); 130 map {
116 } else { 131 $this->{_var_bind_types}->{$_} =
117 $_[0]->_error('Unknown ASN.1 type [0x%02x]', $type); 132 exists($types->{$_}) ? $types->{$_} : undef;
133 } keys(%{$vbl});
134
118 } 135 }
136
119 } 137 }
138
139 $this->{_var_bind_list};
120} 140}
121 141
1221; 1421;
123 143
124=head1 AUTHOR 144=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines