-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmebase.go
114 lines (100 loc) · 3.28 KB
/
mebase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci
import (
"encoding/binary"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type MeBasePacket struct {
EntityClass me.ClassID
EntityInstance uint16
gopacket.Layer
layers.BaseLayer
MsgLayerType gopacket.LayerType
Extended bool
}
func (msg *MeBasePacket) String() string {
return fmt.Sprintf("ClassID: %v, InstanceId: %d/%#x",
msg.EntityClass, msg.EntityInstance, msg.EntityInstance)
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (msg *MeBasePacket) CanDecode() gopacket.LayerClass {
return msg.MsgLayerType
}
// LayerType returns MsgLayerType. It partially satisfies Layer and SerializableLayer
func (msg *MeBasePacket) LayerType() gopacket.LayerType {
return msg.MsgLayerType
}
// LayerContents returns the bytes of the packet layer.
func (msg *MeBasePacket) LayerContents() []byte {
return msg.Contents
}
// LayerPayload returns the bytes contained within the packet layer
func (msg *MeBasePacket) LayerPayload() []byte {
return msg.Payload
}
// NextLayerType returns the layer type contained by this DecodingLayer
func (msg *MeBasePacket) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypeZero
}
// DecodeFromBytes decodes the given bytes into this layer
func (msg *MeBasePacket) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, contentSize int) error {
if len(data) < contentSize {
p.SetTruncated()
layerType := msg.LayerType().String()
if msg.Extended {
layerType += " (extended)"
}
return fmt.Errorf("frame header too small. %v header length %v, %v required",
layerType, len(data), contentSize)
}
msg.EntityClass = me.ClassID(binary.BigEndian.Uint16(data[0:]))
msg.EntityInstance = binary.BigEndian.Uint16(data[2:])
msg.BaseLayer = layers.BaseLayer{Contents: data[:contentSize], Payload: data[contentSize:]}
return nil
}
// SerializeTo provides serialization of this message layer
func (msg *MeBasePacket) SerializeTo(b gopacket.SerializeBuffer) error {
// Add class ID and entity ID
bytes, err := b.PrependBytes(4)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes, uint16(msg.EntityClass))
binary.BigEndian.PutUint16(bytes[2:], msg.EntityInstance)
return nil
}
type layerDecodingLayer interface {
gopacket.Layer
DecodeFromBytes([]byte, gopacket.PacketBuilder) error
NextLayerType() gopacket.LayerType
}
func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error {
err := d.DecodeFromBytes(data, p)
if err != nil {
return err
}
p.AddLayer(d)
next := d.NextLayerType()
if next == gopacket.LayerTypeZero {
return nil
}
return p.NextDecoder(next)
}