Class: Mu::Pcap::UDP

Inherits:
Packet show all
Defined in:
lib/diy/parser/mu/pcap/udp.rb

Constant Summary collapse

FMT_nnnn =
'nnnn'

Constants inherited from Packet

Packet::IGNORE_UDP_PORTS

Instance Attribute Summary collapse

Attributes inherited from Packet

#payload, #payload_raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Packet

#deepdup, isolate_l7, normalize, #payload_bytes, #to_bytes

Constructor Details

#initialize(src_port = 0, dst_port = 0) ⇒ UDP

Returns a new instance of UDP.



11
12
13
14
15
# File 'lib/diy/parser/mu/pcap/udp.rb', line 11

def initialize src_port=0, dst_port=0
    super()
    @src_port = src_port
    @dst_port = dst_port
end

Instance Attribute Details

#dst_portObject

Returns the value of attribute dst_port.



9
10
11
# File 'lib/diy/parser/mu/pcap/udp.rb', line 9

def dst_port
  @dst_port
end

#src_portObject

Returns the value of attribute src_port.



9
10
11
# File 'lib/diy/parser/mu/pcap/udp.rb', line 9

def src_port
  @src_port
end

Class Method Details

.from_bytes(bytes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/diy/parser/mu/pcap/udp.rb', line 22

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

.udp?(packet) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/diy/parser/mu/pcap/udp.rb', line 51

def self.udp? packet
    return packet.is_a?(Ethernet) &&
        packet.payload.is_a?(IP) &&
        packet.payload.payload.is_a?(UDP)
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
# File 'lib/diy/parser/mu/pcap/udp.rb', line 61

def == other
    return super &&
        self.src_port == other.src_port &&
        self.dst_port == other.dst_port
end

#flow_idObject



17
18
19
# File 'lib/diy/parser/mu/pcap/udp.rb', line 17

def flow_id
    return [:udp, @src_port, @dst_port]
end

#to_sObject



57
58
59
# File 'lib/diy/parser/mu/pcap/udp.rb', line 57

def to_s
    return "udp(%d, %d, %s)" % [@src_port, @dst_port, @payload.inspect]
end

#write(io, ip) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/diy/parser/mu/pcap/udp.rb', line 35

def write io, ip
    length = @payload.length
    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