/usr/lib/ruby/1.8/snmp/varbind.rb is in libsnmp-ruby1.8 1.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | #
# Copyright (c) 2004 David R. Halliday
# All rights reserved.
#
# This SNMP library is free software. Redistribution is permitted under the
# same terms and conditions as the standard Ruby distribution. See the
# COPYING file in the Ruby distribution for details.
#
require 'snmp/ber'
include SNMP::BER
module SNMP
class UnsupportedValueTag < RuntimeError; end
class InvalidIpAddress < ArgumentError; end
class VarBindList < Array
def self.decode(data)
list = VarBindList.new
varbind_data, remainder = decode_sequence(data)
while varbind_data != ""
varbind, varbind_data = VarBind.decode(varbind_data)
list << varbind
end
return list, remainder
end
def initialize(varbind_list=[])
super()
if varbind_list.respond_to? :to_str
self << ObjectId.new(varbind_list.to_str).to_varbind
elsif varbind_list.respond_to? :to_varbind
self << varbind_list.to_varbind
else
varbind_list.each do |item|
if item.respond_to? :to_str
self << ObjectId.new(item.to_str).to_varbind
else
self << item.to_varbind
end
end
end
end
def asn1_type
"VarBindList"
end
def encode
varbind_data = ""
self.each do |varbind|
varbind_data << varbind.encode
end
encode_sequence(varbind_data)
end
end
class Integer
include Comparable
def self.decode(value_data)
Integer.new(decode_integer_value(value_data))
end
def asn1_type
"INTEGER"
end
def initialize(value)
@value = value.to_i
end
def <=>(other)
@value <=> other.to_i
end
def coerce(other)
if other.kind_of? Fixnum
return [other, @value]
else
return [other.to_f, self.to_f]
end
end
def to_s
@value.to_s
end
def to_i
@value
end
def to_f
@value.to_f
end
def encode
encode_integer(@value)
end
def to_oid
raise RangeError, "@{value} cannot be an OID (must be >0)" if @value < 0
ObjectId.new([@value])
end
end
class Integer32 < Integer
def initialize(value)
super(value)
raise ArgumentError, "Out of range: #{value}" if value < -2147483648
raise ArgumentError, "Out of range: #{value}" if value > 2147483647
end
end
class OctetString < String
def self.decode(value_data)
OctetString.new(value_data)
end
def asn1_type
"OCTET STRING"
end
def encode
encode_octet_string(self)
end
def to_oid
oid = ObjectId.new
each_byte { |b| oid << b }
oid
end
end
class ObjectId < Array
include Comparable
def self.decode(value_data)
ObjectId.new(decode_object_id_value(value_data))
end
def asn1_type
"OBJECT IDENTIFIER"
end
##
# Create an object id. The input is expected to be either a string
# in the format "n.n.n.n.n.n" or an array of integers.
#
def initialize(id=[])
if id.nil?
raise ArgumentError
elsif id.respond_to? :to_str
super(make_integers(id.to_str.split(".")))
else
super(make_integers(id.to_ary))
end
rescue ArgumentError
raise ArgumentError, "#{id.inspect}:#{id.class} not a valid object ID"
end
def to_varbind
VarBind.new(self, Null)
end
def to_oid
self
end
def to_s
self.join('.')
end
def inspect
"[#{self.to_s}]"
end
def encode
encode_object_id(self)
end
##
# Returns true if this ObjectId is a subtree of the provided parent tree
# ObjectId. For example, "1.3.6.1.5" is a subtree of "1.3.6.1".
#
def subtree_of?(parent_tree)
parent_tree = make_object_id(parent_tree)
if parent_tree.length > self.length
false
else
parent_tree.each_index do |i|
return false if parent_tree[i] != self[i]
end
true
end
end
##
# Returns an index based on the difference between this ObjectId
# and the provided parent ObjectId.
#
# For example, ObjectId.new("1.3.6.1.5").index("1.3.6.1") returns an
# ObjectId of "5".
#
def index(parent_tree)
parent_tree = make_object_id(parent_tree)
if not subtree_of?(parent_tree)
raise ArgumentError, "#{self.to_s} not a subtree of #{parent_tree.to_s}"
elsif self.length == parent_tree.length
raise ArgumentError, "OIDs are the same"
else
ObjectId.new(self[parent_tree.length..-1])
end
end
private
def make_integers(list)
list.collect{|n| Integer(n)}
end
def make_object_id(oid)
oid.kind_of?(ObjectId) ? oid : ObjectId.new(oid)
end
end
class IpAddress
class << self
def decode(value_data)
IpAddress.new(value_data)
end
end
def asn1_type
"IpAddress"
end
##
# Create an IpAddress object. The constructor accepts either a raw
# four-octet string or a formatted string of integers separated by dots
# (i.e. "10.1.2.3").
#
def initialize(value_data)
ip = value_data.to_str
if ip.length > 4
ip = parse_string(ip)
elsif ip.length != 4
raise InvalidIpAddress, "Expected 4 octets or formatted string, got #{value_data.inspect}"
end
@value = ip
end
##
# Returns a raw four-octet string representing this IpAddress.
#
def to_str
@value.dup
end
##
# Returns a formatted, dot-separated string representing this IpAddress.
#
def to_s
octets = []
@value.each_byte { |b| octets << b.to_s }
octets.join('.')
end
def to_oid
oid = ObjectId.new
@value.each_byte { |b| oid << b }
oid
end
def ==(other)
if other.respond_to? :to_str
return @value.eql?(other.to_str)
else
return false
end
end
def eql?(other)
self == other
end
def hash
@value.hash
end
def encode
encode_tlv(IpAddress_TAG, @value)
end
private
def parse_string(ip_string)
parts = ip_string.split(".")
raise InvalidIpAddress, ip_string.inspect if parts.length != 4
value_data = ""
parts.each do |s|
octet = s.to_i
raise InvalidIpAddress, ip_string.inspect if octet > 255
raise InvalidIpAddress, ip_string.inspect if octet < 0
value_data << octet.chr
end
value_data
end
end
class UnsignedInteger < Integer
def initialize(value)
super(value)
raise ArgumentError, "Negative integer invalid: #{value}" if value < 0
raise ArgumentError, "Out of range: #{value}" if value > 4294967295
end
def self.decode(value_data)
self.new(decode_uinteger_value(value_data))
end
end
class Counter32 < UnsignedInteger
def asn1_type
"Counter32"
end
def encode
encode_tagged_integer(Counter32_TAG, @value)
end
end
class Gauge32 < UnsignedInteger
def asn1_type
"Gauge32"
end
def encode
encode_tagged_integer(Gauge32_TAG, @value)
end
end
class Unsigned32 < UnsignedInteger
def asn1_type
"Unsigned32"
end
def encode
encode_tagged_integer(Unsigned32_TAG, @value)
end
end
class TimeTicks < UnsignedInteger
def asn1_type
"TimeTicks"
end
def encode
encode_tagged_integer(TimeTicks_TAG, @value)
end
def to_s
days, remainder = @value.divmod(8640000)
hours, remainder = remainder.divmod(360000)
minutes, remainder = remainder.divmod(6000)
seconds, hundredths = remainder.divmod(100)
case
when days < 1
sprintf('%02d:%02d:%02d.%02d',
hours, minutes, seconds, hundredths)
when days == 1
sprintf('1 day, %02d:%02d:%02d.%02d',
hours, minutes, seconds, hundredths)
when days > 1
sprintf('%d days, %02d:%02d:%02d.%02d',
days, hours, minutes, seconds, hundredths)
end
end
end
class Opaque < OctetString
def self.decode(value_data)
Opaque.new(value_data)
end
def asn1_type
"Opaque"
end
def encode
encode_tlv(Opaque_TAG, self)
end
end
class Counter64 < Integer
def self.decode(value_data)
Counter64.new(decode_integer_value(value_data))
end
def asn1_type
"Counter64"
end
def initialize(value)
super(value)
raise ArgumentError, "Negative integer invalid: #{value}" if value < 0
raise ArgumentError, "Out of range: #{value}" if value > 18446744073709551615
end
def encode
encode_tagged_integer(Counter64_TAG, @value)
end
end
class Null
class << self
def decode(value_data)
Null
end
def encode
encode_null
end
def asn1_type
'Null'
end
def to_s
asn1_type
end
end
end
class NoSuchObject
class << self
def decode(value_data)
NoSuchObject
end
def encode
encode_exception(NoSuchObject_TAG)
end
def asn1_type
'noSuchObject'
end
def to_s
asn1_type
end
end
end
class NoSuchInstance
class << self
def decode(value_data)
NoSuchInstance
end
def encode
encode_exception(NoSuchInstance_TAG)
end
def asn1_type
'noSuchInstance'
end
def to_s
asn1_type
end
end
end
class EndOfMibView
class << self
def decode(value_data)
EndOfMibView
end
def encode
encode_exception(EndOfMibView_TAG)
end
def asn1_type
'endOfMibView'
end
def to_s
asn1_type
end
end
end
class VarBind
attr_accessor :name
attr_accessor :value
alias :oid :name
class << self
def decode(data)
varbind_data, remaining_varbind_data = decode_sequence(data)
name, remainder = decode_object_id(varbind_data)
value, remainder = decode_value(remainder)
assert_no_remainder(remainder)
return VarBind.new(name, value), remaining_varbind_data
end
ValueDecoderMap = {
INTEGER_TAG => Integer,
OCTET_STRING_TAG => OctetString,
NULL_TAG => Null,
OBJECT_IDENTIFIER_TAG => ObjectId,
IpAddress_TAG => IpAddress,
Counter32_TAG => Counter32,
Gauge32_TAG => Gauge32,
# note Gauge32 tag same as Unsigned32
TimeTicks_TAG => TimeTicks,
Opaque_TAG => Opaque,
Counter64_TAG => Counter64,
NoSuchObject_TAG => NoSuchObject,
NoSuchInstance_TAG => NoSuchInstance,
EndOfMibView_TAG => EndOfMibView
}
def decode_value(data)
value_tag, value_data, remainder = decode_tlv(data)
decoder_class = ValueDecoderMap[value_tag]
if decoder_class
value = decoder_class.decode(value_data)
else
raise UnsupportedValueTag, value_tag.to_s
end
return value, remainder
end
end
def initialize(name, value=Null)
if name.kind_of? ObjectId
@name = name
else
@name = ObjectName.new(name)
end
@value = value
end
def asn1_type
"VarBind"
end
def to_varbind
self
end
def to_s
"[name=#{@name.to_s}, value=#{@value.to_s} (#{@value.asn1_type})]"
end
def each
yield self
end
def encode
data = encode_object_id(@name) << value.encode
encode_sequence(data)
end
end
class ObjectName < ObjectId
def asn1_type
"ObjectName"
end
end
end
|