-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathevent_discarded_packet.go
185 lines (155 loc) · 3.67 KB
/
event_discarded_packet.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
package sflow
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type EventDiscardedPacket struct {
SequenceNum uint32
DsClass uint32
DsIndex uint32
Drops uint32
Input uint32
Output uint32
Reason uint32
numRecords uint32
Records []Record
}
func (s EventDiscardedPacket) String() string {
type X EventDiscardedPacket
x := X(s)
return fmt.Sprintf("EventDiscardedPacket: %+v", x)
}
// SampleType returns the type of sFlow sample.
func (s *EventDiscardedPacket) SampleType() int {
return TypeEventDiscardedPacket
}
func (s *EventDiscardedPacket) GetRecords() []Record {
return s.Records
}
func decodEventDiscardedPacket(r io.ReadSeeker) (Sample, error) {
s := &EventDiscardedPacket{}
var err error
err = binary.Read(r, binary.BigEndian, &s.SequenceNum)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.DsClass)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.DsIndex)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Drops)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Input)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Output)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Reason)
if err != nil {
return nil, fmt.Errorf("read reson %v", err)
}
err = binary.Read(r, binary.BigEndian, &s.numRecords)
if err != nil {
return nil, fmt.Errorf("read numRecords %v", err)
}
for i := uint32(0); i < s.numRecords; i++ {
format, length := uint32(0), uint32(0)
err = binary.Read(r, binary.BigEndian, &format)
if err != nil {
return nil, fmt.Errorf("read record format %d %v", i, err)
}
err = binary.Read(r, binary.BigEndian, &length)
if err != nil {
return nil, fmt.Errorf("read record length %d %v", i, err)
}
var rec Record
switch format {
case TypeRawPacketFlowRecord:
rec, err = decodeRawPacketFlow(r)
if err != nil {
return nil, fmt.Errorf("read record flow %d %v", i, err)
}
case TypeExtendedSwitchFlowRecord:
rec, err = decodedExtendedSwitchFlow(r)
if err != nil {
return nil, fmt.Errorf("read record switch flow %d %v", i, err)
}
default:
_, err := r.Seek(int64(length), 1)
if err != nil {
return nil, fmt.Errorf("read record seek %d %v", i, err)
}
continue
}
s.Records = append(s.Records, rec)
}
return s, nil
}
func (s *EventDiscardedPacket) encode(w io.Writer) error {
var err error
// We first need to encode the records.
buf := &bytes.Buffer{}
for _, rec := range s.Records {
err = rec.encode(buf)
if err != nil {
return ErrEncodingRecord
}
}
// Fields
encodedSampleSize := uint32(4 * 8)
// Encoded records
encodedSampleSize += uint32(buf.Len())
err = binary.Write(w, binary.BigEndian, uint32(s.SampleType()))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, encodedSampleSize)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.SequenceNum)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.DsClass)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.DsIndex)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Drops)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Input)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Output)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Reason)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, uint32(len(s.Records)))
if err != nil {
return err
}
_, err = io.Copy(w, buf)
return err
}