Debian Codegraph @

perl/OID/OID.pm

raw
1 package NetSNMP::OID;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 require Exporter;
8 require DynaLoader;
9 use AutoLoader;
10
11 sub compare($$);
12
13 use overload
14 '<=>' => \&compare,
15 'cmp' => \&oidstrcmp,
16 '""' => \&quote_oid,
17 '+' => \&add,
18 ;
19
20 use SNMP;
21
22 sub quote_oid {
23 return $_[0]->{'oidptr'}->to_string();
24 }
25
26 sub length {
27 return $_[0]->{'oidptr'}->length();
28 }
29
30 sub get_indexes {
31 return $_[0]->{'oidptr'}->get_indexes();
32 }
33
34 sub append {
35 my $this = shift;
36 my $str = shift;
37
38 if (ref($str) eq 'NetSNMP::OID') {
39 return $this->{'oidptr'}->append_oid($str->{'oidptr'});
40 }
41 $str = "." . $str if ($str =~ /^\d+/);
42 if ($str =~ /^[.\d]+/) {
43 # oid segment
44 return $this->{'oidptr'}->append($str);
45 }
46 if ($str =~ /^\"(.*)\"$/) {
47 # string index
48 my $newstr = "." . CORE::length($1);
49 map { $newstr .= ".$_" } unpack("c*",$1);
50 return $this->{'oidptr'}->append($newstr);
51 }
52 if ($str =~ /^\'(.*)\'$/) {
53 # string index, implied
54 my $newstr;
55 map { $newstr .= ".$_" } unpack("c*",$1);
56 return $this->{'oidptr'}->append($newstr);
57 }
58 # Just Parse it...
59 return $this->{'oidptr'}->append($str);
60 }
61
62 sub add {
63 my $this = shift;
64 my $str = shift;
65 my ($newoid, %newhash);
66 $newoid = \%newhash;
67 $newoid->{'oidptr'} = $this->{'oidptr'}->clone();
68 bless($newoid, ref($this));
69 $newoid->append($str);
70 return $newoid;
71 }
72
73 use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $AUTOLOAD);
74
75 @ISA = qw(Exporter DynaLoader);
76
77 # Items to export into callers namespace by default. Note: do not export
78 # names by default without a very good reason. Use EXPORT_OK instead.
79 # Do not simply export all your public functions/methods/constants.
80
81 # This allows declaration use NetSNMP::OID ':all';
82 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
83 # will save memory.
84 %EXPORT_TAGS = ( 'all' => [ qw(
85 snmp_oid_compare
86 compare
87 ) ] );
88
89 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
90
91 @EXPORT = qw(
92 snmp_oid_compare
93 compare
94 );
95 $VERSION = '5.09052';
96
97 sub new {
98 my $type = shift;
99 my $arg = shift;
100 if (!$arg) {
101 $arg = $type;
102 $type = "NetSNMP::OID";
103 }
104 SNMP::init_snmp("perl");
105 my $ptr = NetSNMP::OID::newptr($arg);
106 if ($ptr) {
107 return newwithptr($type, $ptr);
108 }
109 }
110
111 sub newwithptr {
112 my $type = shift;
113 my $ptr = shift;
114 my $self = {};
115 if (!$ptr) {
116 $ptr = $type;
117 $type = "NetSNMP::OID";
118 }
119 SNMP::init_snmp("perl");
120 $self->{'oidptr'} = $ptr;
121 bless($self, $type);
122 return $self;
123 }
124
125 sub snmp_oid_compare($$) {
126 my ($oid1, $oid2) = @_;
127 return _snmp_oid_compare($oid1->{oidptr}, $oid2->{oidptr});
128 }
129
130 sub compare($$) {
131 my ($v1, $v2) = @_;
132 snmp_oid_compare($v1, $v2);
133 }
134
135 sub oidstrcmp {
136 my ($v1, $v2) = @_;
137 $v1->{'oidptr'}->to_string cmp $v2->{'oidptr'}->to_string;
138 }
139
140 sub to_array($) {
141 my $self = shift;
142 return $self->{oidptr}->to_array();
143 }
144
145 sub DESTROY {}
146
147 sub AUTOLOAD {
148 # This AUTOLOAD is used to 'autoload' constants from the constant()
149 # XS function. If a constant is not found then control is passed
150 # to the AUTOLOAD in AutoLoader.
151
152 my $constname;
153 ($constname = $AUTOLOAD) =~ s/.*:://;
154 croak "& not defined" if $constname eq 'constant';
155 my $val;
156 ($!, $val) = constant($constname);
157 if ($! != 0) {
158 if ($! =~ /Invalid/ || $!{EINVAL}) {
159 $AutoLoader::AUTOLOAD = $AUTOLOAD;
160 goto &AutoLoader::AUTOLOAD;
161 }
162 else {
163 croak "Your vendor has not defined NetSNMP::OID macro $constname";
164 }
165 }
166 {
167 no strict 'refs';
168 # Fixed between 5.005_53 and 5.005_61
169 if ($] >= 5.00561) {
170 *$AUTOLOAD = sub () { $val };
171 }
172 else {
173 *$AUTOLOAD = sub { $val };
174 }
175 }
176 goto &$AUTOLOAD;
177 }
178
179 bootstrap NetSNMP::OID $VERSION;
180
181 # Preloaded methods go here.
182
183 # Autoload methods go after =cut, and are processed by the autosplit program.
184
185 1;
186 __END__
187
188 =head1 NAME
189
190 NetSNMP::OID - Perl extension for manipulating OIDs
191
192 =head1 SYNOPSIS
193
194 use NetSNMP::OID;
195
196 my $oid = new NetSNMP::OID('sysContact.0');
197
198 if ($oid < new NetSNMP::OID('ifTable')) {
199 do_something();
200 }
201
202 my @numarray = $oid->to_array();
203
204 # appending oids
205 $oid = new NetSNMP::OID('.1.3');
206 $oid += ".6.1";
207 # -> .1.3.6.1
208
209 # appending index strings
210
211 $oid2 = $oid + "\"wes\"";
212 # -> .1.3.6.1.3.119.101.115
213
214 $oid3 = $oid + "\'wes\'";
215 # -> .1.3.6.1.119.101.115
216
217 $len = $oid3->length();
218 # -> 7
219
220 # retrieving indexes from an oid:
221 $arrayref = $tableoid->get_indexes()
222
223 =head1 DESCRIPTION
224
225 The NetSNMP::OID module is a simple wrapper around a C-based net-snmp
226 oid (which is an array of unsigned integers). The OID is internally
227 stored as a C array of integers for speed purposes when doing
228 comparisons, etc.
229
230 The standard logical expression operators (<, >, ==, ...) are
231 overloaded such that lexographical comparisons may be done with them.
232
233 The + operator is overloaded to allow you to append stuff on to the
234 end of a OID, like index segments of a table, for example.
235
236 =head2 EXPORT
237
238 int snmp_oid_compare(oid1, oid2)
239 int compare(oid1, oid2)
240
241 =head1 AUTHOR
242
243 Wes Hardaker, E<lt>[email protected]<gt>
244
245 =head1 SEE ALSO
246
247 L<SNMP>, L<perl>.
248
249 =head1 Copyright
250
251 Copyright (c) 2002 Networks Associates Technology, Inc. All
252 Rights Reserved. This program is free software; you can
253 redistribute it and/or modify it under the same terms as Perl
254 itself.
255
256 =cut