diff --git a/lib/debug.ml b/lib/debug.ml index 2c70401..1b241ee 100644 --- a/lib/debug.ml +++ b/lib/debug.ml @@ -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) ;; diff --git a/lib/utils.ml b/lib/utils.ml index 144473c..8300b73 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -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