layers are inconsistently added during decoding
- Dominant language
- Go
- Stars
- 6.8k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
When decoding a UDP header, the layer is set even if decoding of the header failed due to it being truncated:
https://github.com/google/gopacket/blob/914d8955fc258b9a501fa5e227117c4cd293f981/layers/udp.go#L112-L121
On the other hand, for ICMPv6/ICMPv4, which use `decodingLayerDecoder`, the layer is not set.
https://github.com/google/gopacket/blob/914d8955fc258b9a501fa5e227117c4cd293f981/layers/base.go#L38-L49
What is the reasoning for this difference in behavior?
When creating a packet and requesting the `layers.LayerTypeUDP` layer, this means we get the zero value of the UDP layer back.
Is there an easy way to detect we should not trust the returned value?
In the use case I'm struggling with, I would like to parse truncated packets, but I need to differentiate whether the UDP payload
or the UDP header was truncated. This means I cannot simply check `Metadata().Truncated`.
```go
// Raw contains a truncated IP packet with UDP as L4. The UDP header is truncated as well.
pkt := gopacket.NewPacket(raw, layers.LayerTypeIPv4, ...)
if udp := pkt.Layer(layers.LayerTypeUDP); udp != nil {
// How to check whether truncated header or payload?
}
```
Contributor guide
Assessment
This issue has not been assessed yet.