Class: Rex::Proto::Kademlia::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/kademlia/message.rb

Overview

A simple Kademlia message

Direct Known Subclasses

BootstrapRequest, BootstrapResponse, Ping, Pong

Constant Summary collapse

STANDARD_PACKET =

The header that non-compressed Kad messages use

0xE4
COMPRESSED_PACKET =

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

0xE5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, body = '') ⇒ Message

Construct a new Message from the provided type and body

Parameters:

  • type (String)

    the message type

  • body (String) (defaults to: '')

    the message body



36
37
38
39
# File 'lib/rex/proto/kademlia/message.rb', line 36

def initialize(type, body = '')
  @type = type
  @body = body
end

Instance Attribute Details

#bodyString (readonly)

Returns the message body.

Returns:

  • (String)

    the message body



30
31
32
# File 'lib/rex/proto/kademlia/message.rb', line 30

def body
  @body
end

#typeInteger (readonly)

Returns the message type.

Returns:

  • (Integer)

    the message type



28
29
30
# File 'lib/rex/proto/kademlia/message.rb', line 28

def type
  @type
end

Class Method Details

.from_data(data) ⇒ Message

Construct a new Message from the provided data

Parameters:

  • data (String)

    the data to interpret as a Kademlia message

Returns:

  • (Message)

    the message if valid, nil otherwise



45
46
47
48
49
50
51
52
53
# File 'lib/rex/proto/kademlia/message.rb', line 45

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

Instance Method Details

#==(other) ⇒ Boolean

Compares this Message and another Message for equality

Parameters:

  • other (Message)

    the Message to compare

Returns:

  • (Boolean)

    true iff the two messages have equal types and bodies, false otherwise



66
67
68
# File 'lib/rex/proto/kademlia/message.rb', line 66

def ==(other)
  type == other.type && body == other.body
end

#to_strString

Get this Message as a String

Returns:

  • (String)

    the string representation of this Message



58
59
60
# File 'lib/rex/proto/kademlia/message.rb', line 58

def to_str
  [STANDARD_PACKET, @type].pack('CC') + @body
end