Class: PcapParser::Proto::UDP

Inherits:
Object
  • Object
show all
Defined in:
lib/pcap_parser/proto/udp.rb

Overview

UDP protocol from transport layer. see: en.wikipedia.org/wiki/User_Datagram_Protocol#Packet_structure Reads packet as Big-Endian (network byte order)

Instance Method Summary collapse

Constructor Details

#initialize(bin_header) ⇒ UDP

Returns a new instance of UDP


7
8
9
# File 'lib/pcap_parser/proto/udp.rb', line 7

def initialize(bin_header)
  @binhdr = bin_header
end

Instance Method Details

#chsumInteger

Packet checksum

Returns:

  • (Integer)

    Big-Endian 16bit int


31
32
33
# File 'lib/pcap_parser/proto/udp.rb', line 31

def chsum
  @binhdr[6, 2].unpack("n").pop
end

#dataString

UDP segment data.

Returns:

  • (String)

37
38
39
# File 'lib/pcap_parser/proto/udp.rb', line 37

def data
  @binhdr[8..-1].unpack("a*").pop
end

#lengthInteger

Length in bytes of the UDP header and UDP data.

Returns:

  • (Integer)

    Big-Endian 16bit int


25
26
27
# File 'lib/pcap_parser/proto/udp.rb', line 25

def length
  @binhdr[4, 2].unpack("n").pop
end

#port_dstInteger

Destination port

Returns:

  • (Integer)

    Big-Endian 16bit int


19
20
21
# File 'lib/pcap_parser/proto/udp.rb', line 19

def port_dst
  @binhdr[2, 2].unpack("n").pop
end

#port_srcInteger

Source port

Returns:

  • (Integer)

    Big-Endian 16bit int


13
14
15
# File 'lib/pcap_parser/proto/udp.rb', line 13

def port_src
  @binhdr[0, 2].unpack("n").pop
end

#valid?true, false

Returns:

  • (true, false)

44
45
46
47
48
49
50
51
# File 'lib/pcap_parser/proto/udp.rb', line 44

def valid?
  return true if chsum == 0
  sum = Proto::sum_pack_16int @binhdr.unpack("n4")
  # flit bits in result sum
  # result is th two's complement so to convert we remove 1
  flipped = [sum * -1].pack("n").unpack("n").pop - 1
  0xffff == flipped + sum
end