Class: PacketFu::UDPHeader

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/udp/header.rb

Overview

UDPHeader is a complete UDP struct, used in UDPPacket. Many Internet-critical protocols rely on UDP, such as DNS and World of Warcraft.

For more on UDP packets, see www.networksorcery.com/enp/protocol/udp.htm

Header Definition

Int16   :udp_src
Int16   :udp_dst
Int16   :udp_len  Default: calculated
Int16   :udp_sum  Default: 0. Often calculated.
String  :body

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ UDPHeader

Returns a new instance of UDPHeader.



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

def initialize(args={})
  super(
    Int16.new(args[:udp_src]),
    Int16.new(args[:udp_dst]),
    Int16.new(args[:udp_len] || udp_calc_len),
    Int16.new(args[:udp_sum]),
    StructFu::String.new.read(args[:body])
  )
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



15
16
17
# File 'lib/packetfu/protos/udp/header.rb', line 15

def body
  @body
end

#udp_dstObject

Getter for the UDP destination port.



15
16
17
# File 'lib/packetfu/protos/udp/header.rb', line 15

def udp_dst
  @udp_dst
end

#udp_lenObject

Getter for the length field.



15
16
17
# File 'lib/packetfu/protos/udp/header.rb', line 15

def udp_len
  @udp_len
end

#udp_srcObject

Getter for the UDP source port.



15
16
17
# File 'lib/packetfu/protos/udp/header.rb', line 15

def udp_src
  @udp_src
end

#udp_sumObject

Getter for the checksum.



15
16
17
# File 'lib/packetfu/protos/udp/header.rb', line 15

def udp_sum
  @udp_sum
end

Instance Method Details

#read(str) ⇒ Object

Reads a string to populate the object.



35
36
37
38
39
40
41
42
43
44
# File 'lib/packetfu/protos/udp/header.rb', line 35

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:udp_src].read(str[0,2])
  self[:udp_dst].read(str[2,2])
  self[:udp_len].read(str[4,2])
  self[:udp_sum].read(str[6,2])
  self[:body].read(str[8,str.size])
  self
end

#to_sObject

Returns the object in string form.



30
31
32
# File 'lib/packetfu/protos/udp/header.rb', line 30

def to_s
  self.to_a.map {|x| x.to_s}.join
end

#udp_calc_lenObject

Returns the true length of the UDP packet.



64
65
66
# File 'lib/packetfu/protos/udp/header.rb', line 64

def udp_calc_len
  body.to_s.size + 8
end

#udp_dportObject

Equivalent to udp_dst



91
92
93
# File 'lib/packetfu/protos/udp/header.rb', line 91

def udp_dport
  self.udp_dst
end

#udp_dport=(arg) ⇒ Object

Equivalent to udp_dst=



96
97
98
# File 'lib/packetfu/protos/udp/header.rb', line 96

def udp_dport=(arg)
  self.udp_dst=(arg)
end

#udp_recalc(arg = :all) ⇒ Object

Recalculates calculated fields for UDP.



69
70
71
72
73
74
75
76
77
78
# File 'lib/packetfu/protos/udp/header.rb', line 69

def udp_recalc(arg = :all)
  case arg.to_sym
  when :udp_len
    self.udp_len = udp_calc_len
  when :all
    self.udp_recalc(:udp_len)
  else
    raise ArgumentError, "No such field `#{arg}'"
  end
end

#udp_sportObject

Equivalent to udp_src.to_i



81
82
83
# File 'lib/packetfu/protos/udp/header.rb', line 81

def udp_sport
  self.udp_src
end

#udp_sport=(arg) ⇒ Object

Equivalent to udp_src=



86
87
88
# File 'lib/packetfu/protos/udp/header.rb', line 86

def udp_sport=(arg)
  self.udp_src=(arg)
end

#udp_sum_readableObject

Readability aliases



102
103
104
# File 'lib/packetfu/protos/udp/header.rb', line 102

def udp_sum_readable
  "0x%04x" % udp_sum
end