Class: PacketGen::Header::UDP

Inherits:
Struct
  • Object
show all
Extended by:
HeaderClassMethods
Includes:
HeaderMethods, StructFu
Defined in:
lib/packetgen/header/udp.rb

Overview

UDP header class

Author:

  • Sylvain Daubert

Constant Summary collapse

IP_PROTOCOL =

IP protocol number for UDP

17

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HeaderClassMethods

bind_header, known_headers

Methods included from HeaderMethods

#header_id, #ip_header, #packet, #packet=

Methods included from StructFu

#clone, #set_endianness, #sz, #to_s, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ UDP

Returns a new instance of UDP.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :sport (Integer)

    source port

  • :dport (Integer)

    destination port

  • :length (Integer)

    UDP length. Default: calculated

  • :sum. (Integer)

    UDP checksum. Default: 0



19
20
21
22
23
24
25
26
27
28
# File 'lib/packetgen/header/udp.rb', line 19

def initialize(options={})
  super Int16.new(options[:sport]),
        Int16.new(options[:dport]),
        Int16.new(options[:length]),
        Int16.new(options[:sum]),
        StructFu::String.new.read(options[:body])
  unless options[:length]
    calc_length
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



6
7
8
# File 'lib/packetgen/header/udp.rb', line 6

def body
  @body
end

#dportInteger Also known as: destination_port

Getter for destination port

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/udp.rb', line 6

def dport
  @dport
end

#lengthInteger

Getter for length attribuute

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/udp.rb', line 6

def length
  @length
end

#sportInteger Also known as: source_port

Getter for source port

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/udp.rb', line 6

def sport
  @sport
end

#sumInteger

Getter for sum attribuute

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/udp.rb', line 6

def sum
  @sum
end

Instance Method Details

#calc_lengthInteger

Compute length and set length field

Returns:

  • (Integer)


67
68
69
# File 'lib/packetgen/header/udp.rb', line 67

def calc_length
  self[:length].value = self.sz
end

#calc_sumInteger

Compute checksum and set sum field

Returns:

  • (Integer)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/packetgen/header/udp.rb', line 46

def calc_sum
  ip = ip_header(self)
  sum = ip.pseudo_header_sum
  sum += IP_PROTOCOL
  sum += length
  sum += sport
  sum += dport
  sum += length
  payload = body.to_s
  payload << "\x00" unless payload.size % 2 == 0
  payload.unpack('n*').each { |x| sum += x }

  while sum > 0xffff do
    sum = (sum & 0xffff) + (sum >> 16)
  end
  sum = ~sum & 0xffff
  self[:sum].value = (sum == 0) ? 0xffff : sum
end

#read(str) ⇒ self

Read a IP header from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



33
34
35
36
37
38
39
40
41
42
# File 'lib/packetgen/header/udp.rb', line 33

def read(str)
  return self if str.nil?
  raise ParseError, 'string too short for Eth' if str.size < self.sz
  force_binary str
  self[:sport].read str[0, 2]
  self[:dport].read str[2, 2]
  self[:length].read str[4, 2]
  self[:sum].read str[6, 2]
  self[:body].read str[8..-1]
end