From 7a5fd1e47275591f8692d5fcabc5a5694f31a781 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 14 Jun 2023 16:54:55 +0200 Subject: [PATCH] starting dns record parsing --- bin/main.ml | 6 ++++-- lib/debug.ml | 10 ++++++++++ lib/response.ml | 10 ++++++++-- lib/types.ml | 8 ++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 613cf14..c7ba09e 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -4,7 +4,9 @@ let () = let response = Network.send_request "8.8.8.8" "www.example.com" in print_endline (Utils.get_bytecode response); let response', dns_header = Response.parse_header response in - let dns_question = Response.parse_question response' in + let response'', dns_question = Response.parse_question response' in + let dns_record = Response.parse_record response'' in Debug.print_dns_header dns_header; - Debug.print_dns_question dns_question + Debug.print_dns_question dns_question; + Debug.print_dns_record dns_record ;; diff --git a/lib/debug.ml b/lib/debug.ml index f79f738..53f4f05 100644 --- a/lib/debug.ml +++ b/lib/debug.ml @@ -17,3 +17,13 @@ let print_dns_question (question : Types.dns_question) = question.type_ question.class_ ;; + +let print_dns_record (record : Types.dns_record) = + Printf.printf + "{ name = '%s'; type_ = %d; class_ = %d; ttl = %d; data = '%s' }\n" + (Bytes.to_string record.name) + record.type_ + record.class_ + record.ttl + (Bytes.to_string record.data) +;; diff --git a/lib/response.ml b/lib/response.ml index 56402d4..04b7890 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -13,8 +13,9 @@ let parse_header reader = let rec parse_question reader = let reader', name_b = decode_name_simple reader in let name = String.to_bytes name_b in - match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with - | [ type_; class_ ] -> { name; type_; class_ } + let max_size = 2 in + match List.init max_size (fun offset -> unpack_short_be reader' (offset * 2)) with + | [ type_; class_ ] -> bytes_forward reader' (max_size * 2), { name; type_; class_ } | _ -> failwith "Invalid number of fields" and decode_name_simple reader = @@ -30,3 +31,8 @@ and decode_name_simple reader = let offset, parts = read_parts [] 0 in bytes_forward reader offset, String.concat "." parts ;; + +let parse_record _reader = + let name, type_, class_, ttl, data = Bytes.empty, 0, 0, 0, Bytes.empty in + { name; type_; class_; ttl; data } +;; diff --git a/lib/types.ml b/lib/types.ml index ccfc017..de0d1d0 100644 --- a/lib/types.ml +++ b/lib/types.ml @@ -12,3 +12,11 @@ type dns_question = ; type_ : int ; class_ : int } + +type dns_record = + { name : bytes + ; type_ : int + ; class_ : int + ; ttl : int + ; data : bytes + }