/
reassembly_object.go
103 lines (84 loc) · 2.08 KB
/
reassembly_object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package reassembly
import (
"github.com/dreadl0ck/gopacket"
"sync"
)
// Implements a ScatterGather.
type reassemblyObject struct {
all []byteContainer
Skip int
Direction TCPFlowDirection
saved int
toKeep int
// stats
queuedBytes int
queuedPackets int
overlapBytes int
overlapPackets int
sync.Mutex
}
// Lengths returns the lengths for a reassemblyObject.
func (rl *reassemblyObject) Lengths() (int, int) {
l := 0
for _, r := range rl.all {
l += r.length()
}
return l, rl.saved
}
// Fetch returns the available data for a reassemblyObject.
func (rl *reassemblyObject) Fetch(l int) []byte {
if len(rl.all) == 0 {
return nil
}
if l <= rl.all[0].length() {
return rl.all[0].getBytes()[:l]
}
bytes := make([]byte, 0, l)
for _, bc := range rl.all {
bytes = append(bytes, bc.getBytes()...)
}
return bytes[:l]
}
// KeepFrom will update the toKeep fields value to the supplied offset.
func (rl *reassemblyObject) KeepFrom(offset int) {
rl.toKeep = offset
}
// CaptureInfo returns the gopacket.CaptureInfo for the supplied offset.
func (rl *reassemblyObject) CaptureInfo(offset int) gopacket.CaptureInfo {
if offset < 0 {
return gopacket.CaptureInfo{}
}
current := 0
for _, r := range rl.all {
if current+r.length() > offset {
return r.captureInfo()
}
current += r.length()
}
// Invalid offset
return gopacket.CaptureInfo{}
}
// Info returns information about the reassemblyObject.
func (rl *reassemblyObject) Info() (TCPFlowDirection, bool, bool, int) {
if len(rl.all) == 0 {
return rl.Direction, false, false, rl.Skip
}
return rl.Direction, rl.all[0].isStart(), rl.all[len(rl.all)-1].isEnd(), rl.Skip
}
// Stats return the TCPAssemblyStats for the reassemblyObject.
func (rl *reassemblyObject) Stats() TCPAssemblyStats {
packets := 0
for _, r := range rl.all {
if r.isPacket() {
packets++
}
}
return TCPAssemblyStats{
Chunks: len(rl.all),
Packets: packets,
QueuedBytes: rl.queuedBytes,
QueuedPackets: rl.queuedPackets,
OverlapBytes: rl.overlapBytes,
OverlapPackets: rl.overlapPackets,
}
}