Method: NETSNMP::Varbind#convert_val

Defined in:
lib/netsnmp/varbind.rb

#convert_val(asn_value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/netsnmp/varbind.rb', line 50

def convert_val(asn_value)
  case asn_value
  when OpenSSL::ASN1::OctetString
    val = asn_value.value

    # it's kind of common in snmp, some stuff can't be converted,
    # like Hexa Strings. Parse them into a readable format a la netsnmp
    # https://github.com/net-snmp/net-snmp/blob/ed90aaaaea0d9cc6c5c5533f1863bae598d3b820/snmplib/mib.c#L650
    is_hex_string = val.each_char.any? { |c| !c.match?(/[[:print:]]/) && !c.match?(/[[:space:]]/) }

    val = HexString.new(val) if is_hex_string
    val
  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