Class: PacketGen::Header::UDP

Inherits:
Base
  • Object
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:

  • a source port field (#sport, BinStruct::Int16 type),

  • a destination port field (#dport, BinStruct:Int16 type),

  • a UDP length field (#length, BinStruct:Int16 type),

  • a #checksum field (BinStruct:Int16 type),

  • and a #body.

Examples:

Create a UDP header

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

UDP attributes

udp = PacketGen::Header::UDP.new
udp.sport = 65432
udp.dport = 53
udp.length = 43
udp.checksum = 0xffff
udp.body = 'this is a UDP body'

Author:

  • Sylvain Daubert

  • LemonTree55

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, #to_s

Constructor Details

#initialize(options = {}) ⇒ UDP

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


76
77
78
79
# File 'lib/packetgen/header/udp.rb', line 76

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

Instance Attribute Details

#bodyBinStruct::String, Headerable

UDP body

Returns:


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

define_attr :body, BinStruct::String

#checksumInteger

16-bit UDP checksum

Returns:

  • (Integer)

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

define_attr :checksum, BinStruct::Int16

#dportInteger Also known as: destination_port

16-bit UDP destination port

Returns:

  • (Integer)

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

define_attr :dport, BinStruct::Int16

#lengthInteger

16-bit UDP length

Returns:

  • (Integer)

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

define_attr :length, BinStruct::Int16, default: 8

#sportInteger Also known as: source_port

16-bit UDP source port

Returns:

  • (Integer)

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

define_attr :sport, BinStruct::Int16

Instance Method Details

#calc_checksumInteger

Compute checksum and set #checksum field

Returns:

  • (Integer)

83
84
85
86
87
88
89
90
# File 'lib/packetgen/header/udp.rb', line 83

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)

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

def calc_length
  Base.calculate_and_set_length(self)
end

#reply!self

Invert source and destination port numbers

Returns:

  • (self)

Since:

  • 2.7.0


101
102
103
104
# File 'lib/packetgen/header/udp.rb', line 101

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