json compatible for debugging

This commit is contained in:
Mylloon 2023-06-19 20:44:32 +02:00
parent 7cfb6f83ac
commit 0166c01eba
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 8 additions and 7 deletions

View file

@ -1,7 +1,7 @@
let dns_header (header : Types.dns_header) =
Printf.sprintf
"{ id = %d; flags = %d; num_questions = %d; num_answers = %d; num_authorities = %d; \
num_additionals = %d }"
"{ \"id\": %d, \"flags\": %d, \"num_questions\": %d, \"num_answers\": %d, \
\"num_authorities\": %d, \"num_additionals\": %d }"
header.id
header.flags
header.num_questions
@ -12,7 +12,7 @@ let dns_header (header : Types.dns_header) =
let dns_question (question : Types.dns_question) =
Printf.sprintf
"{ name = '%s'; type_ = %d; class_ = %d }"
"{ \"name\": \"%s\", \"type_\": %d, \"class_\": %d }"
(Bytes.to_string question.name)
question.type_
question.class_
@ -20,10 +20,10 @@ let dns_question (question : Types.dns_question) =
let dns_record (record : Types.dns_record) =
Printf.sprintf
"{ name = '%s'; type_ = %d; class_ = %d; ttl = %d; data = '%s' }"
"{ \"name\": \"%s\", \"type_\": %d, \"class_\": %d, \"ttl\": %d, \"data\": \"%s\" }"
(Bytes.to_string record.name)
record.type_
record.class_
record.ttl
(Utils.get_bytecode record.data)
(Utils.get_bytecode ~json:true record.data)
;;

View file

@ -1,11 +1,12 @@
let get_bytecode data =
let get_bytecode ?(json = false) data =
let backslash = if json then "\\\\" else "\\" in
let result =
List.map
(fun byte ->
let code = Char.code byte in
if code >= 65 && code <= 122
then String.make 1 byte
else Printf.sprintf "\\x%02X" code)
else Printf.sprintf "%sx%02X" backslash code)
(List.of_seq (Bytes.to_seq data))
in
String.concat "" result