Class: PacketGen::Header::UDP
- 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:
Constant Summary collapse
- IP_PROTOCOL =
IP protocol number for UDP
17
Instance Attribute Summary collapse
-
#body ⇒ BinStruct::String, Headerable
UDP body.
-
#checksum ⇒ Integer
16-bit UDP checksum.
-
#dport ⇒ Integer
(also: #destination_port)
16-bit UDP destination port.
-
#length ⇒ Integer
16-bit UDP length.
-
#sport ⇒ Integer
(also: #source_port)
16-bit UDP source port.
Instance Method Summary collapse
-
#calc_checksum ⇒ Integer
Compute checksum and set #checksum field.
-
#calc_length ⇒ Integer
Compute length and set #length field.
-
#initialize(options = {}) ⇒ UDP
constructor
Call {Base#initialize), and automagically compute
length
if:body
option is set. -
#reply! ⇒ self
Invert source and destination port numbers.
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(={}) super self.length += self[:body].sz if self[:body].sz.positive? end |
Instance Attribute Details
#body ⇒ BinStruct::String, Headerable
UDP body
67 |
# File 'lib/packetgen/header/udp.rb', line 67 define_attr :body, BinStruct::String |
#checksum ⇒ Integer
16-bit UDP checksum
63 |
# File 'lib/packetgen/header/udp.rb', line 63 define_attr :checksum, BinStruct::Int16 |
#dport ⇒ Integer Also known as: destination_port
16-bit UDP destination port
55 |
# File 'lib/packetgen/header/udp.rb', line 55 define_attr :dport, BinStruct::Int16 |
#length ⇒ Integer
16-bit UDP length
59 |
# File 'lib/packetgen/header/udp.rb', line 59 define_attr :length, BinStruct::Int16, default: 8 |
#sport ⇒ Integer Also known as: source_port
16-bit UDP source port
51 |
# File 'lib/packetgen/header/udp.rb', line 51 define_attr :sport, BinStruct::Int16 |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set #checksum field
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_length ⇒ Integer
Compute length and set #length field
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
101 102 103 104 |
# File 'lib/packetgen/header/udp.rb', line 101 def reply! self[:sport], self[:dport] = self[:dport], self[:sport] self end |