### Home Classes Methods

### In Files

- lib/packetfu/protos/udp.rb

### Parent

Packet

### Methods

- ::can_parse?
- ::new
- #peek_format
- #read
- #udp_calc_sum
- #udp_recalc

### Included Modules

- PacketFu::EthHeaderMixin
- PacketFu::IPHeaderMixin
- PacketFu::UDPHeaderMixin

### Files

- .document
- README.rdoc

### Class/Module Index

Quicksearch

- PacketFu
- PacketFu::ARPHeader
- PacketFu::ARPHeaderMixin
- PacketFu::ARPPacket
- PacketFu::AddrIpv6
- PacketFu::Capture
- PacketFu::Config
- PacketFu::EthHeader
- PacketFu::EthHeaderMixin
- PacketFu::EthMac
- PacketFu::EthNic
- PacketFu::EthOui
- PacketFu::EthPacket
- PacketFu::HSRPHeader
- PacketFu::HSRPHeaderMixin
- PacketFu::HSRPPacket
- PacketFu::ICMPHeader
- PacketFu::ICMPHeaderMixin
- PacketFu::ICMPPacket
- PacketFu::IPHeader
- PacketFu::IPHeaderMixin
- PacketFu::IPPacket
- PacketFu::IPv6Header
- PacketFu::IPv6HeaderMixin
- PacketFu::IPv6Packet
- PacketFu::Inject
- PacketFu::InvalidHeader
- PacketFu::InvalidPacket
- PacketFu::LLDPHeader
- PacketFu::LLDPHeaderMixin
- PacketFu::LLDPPacket
- PacketFu::Octets
- PacketFu::Packet
- PacketFu::PcapFile
- PacketFu::PcapHeader
- PacketFu::PcapPacket
- PacketFu::PcapPackets
- PacketFu::Read
- PacketFu::TCPHeader
- PacketFu::TCPHeaderMixin
- PacketFu::TCPPacket
- PacketFu::TcpEcn
- PacketFu::TcpFlags
- PacketFu::TcpHlen
- PacketFu::TcpOption
- PacketFu::TcpOption::ECHO
- PacketFu::TcpOption::ECHOREPLY
- PacketFu::TcpOption::EOL
- PacketFu::TcpOption::MSS
- PacketFu::TcpOption::NOP
- PacketFu::TcpOption::SACK
- PacketFu::TcpOption::SACKOK
- PacketFu::TcpOption::TS
- PacketFu::TcpOption::WS
- PacketFu::TcpOptions
- PacketFu::TcpReserved
- PacketFu::Timestamp
- PacketFu::UDPHeader
- PacketFu::UDPHeaderMixin
- PacketFu::UDPPacket
- PacketFu::Utils
- PacketFu::Write
- StructFu
- StructFu::Int
- StructFu::Int16
- StructFu::Int16be
- StructFu::Int16le
- StructFu::Int32
- StructFu::Int32be
- StructFu::Int32le
- StructFu::Int8
- StructFu::IntString
- StructFu::String
- Struct

No matching classes.

# PacketFu::UDPPacket

UDPPacket is used to construct UDP Packets. They contain an EthHeader, an IPHeader, and a UDPHeader.

## Example

    udp_pkt = PacketFu::UDPPacket.new
    udp_pkt.udp_src=rand(0xffff-1024) + 1024
    udp_pkt.udp_dst=53

    udp_pkt.ip_saddr="1.2.3.4"
    udp_pkt.ip_daddr="10.20.30.40"

    udp_pkt.recalc
    udp_pkt.to_f('/tmp/udp.pcap')

## Parameters

    :eth
      A pre-generated EthHeader object.
    :ip
      A pre-generated IPHeader object.
    :flavor
      TODO: Sets the "flavor" of the UDP packet. UDP packets don't tend have a lot of
      flavor, but their underlying ip headers do.
    :config
     A hash of return address details, often the output of Utils.whoami?

### Attributes

eth_header[RW]
ip_header[RW]
udp_header[RW]

### Public Class Methods

can_parse?(str) click to toggle source

    # File lib/packetfu/protos/udp.rb, line 44
    def self.can_parse?(str)
      return false unless str.size >= 28
      return false unless EthPacket.can_parse? str
      return false unless IPPacket.can_parse? str
      return false unless str[23,1] == "\x11"
      return true
    end

new(args={}) click to toggle source

    # File lib/packetfu/protos/udp.rb, line 63
    def initialize(args={})
      @eth_header = EthHeader.new(args).read(args[:eth])
      @ip_header = IPHeader.new(args).read(args[:ip])
      @ip_header.ip_proto=0x11
      @udp_header = UDPHeader.new(args).read(args[:icmp])
      @ip_header.body = @udp_header
      @eth_header.body = @ip_header
      @headers = [@eth_header, @ip_header, @udp_header]
      super
      udp_calc_sum
    end

### Public Instance Methods

peek_format() click to toggle source

Peek provides summary data on packet contents.

    # File lib/packetfu/protos/udp.rb, line 127
    def peek_format
      peek_data = ["U  "]
      peek_data << "%-5d" % self.to_s.size
      peek_data << "%-21s" % "#{self.ip_saddr}:#{self.udp_sport}"
      peek_data << "->"
      peek_data << "%21s" % "#{self.ip_daddr}:#{self.udp_dport}"
      peek_data << "%23s" % "I:"
      peek_data << "%04x" % self.ip_id
      peek_data.join
    end

read(str=nil, args={}) click to toggle source

    # File lib/packetfu/protos/udp.rb, line 52
    def read(str=nil, args={})
      raise "Cannot parse `#{str}'" unless self.class.can_parse?(str)
      @eth_header.read(str)
      if args[:strip]
        udp_body_len = self.ip_len - self.ip_hlen - 8
        @udp_header.body.read(@udp_header.body.to_s[0,udp_body_len])
      end
      super(args)
      self
    end

udp_calc_sum() click to toggle source

udp_calc_sum() computes the UDP checksum, and is called upon intialization. It usually should be called just prior to dropping packets to a file or on the wire.

    # File lib/packetfu/protos/udp.rb, line 77
    def udp_calc_sum
      # This is /not/ delegated down to @udp_header since we need info
      # from the IP header, too.
      checksum = (ip_src.to_i >> 16)
      checksum += (ip_src.to_i & 0xffff)
      checksum += (ip_dst.to_i >> 16)
      checksum += (ip_dst.to_i & 0xffff)
      checksum += 0x11
      checksum += udp_len.to_i
      checksum += udp_src.to_i
      checksum += udp_dst.to_i
      checksum += udp_len.to_i
      if udp_len.to_i >= 8
        # For IP trailers. This isn't very reliable. :/
        real_udp_payload = payload.to_s[0,(udp_len.to_i-8)]
      else
        # I'm not going to mess with this right now.
        real_udp_payload = payload
      end
      chk_payload = (real_udp_payload.size % 2 == 0 ? real_udp_payload : real_udp_payload + "\x00")
      chk_payload.unpack("n*").each {|x| checksum = checksum+x}
      checksum = checksum % 0xffff
      checksum = 0xffff - checksum
      checksum == 0 ? 0xffff : checksum
      @udp_header.udp_sum = checksum
    end

udp_recalc(args=:all) click to toggle source

udp_recalc() recalculates various fields of the UDP packet. Valid arguments are:

    :all
      Recomputes all calculated fields.
    :udp_sum
      Recomputes the UDP checksum.
    :udp_len
      Recomputes the UDP length.

    # File lib/packetfu/protos/udp.rb, line 112
    def udp_recalc(args=:all)
      case args
      when :udp_len
        @udp_header.udp_recalc
      when :udp_sum
        udp_calc_sum
      when :all
        @udp_header.udp_recalc
        udp_calc_sum
      else
        raise ArgumentError, "No such field `#{arg}'"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.