Class: NETSNMP::PDU
- Inherits:
-
Object
- Object
- NETSNMP::PDU
- 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
Constant Summary collapse
- MAXREQUESTID =
0x7fffffff
Instance Attribute Summary collapse
-
#community ⇒ Object
readonly
Returns the value of attribute community.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#varbinds ⇒ Object
readonly
Returns the value of attribute varbinds.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.build(type, **args) ⇒ Object
factory method that abstracts initialization of the pdu types that the library supports.
- .decode(der) ⇒ Object
Instance Method Summary collapse
-
#add_varbind(oid:, **options) ⇒ Object
(also: #<<)
Adds a request varbind to the pdu.
-
#initialize(type:, headers:, request_id: SecureRandom.random_number(MAXREQUESTID), error_status: 0, error_index: 0, varbinds: []) ⇒ PDU
constructor
A new instance of PDU.
- #to_asn ⇒ Object
- #to_der ⇒ Object
- #to_hex ⇒ Object
Constructor Details
#initialize(type:, headers:, request_id: SecureRandom.random_number(MAXREQUESTID), error_status: 0, error_index: 0, varbinds: []) ⇒ PDU
Returns a new instance of PDU.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/netsnmp/pdu.rb', line 71 def initialize(type:, headers:, request_id: SecureRandom.random_number(MAXREQUESTID), error_status: 0, error_index: 0, varbinds: []) @version, @community = headers @version = @version.to_i @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
#community ⇒ Object (readonly)
Returns the value of attribute community.
69 70 71 |
# File 'lib/netsnmp/pdu.rb', line 69 def community @community end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
69 70 71 |
# File 'lib/netsnmp/pdu.rb', line 69 def request_id @request_id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
67 68 69 |
# File 'lib/netsnmp/pdu.rb', line 67 def type @type end |
#varbinds ⇒ Object (readonly)
Returns the value of attribute varbinds.
67 68 69 |
# File 'lib/netsnmp/pdu.rb', line 67 def varbinds @varbinds end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
69 70 71 |
# File 'lib/netsnmp/pdu.rb', line 69 def version @version end |
Class Method Details
.build(type, **args) ⇒ Object
factory method that abstracts initialization of the pdu types that the library supports.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/netsnmp/pdu.rb', line 51 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) ⇒ 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 42 43 44 45 |
# File 'lib/netsnmp/pdu.rb', line 14 def decode(der) asn_tree = case der when String OpenSSL::ASN1.decode(der) when OpenSSL::ASN1::ASN1Data der else raise "#{der}: unexpected data" end *headers, request = asn_tree.value version, community = headers.map(&:value) type = request.tag *request_headers, varbinds = request.value request_id, error_status, error_index = request_headers.map(&:value).map(&: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, headers: [version, community], error_status: error_status, error_index: error_index, request_id: request_id, varbinds: varbs) end |
Instance Method Details
#add_varbind(oid:, **options) ⇒ Object Also known as: <<
Adds a request varbind to the pdu
102 103 104 |
# File 'lib/netsnmp/pdu.rb', line 102 def add_varbind(oid:, **) @varbinds << Varbind.new(oid, **) end |
#to_asn ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/netsnmp/pdu.rb', line 107 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_der ⇒ Object
89 90 91 |
# File 'lib/netsnmp/pdu.rb', line 89 def to_der to_asn.to_der end |
#to_hex ⇒ Object
93 94 95 |
# File 'lib/netsnmp/pdu.rb', line 93 def to_hex to_asn.to_hex end |