Class: PacketGen::Header::UDP

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

Overview

A UDP header consists of:

Create a UDP header

# standalone
udp = PacketGen::Header::UDP.new
# in a packet
pkt = PAcketGen.gen('IP').eadd('UDP')
# access to IP header
pkt.udp    # => PacketGen::Header::UDP

UDP attributes

udp.sport = 65432
udp.dport = 53
udp.length = 43
udp.checksum = 0xffff
udp.body.read 'this is a UDP body'

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, define_bit_fields_on, known_headers

Methods included from HeaderMethods

#header_id, #inspect, #ip_header, #packet, #packet=, #protocol_name

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

  • :checksum. (Integer)

    UDP checksum. Default: 0



45
46
47
48
49
50
51
52
53
54
# File 'lib/packetgen/header/udp.rb', line 45

def initialize(options={})
  super Int16.new(options[:sport]),
        Int16.new(options[:dport]),
        Int16.new(options[:length]),
        Int16.new(options[:checksum]),
        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



32
33
34
# File 'lib/packetgen/header/udp.rb', line 32

def body
  @body
end

#checksumInteger

Getter for checksum attribuute

Returns:

  • (Integer)


32
33
34
# File 'lib/packetgen/header/udp.rb', line 32

def checksum
  @checksum
end

#dportInteger Also known as: destination_port

Getter for destination port

Returns:

  • (Integer)


32
33
34
# File 'lib/packetgen/header/udp.rb', line 32

def dport
  @dport
end

#lengthInteger

Getter for length attribuute

Returns:

  • (Integer)


32
33
34
# File 'lib/packetgen/header/udp.rb', line 32

def length
  @length
end

#sportInteger Also known as: source_port

Getter for source port

Returns:

  • (Integer)


32
33
34
# File 'lib/packetgen/header/udp.rb', line 32

def sport
  @sport
end

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/packetgen/header/udp.rb', line 72

def calc_checksum
  ip = ip_header(self)
  sum = ip.pseudo_header_checksum
  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[:checksum].value = (sum == 0) ? 0xffff : sum
end

#calc_lengthInteger

Compute length and set length field

Returns:

  • (Integer)


93
94
95
# File 'lib/packetgen/header/udp.rb', line 93

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

#read(str) ⇒ self

Read a IP header from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



59
60
61
62
63
64
65
66
67
68
# File 'lib/packetgen/header/udp.rb', line 59

def read(str)
  return self if str.nil?
  raise ParseError, 'string too short for UDP' 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[:checksum].read str[6, 2]
  self[:body].read str[8..-1]
end