From 317d89301c8f5c5dac323c5ab70fe176559d0f01 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 5 Jul 2023 02:14:14 +0200 Subject: [PATCH] WIP: try to implement cname --- bin/main.ml | 2 +- lib/network.ml | 18 ++++++++++++++++-- lib/types.ml | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 5f50693..57f315b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,3 +1,3 @@ open Dnstoy -let () = print_endline (Network.resolve "www.mylloon.fr" Types.DNSType.a) +let () = print_endline (Network.resolve "www.facebook.com" Types.DNSType.a) diff --git a/lib/network.ml b/lib/network.ml index c2fec9a..46a7be3 100644 --- a/lib/network.ml +++ b/lib/network.ml @@ -40,7 +40,12 @@ and resolve_aux nameserver domain_name record_type = (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"))) + | None -> + (match get_alias response with + | Some cname_domain -> + resolve_aux (resolve cname_domain DNSType.a) domain_name record_type + | None -> + raise (Failure ("Something went wrong - " ^ Debug.dns_packet response))))) and get_answer packet = match List.find_opt (fun el -> el.type_ = DNSType.a) packet.answers with @@ -53,7 +58,16 @@ and get_nameserver_ip packet = | None -> None and get_nameserver packet = - match List.find_opt (fun el -> el.type_ = DNSType.ns) packet.authorities with + match + List.find_opt + (fun el -> el.type_ = DNSType.ns || el.type_ = DNSType.soa) + packet.authorities + with + | Some record -> Some (String.of_bytes record.data) + | None -> None + +and get_alias packet = + match List.find_opt (fun el -> el.type_ = DNSType.cname) packet.answers with | Some record -> Some (String.of_bytes record.data) | None -> None ;; diff --git a/lib/types.ml b/lib/types.ml index d38ff32..c927b28 100644 --- a/lib/types.ml +++ b/lib/types.ml @@ -37,5 +37,7 @@ type dns_packet = module DNSType = struct let a = 1 let ns = 2 + let cname = 5 + let soa = 6 let txt = 16 end