Add query building
This commit is contained in:
parent
6a7042fe6d
commit
ce7585ac6c
2 changed files with 26 additions and 1 deletions
20
lib/query.ml
20
lib/query.ml
|
@ -32,3 +32,23 @@ let encode_dns_name domain_name =
|
|||
in
|
||||
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)
|
||||
;;
|
||||
|
|
|
@ -17,5 +17,10 @@ let () =
|
|||
(* Dns name *)
|
||||
let dns_name = Query.encode_dns_name "google.com" in
|
||||
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))
|
||||
;;
|
||||
|
|
Reference in a new issue