Class: Net::SNMP::Message

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/net/snmp/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Debug

#print_packet, #time

Constructor Details

#initialize(packet) ⇒ Message

Returns a new instance of Message.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/net/snmp/message.rb', line 31

def initialize(packet)
  @version = nil
  @version_ptr = FFI::MemoryPointer.new(:long, 1)
  @community_ptr = FFI::MemoryPointer.new(:uchar, 100)
  @packet = packet
  @packet_length = packet[0].length
  @type_ptr = FFI::MemoryPointer.new(:int, 1)
  @data_ptr = FFI::MemoryPointer.new(:char, @packet_length)
  @data_ptr.write_bytes(packet[0])
  @cursor_ptr = @data_ptr
  @bytes_remaining_ptr = FFI::MemoryPointer.new(:int, 1)
  @bytes_remaining_ptr.write_bytes([@packet_length].pack("L"))
  @source_address = @packet[1][3]
  @source_port = @packet[1][1]
  #debug "MESSAGE INITIALIZED\n#{self}"
  parse
end

Instance Attribute Details

#communityObject

Returns the value of attribute community.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def community
  @community
end

#community_ptrObject

Returns the value of attribute community_ptr.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def community_ptr
  @community_ptr
end

#pduObject

Returns the value of attribute pdu.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def pdu
  @pdu
end

#source_addressObject

Returns the value of attribute source_address.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def source_address
  @source_address
end

#source_portObject

Returns the value of attribute source_port.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def source_port
  @source_port
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def version
  @version
end

#version_ptrObject

Returns the value of attribute version_ptr.



10
11
12
# File 'lib/net/snmp/message.rb', line 10

def version_ptr
  @version_ptr
end

Class Method Details

.parse(packet) ⇒ Object



6
7
8
# File 'lib/net/snmp/message.rb', line 6

def self.parse(packet)
  Message.new(packet)
end

Instance Method Details

#echoObject

Sends a response PDU to the source of the message with all of the same varbinds. (Useful for sets & informs, where this is how you indicate success)



60
61
62
63
64
65
66
# File 'lib/net/snmp/message.rb', line 60

def echo
  response_pdu = make_response_pdu
  pdu.varbinds.each do |vb|
    response_pdu.add_varbind(oid: vb.oid, type: vb.type, value: vb.value)
  end
  respond(response_pdu)
end

#make_response_pduObject

Constructs a PDU for responding to this message. This makes sure the PDU has the right request ID, version, and community set.



71
72
73
74
75
76
77
# File 'lib/net/snmp/message.rb', line 71

def make_response_pdu
  response_pdu = PDU.new(Constants::SNMP_MSG_RESPONSE)
  response_pdu.reqid = pdu.reqid
  response_pdu.version = version
  response_pdu.community = pdu.community
  response_pdu
end

#respond(response_pdu) ⇒ Object

Sends the given PDU back to the origin of this message. The origin is the same address and port that the message was received from.



52
53
54
55
56
# File 'lib/net/snmp/message.rb', line 52

def respond(response_pdu)
  Session.open(peername: source_address, port: source_port, version: version_name) do |sess|
    sess.send_pdu(response_pdu)
  end
end

#version_nameObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/net/snmp/message.rb', line 18

def version_name
  case @version
  when Constants::SNMP_VERSION_1
    '1'
  when Constants::SNMP_VERSION_2c
    '2c'
  when Constants::SNMP_VERSION_3
    '3'
  else
    raise "Invalid SNMP version: #{@version}"
  end
end