Add query building

This commit is contained in:
Mylloon 2023-06-05 21:44:44 +02:00
parent 6a7042fe6d
commit ce7585ac6c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 26 additions and 1 deletions

View file

@ -32,3 +32,23 @@ let encode_dns_name domain_name =
in in
Bytes.cat (Bytes.concat Bytes.empty encoded_parts) (Bytes.of_string "\x00") Bytes.cat (Bytes.concat Bytes.empty encoded_parts) (Bytes.of_string "\x00")
;; ;;
let build ?(id = None) domain_name record_type =
let class_IN = 1 in
let header =
{ id =
(match id with
| Some i -> i
| None -> Random.int 65535)
; flags = 1 lsl 8
; num_questions = 1
; num_answers = 0
; num_authorities = 0
; num_additionals = 0
}
in
let question =
{ name = encode_dns_name domain_name; type_ = record_type; class_ = class_IN }
in
Bytes.cat (header_to_bytes header) (question_to_bytes question)
;;

View file

@ -17,5 +17,10 @@ let () =
(* Dns name *) (* Dns name *)
let dns_name = Query.encode_dns_name "google.com" in let dns_name = Query.encode_dns_name "google.com" in
assert ("\\x06google\\x03com\\x00" = Utils.get_bytecode dns_name); assert ("\\x06google\\x03com\\x00" = Utils.get_bytecode dns_name);
assert (6 = Char.code (Bytes.get dns_name 0)) assert (6 = Char.code (Bytes.get dns_name 0));
(* Query *)
let type_A = 1 in
assert (
"\\x15\\xB3\\x01\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x07example\\x03com\\x00\\x00\\x01\\x00\\x01"
= Utils.get_bytecode (Query.build ~id:(Some 5555) "example.com" type_A))
;; ;;