From 4ebb582a060f2418da6f045b832f8f746426edb8 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 4 Jul 2023 13:12:59 +0200 Subject: [PATCH] resolver --- lib/network.ml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/network.ml b/lib/network.ml index db83c46..c2fec9a 100644 --- a/lib/network.ml +++ b/lib/network.ml @@ -1,5 +1,6 @@ open Unix open Query +open Types let send_request dns url record_type = let query = build url record_type in @@ -24,3 +25,35 @@ let send ip_address domain_name record_type = let query = send_request ip_address domain_name record_type in Response.parse_dns_packet query ;; + +let rec resolve domain_name record_type = resolve_aux "198.41.0.4" domain_name record_type + +and resolve_aux nameserver domain_name record_type = + Printf.printf "Querying %s for %s\n" nameserver domain_name; + let response = send nameserver domain_name record_type in + match get_answer response with + | Some ip -> String.of_bytes ip + | None -> + (match get_nameserver_ip response with + | Some ns_ip -> resolve_aux ns_ip domain_name record_type + | None -> + (match get_nameserver response with + | Some ns_domain -> + resolve_aux (resolve ns_domain DNSType.a) domain_name record_type + | None -> raise (Failure "Something went wrong"))) + +and get_answer packet = + match List.find_opt (fun el -> el.type_ = DNSType.a) packet.answers with + | Some record -> Some record.data + | None -> None + +and get_nameserver_ip packet = + match List.find_opt (fun el -> el.type_ = DNSType.a) packet.additionals with + | Some record -> Some (String.of_bytes record.data) + | None -> None + +and get_nameserver packet = + match List.find_opt (fun el -> el.type_ = DNSType.ns) packet.authorities with + | Some record -> Some (String.of_bytes record.data) + | None -> None +;;