forked from google/stenographer
/
base.go
338 lines (309 loc) · 8.33 KB
/
base.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package base provides common utilities for other stenographer libraries.
package base
import (
"container/heap"
"flag"
"fmt"
"io"
"log"
"sort"
"sync"
"syscall"
"code.google.com/p/gopacket"
"code.google.com/p/gopacket/layers"
"code.google.com/p/gopacket/pcapgo"
"golang.org/x/net/context"
)
var VerboseLogging = flag.Int("v", -1, "log many verbose logs")
// V provides verbose logging which can be turned on/off with the -v flag.
func V(level int, fmt string, args ...interface{}) {
if *VerboseLogging >= level {
log.Printf(fmt, args...)
}
}
// Packet is a single packet with its metadata.
type Packet struct {
Data []byte // The actual bytes that make up the packet
gopacket.CaptureInfo // Metadata about when/how the packet was captured
}
// PacketChan provides an async method for passing multiple ordered packets
// between goroutines.
type PacketChan struct {
mu sync.Mutex
c chan *Packet
// C can be used to send packets on this channel in a select. Do NOT
// call 'close' on it... instead call the Close function.
C chan<- *Packet
err error
done chan struct{}
}
// Receive provides the channel from which to read packets. It always
// returns the same channel.
func (p *PacketChan) Receive() <-chan *Packet { return p.c }
// Send sends a single packet on the channel to the receiver.
func (p *PacketChan) Send(pkt *Packet) { p.c <- pkt }
// Close closes the sending channel and sets the PacketChan's error based
// in its input.
func (p *PacketChan) Close(err error) {
p.mu.Lock()
p.err = err
p.mu.Unlock()
close(p.c)
close(p.done)
}
// Done returns a channel that is closed when this packet channel is complete.
func (p *PacketChan) Done() <-chan struct{} {
return p.done
}
// NewPacketChan returns a new PacketChan channel for passing packets around.
func NewPacketChan(buffer int) *PacketChan {
pc := &PacketChan{
c: make(chan *Packet, buffer),
done: make(chan struct{}),
}
pc.C = pc.c
return pc
}
// Discard discards all remaining packets on the receiving end. If you stop
// using the channel before reading all packets, you must call this function.
// It's a good idea to defer this regardless.
func (p *PacketChan) Discard() {
go func() {
discarded := 0
for _ = range p.c {
discarded++
}
if discarded > 0 {
V(2, "discarded %v", discarded)
}
}()
}
// Err gets the current error for the channel, if any exists. This may be
// called during Next(), but if an error occurs it may only be set after Next()
// returns false the first time.
func (p *PacketChan) Err() error {
p.mu.Lock()
defer p.mu.Unlock()
return p.err
}
// indexedPacket is used internally by MergePacketChans.
type indexedPacket struct {
*Packet
i int
}
// packetHeap is used internally by MergePacketChans.
type packetHeap []indexedPacket
func (p packetHeap) Len() int { return len(p) }
func (p packetHeap) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p packetHeap) Less(i, j int) bool { return p[i].Timestamp.Before(p[j].Timestamp) }
func (p *packetHeap) Push(x interface{}) { *p = append(*p, x.(indexedPacket)) }
func (p *packetHeap) Pop() (x interface{}) {
index := len(*p) - 1
*p, x = (*p)[:index], (*p)[index]
return
}
// ConcatPacketChans concatenates packet chans in order.
func ConcatPacketChans(ctx context.Context, in <-chan *PacketChan) *PacketChan {
out := NewPacketChan(100)
go func() {
for c := range in {
c := c
defer c.Discard()
L:
for c.Err() == nil {
select {
case pkt := <-c.Receive():
if pkt == nil {
break L
}
out.Send(pkt)
case <-ctx.Done():
out.Close(ctx.Err())
return
}
}
if err := c.Err(); err != nil {
out.Close(err)
return
}
}
out.Close(nil)
}()
return out
}
// MergePacketChans merges an incoming set of packet chans, each sorted by
// time, returning a new single packet chan that's also sorted by time.
func MergePacketChans(ctx context.Context, in []*PacketChan) *PacketChan {
out := NewPacketChan(100)
go func() {
count := 0
defer func() {
V(1, "merged %d streams for %d total packets", len(in), count)
}()
var h packetHeap
for i := range in {
defer in[i].Discard()
}
for i, c := range in {
select {
case pkt := <-c.Receive():
if pkt != nil {
heap.Push(&h, indexedPacket{Packet: pkt, i: i})
}
if err := c.Err(); err != nil {
out.Close(err)
return
}
case <-ctx.Done():
out.Close(ctx.Err())
return
}
}
for h.Len() > 0 && !ContextDone(ctx) {
p := heap.Pop(&h).(indexedPacket)
count++
if pkt := <-in[p.i].Receive(); pkt != nil {
heap.Push(&h, indexedPacket{Packet: pkt, i: p.i})
}
out.c <- p.Packet
if err := in[p.i].Err(); err != nil {
out.Close(err)
return
}
}
out.Close(ctx.Err())
}()
return out
}
// Positions detail the offsets of packets within a blockfile.
type Positions []int64
var (
AllPositions = Positions{-1}
NoPositions = Positions{}
)
func (p Positions) IsAllPositions() bool {
return len(p) == 1 && p[0] == -1
}
func (a Positions) Less(i, j int) bool {
return a[i] < a[j]
}
func (a Positions) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a Positions) Len() int {
return len(a)
}
func (a Positions) Sort() {
sort.Sort(a)
}
// Union returns the union of a and b. a and b must be sorted in advance.
// Returned slice will be sorted.
func (a Positions) Union(b Positions) (out Positions) {
switch {
case a.IsAllPositions():
return a
case b.IsAllPositions():
return b
case len(a) == 0:
return b
case len(b) == 0:
return a
}
out = make(Positions, 0, len(a)+len(b)/2)
ib := 0
for _, pos := range a {
for ib < len(b) && b[ib] < pos {
out = append(out, b[ib])
ib++
}
if ib < len(b) && b[ib] == pos {
ib++
}
out = append(out, pos)
}
out = append(out, b[ib:]...)
return out
}
// Intersect returns the intersection of a and b. a and b must be sorted in
// advance. Returned slice will be sorted.
func (a Positions) Intersect(b Positions) (out Positions) {
switch {
case a.IsAllPositions():
return b
case b.IsAllPositions():
return a
case len(a) == 0:
return a
case len(b) == 0:
return b
}
out = make(Positions, 0, len(a)/2)
ib := 0
for _, pos := range a {
for ib < len(b) && b[ib] < pos {
ib++
}
if ib < len(b) && b[ib] == pos {
out = append(out, pos)
ib++
}
}
return out
}
func PathDiskFreePercentage(path string) (int, error) {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return 0, err
}
return int(100 * stat.Bavail / stat.Blocks), nil
}
// snapLen is the max packet size we'll return in pcap files to users.
const snapLen = 65536
// PacketsToFile writes all packets from 'in' to 'out', writing out all packets
// in a valid PCAP file format.
func PacketsToFile(in *PacketChan, out io.Writer) error {
w := pcapgo.NewWriter(out)
w.WriteFileHeader(snapLen, layers.LinkTypeEthernet)
count := 0
defer in.Discard()
for p := range in.Receive() {
if len(p.Data) > snapLen {
p.Data = p.Data[:snapLen]
}
if err := w.WritePacket(p.CaptureInfo, p.Data); err != nil {
// This can happen if our pipe is broken, and we don't want to blow stack
// traces all over our users when that happens, so Error/Exit instead of
// Fatal.
return fmt.Errorf("error writing packet: %v", err)
}
count++
}
return in.Err()
}
// ContextDone returns true if a context is complete.
func ContextDone(ctx context.Context) bool {
// There's two ways we could do this: by checking ctx.Done or by
// seeing if ctx.Err != nil. The latter, though, uses a single
// exclusive mutex, so when the context is shared by a ton of
// goroutines, it can actually block things quite a bit. Checking
// ctx.Done is much more scalable across multiple goroutines.
select {
case <-ctx.Done():
return true
default:
return false
}
}