Class: ICMP4EM::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/icmp4em/packet.rb

Constant Summary collapse

ICMP_CODE =
0
ICMP_ECHO_REQUEST =
8
ICMP_ECHO_REPLY =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Packet

Returns a new instance of Packet.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/icmp4em/packet.rb', line 35

def initialize args = {}
  @type         = args[:type] || ICMP_ECHO_REQUEST
  @code         = args[:code] || ICMP_CODE
  @manager_id   = args[:manager_id]
  @request_id   = args[:request_id]
  @retry_id     = args[:retry_id]

  if args[:payload].nil?
    @payload = ""
  elsif args[:payload].is_a? Integer
    @payload = "A" * args[:payload]
  else
    @payload = args[:payload]
  end
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



9
10
11
# File 'lib/icmp4em/packet.rb', line 9

def checksum
  @checksum
end

#codeObject

Returns the value of attribute code.



8
9
10
# File 'lib/icmp4em/packet.rb', line 8

def code
  @code
end

#manager_idObject

Returns the value of attribute manager_id.



10
11
12
# File 'lib/icmp4em/packet.rb', line 10

def manager_id
  @manager_id
end

#payloadObject

Returns the value of attribute payload.



13
14
15
# File 'lib/icmp4em/packet.rb', line 13

def payload
  @payload
end

#request_idObject

Returns the value of attribute request_id.



11
12
13
# File 'lib/icmp4em/packet.rb', line 11

def request_id
  @request_id
end

#retry_idObject

Returns the value of attribute retry_id.



12
13
14
# File 'lib/icmp4em/packet.rb', line 12

def retry_id
  @retry_id
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/icmp4em/packet.rb', line 7

def type
  @type
end

Class Method Details

.from_bytes(data) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/icmp4em/packet.rb', line 15

def self.from_bytes data
  raise ArgumentError, "Must provide at least eight bytes in order to craft an ICMP packet" unless data.length >= 8

  packet = Packet.new
  fields = data.unpack("C2 n3 A*")

  packet.type = fields.shift
  packet.code = fields.shift
  packet.checksum = fields.shift
  packet.manager_id = fields.shift

  sequence = fields.shift
  packet.request_id = sequence >> 4
  packet.retry_id = sequence & (2**4 - 1)

  packet.payload = fields.shift

  packet
end

Instance Method Details

#is_reply?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/icmp4em/packet.rb', line 55

def is_reply?
  @type == ICMP_ECHO_REPLY
end

#is_request?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/icmp4em/packet.rb', line 51

def is_request?
  @type == ICMP_ECHO_REQUEST
end

#keyObject



71
72
73
# File 'lib/icmp4em/packet.rb', line 71

def key
  [@manager_id, sequence].pack("n2")
end

#key_stringObject



75
76
77
# File 'lib/icmp4em/packet.rb', line 75

def key_string
  key.unpack("H*").first
end

#sequenceObject



67
68
69
# File 'lib/icmp4em/packet.rb', line 67

def sequence
  (@request_id << 4) + @retry_id
end

#to_bytesObject



63
64
65
# File 'lib/icmp4em/packet.rb', line 63

def to_bytes
  [@type, @code, compute_checksum, @manager_id, sequence, @payload].pack("C2 n3 A*")
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/icmp4em/packet.rb', line 59

def valid_checksum?
  @checksum == compute_checksum
end