]>
gitweb.michael.orlitzky.com - djbdns-logparse.git/blob - djbdns/common.py
2 Information needed to parse *both* tinydns and dnscache logs.
4 from struct
import pack
6 # A pattern to match the timestamp format that the tai64nlocal program
7 # produces. It appears in both dnscache and tinydns lines, after
8 # they've been piped through tai64nlocal, of course.
9 TIMESTAMP_PAT
= r
'[\d-]+ [\d:\.]+'
11 # A dictionary mapping query type identifiers, in decimal, to their
12 # friendly names for tinydns. Reference:
14 # https://en.wikipedia.org/wiki/List_of_DNS_record_types
16 # This list *should* be exhaustive, and we hope it is, because the log
17 # parser will now crash if it encounters a type it doesn't know about.
108 def convert_ip(ip
: str) -> str:
110 Convert a hex string representing an IP address to
117 The hexadecimal representation of either an IPv4 or an IPv6
123 The usual decimal dotted-quad representation is returned for an
124 IPv4 address. IPv6 addresses are returned almost as-is, but with
125 colons inserted in the appropriate places, between every four
131 >>> convert_ip("7f000001")
133 >>> convert_ip("00000000000000000000ffff7f000001")
134 '0000:0000:0000:0000:0000:ffff:7f00:0001'
137 # IPv4, eg. "7f000001" -> "7f 00 00 01" -> "127.0.0.1"
138 return ".".join(map(str, pack(">L", int(ip
, 16))))
140 # IPv6 is actually simpler -- it's just a string-slicing operation.
141 return ":".join([ip
[(4*i
) : (4*i
+4)] for i
in range(8)])