diff options
Diffstat (limited to 'src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go')
-rw-r--r-- | src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go index dff8bf2da0a..c6d79634c13 100644 --- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go +++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/google/gopacket/writer.go @@ -6,7 +6,9 @@ package gopacket -import () +import ( + "fmt" +) // SerializableLayer allows its implementations to be written out as a set of bytes, // so those bytes may be sent on the wire or otherwise used by the caller. @@ -34,6 +36,8 @@ type SerializableLayer interface { // LayerPayload. It just serializes based on struct fields, neither // modifying nor using contents/payload. SerializeTo(b SerializeBuffer, opts SerializeOptions) error + // LayerType returns the type of the layer that is being serialized to the buffer + LayerType() LayerType } // SerializeOptions provides options for behaviors that SerializableLayers may want to @@ -86,7 +90,7 @@ type SerializeBuffer interface { // overwritten by the caller. The caller must only call PrependBytes if they // know they're going to immediately overwrite all bytes returned. PrependBytes(num int) ([]byte, error) - // AppendBytes returns a set of bytes which prepends the current bytes in this + // AppendBytes returns a set of bytes which appends the current bytes in this // buffer. These bytes start in an indeterminate state, so they should be // overwritten by the caller. The caller must only call AppendBytes if they // know they're going to immediately overwrite all bytes returned. @@ -95,12 +99,19 @@ type SerializeBuffer interface { // the byte slice returned by any previous call to Bytes() for this buffer // should be considered invalidated. Clear() error + // Layers returns all the Layers that have been successfully serialized into this buffer + // already. + Layers() []LayerType + // PushLayer adds the current Layer to the list of Layers that have been serialized + // into this buffer. + PushLayer(LayerType) } type serializeBuffer struct { data []byte start int prepended, appended int + layers []LayerType } // NewSerializeBuffer creates a new instance of the default implementation of @@ -169,9 +180,18 @@ func (w *serializeBuffer) AppendBytes(num int) ([]byte, error) { func (w *serializeBuffer) Clear() error { w.start = w.prepended w.data = w.data[:w.start] + w.layers = w.layers[:0] return nil } +func (w *serializeBuffer) Layers() []LayerType { + return w.layers +} + +func (w *serializeBuffer) PushLayer(l LayerType) { + w.layers = append(w.layers, l) +} + // SerializeLayers clears the given write buffer, then writes all layers into it so // they correctly wrap each other. Note that by clearing the buffer, it // invalidates all slices previously returned by w.Bytes() @@ -191,6 +211,22 @@ func SerializeLayers(w SerializeBuffer, opts SerializeOptions, layers ...Seriali if err != nil { return err } + w.PushLayer(layer.LayerType()) } return nil } + +// SerializePacket is a convenience function that calls SerializeLayers +// on packet's Layers(). +// It returns an error if one of the packet layers is not a SerializebleLayer. +func SerializePacket(buf SerializeBuffer, opts SerializeOptions, packet Packet) error { + sls := []SerializableLayer{} + for _, layer := range packet.Layers() { + sl, ok := layer.(SerializableLayer) + if !ok { + return fmt.Errorf("layer %s is not serializable", layer.LayerType().String()) + } + sls = append(sls, sl) + } + return SerializeLayers(buf, opts, sls...) +} |