Class: SNMP::VarBind

Inherits:
Object
  • Object
show all
Extended by:
BER::Decode
Includes:
BER::Encode
Defined in:
lib/snmp/varbind.rb

Constant Summary collapse

ValueDecoderMap =
{
  BER::INTEGER_TAG           => Integer,
  BER::OCTET_STRING_TAG      => OctetString,
  BER::NULL_TAG              => Null,
  BER::OBJECT_IDENTIFIER_TAG => ObjectId,
  BER::IpAddress_TAG         => IpAddress,
  BER::Counter32_TAG         => Counter32,
  BER::Gauge32_TAG           => Gauge32,
  # note Gauge32 tag same as Unsigned32
  BER::TimeTicks_TAG         => TimeTicks,
  BER::Opaque_TAG            => Opaque,
  BER::Counter64_TAG         => Counter64,
  BER::NoSuchObject_TAG      => NoSuchObject,
  BER::NoSuchInstance_TAG    => NoSuchInstance,
  BER::EndOfMibView_TAG      => EndOfMibView
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BER::Decode

assert_no_remainder, build_integer, decode_integer, decode_integer_value, decode_ip_address, decode_object_id, decode_object_id_value, decode_octet_string, decode_sequence, decode_timeticks, decode_tlv, decode_uinteger_value

Methods included from BER::Encode

#encode_exception, #encode_integer, #encode_length, #encode_null, #encode_object_id, #encode_octet_string, #encode_sequence, #encode_tagged_integer, #encode_tlv, #integer_to_octets

Constructor Details

#initialize(name, value = Null) ⇒ VarBind

Returns a new instance of VarBind.


598
599
600
601
602
603
604
605
# File 'lib/snmp/varbind.rb', line 598

def initialize(name, value=Null)
  if name.kind_of? ObjectId
    @name = name
  else
    @name = ObjectName.new(name)
  end
  @value = value
end

Instance Attribute Details

#nameObject Also known as: oid

Returns the value of attribute name


555
556
557
# File 'lib/snmp/varbind.rb', line 555

def name
  @name
end

#valueObject

Returns the value of attribute value


556
557
558
# File 'lib/snmp/varbind.rb', line 556

def value
  @value
end

Class Method Details

.decode(data, mib = nil) ⇒ Object


561
562
563
564
565
566
567
# File 'lib/snmp/varbind.rb', line 561

def decode(data, mib=nil)
  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).with_mib(mib), remaining_varbind_data
end

.decode_value(data) ⇒ Object


586
587
588
589
590
591
592
593
594
595
# File 'lib/snmp/varbind.rb', line 586

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

Instance Method Details

#asn1_typeObject


616
617
618
# File 'lib/snmp/varbind.rb', line 616

def asn1_type
  "VarBind"
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (SNMP::VarBind)

    the object that the method was called on


628
629
630
# File 'lib/snmp/varbind.rb', line 628

def each
  yield self
end

#encodeObject


632
633
634
635
# File 'lib/snmp/varbind.rb', line 632

def encode
  data = encode_object_id(@name) << value.encode
  encode_sequence(data)
end

#to_sObject


624
625
626
# File 'lib/snmp/varbind.rb', line 624

def to_s
  "[name=#{@name.to_s}, value=#{@value.to_s} (#{@value.asn1_type})]"
end

#to_varbindObject


620
621
622
# File 'lib/snmp/varbind.rb', line 620

def to_varbind
  self
end

#with_mib(mib) ⇒ Object

Adds MIB information to this varbind for use with to_s.


610
611
612
613
614
# File 'lib/snmp/varbind.rb', line 610

def with_mib(mib)
  @name.with_mib(mib) if @name
  @value.with_mib(mib) if @value.respond_to? :with_mib
  self
end