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.



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

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

Instance Attribute Details

#bodyTypes::String, Header::Base



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

define_field :body, Types::String

#checksumInteger

16-bit UDP checksum

Returns:

  • (Integer)


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

define_field :checksum, Types::Int16

#dportInteger Also known as: destination_port

16-bit UDP destination port

Returns:

  • (Integer)


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

define_field :dport, Types::Int16

#lengthInteger

16-bit UDP length

Returns:

  • (Integer)


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

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

#sportInteger Also known as: source_port

16-bit UDP source port

Returns:

  • (Integer)


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

define_field :sport, Types::Int16

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)


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

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)


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

def calc_length
  Base.calculate_and_set_length self
end

#reply!self

Invert source and destination port numbers

Returns:

  • (self)

Since:

  • 2.7.0



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

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