123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package layers
- import (
- "encoding/binary"
- "fmt"
- "github.com/google/gopacket"
- )
- type EthernetCTPFunction uint16
- const (
- EthernetCTPFunctionReply EthernetCTPFunction = 1
- EthernetCTPFunctionForwardData EthernetCTPFunction = 2
- )
- type EthernetCTP struct {
- BaseLayer
- SkipCount uint16
- }
- func (c *EthernetCTP) LayerType() gopacket.LayerType {
- return LayerTypeEthernetCTP
- }
- type EthernetCTPForwardData struct {
- BaseLayer
- Function EthernetCTPFunction
- ForwardAddress []byte
- }
- func (c *EthernetCTPForwardData) LayerType() gopacket.LayerType {
- return LayerTypeEthernetCTPForwardData
- }
- func (c *EthernetCTPForwardData) ForwardEndpoint() gopacket.Endpoint {
- return gopacket.NewEndpoint(EndpointMAC, c.ForwardAddress)
- }
- type EthernetCTPReply struct {
- BaseLayer
- Function EthernetCTPFunction
- ReceiptNumber uint16
- Data []byte
- }
- func (c *EthernetCTPReply) LayerType() gopacket.LayerType {
- return LayerTypeEthernetCTPReply
- }
- func (c *EthernetCTPReply) Payload() []byte { return c.Data }
- func decodeEthernetCTP(data []byte, p gopacket.PacketBuilder) error {
- c := &EthernetCTP{
- SkipCount: binary.LittleEndian.Uint16(data[:2]),
- BaseLayer: BaseLayer{data[:2], data[2:]},
- }
- if c.SkipCount%2 != 0 {
- return fmt.Errorf("EthernetCTP skip count is odd: %d", c.SkipCount)
- }
- p.AddLayer(c)
- return p.NextDecoder(gopacket.DecodeFunc(decodeEthernetCTPFromFunctionType))
- }
- func decodeEthernetCTPFromFunctionType(data []byte, p gopacket.PacketBuilder) error {
- function := EthernetCTPFunction(binary.LittleEndian.Uint16(data[:2]))
- switch function {
- case EthernetCTPFunctionReply:
- reply := &EthernetCTPReply{
- Function: function,
- ReceiptNumber: binary.LittleEndian.Uint16(data[2:4]),
- Data: data[4:],
- BaseLayer: BaseLayer{data, nil},
- }
- p.AddLayer(reply)
- p.SetApplicationLayer(reply)
- return nil
- case EthernetCTPFunctionForwardData:
- forward := &EthernetCTPForwardData{
- Function: function,
- ForwardAddress: data[2:8],
- BaseLayer: BaseLayer{data[:8], data[8:]},
- }
- p.AddLayer(forward)
- return p.NextDecoder(gopacket.DecodeFunc(decodeEthernetCTPFromFunctionType))
- }
- return fmt.Errorf("Unknown EthernetCTP function type %v", function)
- }
|