add parser header

This commit is contained in:
Mylloon 2023-06-13 02:36:32 +02:00
parent d7c4b781c5
commit e15431d04e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 17 additions and 2 deletions

View file

@ -1,4 +1,4 @@
(library (library
(name dnstoy) (name dnstoy)
(modules types utils query network) (modules types utils network query response)
(libraries unix)) (libraries unix))

View file

@ -13,7 +13,7 @@ let header_to_bytes header =
] ]
;; ;;
let question_to_bytes question = let question_to_bytes (question : dns_question) =
let buffer = let buffer =
new_buffer (2 * (get_obj_size question - 1)) [ question.type_; question.class_ ] new_buffer (2 * (get_obj_size question - 1)) [ question.type_; question.class_ ]
in in

9
lib/response.ml Normal file
View file

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

View file

@ -26,3 +26,9 @@ let new_buffer size list =
if verification != size then failwith "Issue converting header to bytes"; if verification != size then failwith "Issue converting header to bytes";
buffer 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
;;