]> gitweb.michael.orlitzky.com - djbdns-logparse.git/blobdiff - djbdns/tinydns.py
djbdns/{dnscache,tinydns}.py: explicit imports from djbdns.common.
[djbdns-logparse.git] / djbdns / tinydns.py
index 52b06a4e338ea8acc2ce51da8ed90342b88ad80f..5c4275e5d74189ae61a86c7a142e4c5c8a5b9406 100644 (file)
@@ -1,6 +1,8 @@
-from re import compile
+# Don't clobber the global compile() with a named import.
+import re
+
 from typing import Optional
-from djbdns.common import *
+from djbdns.common import convert_ip, query_type_name, timestamp_pat
 
 # The "hex4" pattern matches a string of four hexadecimal digits. This
 # is used, for example, by tinydns to encode the query type
@@ -13,7 +15,7 @@ hex4_pat = r'[0-9a-f]{4}'
 ip_pat = r'[0-9a-f]{8,32}'
 
 # The regex to match tinydns log lines.
-tinydns_log_re = compile(
+tinydns_log_re = re.compile(
     rf'({timestamp_pat}) ({ip_pat}):({hex4_pat}):({hex4_pat}) ([\+\-IC/]) ({hex4_pat}) (.*)'
 )
 
@@ -63,33 +65,34 @@ def handle_tinydns_log(line : str) -> Optional[str]:
     if not match:
         return None
 
-    (timestamp, ip, port, id, code, type, name) = match.groups()
+    (timestamp, ip, port, request_id, code, query_type, name) = match.groups()
     ip = convert_ip(ip)
     port = int(port, 16)
-    id = int(id, 16)
+    request_id = int(request_id, 16)
 
     # Convert the "type" field to a human-readable record type name
     # using the query_type dictionary. If the right name isn't present
     # in the dictionary, we use the (decimal) type id instead.
-    type = int(type, 16)                # "001c" -> 28
-    type = query_type.get(type, type)   # 28 -> "aaaa"
+    query_type = int(query_type, 16)                     # "001c" -> 28
+    query_type = query_type_name.get(query_type, type)   # 28 -> "aaaa"
 
     line_tpl = "{timestamp} "
 
     reason = query_drop_reason[code]
     if code == "+":
-        line_tpl += "sent response to {ip}:{port} (id {id}): {type} {name}"
+        line_tpl += "sent response to {ip}:{port} (id {request_id}): "
+        line_tpl += "{query_type} {name}"
     else:
         line_tpl += "dropped query ({reason}) from {ip}:{port}"
         if code != "/":
             # If the query can actually be parsed, the log line is a
             # bit more informative than it would have been otherwise.
-            line_tpl += " (id {id}): {type} {name}"
+            line_tpl += " (id {request_id}): {query_type} {name}"
 
     return line_tpl.format(timestamp=timestamp,
                            reason=reason,
                            ip=ip,
                            port=port,
-                           id=id,
-                           type=type,
+                           request_id=request_id,
+                           query_type=query_type,
                            name=name)