MCPcopy Index your code
hub / github.com/google/gopacket / SerializeTo

Method SerializeTo

layers/bfd.go:402–454  ·  view source on GitHub ↗

SerializeTo writes the serialized form of this layer into the SerializationBuffer, implementing gopacket.SerializableLayer. See the docs for gopacket.SerializableLayer for more info.

(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions)

Source from the content-addressed store, hash-verified

400// SerializationBuffer, implementing gopacket.SerializableLayer.
401// See the docs for gopacket.SerializableLayer for more info.
402func (d *BFD) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
403 data, err := b.PrependBytes(bfdMinimumRecordSizeInBytes)
404 if err != nil {
405 return err
406 }
407
408 // Pack the first few fields into the first 32 bits.
409 data[0] = byte(byte(d.Version<<5) | byte(d.Diagnostic))
410 h := uint8(0)
411 h |= (uint8(d.State) << 6)
412 h |= (uint8(bool2uint8(d.Poll)) << 5)
413 h |= (uint8(bool2uint8(d.Final)) << 4)
414 h |= (uint8(bool2uint8(d.ControlPlaneIndependent)) << 3)
415 h |= (uint8(bool2uint8(d.AuthPresent)) << 2)
416 h |= (uint8(bool2uint8(d.Demand)) << 1)
417 h |= uint8(bool2uint8(d.Multipoint))
418 data[1] = byte(h)
419 data[2] = byte(d.DetectMultiplier)
420 data[3] = byte(d.Length())
421
422 // The remaining fields can just be copied in big endian order.
423 binary.BigEndian.PutUint32(data[4:], uint32(d.MyDiscriminator))
424 binary.BigEndian.PutUint32(data[8:], uint32(d.YourDiscriminator))
425 binary.BigEndian.PutUint32(data[12:], uint32(d.DesiredMinTxInterval))
426 binary.BigEndian.PutUint32(data[16:], uint32(d.RequiredMinRxInterval))
427 binary.BigEndian.PutUint32(data[20:], uint32(d.RequiredMinEchoRxInterval))
428
429 if d.AuthPresent && (d.AuthHeader != nil) {
430 auth, err := b.AppendBytes(int(d.AuthHeader.Length()))
431 if err != nil {
432 return err
433 }
434
435 auth[0] = byte(d.AuthHeader.AuthType)
436 auth[1] = byte(d.AuthHeader.Length())
437 auth[2] = byte(d.AuthHeader.KeyID)
438
439 switch d.AuthHeader.AuthType {
440 case BFDAuthTypePassword:
441 copy(auth[3:], d.AuthHeader.Data)
442 case BFDAuthTypeKeyedMD5, BFDAuthTypeMeticulousKeyedMD5:
443 auth[3] = byte(0)
444 binary.BigEndian.PutUint32(auth[4:], uint32(d.AuthHeader.SequenceNumber))
445 copy(auth[8:], d.AuthHeader.Data)
446 case BFDAuthTypeKeyedSHA1, BFDAuthTypeMeticulousKeyedSHA1:
447 auth[3] = byte(0)
448 binary.BigEndian.PutUint32(auth[4:], uint32(d.AuthHeader.SequenceNumber))
449 copy(auth[8:], d.AuthHeader.Data)
450 }
451 }
452
453 return nil
454}
455
456// CanDecode returns a set of layers that BFD objects can decode.
457// As BFD objects can only decide the BFD layer, we can return just that layer.

Callers

nothing calls this directly

Calls 4

LengthMethod · 0.95
bool2uint8Function · 0.85
PrependBytesMethod · 0.65
AppendBytesMethod · 0.65

Tested by

no test coverage detected