starting dns record parsing
This commit is contained in:
parent
bf337663df
commit
7a5fd1e472
4 changed files with 30 additions and 4 deletions
|
@ -4,7 +4,9 @@ let () =
|
|||
let response = Network.send_request "8.8.8.8" "www.example.com" in
|
||||
print_endline (Utils.get_bytecode response);
|
||||
let response', dns_header = Response.parse_header response in
|
||||
let dns_question = Response.parse_question response' in
|
||||
let response'', dns_question = Response.parse_question response' in
|
||||
let dns_record = Response.parse_record response'' in
|
||||
Debug.print_dns_header dns_header;
|
||||
Debug.print_dns_question dns_question
|
||||
Debug.print_dns_question dns_question;
|
||||
Debug.print_dns_record dns_record
|
||||
;;
|
||||
|
|
10
lib/debug.ml
10
lib/debug.ml
|
@ -17,3 +17,13 @@ let print_dns_question (question : Types.dns_question) =
|
|||
question.type_
|
||||
question.class_
|
||||
;;
|
||||
|
||||
let print_dns_record (record : Types.dns_record) =
|
||||
Printf.printf
|
||||
"{ name = '%s'; type_ = %d; class_ = %d; ttl = %d; data = '%s' }\n"
|
||||
(Bytes.to_string record.name)
|
||||
record.type_
|
||||
record.class_
|
||||
record.ttl
|
||||
(Bytes.to_string record.data)
|
||||
;;
|
||||
|
|
|
@ -13,8 +13,9 @@ let parse_header reader =
|
|||
let rec parse_question reader =
|
||||
let reader', name_b = decode_name_simple reader in
|
||||
let name = String.to_bytes name_b in
|
||||
match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with
|
||||
| [ type_; class_ ] -> { name; type_; class_ }
|
||||
let max_size = 2 in
|
||||
match List.init max_size (fun offset -> unpack_short_be reader' (offset * 2)) with
|
||||
| [ type_; class_ ] -> bytes_forward reader' (max_size * 2), { name; type_; class_ }
|
||||
| _ -> failwith "Invalid number of fields"
|
||||
|
||||
and decode_name_simple reader =
|
||||
|
@ -30,3 +31,8 @@ and decode_name_simple reader =
|
|||
let offset, parts = read_parts [] 0 in
|
||||
bytes_forward reader offset, String.concat "." parts
|
||||
;;
|
||||
|
||||
let parse_record _reader =
|
||||
let name, type_, class_, ttl, data = Bytes.empty, 0, 0, 0, Bytes.empty in
|
||||
{ name; type_; class_; ttl; data }
|
||||
;;
|
||||
|
|
|
@ -12,3 +12,11 @@ type dns_question =
|
|||
; type_ : int
|
||||
; class_ : int
|
||||
}
|
||||
|
||||
type dns_record =
|
||||
{ name : bytes
|
||||
; type_ : int
|
||||
; class_ : int
|
||||
; ttl : int
|
||||
; data : bytes
|
||||
}
|
||||
|
|
Reference in a new issue