Skip to content

Commit 54e19af

Browse files
committed
Adapt new V9OptionsTemplateFlowSet stub
Resolves #29
1 parent fcddb49 commit 54e19af

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

netflow/v9.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import ipaddress
1616
import struct
1717

18-
__all__ = ["V9DataFlowSet", "V9DataRecord", "V9ExportPacket", "V9Header", "V9TemplateField",
18+
__all__ = ["V9DataFlowSet", "V9DataRecord", "V9ExportPacket", "V9Header", "V9TemplateField", "V9OptionsTemplateFlowSet"
1919
"V9TemplateFlowSet", "V9TemplateNotRecognized", "V9TemplateRecord"]
2020

2121
V9_FIELD_TYPES = {
@@ -194,8 +194,6 @@ def __init__(self, data, templates):
194194
raise V9TemplateNotRecognized
195195

196196
template = templates[self.template_id]
197-
if len(template.fields) == 0:
198-
return #ignore options templates at the moment
199197

200198
# As the field lengths are variable V9 has padding to next 32 Bit
201199
padding_size = 4 - (self.length % 4) # 4 Byte
@@ -264,13 +262,18 @@ def __repr__(self):
264262
self.template_id, self.field_count,
265263
' '.join([V9_FIELD_TYPES[field.field_type] for field in self.fields]))
266264

265+
267266
class V9OptionsTemplateFlowSet:
267+
"""An options template flowset. Always uses flowset ID 1.
268+
TODO: not handled at the moment, only stub implementation
269+
"""
268270
def __init__(self, data):
269271
pack = struct.unpack('!HHH', data[:6])
270272
self.flowset_id = pack[0]
271273
self.length = pack[1]
272274
self.template_id = pack[2]
273275

276+
274277
class V9TemplateFlowSet:
275278
"""A template flowset, which holds an id that is used by data flowsets to
276279
reference back to the template. The template then has fields which hold
@@ -348,6 +351,7 @@ def __init__(self, data, templates):
348351
skipped_flowsets_offsets = []
349352
while offset != len(data):
350353
flowset_id = struct.unpack('!H', data[offset:offset + 2])[0]
354+
351355
if flowset_id == 0: # TemplateFlowSet always have id 0
352356
tfs = V9TemplateFlowSet(data[offset:])
353357

@@ -361,14 +365,13 @@ def __init__(self, data, templates):
361365
# Update the templates with the provided templates, even if they are the same
362366
self._templates.update(tfs.templates)
363367
offset += tfs.length
364-
elif flowset_id == 1:
365-
otfs = V9OptionsTemplateFlowSet(data[offset:])
366-
if not self._new_templates:
367-
if otfs.template_id not in self._templates:
368-
self._new_templates = True
369368

370-
self._templates.update({otfs.template_id: V9TemplateRecord(otfs.template_id, 0, {})})
369+
elif flowset_id == 1: # Option templates always use ID 1
370+
# TODO: Options templates are ignored, to prevent template ID collision
371+
# (if a collision can occur is not yet tested)
372+
otfs = V9OptionsTemplateFlowSet(data[offset:])
371373
offset += otfs.length
374+
372375
else:
373376
try:
374377
dfs = V9DataFlowSet(data[offset:], self._templates)

0 commit comments

Comments
 (0)