class PacketGen::Header::UDP

UDP header ({tools.ietf.org/html/rfc768 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

Constants

IP_PROTOCOL

IP protocol number for UDP

Public Class Methods

new(options={}) click to toggle source

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

Calls superclass method PacketGen::Header::Base::new
# File lib/packetgen/header/udp.rb, line 72
def initialize(options={})
  super
  self.length += self[:body].sz if self[:body].sz.positive?
end

Public Instance Methods

calc_checksum() click to toggle source

Compute checksum and set checksum field @return [Integer]

# 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_length() click to toggle source

Compute length and set length field @return [Integer]

# File lib/packetgen/header/udp.rb, line 90
def calc_length
  Base.calculate_and_set_length self
end
reply!() click to toggle source

Invert source and destination port numbers @return [self] @since 2.7.0

# File lib/packetgen/header/udp.rb, line 97
def reply!
  self[:sport], self[:dport] = self[:dport], self[:sport]
  self
end