Module: Xlat::Protocols::Ip::Ipv4

Extended by:
Common
Defined in:
lib/xlat/protocols/ip/ipv4.rb

Class Method Summary collapse

Methods included from Common

sum16be

Class Method Details

.apply(bytes, offset, cs_delta, icmp_payload) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xlat/protocols/ip/ipv4.rb', line 86

def self.apply(bytes, offset, cs_delta, icmp_payload)
  # decrement TTL
  unless icmp_payload
    ttl = bytes.get_value(:U8, offset + 8)
    if ttl > 0
      ttl -= 1
      bytes.set_value(:U8, offset + 8, ttl)
      cs_delta -= 0x0100 # checksum computation is performed per 2 octets
    end
  end

  if cs_delta != 0
    checksum = bytes.get_value(:U16, offset + 10)
    checksum = Ip.checksum_adjust(checksum, cs_delta)
    bytes.set_value(:U16, offset + 10, checksum)
  end
end

.icmp_cs_delta(packet) ⇒ Object



47
48
49
# File 'lib/xlat/protocols/ip/ipv4.rb', line 47

def self.icmp_cs_delta(packet)
  0
end

.icmp_protocol_idObject



43
44
45
# File 'lib/xlat/protocols/ip/ipv4.rb', line 43

def self.icmp_protocol_id
  Icmp::Base::V4_PROTOCOL_ID
end

.new_icmp(packet, type) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xlat/protocols/ip/ipv4.rb', line 51

def self.new_icmp(packet, type)
  case type
  when Icmp::Echo::V4_TYPE_REQUEST
    Icmp::Echo.new(packet, true)
  when Icmp::Echo::V4_TYPE_REPLY
    Icmp::Echo.new(packet, false)
  when Icmp::Error::V4_TYPE_DEST_UNREACH, Icmp::Error::V4_TYPE_TIME_EXCEEDED, Icmp::Error::V4_TYPE_PARAMETER_PROBLEM
    Icmp::Error.new(packet)
  else
    Icmp::Base.new(packet)
  end
end

.parse(packet, b0) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xlat/protocols/ip/ipv4.rb', line 64

def self.parse(packet, b0)
  bytes = packet.bytes
  offset = packet.bytes_offset

  header_length = (b0 & 0x0f) * 4
  return false if header_length < 20

  # tos?

  total_length = bytes.get_value(:U16, offset + 2)
  return false if total_length < header_length
  return false unless packet.set_l4_region(header_length, total_length - header_length)

  # ignore identification
  return false if bytes.get_value(:U16, offset + 6) & 0xbfff != 0 # discard fragments

  proto = bytes.get_value(:U8, offset + 9)
  packet.proto = proto

  true
end

.to_iObject



35
36
37
# File 'lib/xlat/protocols/ip/ipv4.rb', line 35

def self.to_i
  4
end

.tuple(bytes, offset) ⇒ Object



39
40
41
# File 'lib/xlat/protocols/ip/ipv4.rb', line 39

def self.tuple(bytes, offset)
  bytes.slice(offset + 12, 8)
end