diff --git a/lib/dune b/lib/dune index a51dcb1..b1e0c0a 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,4 @@ (library (name dnstoy) - (modules types utils query network) + (modules types utils network query response) (libraries unix)) diff --git a/lib/query.ml b/lib/query.ml index d6218a3..45949cb 100644 --- a/lib/query.ml +++ b/lib/query.ml @@ -13,7 +13,7 @@ let header_to_bytes header = ] ;; -let question_to_bytes question = +let question_to_bytes (question : dns_question) = let buffer = new_buffer (2 * (get_obj_size question - 1)) [ question.type_; question.class_ ] in diff --git a/lib/response.ml b/lib/response.ml new file mode 100644 index 0000000..002a2cd --- /dev/null +++ b/lib/response.ml @@ -0,0 +1,9 @@ +open Types +open Utils + +let parse_header reader = + match List.init 6 (fun offset -> unpack_short_be reader (offset * 2)) with + | [ id; flags; num_questions; num_answers; num_authorities; num_additionals ] -> + { id; flags; num_questions; num_answers; num_authorities; num_additionals } + | _ -> failwith "Invalid number of fields" +;; diff --git a/lib/utils.ml b/lib/utils.ml index 369b526..3d996d3 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -26,3 +26,9 @@ let new_buffer size list = if verification != size then failwith "Issue converting header to bytes"; buffer ;; + +let unpack_short_be reader offset = + let msb = int_of_char (Bytes.get reader offset) in + let lsb = int_of_char (Bytes.get reader (offset + 1)) in + (msb lsl 8) + lsb +;;