SNMP GetRequest parsing
This commit is contained in:
parent
79e59cf3aa
commit
c49e1f0c94
2 changed files with 76 additions and 2 deletions
|
@ -51,5 +51,57 @@ module Net
|
|||
|
||||
end
|
||||
|
||||
class SnmpPdu
|
||||
class Error < Exception; end
|
||||
|
||||
attr_reader :version, :community, :pdu_type, :request_id, :variables
|
||||
|
||||
#--
|
||||
# TODO, improve the error-trapping.
|
||||
# We want to wrap up Ruby errors like array-ranges, which can appear if we get bad data.
|
||||
# We should probably do the whole parse under a catch-all block.
|
||||
def initialize ber_object
|
||||
begin
|
||||
parse_ber_object ber_object
|
||||
rescue RuntimeError
|
||||
# Wrap any basic parsing error so it becomes a PDU-format error
|
||||
raise Error.new( "snmp-pdu format error" )
|
||||
end
|
||||
end
|
||||
|
||||
def parse_ber_object ber_object
|
||||
@version = ber_object[0].to_i
|
||||
unless [0,2].include?(@version)
|
||||
raise Error.new("unknown snmp-version: #{@version}")
|
||||
end
|
||||
|
||||
@community = ber_object[1].to_s
|
||||
|
||||
data = ber_object[2]
|
||||
app_tag = data.ber_identifier & 31
|
||||
case app_tag
|
||||
when 0
|
||||
@pdu_type = :get_request
|
||||
parse_get_request data
|
||||
else
|
||||
raise Error.new( "unknown snmp-pdu type: #{app_tag}" )
|
||||
end
|
||||
end
|
||||
|
||||
#--
|
||||
# Defined in RFC1157, pgh 4.1.2.
|
||||
def parse_get_request data
|
||||
@request_id = data[0].to_i
|
||||
# data[1] is error-status, always 0.
|
||||
# data[2] is error-index, always 0.
|
||||
@variables = data[3].map {|v|
|
||||
# A variable-binding, of which there may be several,
|
||||
# consists of an OID and a BER null.
|
||||
# We're ignoring the null, we might want to verify it instead.
|
||||
v[0]
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue