Class: Mu::Pcap::SCTP::Chunk::Data

Inherits:
Mu::Pcap::SCTP::Chunk show all
Defined in:
lib/mu/pcap/sctp/chunk/data.rb

Constant Summary collapse

FLAG_LAST_SEG =
0x01
FLAG_FIRST_SEG =
0x02
FLAG_UNORDERED =
0x04

Constants inherited from Packet

Packet::IGNORE_UDP_PORTS

Instance Attribute Summary collapse

Attributes inherited from Mu::Pcap::SCTP::Chunk

#flags, #size, #type

Attributes inherited from Packet

#payload, #payload_raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mu::Pcap::SCTP::Chunk

dummy_chunk, #padded_size

Methods inherited from Packet

#==, #deepdup, #flow_id, isolate_l7, normalize, #payload_bytes, #to_bytes

Constructor Details

#initializeData

Returns a new instance of Data.



17
18
19
20
21
22
23
24
25
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 17

def initialize
    super
    
    @type = CHUNK_DATA
    @tsn  = 0
    @sid  = 0
    @ssn  = 0
    @ppid = 0
end

Instance Attribute Details

#ppidObject

Returns the value of attribute ppid.



15
16
17
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 15

def ppid
  @ppid
end

#sidObject

Returns the value of attribute sid.



15
16
17
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 15

def sid
  @sid
end

#ssnObject

Returns the value of attribute ssn.



15
16
17
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 15

def ssn
  @ssn
end

#tsnObject

Returns the value of attribute tsn.



15
16
17
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 15

def tsn
  @tsn
end

Class Method Details

.from_bytes(flags, size, bytes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 27

def self.from_bytes flags, size, bytes
    # Basic validation
    Pcap.assert(bytes.length >= 12,
                "Truncated data chunk header: 12 > #{bytes.length}")
    
    # Read init chunk header
    tsn, sid, ssn, ppid = bytes.unpack('NnnN')
    
    # Create data chunk
    data       = Data.new
    data.flags = flags
    data.size  = size
    data.tsn   = tsn
    data.sid   = sid
    data.ssn   = ssn
    data.ppid  = ppid
    
    # Save the payload
    data.payload_raw = data.payload = bytes[12..size - 5]
    
    # Return the result
    return data
end

Instance Method Details

#complete_segment?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 92

def complete_segment?
    if 1 == @flags[1] and 1 == @flags[0]
        return true
    else
        return false
    end
end

#first_segment?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 76

def first_segment?
    if 1 == @flags[1] and 0 == @flags[0]
        return true
    else
        return false
    end
end

#last_segment?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 84

def last_segment?
    if 0 == @flags[1] and 1 == @flags[0]
        return true
    else
        return false
    end
end

#ordered?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 68

def ordered?
    if 0 == @flags[2]
        return true
    else
        return false
    end
end

#to_sObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 100

def to_s
    flags_s = '['
    
    if ordered?
        flags_s += 'ordered, '
    else
        flags_s += 'unordered, '
    end
    
    if complete_segment?
        flags_s += 'complete segment'
    elsif first_segment?
        flags_s += 'first segment'
    elsif last_segment?
        flags_s += 'last segment'
    else
        flags_s += 'middle segment'
    end
    
    flags_s += ']'
    
    return "data(%s, %d, %d, %d, %d, %d, %s)" % [flags_s,
                                                 @size,
                                                 @tsn,
                                                 @sid,
                                                 @ssn,
                                                 @ppid,
                                                 @payload.inspect]
end

#write(io, ip) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mu/pcap/sctp/chunk/data.rb', line 51

def write io, ip
    chunk_header = [@type, @flags, @size].pack('CCn')
    data_header  = [@tsn, @sid, @ssn, @ppid].pack('NnnN')
    
    # Write Chunk header followed by the Data chunk header and payload
    io.write(chunk_header)
    io.write(data_header)
    io.write(@payload_raw)
    
    # Write padding, if necessary
    if size < padded_size
        (padded_size - size).times do
            io.write("\x00")
        end
    end
end