-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
307 lines (293 loc) · 9.67 KB
/
config.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"fmt"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
"log"
"net"
"os"
"sort"
"time"
)
const (
defaultMaxFlows = 65536
defaultSnapLength = 1518
exportBufferSize = 1472
defaultFlowActiveTimeout = 1800
defaultFlowIdleTimeout = 15
)
func (cache *IETFIpfixPsamp_Ipfix_Cache) NewCache(ianaIEsUint map[uint16]IERecord, ianaIEsString map[string]IERecord) Cache {
maxFlows := uint32(defaultMaxFlows)
activeTimeout := uint32(defaultFlowActiveTimeout)
idleTimeout := uint32(defaultFlowIdleTimeout)
cacheFields := []CacheField{}
if cache.TimeoutCache != nil && cache.TimeoutCache.CacheLayout != nil {
for _, field := range cache.TimeoutCache.CacheLayout.CacheField {
cacheField, err := NewCacheField(field.IeId, field.IeLength, field.IeName, bool(field.IsFlowKey), field.Name, ianaIEsUint, ianaIEsString)
if err == nil {
cacheFields = append(cacheFields, cacheField)
}
if cache.TimeoutCache.MaxFlows != nil {
maxFlows = *cache.TimeoutCache.MaxFlows
}
if cache.TimeoutCache.ActiveTimeout != nil {
activeTimeout = *cache.TimeoutCache.ActiveTimeout
}
if cache.TimeoutCache.IdleTimeout != nil {
idleTimeout = *cache.TimeoutCache.IdleTimeout
}
}
} else if cache.NaturalCache != nil && cache.NaturalCache.CacheLayout != nil {
for _, field := range cache.NaturalCache.CacheLayout.CacheField {
cacheField, err := NewCacheField(field.IeId, field.IeLength, field.IeName, bool(field.IsFlowKey), field.Name, ianaIEsUint, ianaIEsString)
if err == nil {
cacheFields = append(cacheFields, cacheField)
}
if cache.NaturalCache.MaxFlows != nil {
maxFlows = *cache.NaturalCache.MaxFlows
}
if cache.NaturalCache.ActiveTimeout != nil {
activeTimeout = *cache.NaturalCache.ActiveTimeout
}
if cache.NaturalCache.IdleTimeout != nil {
idleTimeout = *cache.NaturalCache.IdleTimeout
}
}
} else if cache.PermanentCache != nil && cache.PermanentCache.CacheLayout != nil {
for _, field := range cache.PermanentCache.CacheLayout.CacheField {
cacheField, err := NewCacheField(field.IeId, field.IeLength, field.IeName, bool(field.IsFlowKey), field.Name, ianaIEsUint, ianaIEsString)
if err == nil {
cacheFields = append(cacheFields, cacheField)
}
if cache.PermanentCache.MaxFlows != nil {
maxFlows = *cache.PermanentCache.MaxFlows
}
activeTimeout = 0
idleTimeout = 0
}
} else if cache.ImmediateCache != nil && cache.ImmediateCache.CacheLayout != nil {
for _, field := range cache.ImmediateCache.CacheLayout.CacheField {
cacheField, err := NewCacheField(field.IeId, field.IeLength, field.IeName, bool(field.IsFlowKey), field.Name, ianaIEsUint, ianaIEsString)
if err == nil {
cacheFields = append(cacheFields, cacheField)
}
maxFlows = 0
}
}
sort.SliceStable(cacheFields,
func(i int, j int) bool {
if cacheFields[i].FieldName == cacheFields[j].FieldName {
return cacheFields[i].IeId < cacheFields[j].IeId
}
return cacheFields[i].FieldName < cacheFields[j].FieldName
})
var c Cache
c.Name = ""
if cache.Name != nil {
c.Name = *cache.Name
}
c.ExportingProcessName = cache.ExportingProcess
c.Parameters.maxFlows = maxFlows
c.Parameters.activeTimeout = activeTimeout
c.Parameters.idleTimeout = idleTimeout
c.Data = NewCacheData(maxFlows)
c.Fields = cacheFields
c.destinationPointers = []*Destination{}
for _, v := range c.Fields {
c.dataRecordSize += v.IeLength
}
return c
}
func (destination *IETFIpfixPsamp_Ipfix_ExportingProcess_Destination) NewDestination(epName string) Destination {
var dest Destination
dest.ExportingProcessName = epName
dest.Protocol = "udp"
dest.Port = uint16(4739)
dest.IPAddress = ""
dest.Version = uint16(10)
dest.BufferSize = uint32(exportBufferSize)
dest.UsedBufferSize = 0
dest.Name = ""
dest.templateRefreshTimeout = 600
dest.optionTemplateRefreshTimeout = 600
if destination.Name != nil {
dest.Name = *destination.Name
}
if destination.TcpExporter != nil {
dest.Protocol = "tcp"
if destination.TcpExporter.DestinationIPAddress != nil {
dest.IPAddress = *destination.TcpExporter.DestinationIPAddress
} else {
fmt.Errorf("destinatinoIPAddress is not specified\n")
}
if destination.TcpExporter.DestinationPort != nil {
dest.Port = *destination.TcpExporter.DestinationPort
}
if destination.TcpExporter.IpfixVersion != nil {
dest.Version = *destination.TcpExporter.IpfixVersion
}
if destination.TcpExporter.SendBufferSize != nil {
dest.BufferSize = *destination.TcpExporter.SendBufferSize
} else {
dest.BufferSize = exportBufferSize
}
} else if destination.UdpExporter != nil {
dest.Protocol = "udp"
if destination.UdpExporter != nil {
dest.IPAddress = *destination.UdpExporter.DestinationIPAddress
} else {
fmt.Errorf("destinatinoIPAddress is not specified\n")
}
if destination.UdpExporter.DestinationPort != nil {
dest.Port = *destination.UdpExporter.DestinationPort
}
if destination.UdpExporter.IpfixVersion != nil {
dest.Version = *destination.UdpExporter.IpfixVersion
}
if destination.UdpExporter.TemplateRefreshTimeout != nil {
dest.templateRefreshTimeout = *destination.UdpExporter.TemplateRefreshTimeout
}
if destination.UdpExporter.OptionsTemplateRefreshTimeout != nil {
dest.optionTemplateRefreshTimeout = *destination.UdpExporter.OptionsTemplateRefreshTimeout
}
if destination.UdpExporter.TemplateRefreshPacket != nil {
dest.templateRefreshPacket = *destination.UdpExporter.TemplateRefreshPacket
}
if destination.UdpExporter.OptionsTemplateRefreshPacket != nil {
dest.optionTemplateRefreshPacket = *destination.UdpExporter.OptionsTemplateRefreshPacket
}
} else {
fmt.Errorf("Unsported export protocol\n")
}
dest.connection, _ = net.Dial(dest.Protocol,
fmt.Sprintf("%s:%d", dest.IPAddress, dest.Port))
dest.buffer = make([]byte, dest.BufferSize)
dest.BaseTime = time.Now()
return dest
}
func (op *IETFIpfixPsamp_Ipfix_ObservationPoint) NewPacketReader(pcapSource string, file **os.File) (PacketReader, error) {
if op.OfflineFile != nil {
if op.PcapgoImplementation != nil {
var err error
*file, err = os.Open(pcapSource)
if err != nil {
return nil, err
}
return pcapgo.NewReader(*file)
}
return pcap.OpenOffline(pcapSource)
}
if op.PcapgoImplementation != nil {
return pcapgo.NewEthernetHandle(pcapSource)
}
snapLength := int32(defaultSnapLength)
if op.SnapLength != nil {
snapLength = *op.SnapLength
}
return pcap.OpenLive(pcapSource, snapLength, (op.Promiscuous != nil), pcap.BlockForever)
}
func (op *IETFIpfixPsamp_Ipfix_ObservationPoint) NewPacketSource(ifName string, spName []string) PacketSource {
var packetSource PacketSource
var err error
packetSource.SelectionProcessName = spName
packetSource.selectorPointers = []*Selector{}
packetSource.reader, err = op.NewPacketReader(ifName, &packetSource.file)
if err != nil {
log.Fatal(err)
}
packetSource.Name = ifName
if op.ObservationDomainId != nil {
packetSource.observationDomainId = *op.ObservationDomainId
}
return packetSource
}
func (selector *IETFIpfixPsamp_Ipfix_SelectionProcess_Selector) NewSelector(spName string, cacheName string) Selector {
var s Selector
s.Name = ""
s.SelectionProcessName = spName
s.CacheName = cacheName
if selector.Name != nil {
s.Name = *selector.Name
}
if selector.SelectAll == true {
s.Algorithm = SelectAll
} else if selector.SampCountBased != nil {
s.Algorithm = CountBasedSampling
if selector.SampCountBased.PacketInterval != nil {
s.Interval = *selector.SampCountBased.PacketInterval
} else {
s.Interval = 1
}
if selector.SampCountBased.PacketSpace != nil {
s.Space = *selector.SampCountBased.PacketSpace
} else {
s.Space = 0
}
} else if selector.SampTimeBased != nil {
s.Algorithm = TimeBasedSampling
if selector.SampTimeBased.TimeInterval != nil {
s.Interval = *selector.SampTimeBased.TimeInterval
} else {
s.Interval = 1
}
if selector.SampTimeBased.TimeSpace != nil {
s.Space = *selector.SampTimeBased.TimeSpace
} else {
s.Space = 0
}
}
return s
}
func (ipfix *IETFIpfixPsamp_Ipfix) NewCaches(ianaIEsUint map[uint16]IERecord, ianaIEsString map[string]IERecord) []Cache {
caches := []Cache{}
for _, cache := range ipfix.Cache {
caches = append(caches, cache.NewCache(ianaIEsUint, ianaIEsString))
}
sort.SliceStable(caches,
func(i int, j int) bool {
return caches[i].Name < caches[j].Name
})
for i, _ := range caches {
caches[i].Index = i
}
return caches
}
func (ipfix *IETFIpfixPsamp_Ipfix) NewDestinations() []Destination {
destinations := []Destination{}
for epName, ep := range ipfix.ExportingProcess {
for _, destination := range ep.Destination {
destinations = append(destinations, destination.NewDestination(epName))
}
}
sort.SliceStable(destinations,
func(i int, j int) bool {
return destinations[i].Name < destinations[j].Name
})
return destinations
}
func (ipfix *IETFIpfixPsamp_Ipfix) NewPacketSources() []PacketSource {
packetSources := []PacketSource{}
for _, op := range ipfix.ObservationPoint {
if op.OfflineFile != nil {
packetSources = append(packetSources, op.NewPacketSource(*op.OfflineFile, op.SelectionProcess))
continue // if offlineFile is specified, IfName is not processed.
}
for _, v := range op.IfName {
packetSources = append(packetSources, op.NewPacketSource(v, op.SelectionProcess))
}
}
return packetSources
}
func (ipfix *IETFIpfixPsamp_Ipfix) NewSelectors() []Selector {
selectors := []Selector{}
for spName, sp := range ipfix.SelectionProcess {
for _, selector := range sp.Selector {
selectors = append(selectors, selector.NewSelector(spName, *sp.Cache))
}
}
sort.SliceStable(selectors,
func(i int, j int) bool {
return selectors[i].Name < selectors[j].Name
})
return selectors
}