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.
(ci gopacket.CaptureInfo, data []byte)
| 354 | |
| 355 | // 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. |
| 356 | func (w *NgWriter) WritePacket(ci gopacket.CaptureInfo, data []byte) error { |
| 357 | if ci.InterfaceIndex >= int(w.intf) || ci.InterfaceIndex < 0 { |
| 358 | return fmt.Errorf("Can't send statistics for non existent interface %d; have only %d interfaces", ci.InterfaceIndex, w.intf) |
| 359 | } |
| 360 | if ci.CaptureLength != len(data) { |
| 361 | return fmt.Errorf("capture length %d does not match data length %d", ci.CaptureLength, len(data)) |
| 362 | } |
| 363 | if ci.CaptureLength > ci.Length { |
| 364 | return fmt.Errorf("invalid capture info %+v: capture length > length", ci) |
| 365 | } |
| 366 | |
| 367 | length := uint32(len(data)) + 32 |
| 368 | padding := (4 - length&3) & 3 |
| 369 | length += padding |
| 370 | |
| 371 | ts := ci.Timestamp.UnixNano() |
| 372 | |
| 373 | binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeEnhancedPacket)) |
| 374 | binary.LittleEndian.PutUint32(w.buf[4:8], length) |
| 375 | binary.LittleEndian.PutUint32(w.buf[8:12], uint32(ci.InterfaceIndex)) |
| 376 | binary.LittleEndian.PutUint32(w.buf[12:16], uint32(ts>>32)) |
| 377 | binary.LittleEndian.PutUint32(w.buf[16:20], uint32(ts)) |
| 378 | binary.LittleEndian.PutUint32(w.buf[20:24], uint32(ci.CaptureLength)) |
| 379 | binary.LittleEndian.PutUint32(w.buf[24:28], uint32(ci.Length)) |
| 380 | |
| 381 | if _, err := w.w.Write(w.buf[:28]); err != nil { |
| 382 | return err |
| 383 | } |
| 384 | |
| 385 | if _, err := w.w.Write(data); err != nil { |
| 386 | return err |
| 387 | } |
| 388 | |
| 389 | binary.LittleEndian.PutUint32(w.buf[:4], 0) |
| 390 | _, err := w.w.Write(w.buf[4-padding : 8]) // padding + length |
| 391 | return err |
| 392 | } |
| 393 | |
| 394 | // Flush writes out buffered data to the storage media. Must be called before closing the underlying file. |
| 395 | func (w *NgWriter) Flush() error { |