class Mu::Pcap::UDP

Constants

FMT_nnnn

Attributes

dst_port[RW]
src_port[RW]

Public Class Methods

build_pkt(ip_pkt,src_port,dst_port) click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 10
def build_pkt(ip_pkt,src_port,dst_port)
    if ip_pkt && ip_pkt.kind_of?(IP)
        the_udp_pkt = self.new(src_port,dst_port)
        ip_pkt.payload = the_udp_pkt
    else
        raise 'can not build udp pkt with not ip pkt'
    end
end
from_bytes(bytes) click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 33
def self.from_bytes bytes
    bytes_length = bytes.length
    bytes_length >= 8 or
        raise ParseError, "Truncated UDP header: expected 8 bytes, got #{bytes_length} bytes"
    sport, dport, length, checksum = bytes.unpack(FMT_nnnn)
    bytes_length >= length or
        raise ParseError, "Truncated UDP packet: expected #{length} bytes, got #{bytes_length} bytes"
    udp = UDP.new sport, dport
    udp.payload_raw = bytes[8..-1]
    udp.payload = bytes[8..length]
    return udp
end
new(src_port=0, dst_port=0) click to toggle source
Calls superclass method Mu::Pcap::Packet::new
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 21
def initialize(src_port=0, dst_port=0)
    super()
    @src_port = src_port
    @dst_port = dst_port
end
udp?(packet) click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 62
def self.udp? packet
    return packet.is_a?(Ethernet) &&
        packet.payload.is_a?(IP) &&
        packet.payload.payload.is_a?(UDP)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method Mu::Pcap::Packet#==
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 72
def == other
    return super &&
        self.src_port == other.src_port &&
        self.dst_port == other.dst_port
end
flow_id() click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 27
def flow_id
    return [:udp, @src_port, @dst_port]
end
to_s() click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 68
def to_s
    return "udp(%d, %d, %s)" % [@src_port, @dst_port, @payload.inspect]
end
write(io, ip) click to toggle source
# File lib/woolen_common/pcap/mu/pcap/udp.rb, line 46
def write io, ip
    length = @payload.bytesize
    length_8 = length + 8
    if length_8 > 65535
        Pcap.warning "UDP payload is too large"
    end
    pseudo_header = ip.pseudo_header length_8
    header = [@src_port, @dst_port, length_8, 0] \
.pack FMT_nnnn
    checksum = IP.checksum(pseudo_header + header + @payload)
    header = [@src_port, @dst_port, length_8, checksum] \
.pack FMT_nnnn
    io.write header
    io.write @payload
end