/
ngwrite.go
397 lines (356 loc) · 11.7 KB
/
ngwrite.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2018 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package pcapgo
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"runtime"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// NgWriterOptions holds options for creating a pcapng file
type NgWriterOptions struct {
// SectionInfo will be written to the section header
SectionInfo NgSectionInfo
}
// DefaultNgWriterOptions contain defaults for a pcapng writer used by NewWriter
var DefaultNgWriterOptions = NgWriterOptions{
SectionInfo: NgSectionInfo{
Hardware: runtime.GOARCH,
OS: runtime.GOOS,
Application: "gopacket", //spread the word
},
}
// DefaultNgInterface contains default interface options used by NewWriter
var DefaultNgInterface = NgInterface{
Name: "intf0",
OS: runtime.GOOS,
SnapLength: 0, //unlimited
TimestampResolution: 9,
}
// NgWriter holds the internal state of a pcapng file writer. Internally a bufio.NgWriter is used, therefore Flush must be called before closing the underlying file.
type NgWriter struct {
w *bufio.Writer
options NgWriterOptions
intf uint32
buf [28]byte
}
// NewNgWriter initializes and returns a new writer. Additionally, one section and one interface (without statistics) is written to the file. Interface and section options are used from DefaultNgInterface and DefaultNgWriterOptions.
// Flush must be called before the file is closed, or if eventual unwritten information should be written out to the storage device.
//
// Written files are in little endian format. Interface timestamp resolution is fixed to 9 (to match time.Time).
func NewNgWriter(w io.Writer, linkType layers.LinkType) (*NgWriter, error) {
intf := DefaultNgInterface
intf.LinkType = linkType
return NewNgWriterInterface(w, intf, DefaultNgWriterOptions)
}
// NewNgWriterInterface initializes and returns a new writer. Additionally, one section and one interface (without statistics) is written to the file.
// Flush must be called before the file is closed, or if eventual unwritten information should be written out to the storage device.
//
// Written files are in little endian format. Interface timestamp resolution is fixed to 9 (to match time.Time).
func NewNgWriterInterface(w io.Writer, intf NgInterface, options NgWriterOptions) (*NgWriter, error) {
ret := &NgWriter{
w: bufio.NewWriter(w),
options: options,
}
if err := ret.writeSectionHeader(); err != nil {
return nil, err
}
if _, err := ret.AddInterface(intf); err != nil {
return nil, err
}
return ret, nil
}
// ngOptionLength returns the needed length for one option value (without padding)
func ngOptionLength(option ngOption) int {
switch val := option.raw.(type) {
case []byte:
return len(val)
case string:
return len(val)
case time.Time:
return 8
case uint64:
return 8
case uint32:
return 4
case uint8:
return 1
default:
panic("This should never happen")
}
}
// prepareNgOptions fills out the length value of the given options and returns the number of octets needed for all the given options including padding.
func prepareNgOptions(options []ngOption) uint32 {
var ret uint32
for i, option := range options {
length := ngOptionLength(option)
options[i].length = uint16(length)
length += (4-length&3)&3 + // padding
4 //header
ret += uint32(length)
}
if ret > 0 {
ret += 4 // end of options
}
return ret
}
// writeOptions writes the given options to the file. prepareOptions must be called beforehand.
func (w *NgWriter) writeOptions(options []ngOption) error {
if len(options) == 0 {
return nil
}
var zero [4]byte
for _, option := range options {
binary.LittleEndian.PutUint16(w.buf[0:2], uint16(option.code))
binary.LittleEndian.PutUint16(w.buf[2:4], option.length)
if _, err := w.w.Write(w.buf[:4]); err != nil {
return err
}
switch val := option.raw.(type) {
case []byte:
if _, err := w.w.Write(val); err != nil {
return err
}
padding := uint8((4 - option.length&3) & 3)
if padding < 4 {
if _, err := w.w.Write(zero[:padding]); err != nil {
return err
}
}
case string:
if _, err := w.w.Write([]byte(val)); err != nil {
return err
}
padding := uint8((4 - option.length&3) & 3)
if padding < 4 {
if _, err := w.w.Write(zero[:padding]); err != nil {
return err
}
}
case time.Time:
ts := val.UnixNano()
binary.LittleEndian.PutUint32(w.buf[:4], uint32(ts>>32))
binary.LittleEndian.PutUint32(w.buf[4:8], uint32(ts))
if _, err := w.w.Write(w.buf[:8]); err != nil {
return err
}
case uint64:
binary.LittleEndian.PutUint64(w.buf[:8], val)
if _, err := w.w.Write(w.buf[:8]); err != nil {
return err
}
case uint32:
binary.LittleEndian.PutUint32(w.buf[:4], val)
if _, err := w.w.Write(w.buf[:4]); err != nil {
return err
}
case uint8:
binary.LittleEndian.PutUint32(w.buf[:4], 0) // padding
w.buf[0] = val
if _, err := w.w.Write(w.buf[:4]); err != nil {
return err
}
default:
panic("This should never happen")
}
}
// options must be folled by an end of options option
binary.LittleEndian.PutUint16(w.buf[0:2], uint16(ngOptionCodeEndOfOptions))
binary.LittleEndian.PutUint16(w.buf[2:4], 0)
_, err := w.w.Write(w.buf[:4])
return err
}
// writeSectionHeader writes a section header to the file
func (w *NgWriter) writeSectionHeader() error {
var scratch [4]ngOption
i := 0
info := w.options.SectionInfo
if info.Application != "" {
scratch[i].code = ngOptionCodeUserApplication
scratch[i].raw = info.Application
i++
}
if info.Comment != "" {
scratch[i].code = ngOptionCodeComment
scratch[i].raw = info.Comment
i++
}
if info.Hardware != "" {
scratch[i].code = ngOptionCodeHardware
scratch[i].raw = info.Hardware
i++
}
if info.OS != "" {
scratch[i].code = ngOptionCodeOS
scratch[i].raw = info.OS
i++
}
options := scratch[:i]
length := prepareNgOptions(options) +
24 + // header
4 // trailer
binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeSectionHeader))
binary.LittleEndian.PutUint32(w.buf[4:8], length)
binary.LittleEndian.PutUint32(w.buf[8:12], ngByteOrderMagic)
binary.LittleEndian.PutUint16(w.buf[12:14], ngVersionMajor)
binary.LittleEndian.PutUint16(w.buf[14:16], ngVersionMinor)
binary.LittleEndian.PutUint64(w.buf[16:24], 0xFFFFFFFFFFFFFFFF) // unspecified
if _, err := w.w.Write(w.buf[:24]); err != nil {
return err
}
if err := w.writeOptions(options); err != nil {
return err
}
binary.LittleEndian.PutUint32(w.buf[0:4], length)
_, err := w.w.Write(w.buf[:4])
return err
}
// AddInterface adds the specified interface to the file, excluding statistics. Interface timestamp resolution is fixed to 9 (to match time.Time). Empty values are not written.
func (w *NgWriter) AddInterface(intf NgInterface) (id int, err error) {
id = int(w.intf)
w.intf++
var scratch [7]ngOption
i := 0
if intf.Name != "" {
scratch[i].code = ngOptionCodeInterfaceName
scratch[i].raw = intf.Name
i++
}
if intf.Comment != "" {
scratch[i].code = ngOptionCodeComment
scratch[i].raw = intf.Comment
i++
}
if intf.Description != "" {
scratch[i].code = ngOptionCodeInterfaceDescription
scratch[i].raw = intf.Description
i++
}
if intf.Filter != "" {
scratch[i].code = ngOptionCodeInterfaceFilter
scratch[i].raw = append([]byte{0}, []byte(intf.Filter)...)
i++
}
if intf.OS != "" {
scratch[i].code = ngOptionCodeInterfaceOS
scratch[i].raw = intf.OS
i++
}
if intf.TimestampOffset != 0 {
scratch[i].code = ngOptionCodeInterfaceTimestampOffset
scratch[i].raw = intf.TimestampOffset
i++
}
scratch[i].code = ngOptionCodeInterfaceTimestampResolution
scratch[i].raw = uint8(9) // fix resolution to nanoseconds (time.Time) in decimal
i++
options := scratch[:i]
length := prepareNgOptions(options) +
16 + // header
4 // trailer
binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeInterfaceDescriptor))
binary.LittleEndian.PutUint32(w.buf[4:8], length)
binary.LittleEndian.PutUint16(w.buf[8:10], uint16(intf.LinkType))
binary.LittleEndian.PutUint16(w.buf[10:12], 0) // reserved value
binary.LittleEndian.PutUint32(w.buf[12:16], intf.SnapLength)
if _, err := w.w.Write(w.buf[:16]); err != nil {
return 0, err
}
if err := w.writeOptions(options); err != nil {
return 0, err
}
binary.LittleEndian.PutUint32(w.buf[0:4], length)
_, err = w.w.Write(w.buf[:4])
return id, err
}
// WriteInterfaceStats writes the given interface statistics for the given interface id to the file. Empty values are not written.
func (w *NgWriter) WriteInterfaceStats(intf int, stats NgInterfaceStatistics) error {
if intf >= int(w.intf) || intf < 0 {
return fmt.Errorf("Can't send statistics for non existent interface %d; have only %d interfaces", intf, w.intf)
}
var scratch [4]ngOption
i := 0
if !stats.StartTime.IsZero() {
scratch[i].code = ngOptionCodeInterfaceStatisticsStartTime
scratch[i].raw = stats.StartTime
i++
}
if !stats.EndTime.IsZero() {
scratch[i].code = ngOptionCodeInterfaceStatisticsEndTime
scratch[i].raw = stats.EndTime
i++
}
if stats.PacketsDropped != NgNoValue64 {
scratch[i].code = ngOptionCodeInterfaceStatisticsInterfaceDropped
scratch[i].raw = stats.PacketsDropped
i++
}
if stats.PacketsReceived != NgNoValue64 {
scratch[i].code = ngOptionCodeInterfaceStatisticsInterfaceReceived
scratch[i].raw = stats.PacketsReceived
i++
}
options := scratch[:i]
length := prepareNgOptions(options) + 24
ts := stats.LastUpdate.UnixNano()
if stats.LastUpdate.IsZero() {
ts = 0
}
binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeInterfaceStatistics))
binary.LittleEndian.PutUint32(w.buf[4:8], length)
binary.LittleEndian.PutUint32(w.buf[8:12], uint32(intf))
binary.LittleEndian.PutUint32(w.buf[12:16], uint32(ts>>32))
binary.LittleEndian.PutUint32(w.buf[16:20], uint32(ts))
if _, err := w.w.Write(w.buf[:20]); err != nil {
return err
}
if err := w.writeOptions(options); err != nil {
return err
}
binary.LittleEndian.PutUint32(w.buf[0:4], length)
_, err := w.w.Write(w.buf[:4])
return err
}
// WritePacket writes out packet with the given data and capture info. The given InterfaceIndex must already be added to the file. InterfaceIndex 0 is automatically added by the NewWriter* methods.
func (w *NgWriter) WritePacket(ci gopacket.CaptureInfo, data []byte) error {
if ci.InterfaceIndex >= int(w.intf) || ci.InterfaceIndex < 0 {
return fmt.Errorf("Can't send statistics for non existent interface %d; have only %d interfaces", ci.InterfaceIndex, w.intf)
}
if ci.CaptureLength != len(data) {
return fmt.Errorf("capture length %d does not match data length %d", ci.CaptureLength, len(data))
}
if ci.CaptureLength > ci.Length {
return fmt.Errorf("invalid capture info %+v: capture length > length", ci)
}
length := uint32(len(data)) + 32
padding := (4 - length&3) & 3
length += padding
ts := ci.Timestamp.UnixNano()
binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeEnhancedPacket))
binary.LittleEndian.PutUint32(w.buf[4:8], length)
binary.LittleEndian.PutUint32(w.buf[8:12], uint32(ci.InterfaceIndex))
binary.LittleEndian.PutUint32(w.buf[12:16], uint32(ts>>32))
binary.LittleEndian.PutUint32(w.buf[16:20], uint32(ts))
binary.LittleEndian.PutUint32(w.buf[20:24], uint32(ci.CaptureLength))
binary.LittleEndian.PutUint32(w.buf[24:28], uint32(ci.Length))
if _, err := w.w.Write(w.buf[:28]); err != nil {
return err
}
if _, err := w.w.Write(data); err != nil {
return err
}
binary.LittleEndian.PutUint32(w.buf[:4], 0)
_, err := w.w.Write(w.buf[4-padding : 8]) // padding + length
return err
}
// Flush writes out buffered data to the storage media. Must be called before closing the underlying file.
func (w *NgWriter) Flush() error {
return w.w.Flush()
}