Class: PacketGen::Header::UDP

Inherits:
Base show all
Defined in:
lib/packetgen/header/udp.rb

Overview

UDP header (RFC 768)

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|             Length            |           Checksum            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

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'

Author:

  • Sylvain Daubert

Constant Summary collapse

IP_PROTOCOL =

IP protocol number for UDP

17

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, fields, #fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(options = {}) ⇒ UDP

Call {Base#initialize), and automagically compute length if :body option is set.


72
73
74
75
# File 'lib/packetgen/header/udp.rb', line 72

def initialize(options={})
  super
  self.length += self[:body].sz if self[:body].sz.positive?
end

Instance Attribute Details

#bodyTypes::String, Header::Base


63
# File 'lib/packetgen/header/udp.rb', line 63

define_field :body, Types::String

#checksumInteger

16-bit UDP checksum

Returns:

  • (Integer)

60
# File 'lib/packetgen/header/udp.rb', line 60

define_field :checksum, Types::Int16

#dportInteger Also known as: destination_port

16-bit UDP destination port

Returns:

  • (Integer)

52
# File 'lib/packetgen/header/udp.rb', line 52

define_field :dport, Types::Int16

#lengthInteger

16-bit UDP length

Returns:

  • (Integer)

56
# File 'lib/packetgen/header/udp.rb', line 56

define_field :length, Types::Int16, default: 8

#sportInteger Also known as: source_port

16-bit UDP source port

Returns:

  • (Integer)

48
# File 'lib/packetgen/header/udp.rb', line 48

define_field :sport, Types::Int16

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)

79
80
81
82
83
84
85
86
# File 'lib/packetgen/header/udp.rb', line 79

def calc_checksum
  ip = ip_header(self)
  sum = ip.pseudo_header_checksum
  sum += IP_PROTOCOL
  sum += self.sz
  sum += IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)
end

#calc_lengthInteger

Compute length and set length field

Returns:

  • (Integer)

90
91
92
# File 'lib/packetgen/header/udp.rb', line 90

def calc_length
  Base.calculate_and_set_length self
end

#reply!self

Invert source and destination port numbers

Returns:

  • (self)

Since:

  • 2.7.0


97
98
99
100
# File 'lib/packetgen/header/udp.rb', line 97

def reply!
  self[:sport], self[:dport] = self[:dport], self[:sport]
  self
end