A resolver follows alias (CNAME) records until it finds an address, and caches every record for its own TTL.
What to build
A program that answers a list of DNS queries and counts how many server queries each one needed.
Requirements
- The server knows records
name TYPE value ttl, where TYPE isA(value is an address) orCNAME(value is another name). - To answer a query, look up the name. A cached record that has not expired (expiry > now) is free; otherwise ask the server (that is one query) and cache the record with expiry = now + ttl. For a CNAME, continue with its value until an A record is found.
- Print
time name -> address (K queries). If a name on the way has no record, printtime name -> nxdomain (K queries), where the failed lookup counts as a query and is not cached.
Input
Line 1: r, the number of records. Then r lines name TYPE value ttl. Then a line with q, and q lines time name in non-decreasing time order.
Output
One line per query.
Constraints
1 ≤ r ≤ 20; 1 ≤ q ≤ 30; alias chains have no loops.
Example
Input:
3
www.shop.test CNAME shop.test 100
shop.test A 10.0.0.1 50
other.test A 10.0.0.2 10
4
0 www.shop.test
10 www.shop.test
60 www.shop.test
70 other.test
Output:
0 www.shop.test -> 10.0.0.1 (2 queries)
10 www.shop.test -> 10.0.0.1 (0 queries)
60 www.shop.test -> 10.0.0.1 (1 queries)
70 other.test -> 10.0.0.2 (1 queries)
Why it works
Each record in the chain is cached on its own with its own lifetime, so a lookup may be partly answered from the cache. Following the alias chain until an address shows why CDN names cost several lookups on a cold cache.
Examples
3 www.shop.test CNAME shop.test 100 shop.test A 10.0.0.1 50 other.test A 10.0.0.2 10 4 0 www.shop.test 10 www.shop.test 60 www.shop.test 70 other.test
0 www.shop.test -> 10.0.0.1 (2 queries) 10 www.shop.test -> 10.0.0.1 (0 queries) 60 www.shop.test -> 10.0.0.1 (1 queries) 70 other.test -> 10.0.0.2 (1 queries)
1 a.test A 1.1.1.1 5 2 0 b.test 1 a.test
0 b.test -> nxdomain (1 queries) 1 a.test -> 1.1.1.1 (1 queries)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a cache dict from name to (record type, value, expiry). A cache entry is usable when its expiry is greater than the current time.
Hint 2
To resolve a name: look it up (cache or server). If it is a CNAME, resolve the target next; stop when you reach an A record.
Hint 3
Count one query for every record you had to ask the server for. A name with no record gives nxdomain, and that failed lookup also counts as one query.
Solution
Show one correct solution (try it yourself first)
r = int(input())
records = {}
for _ in range(r):
name, kind, value, ttl = input().split()
records[name] = (kind, value, int(ttl))
q = int(input())
cache = {}
for _ in range(q):
t, name = input().split()
t = int(t)
asked = 0
current = name
answer = None
while answer is None:
if current in cache and cache[current][2] > t:
kind, value, _expiry = cache[current]
elif current in records:
asked += 1
kind, value, ttl = records[current]
cache[current] = (kind, value, t + ttl)
else:
asked += 1
answer = "nxdomain"
break
if kind == "A":
answer = value
else:
current = value
print(str(t) + " " + name + " -> " + answer + " (" + str(asked) + " queries)")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step