Class: PacketGen::Header::UDP
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- 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:
-
a source port field (#sport, Types::Int16 type),
-
a destination port field (#dport,
Int16
type), -
a UDP length field (#length,
Int16
type), -
a #checksum field (
Int16
type), -
and a #body.
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'
Constant Summary collapse
- IP_PROTOCOL =
IP protocol number for UDP
17
Instance Attribute Summary collapse
- #body ⇒ Types::String, Header::Base
-
#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
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(={}) super self.length += self[:body].sz if self[:body].sz.positive? end |
Instance Attribute Details
#body ⇒ Types::String, Header::Base
64 |
# File 'lib/packetgen/header/udp.rb', line 64 define_field :body, Types::String |
#checksum ⇒ Integer
16-bit UDP checksum
61 |
# File 'lib/packetgen/header/udp.rb', line 61 define_field :checksum, Types::Int16 |
#dport ⇒ Integer Also known as: destination_port
16-bit UDP destination port
53 |
# File 'lib/packetgen/header/udp.rb', line 53 define_field :dport, Types::Int16 |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set checksum
field
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_length ⇒ Integer
Compute length and set length
field
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
98 99 100 101 |
# File 'lib/packetgen/header/udp.rb', line 98 def reply! self[:sport], self[:dport] = self[:dport], self[:sport] self end |