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.
(intf NgInterface)
| 235 | |
| 236 | // 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. |
| 237 | func (w *NgWriter) AddInterface(intf NgInterface) (id int, err error) { |
| 238 | id = int(w.intf) |
| 239 | w.intf++ |
| 240 | |
| 241 | var scratch [7]ngOption |
| 242 | i := 0 |
| 243 | if intf.Name != "" { |
| 244 | scratch[i].code = ngOptionCodeInterfaceName |
| 245 | scratch[i].raw = intf.Name |
| 246 | i++ |
| 247 | } |
| 248 | if intf.Comment != "" { |
| 249 | scratch[i].code = ngOptionCodeComment |
| 250 | scratch[i].raw = intf.Comment |
| 251 | i++ |
| 252 | } |
| 253 | if intf.Description != "" { |
| 254 | scratch[i].code = ngOptionCodeInterfaceDescription |
| 255 | scratch[i].raw = intf.Description |
| 256 | i++ |
| 257 | } |
| 258 | if intf.Filter != "" { |
| 259 | scratch[i].code = ngOptionCodeInterfaceFilter |
| 260 | scratch[i].raw = append([]byte{0}, []byte(intf.Filter)...) |
| 261 | i++ |
| 262 | } |
| 263 | if intf.OS != "" { |
| 264 | scratch[i].code = ngOptionCodeInterfaceOS |
| 265 | scratch[i].raw = intf.OS |
| 266 | i++ |
| 267 | } |
| 268 | if intf.TimestampOffset != 0 { |
| 269 | scratch[i].code = ngOptionCodeInterfaceTimestampOffset |
| 270 | scratch[i].raw = intf.TimestampOffset |
| 271 | i++ |
| 272 | } |
| 273 | scratch[i].code = ngOptionCodeInterfaceTimestampResolution |
| 274 | scratch[i].raw = uint8(9) // fix resolution to nanoseconds (time.Time) in decimal |
| 275 | i++ |
| 276 | options := scratch[:i] |
| 277 | |
| 278 | length := prepareNgOptions(options) + |
| 279 | 16 + // header |
| 280 | 4 // trailer |
| 281 | |
| 282 | binary.LittleEndian.PutUint32(w.buf[:4], uint32(ngBlockTypeInterfaceDescriptor)) |
| 283 | binary.LittleEndian.PutUint32(w.buf[4:8], length) |
| 284 | binary.LittleEndian.PutUint16(w.buf[8:10], uint16(intf.LinkType)) |
| 285 | binary.LittleEndian.PutUint16(w.buf[10:12], 0) // reserved value |
| 286 | binary.LittleEndian.PutUint32(w.buf[12:16], intf.SnapLength) |
| 287 | if _, err := w.w.Write(w.buf[:16]); err != nil { |
| 288 | return 0, err |
| 289 | } |
| 290 | |
| 291 | if err := w.writeOptions(options); err != nil { |
| 292 | return 0, err |
| 293 | } |
| 294 |