add parser header
This commit is contained in:
parent
d7c4b781c5
commit
e15431d04e
4 changed files with 17 additions and 2 deletions
2
lib/dune
2
lib/dune
|
@ -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))
|
||||||
|
|
|
@ -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
9
lib/response.ml
Normal 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"
|
||||||
|
;;
|
|
@ -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
|
||||||
|
;;
|
||||||
|
|
Reference in a new issue