I am trying to implement a vlan switch with the Dot1Q
layer. I can create a packet with NewPacket()
that either contains the layer or doesn't. My problem is I do not know how to remove/add the Dot1Q layer to tag/untag the vlan packets.
I could get NextLayerType
from the Dot1Q
layer and use NewPacket
, but then I would lose the Ethernet
layer?
Another possible approach would be to serialize the whole packet, excluding the Dot1Q
layer and then create a new one from the byte slice. Something like this (but using the real layers from the packet):
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{}
gopacket.SerializeLayers(buf, opts,
&layers.Ethernet{},
&layers.IPv4{},
&layers.TCP{},
gopacket.Payload([]byte{1, 2, 3, 4}))
bytes := buf.Bytes()
gopacket.NewPacket(bytes, layers.LayerTypeEthernet, gopacket.Default)
But this seams overly complicated (since I cannot directly access each layer) and not very performance optimized.
Is there a better way to remove or add a layer (between other layers)?