Class: PacketGen::Header::UDP
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/udp.rb
Overview
UDP header class
Constant Summary collapse
- IP_PROTOCOL =
IP protocol number for UDP
17
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#dport ⇒ Integer
(also: #destination_port)
Getter for destination port.
-
#length ⇒ Integer
Getter for length attribuute.
-
#sport ⇒ Integer
(also: #source_port)
Getter for source port.
-
#sum ⇒ Integer
Getter for sum attribuute.
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Compute length and set
lengthfield. -
#calc_sum ⇒ Integer
Compute checksum and set
sumfield. -
#initialize(options = {}) ⇒ UDP
constructor
A new instance of UDP.
-
#read(str) ⇒ self
Read a IP header from a string.
Methods included from HeaderClassMethods
Methods included from HeaderMethods
#header_id, #ip_header, #packet, #packet=
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.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/packetgen/header/udp.rb', line 19 def initialize(={}) super Int16.new([:sport]), Int16.new([:dport]), Int16.new([:length]), Int16.new([:sum]), StructFu::String.new.read([:body]) unless [:length] calc_length end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
6 7 8 |
# File 'lib/packetgen/header/udp.rb', line 6 def body @body end |
#dport ⇒ Integer Also known as: destination_port
Getter for destination port
6 7 8 |
# File 'lib/packetgen/header/udp.rb', line 6 def dport @dport end |
#length ⇒ Integer
Getter for length attribuute
6 7 8 |
# File 'lib/packetgen/header/udp.rb', line 6 def length @length end |
#sport ⇒ Integer Also known as: source_port
Getter for source port
6 7 8 |
# File 'lib/packetgen/header/udp.rb', line 6 def sport @sport end |
#sum ⇒ Integer
Getter for sum attribuute
6 7 8 |
# File 'lib/packetgen/header/udp.rb', line 6 def sum @sum end |
Instance Method Details
#calc_length ⇒ Integer
Compute length and set length field
67 68 69 |
# File 'lib/packetgen/header/udp.rb', line 67 def calc_length self[:length].value = self.sz end |
#calc_sum ⇒ Integer
Compute checksum and set sum field
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/packetgen/header/udp.rb', line 46 def calc_sum ip = ip_header(self) sum = ip.pseudo_header_sum 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[:sum].value = (sum == 0) ? 0xffff : sum end |
#read(str) ⇒ self
Read a IP header from a string
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/packetgen/header/udp.rb', line 33 def read(str) return self if str.nil? raise ParseError, 'string too short for Eth' 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[:sum].read str[6, 2] self[:body].read str[8..-1] end |