]>
gitweb.michael.orlitzky.com - djbdns-logparse.git/blob - djbdns/common.py
1 from struct
import pack
3 # A pattern to match the timestamp format that the tai64nlocal program
4 # produces. It appears in both dnscache and tinydns lines, after
5 # they've been piped through tai64nlocal, of course.
6 timestamp_pat
= r
'[\d-]+ [\d:\.]+'
8 # A dictionary mapping query type identifiers, in decimal, to their
9 # friendly names for tinydns. Reference:
11 # https://en.wikipedia.org/wiki/List_of_DNS_record_types
13 # Note that mapping here is non-exhaustive, and that tinydns will
14 # log responses for record types that it does not know about.
39 def convert_ip(ip
: str) -> str:
41 Convert a hex string representing an IP address to
48 The hexadecimal representation of either an IPv4 or an IPv6
54 The usual decimal dotted-quad representation is returned for an
55 IPv4 address. IPv6 addresses are returned almost as-is, but with
56 colons inserted in the appropriate places, between every four
62 >>> convert_ip("7f000001")
64 >>> convert_ip("00000000000000000000ffff7f000001")
65 '0000:0000:0000:0000:0000:ffff:7f00:0001'
68 # IPv4, eg. "7f000001" -> "7f 00 00 01" -> "127.0.0.1"
69 return ".".join(map(str, pack(">L", int(ip
, 16))))
71 # IPv6 is actually simpler -- it's just a string-slicing operation.
72 return ":".join([ip
[(4*i
) : (4*i
+4)] for i
in range(8)])