-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrelaxed_decode.go
210 lines (174 loc) · 6.96 KB
/
relaxed_decode.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* 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 (
"errors"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type UnknownAttributeInfo struct {
EntityClass me.ClassID
EntityInstance uint16
AttributeMask uint16
AttributeData []byte
ErrorType me.AttributeErrorType
}
type UnknownAttributes struct {
// Each Attributes entry relates one or more unknown attributes to a specific managed
// entity. For message types such as MIB Upload Next responses, there may be multiple
// Managed Entities in a single response if the Extended Message set is being used. This
// error condition is specified with the ErrorType of "UnknownAttribute"
//
// For MIB Upload Next responses, the error type "InvalidTableAttributes" indicates
// the the response has a table attribute encoded which is in violation of G.988 but
// has been seen with more than one ONU vendor.
Attributes []UnknownAttributeInfo
gopacket.Layer
layers.BaseLayer
MsgLayerType gopacket.LayerType
}
func (msg *UnknownAttributes) String() string {
return fmt.Sprintf("unknown or invalid table attributes, %v Managed Entities", len(msg.Attributes))
}
// LayerType returns LayerTypeGetNextResponse
func (msg *UnknownAttributes) LayerType() gopacket.LayerType {
return LayerTypeUnknownAttributes
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (msg *UnknownAttributes) CanDecode() gopacket.LayerClass {
return LayerTypeUnknownAttributes
}
// LayerContents returns the bytes of the packet layer.
func (msg *UnknownAttributes) LayerContents() []byte {
return msg.Contents
}
// LayerPayload returns the bytes contained within the packet layer
func (msg *UnknownAttributes) LayerPayload() []byte {
return msg.Payload
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (msg *UnknownAttributes) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypeZero
}
// DecodeFromBytes decodes the given bytes of a Get Next Response into this layer
func (msg *UnknownAttributes) DecodeFromBytes(_ []byte, _ gopacket.PacketBuilder) error {
// This is not a real layer. It is used to pass on relaxed decode error information
// as an ErrorLayer
return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
}
func decodeUnknownAttributes(_ []byte, _ gopacket.PacketBuilder) error {
return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
}
func (msg *UnknownAttributes) Error() error {
return fmt.Errorf("%v managed entities with unknown or invalid table attributes detected during decode",
len(msg.Attributes))
}
func newUnknownAttributesLayer(prevLayer gopacket.Layer, errInfo []me.IRelaxedDecodeError, p gopacket.PacketBuilder) error {
// Add the previous layer
p.AddLayer(prevLayer)
// Append unknown attributes layer and also set ErrorLayer
errLayer := &UnknownAttributes{
Attributes: make([]UnknownAttributeInfo, 0),
MsgLayerType: LayerTypeUnknownAttributes,
}
for _, item := range errInfo {
unknown, ok := item.(*me.UnknownAttributeDecodeError)
if !ok {
return fmt.Errorf("only UnknownAttributeDecodeError information can be encoded. Found %T",
unknown)
}
data := UnknownAttributeInfo{
EntityClass: unknown.EntityClass,
EntityInstance: unknown.EntityInstance,
AttributeMask: unknown.AttributeMask,
ErrorType: unknown.ErrorType,
}
if unknown.Contents != nil {
data.AttributeData = make([]byte, len(unknown.Contents))
copy(data.AttributeData, unknown.Contents)
}
errLayer.Attributes = append(errLayer.Attributes, data)
}
p.AddLayer(errLayer)
p.SetErrorLayer(errLayer)
// Return a valid error so that packet decoding stops
return errLayer.Error()
}
func (info *UnknownAttributeInfo) Error() error {
return fmt.Errorf("%v detected during attribute decode. Attribute Mask: 0x%04x",
info.ErrorType, info.AttributeMask)
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
type UnknownAlarms struct {
InvalidAlarmData []byte
gopacket.Layer
layers.BaseLayer
MsgLayerType gopacket.LayerType
}
func (msg *UnknownAlarms) String() string {
return "invalid alarm bits detected or alarm not supported"
}
// LayerType returns LayerTypeGetNextResponse
func (msg *UnknownAlarms) LayerType() gopacket.LayerType {
return LayerTypeUnknownAlarm
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (msg *UnknownAlarms) CanDecode() gopacket.LayerClass {
return LayerTypeUnknownAlarm
}
// LayerContents returns the bytes of the packet layer.
func (msg *UnknownAlarms) LayerContents() []byte {
return msg.Contents
}
// LayerPayload returns the bytes contained within the packet layer
func (msg *UnknownAlarms) LayerPayload() []byte {
return msg.Payload
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (msg *UnknownAlarms) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypeZero
}
// DecodeFromBytes decodes the given bytes of a Get Next Response into this layer
func (msg *UnknownAlarms) DecodeFromBytes(_ []byte, _ gopacket.PacketBuilder) error {
// This is not a real layer. It is used to pass on relaxed decode error information
// as an ErrorLayer
return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
}
func decodeUnknownAlarms(_ []byte, _ gopacket.PacketBuilder) error {
return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
}
func (msg *UnknownAlarms) Error() error {
return errors.New("invalid alarm bits detected or alarm not supported")
}
func newUnknownAlarmsLayer(prevLayer gopacket.Layer, errInfo me.IRelaxedDecodeError, p gopacket.PacketBuilder) error {
// Add the previous layer
p.AddLayer(prevLayer)
// Add unknown alarm error layer and also set ErrorLayer
unknown, ok := errInfo.(*me.UnknownAlarmDecodeError)
if !ok {
return fmt.Errorf("only UnknownAlarmsError information can be encoded. Found %T",
unknown)
}
errLayer := &UnknownAlarms{
InvalidAlarmData: unknown.Contents,
MsgLayerType: LayerTypeUnknownAlarm,
}
p.AddLayer(errLayer)
p.SetErrorLayer(errLayer)
// Return a valid error so that packet decoding stops
return errLayer.Error()
}