ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-SNMP-XS/XS.pm
Revision: 1.1
Committed: Wed Apr 8 10:30:46 2009 UTC (15 years, 2 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Net::SNMP::XS - speed up Net::SNMP by decoding in XS, with limitations
4    
5     =head1 SYNOPSIS
6    
7     use Net::SNMP::XS;
8    
9     # loading it is enough, there are no public symbols
10    
11     =head1 DESCRIPTION
12    
13     This module tries to speed up Net::SNMP response packet decoding.
14    
15     It does this by overriding a few selected internal method by (almost)
16     equivalent XS methods.
17    
18     This currently reduces decode time by some 70%.
19    
20     There are currently the following limitations when using this module:
21    
22     =over 4
23    
24     =item leading dots are required for iods
25    
26     =item error handling is currently broken (but parse errors will be detected)
27    
28     =item oid components are limited to unsigned 32 bit integers
29    
30     =item translation will be ignored (all values will be delivered "raw")
31    
32     =back
33    
34     =cut
35    
36     package Net::SNMP::XS;
37    
38     use strict qw(vars subs);
39     no warnings;
40    
41     use Guard;
42    
43     use Net::SNMP::PDU ();
44     use Net::SNMP::Message ();
45     use Net::SNMP::MessageProcessing ();
46    
47     our $VERSION;
48     our $old_prepare;
49    
50     BEGIN {
51     $VERSION = '0.01';
52    
53     $old_prepare = \&Net::SNMP::MessageProcessing::prepare_data_elements;
54    
55     # this overrides many methods inside
56     require XSLoader;
57     XSLoader::load Net::SNMP::XS, $VERSION;
58     }
59    
60     sub Net::SNMP::MessageProcessing::prepare_data_elements {
61     my ($self, $msg) = @_;
62    
63     set_msg $msg, $msg->{_buffer};
64     scope_guard \&clr_msg;
65     &$old_prepare
66     }
67    
68     # almost direct copy from Net::SNMP::Message, as we cannot access $process_methods
69     {
70     package Net::SNMP::Message;
71    
72     my @process_methods;
73    
74     $process_methods [INTEGER ] = \&_process_integer32;
75     $process_methods [OCTET_STRING ] = \&_process_octet_string;
76     $process_methods [NULL ] = \&_process_null;
77     $process_methods [OBJECT_IDENTIFIER] = \&_process_object_identifier;
78     $process_methods [SEQUENCE ] = \&_process_sequence;
79     $process_methods [IPADDRESS ] = \&_process_ipaddress;
80     $process_methods [COUNTER ] = \&_process_counter;
81     $process_methods [GAUGE ] = \&_process_gauge;
82     $process_methods [TIMETICKS ] = \&_process_timeticks;
83     $process_methods [OPAQUE ] = \&_process_opaque;
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    
98     # should be done in XS
99     sub process
100     {
101     # my ($this, $expected, $found) = @_;
102    
103     return $_[0]->_error if defined($_[0]->{_error});
104     return $_[0]->_error unless defined(my $type = _buffer_get($_[0], 1));
105    
106     $type = unpack 'C', $type;
107    
108     if ($process_methods[$type]) {
109     if (@_ >= 2 && (defined $_[1]) && $type != $_[1]) {
110     return $_[0]->_error(
111     'Expected %s, but found %s', asn1_itoa($_[1]), asn1_itoa($type)
112     );
113     }
114     $_[2] = $type if (@_ == 3);
115     $process_methods[$type]->($_[0], $type);
116     } else {
117     $_[0]->_error('Unknown ASN.1 type [0x%02x]', $type);
118     }
119     }
120     }
121    
122     1;
123    
124     =head1 AUTHOR
125    
126     Marc Lehmann <schmorp@schmorp.de>
127     http://home.schmorp.de/
128    
129     =cut
130