1import ipaddress
2import re
3
4
5def packet(line):
6 match = re.fullmatch(r"([0-9.]+) (?:\S+\s+(?:In|Out)\s+)?IP6? (\S+) > (\S+): Flags \[([A-Za-z.*]+)\], (.*)", line.strip())
7 if not match:
8 return None
9 stamp, source, destination, flags, details = match.groups()
10 endpoints = {}
11 try:
12 for key, value in (("source", source), ("destination", destination)):
13 host, _, port = value.rpartition(".")
14 host = ipaddress.ip_address(host)
15 endpoints[key] = f"[{host}]:{int(port)}"
16 except ValueError:
17 return None
18 row = {"ts": float(stamp), **endpoints, "flags": flags}
19 for key in ("seq", "ack", "win", "length"):
20 if value := re.search(r"\b" + key + r" ([0-9]+(?::[0-9]+)?)", details):
21 row[key] = value[1]
22 row["sack"] = re.findall(r"\{([0-9]+:[0-9]+)\}", details)
23 return row