This commit is contained in:
Mylloon 2023-07-04 13:12:59 +02:00
parent aa93508881
commit 4ebb582a06
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -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
;;