WriteInterfaceStats writes the given interface statistics for the given interface id to the file. Empty values are not written.
(intf int, stats NgInterfaceStatistics)
| 299 | |
| 300 | // WriteInterfaceStats writes the given interface statistics for the given interface id to the file. Empty values are not written. |
| 301 | func (w *NgWriter) WriteInterfaceStats(intf int, stats NgInterfaceStatistics) error { |
| 302 | if intf >= int(w.intf) || intf < 0 { |
| 303 | return fmt.Errorf("Can't send statistics for non existent interface %d; have only %d interfaces", intf, w.intf) |
| 304 | } |
| 305 | |
| 306 | var scratch [4]ngOption |
| 307 | i := 0 |
| 308 | if !stats.StartTime.IsZero() { |
| 309 | scratch[i].code = ngOptionCodeInterfaceStatisticsStartTime |
| 310 | scratch[i].raw = stats.StartTime |
| 311 | i++ |
| 312 | } |
| 313 | if !stats.EndTime.IsZero() { |
| 314 | scratch[i].code = ngOptionCodeInterfaceStatisticsEndTime |
| 315 | scratch[i].raw = stats.EndTime |
| 316 | i++ |
| 317 | } |
| 318 | if stats.PacketsDropped != NgNoValue64 { |
| 319 | scratch[i].code = ngOptionCodeInterfaceStatisticsInterfaceDropped |
| 320 | scratch[i].raw = stats.PacketsDropped |
| 321 | i++ |
| 322 | } |
| 323 | if stats.PacketsReceived != NgNoValue64 { |
| 324 | scratch[i].code = ngOptionCodeInterfaceStatisticsInterfaceReceived |
| 325 | scratch[i].raw = stats.PacketsReceived |
| 326 | i++ |
| 327 | } |
| 328 | options := scratch[:i] |
| 329 | |
| 330 | length := prepareNgOptions(options) + 24 |
| 331 | |
| 332 | ts := stats.LastUpdate.UnixNano() |
| 333 | if stats.LastUpdate.IsZero() { |
| 334 | ts = 0 |
| 335 | } |
| 336 | |
| 337 | binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeInterfaceStatistics)) |
| 338 | binary.LittleEndian.PutUint32(w.buf[4:8], length) |
| 339 | binary.LittleEndian.PutUint32(w.buf[8:12], uint32(intf)) |
| 340 | binary.LittleEndian.PutUint32(w.buf[12:16], uint32(ts>>32)) |
| 341 | binary.LittleEndian.PutUint32(w.buf[16:20], uint32(ts)) |
| 342 | if _, err := w.w.Write(w.buf[:20]); err != nil { |
| 343 | return err |
| 344 | } |
| 345 | |
| 346 | if err := w.writeOptions(options); err != nil { |
| 347 | return err |
| 348 | } |
| 349 | |
| 350 | binary.LittleEndian.PutUint32(w.buf[0:4], length) |
| 351 | _, err := w.w.Write(w.buf[:4]) |
| 352 | return err |
| 353 | } |
| 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 { |