Skip to content

Commit 1646a52

Browse files
committed
Store IP addresses (v4 + v6) as strings rather than ints
As mentioned by @pR0Ps in https://github.com/bitkeks/python-netflow-v9-softflowd/blame/6b9d20c8a6e2145aa2bd3c094b1f18fe31794555/analyze_json.py#L83 IP addresses, especially in IPv6, should better be stored as parsed strings instead of their raw integer values. Implemented.
1 parent 6b9d20c commit 1646a52

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

analyze_json.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,9 @@ def __repr__(self):
8080

8181
@staticmethod
8282
def get_ips(flow):
83-
# TODO: These values should be parsed into strings in the collection phase.
84-
# The floating point representation of an IPv6 address in JSON
85-
# could lose precision.
86-
8783
# IPv4
88-
if flow.get('IP_PROTOCOL_VERSION') == 4 \
89-
or 'IPV4_SRC_ADDR' in flow \
90-
or 'IPV4_DST_ADDR' in flow:
84+
if flow.get('IP_PROTOCOL_VERSION') == 4 or \
85+
'IPV4_SRC_ADDR' in flow or 'IPV4_DST_ADDR' in flow:
9186
return Pair(
9287
ipaddress.ip_address(flow['IPV4_SRC_ADDR']),
9388
ipaddress.ip_address(flow['IPV4_DST_ADDR'])

netflow/v9.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Licensed under MIT License. See LICENSE.
1414
"""
1515

16+
import ipaddress
1617
import struct
1718

1819

@@ -200,7 +201,6 @@ def __init__(self, data, templates):
200201
for field in template.fields:
201202
flen = field.field_length
202203
fkey = FIELD_TYPES[field.field_type]
203-
fdata = None
204204

205205
# The length of the value byte slice is defined in the template
206206
dataslice = data[offset:offset+flen]
@@ -210,7 +210,16 @@ def __init__(self, data, templates):
210210
for idx, byte in enumerate(reversed(bytearray(dataslice))):
211211
fdata += byte << (idx * 8)
212212

213-
new_record.data[fkey] = fdata
213+
# Special handling of IP addresses to convert integers to strings to not lose precision in dump
214+
if fkey in ["IPV4_SRC_ADDR", "IPV4_DST_ADDR", "IPV6_SRC_ADDR", "IPV6_DST_ADDR"]:
215+
try:
216+
ip = ipaddress.ip_address(fdata)
217+
except ValueError:
218+
print("IP address could not be parsed: {}".format(fdata))
219+
continue
220+
new_record.data[fkey] = ip.compressed
221+
else:
222+
new_record.data[fkey] = fdata
214223

215224
offset += flen
216225

0 commit comments

Comments
 (0)