scroot / gopacket

Automatically exported from code.google.com/p/gopacket
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

IPv6Destination SerializeTo() support #28

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
diff --git a/layers/ip6.go b/layers/ip6.go
index 3f521fc..32ff67f 100644
--- a/layers/ip6.go
+++ b/layers/ip6.go
@@ -197,6 +197,14 @@ func decodeIPv6HeaderTLVOption(data []byte) (h 
ipv6HeaderTLVOption) {
        return
 }

+func (h ipv6HeaderTLVOption) Data() []byte {
+       data := make([]byte, int(h.OptionLength) + 2)
+       data[0] = h.OptionType
+       data[1] = h.OptionLength
+       copy(data[2:], h.OptionData)
+       return data
+}
+
 // IPv6HopByHopOption is a TLV option present in an IPv6 hop-by-hop extension.
 type IPv6HopByHopOption ipv6HeaderTLVOption

@@ -341,3 +349,21 @@ func decodeIPv6Destination(data []byte, p 
gopacket.PacketBuilder) error {
        p.AddLayer(i)
        return p.NextDecoder(i.NextHeader)
 }
+
+// SerializeTo writes the serialized form of this layer into the
+// SerializationBuffer, implementing gopacket.SerializableLayer.
+// See the docs for gopacket.SerializableLayer for more info.
+func (i *IPv6Destination) SerializeTo(b gopacket.SerializeBuffer, opts 
gopacket.SerializeOptions) error {
+       var optionData []byte
+       for _,opt := range i.Options {
+               optionData = append(optionData, 
ipv6HeaderTLVOption(opt).Data()...)
+       }
+       bytes, err := b.PrependBytes(2+len(optionData))
+       if err != nil {
+               return err
+       }
+       bytes[0] = uint8(i.NextHeader)
+       bytes[1] = i.HeaderLength
+       copy(bytes[2:], optionData)
+       return nil
+}

Original issue reported on code.google.com by Hiroaki.Kawai@gmail.com on 25 Jul 2014 at 1:16

GoogleCodeExporter commented 9 years ago
I've added the given code with some minor changes/optimizations.  See 
https://code.google.com/p/gopacket/source/detail?r=a7c3800045de6b7d548121c8f860a
ca38b4c255f

However, I'm unable to find any good pcaps with example IPv6Destination 
packets.  If you have the time/inclination and could capture me an example 
packet to use, I'd love to add a test for this behavior.

Original comment by gconnell@google.com on 28 Jul 2014 at 5:23