Class: SNMP::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, community, pdu) ⇒ Message


100
101
102
103
104
# File 'lib/snmp/pdu.rb', line 100

def initialize(version, community, pdu)
  @version = version
  @community = community
  @pdu = pdu
end

Instance Attribute Details

#communityObject (readonly)

Returns the value of attribute community


44
45
46
# File 'lib/snmp/pdu.rb', line 44

def community
  @community
end

#pduObject (readonly)

Returns the value of attribute pdu


45
46
47
# File 'lib/snmp/pdu.rb', line 45

def pdu
  @pdu
end

#versionObject (readonly)

Returns the value of attribute version


43
44
45
# File 'lib/snmp/pdu.rb', line 43

def version
  @version
end

Class Method Details

.decode(data, mib = nil) ⇒ Object


48
49
50
51
52
53
54
55
56
# 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) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Method Details

#encodeObject


120
121
122
123
124
125
# 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) ⇒ Object


110
111
112
113
114
115
116
117
118
# 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

#responseObject


106
107
108
# File 'lib/snmp/pdu.rb', line 106

def response
  Message.new(@version, @community, Response.from_pdu(@pdu))
end