class Rex::Proto::Kademlia::Message

A simple Kademlia message

Constants

COMPRESSED_PACKET

The header that compressed Kad messages use, which is currently unsupported

STANDARD_PACKET

The header that non-compressed Kad messages use

Attributes

body[R]

@return [String] the message body

type[R]

@return [Integer] the message type

Public Class Methods

from_data(data) click to toggle source

Construct a new Message from the provided data

@param data [String] the data to interpret as a Kademlia message @return [Message] the message if valid, nil otherwise

# File lib/rex/proto/kademlia/message.rb, line 44
def self.from_data(data)
  return if data.length < 2
  header, type = data.unpack('CC')
  if header == COMPRESSED_PACKET
    fail NotImplementedError, "Unable to handle #{data.length}-byte compressed Kademlia message"
  end
  return if header != STANDARD_PACKET
  Message.new(type, data[2, data.length])
end
new(type, body = '') click to toggle source

Construct a new Message from the provided type and body

@param type [String] the message type @param body [String] the message body

# File lib/rex/proto/kademlia/message.rb, line 35
def initialize(type, body = '')
  @type = type
  @body = body
end

Public Instance Methods

==(other) click to toggle source

Compares this Message and another Message for equality

@param other [Message] the Message to compare @return [Boolean] true iff the two messages have equal types and bodies, false otherwise

# File lib/rex/proto/kademlia/message.rb, line 65
def ==(other)
  type == other.type && body == other.body
end
to_str() click to toggle source

Get this Message as a String

@return [String] the string representation of this Message

# File lib/rex/proto/kademlia/message.rb, line 57
def to_str
  [STANDARD_PACKET, @type].pack('CC') + @body
end