1// Copyright 2012 Google, Inc. All rights reserved.
2// Copyright 2009-2011 Andreas Krennmair. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style license
5// that can be found in the LICENSE file in the root of the source
6// tree.
7
8package layers
9
10import (
11	"encoding/binary"
12	"github.com/google/gopacket"
13)
14
15// UDPLite is the layer for UDP-Lite headers (rfc 3828).
16type UDPLite struct {
17	BaseLayer
18	SrcPort, DstPort UDPLitePort
19	ChecksumCoverage uint16
20	Checksum         uint16
21	sPort, dPort     []byte
22}
23
24// LayerType returns gopacket.LayerTypeUDPLite
25func (u *UDPLite) LayerType() gopacket.LayerType { return LayerTypeUDPLite }
26
27func decodeUDPLite(data []byte, p gopacket.PacketBuilder) error {
28	udp := &UDPLite{
29		SrcPort:          UDPLitePort(binary.BigEndian.Uint16(data[0:2])),
30		sPort:            data[0:2],
31		DstPort:          UDPLitePort(binary.BigEndian.Uint16(data[2:4])),
32		dPort:            data[2:4],
33		ChecksumCoverage: binary.BigEndian.Uint16(data[4:6]),
34		Checksum:         binary.BigEndian.Uint16(data[6:8]),
35		BaseLayer:        BaseLayer{data[:8], data[8:]},
36	}
37	p.AddLayer(udp)
38	p.SetTransportLayer(udp)
39	return p.NextDecoder(gopacket.LayerTypePayload)
40}
41
42func (u *UDPLite) TransportFlow() gopacket.Flow {
43	return gopacket.NewFlow(EndpointUDPLitePort, u.sPort, u.dPort)
44}
45