Class: NETSNMP::PDU

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

Overview

Abstracts the PDU base structure into a ruby object. It gives access to its varbinds.

Direct Known Subclasses

ScopedPDU

Constant Summary collapse

MAXREQUESTID =
0x7fffffff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, version:, community:, request_id: SecureRandom.random_number(MAXREQUESTID), error_status: 0, error_index: 0, varbinds: []) ⇒ PDU

Returns a new instance of PDU.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/netsnmp/pdu.rb', line 65

def initialize(type:,
               version:,
               community:,
               request_id: SecureRandom.random_number(MAXREQUESTID),
               error_status: 0,
               error_index: 0,
               varbinds: [])
  @version = version.to_i
  @community = community
  @error_status = error_status
  @error_index = error_index
  @type = type
  @varbinds = []
  varbinds.each do |varbind|
    add_varbind(**varbind)
  end
  @request_id = request_id
  check_error_status(@error_status)
end

Instance Attribute Details

#communityObject (readonly)

Returns the value of attribute community.



63
64
65
# File 'lib/netsnmp/pdu.rb', line 63

def community
  @community
end

#request_idObject (readonly)

Returns the value of attribute request_id.



63
64
65
# File 'lib/netsnmp/pdu.rb', line 63

def request_id
  @request_id
end

#typeObject (readonly)

Returns the value of attribute type.



63
64
65
# File 'lib/netsnmp/pdu.rb', line 63

def type
  @type
end

#varbindsObject (readonly)

Returns the value of attribute varbinds.



63
64
65
# File 'lib/netsnmp/pdu.rb', line 63

def varbinds
  @varbinds
end

#versionObject (readonly)

Returns the value of attribute version.



63
64
65
# File 'lib/netsnmp/pdu.rb', line 63

def version
  @version
end

Class Method Details

.build(type, **args) ⇒ Object

factory method that abstracts initialization of the pdu types that the library supports.

Parameters:

  • type (Symbol)

    the type of pdu structure to build



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

def build(type, **args)
  typ = case type
        when :get       then 0
        when :getnext   then 1
        #          when :getbulk   then 5
        when :set       then 3
        when :inform    then 6
        when :trap      then 7
        when :response  then 2
        when :report    then 8
        else raise Error, "#{type} is not supported as type"
        end
  new(type: typ, **args)
end

.decode(der, **args) ⇒ Object



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
# File 'lib/netsnmp/pdu.rb', line 14

def decode(der, **args)
  der = OpenSSL::ASN1.decode(der) if der.is_a?(String)

  *headers, request = der.value

  version, community = headers.map(&:value)

  type = request.tag

  *request_headers, varbinds = request.value

  request_id, error_status, error_index = request_headers.map { |x| x.value.to_i }

  varbs = varbinds.value.map do |varbind|
    oid_asn, val_asn = varbind.value
    oid = oid_asn.value
    { oid: oid, value: val_asn }
  end

  new(type: type,
      version: version.to_i,
      community: community,
      error_status: error_status.to_i,
      error_index: error_index.to_i,
      request_id: request_id.to_i,
      varbinds: varbs,
      **args)
end

Instance Method Details

#add_varbind(oid:, **options) ⇒ Object Also known as: <<

Adds a request varbind to the pdu

Parameters:

  • oid (OID)

    a valid oid

  • options (Hash)

    additional request varbind options

Options Hash (**options):

  • :value (Object)

    the value for the oid



98
99
100
# File 'lib/netsnmp/pdu.rb', line 98

def add_varbind(oid:, **options)
  @varbinds << Varbind.new(oid, **options)
end

#to_asnObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/netsnmp/pdu.rb', line 103

def to_asn
  request_id_asn = OpenSSL::ASN1::Integer.new(@request_id).with_label(:request_id)
  error_asn = OpenSSL::ASN1::Integer.new(@error_status).with_label(:error)
  error_index_asn = OpenSSL::ASN1::Integer.new(@error_index).with_label(:error_index)

  varbind_asns = OpenSSL::ASN1::Sequence.new(@varbinds.map(&:to_asn)).with_label(:varbinds)

  request_asn = OpenSSL::ASN1::ASN1Data.new([request_id_asn,
                                             error_asn, error_index_asn,
                                             varbind_asns], @type,
                                            :CONTEXT_SPECIFIC).with_label(:request)

  OpenSSL::ASN1::Sequence.new([*encode_headers_asn, request_asn]).with_label(:pdu)
end

#to_derObject



85
86
87
# File 'lib/netsnmp/pdu.rb', line 85

def to_der
  to_asn.to_der
end

#to_hexObject



89
90
91
# File 'lib/netsnmp/pdu.rb', line 89

def to_hex
  to_asn.to_hex
end