Class: PacketGen::Header::UDP
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/udp.rb
Overview
A UDP header consists of:
-
a source port field (#sport, StructFu::Int16 type),
-
a destination port field (#dport,
Int16type), -
a UDP length field (#length,
Int16type), -
a #checksum field (
Int16type), -
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 ⇒ Object
Returns the value of attribute body.
-
#checksum ⇒ Integer
Getter for checksum attribuute.
-
#dport ⇒ Integer
(also: #destination_port)
Getter for destination port.
-
#length ⇒ Integer
Getter for length attribuute.
-
#sport ⇒ Integer
(also: #source_port)
Getter for source port.
Instance Method Summary collapse
-
#calc_checksum ⇒ Integer
Compute checksum and set
checksumfield. -
#calc_length ⇒ Integer
Compute length and set
lengthfield. -
#initialize(options = {}) ⇒ UDP
constructor
A new instance of UDP.
-
#read(str) ⇒ self
Read a IP header from a string.
Methods included from HeaderClassMethods
bind_header, define_bit_fields_on, known_headers
Methods included from HeaderMethods
#header_id, #inspect, #ip_header, #packet, #packet=, #protocol_name
Methods included from StructFu
#clone, #set_endianness, #sz, #to_s, #typecast
Methods inherited from Struct
Constructor Details
#initialize(options = {}) ⇒ UDP
Returns a new instance of UDP.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/packetgen/header/udp.rb', line 45 def initialize(={}) super Int16.new([:sport]), Int16.new([:dport]), Int16.new([:length]), Int16.new([:checksum]), StructFu::String.new.read([:body]) unless [:length] calc_length end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
32 33 34 |
# File 'lib/packetgen/header/udp.rb', line 32 def body @body end |
#checksum ⇒ Integer
Getter for checksum attribuute
32 33 34 |
# File 'lib/packetgen/header/udp.rb', line 32 def checksum @checksum end |
#dport ⇒ Integer Also known as: destination_port
Getter for destination port
32 33 34 |
# File 'lib/packetgen/header/udp.rb', line 32 def dport @dport end |
#length ⇒ Integer
Getter for length attribuute
32 33 34 |
# File 'lib/packetgen/header/udp.rb', line 32 def length @length end |
#sport ⇒ Integer Also known as: source_port
Getter for source port
32 33 34 |
# File 'lib/packetgen/header/udp.rb', line 32 def sport @sport end |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set checksum field
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/packetgen/header/udp.rb', line 72 def calc_checksum ip = ip_header(self) sum = ip.pseudo_header_checksum sum += IP_PROTOCOL sum += length sum += sport sum += dport sum += length payload = body.to_s payload << "\x00" unless payload.size % 2 == 0 payload.unpack('n*').each { |x| sum += x } while sum > 0xffff do sum = (sum & 0xffff) + (sum >> 16) end sum = ~sum & 0xffff self[:checksum].value = (sum == 0) ? 0xffff : sum end |
#calc_length ⇒ Integer
Compute length and set length field
93 94 95 |
# File 'lib/packetgen/header/udp.rb', line 93 def calc_length self[:length].value = self.sz end |
#read(str) ⇒ self
Read a IP header from a string
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/packetgen/header/udp.rb', line 59 def read(str) return self if str.nil? raise ParseError, 'string too short for UDP' if str.size < self.sz force_binary str self[:sport].read str[0, 2] self[:dport].read str[2, 2] self[:length].read str[4, 2] self[:checksum].read str[6, 2] self[:body].read str[8..-1] end |