Class: NETSNMP::Varbind

Inherits:
Object
  • Object
show all
Defined in:
lib/netsnmp/varbind.rb

Overview

Abstracts the PDU variable structure into a ruby object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid, value: nil, type: nil) ⇒ Varbind

Returns a new instance of Varbind.



9
10
11
12
13
# File 'lib/netsnmp/varbind.rb', line 9

def initialize(oid , value: nil, type: nil)
  @oid = OID.build(oid)
  @type = type
  @value = convert_val(value) if value
end

Instance Attribute Details

#oidObject (readonly)

Returns the value of attribute oid.



7
8
9
# File 'lib/netsnmp/varbind.rb', line 7

def oid
  @oid
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/netsnmp/varbind.rb', line 7

def value
  @value
end

Instance Method Details

#convert_application_asn(asn) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/netsnmp/varbind.rb', line 77

def convert_application_asn(asn)
  case asn.tag
    when 0 # IP Address
      IPAddr.new_ntoh(asn.value)
    when 1 # ASN counter 32
      asn.value.unpack("n*")[0] || 0
    when 2 # gauge
    when 3 # timeticks
      asn.value.unpack("N*")[0] || 0
    when 4 # opaque
    when 5 # NSAP
    when 6 # ASN Counter 64
    when 7 # ASN UInteger
  end
end

#convert_to_asn(typ, value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/netsnmp/varbind.rb', line 63

def convert_to_asn(typ, value)
  return [typ, value] unless typ.is_a?(Symbol)
  case typ
    when :ipaddress then 0
    when :counter32 then 1
    when :gauge then 2
    when :timetick then [3, [ value].pack("N") ]
    when :opaque then 4
    when :nsap then 5
    when :counter64 then 6
    when :uinteger then 7
  end
end

#convert_val(asn_value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/netsnmp/varbind.rb', line 48

def convert_val(asn_value)
  case asn_value
  when OpenSSL::ASN1::Primitive
    val = asn_value.value
    val = val.to_i if val.is_a?(OpenSSL::BN)
    val
  when OpenSSL::ASN1::ASN1Data
    # application data
    convert_application_asn(asn_value)
  when OpenSSL::BN
  else
   asn_value # assume it's already primitive
  end 
end

#to_asnObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/netsnmp/varbind.rb', line 23

def to_asn
  asn_oid = OID.to_asn(@oid)
  asn_val = if @type
    asn_type, asn_val = convert_to_asn(@type, @value)
    OpenSSL::ASN1::ASN1Data.new(asn_val, asn_type, :APPLICATION)
  else
    case @value
    when String
      OpenSSL::ASN1::OctetString.new(@value)
    when Integer
      OpenSSL::ASN1::Integer.new(@value)
    when true, false
      OpenSSL::ASN1::Boolean.new(@value)
    when nil
      OpenSSL::ASN1::Null.new(nil)
    when IPAddr
      OpenSSL::ASN1::ASN1Data.new(@value.hton, 0, :APPLICATION)
    else
      raise Error, "#{@value}: unsupported varbind type"
    end
  end
  OpenSSL::ASN1::Sequence.new( [asn_oid, asn_val] )
end

#to_derObject



19
20
21
# File 'lib/netsnmp/varbind.rb', line 19

def to_der
  to_asn.to_der
end

#to_sObject



15
16
17
# File 'lib/netsnmp/varbind.rb', line 15

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} @oid=#{@oid} @value=#{@value}>"
end