class SNMP::Message

Attributes

community[R]
pdu[R]
version[R]

Public Class Methods

new(version, community, pdu) click to toggle source
# File lib/snmp/pdu.rb, line 100
def initialize(version, community, pdu)
  @version = version
  @community = community
  @pdu = pdu
end

Public Instance Methods

decode(data, mib=nil) click to toggle source
# File lib/snmp/pdu.rb, line 48
def decode(data, mib=nil)
  message_data, remainder = decode_sequence(data)
  assert_no_remainder(remainder)
  version, remainder = decode_version(message_data)
  community, remainder = decode_octet_string(remainder)
  pdu, remainder = decode_pdu(version, remainder, mib)
  assert_no_remainder(remainder)
  Message.new(version, community, pdu)
end
decode_pdu(version, data, mib=nil) click to toggle source
# File lib/snmp/pdu.rb, line 70
def decode_pdu(version, data, mib=nil)
  pdu_tag, pdu_data, remainder = decode_tlv(data)
  case pdu_tag
  when GetRequest_PDU_TAG
    pdu = PDU.decode(GetRequest, pdu_data, mib)
  when GetNextRequest_PDU_TAG
    pdu = PDU.decode(GetNextRequest, pdu_data, mib)
  when Response_PDU_TAG
    pdu = PDU.decode(Response, pdu_data, mib)
  when SetRequest_PDU_TAG
    pdu = PDU.decode(SetRequest, pdu_data, mib)
  when SNMPv1_Trap_PDU_TAG
    raise InvalidPduTag, "SNMPv1-trap not valid for #{version.to_s}" if version != :SNMPv1
    pdu = SNMPv1_Trap.decode(pdu_data, mib)
  when GetBulkRequest_PDU_TAG
    raise InvalidPduTag, "get-bulk not valid for #{version.to_s}" if version != :SNMPv2c
    pdu = PDU.decode(GetBulkRequest, pdu_data, mib)
  when InformRequest_PDU_TAG
    raise InvalidPduTag, "inform not valid for #{version.to_s}" if version != :SNMPv2c
    pdu = PDU.decode(InformRequest, pdu_data, mib)
  when SNMPv2_Trap_PDU_TAG
    raise InvalidPduTag, "SNMPv2c-trap not valid for #{version.to_s}" if version != :SNMPv2c
    pdu = PDU.decode(SNMPv2_Trap, pdu_data, mib)
  else
    raise UnsupportedPduTag, pdu_tag.to_s
  end
  return pdu, remainder
end
decode_version(data) click to toggle source
# File lib/snmp/pdu.rb, line 58
def decode_version(data)
  version_data, remainder = decode_integer(data)
  if version_data == SNMP_V1
    version = :SNMPv1
  elsif version_data == SNMP_V2C
    version = :SNMPv2c
  else
    raise UnsupportedVersion, version_data.to_s
  end
  return version, remainder
end
encode() click to toggle source
# File lib/snmp/pdu.rb, line 120
def encode
  data = encode_version(@version)
  data << encode_octet_string(@community)
  data << @pdu.encode
  encode_sequence(data)
end
encode_version(version) click to toggle source
# File lib/snmp/pdu.rb, line 110
def encode_version(version)
  if version == :SNMPv1
    encode_integer(SNMP_V1)
  elsif version == :SNMPv2c
    encode_integer(SNMP_V2C)
  else
    raise UnsupportedVersion, version.to_s
  end
end
response() click to toggle source
# File lib/snmp/pdu.rb, line 106
def response
  Message.new(@version, @community, Response.from_pdu(@pdu))
end