starting dns record parsing

This commit is contained in:
Mylloon 2023-06-14 16:54:55 +02:00
parent bf337663df
commit 7a5fd1e472
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 30 additions and 4 deletions

View file

@ -4,7 +4,9 @@ let () =
let response = Network.send_request "8.8.8.8" "www.example.com" in let response = Network.send_request "8.8.8.8" "www.example.com" in
print_endline (Utils.get_bytecode response); print_endline (Utils.get_bytecode response);
let response', dns_header = Response.parse_header response in 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_header dns_header;
Debug.print_dns_question dns_question Debug.print_dns_question dns_question;
Debug.print_dns_record dns_record
;; ;;

View file

@ -17,3 +17,13 @@ let print_dns_question (question : Types.dns_question) =
question.type_ question.type_
question.class_ 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)
;;

View file

@ -13,8 +13,9 @@ let parse_header reader =
let rec parse_question reader = let rec parse_question reader =
let reader', name_b = decode_name_simple reader in let reader', name_b = decode_name_simple reader in
let name = String.to_bytes name_b in let name = String.to_bytes name_b in
match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with let max_size = 2 in
| [ type_; class_ ] -> { name; type_; class_ } 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" | _ -> failwith "Invalid number of fields"
and decode_name_simple reader = and decode_name_simple reader =
@ -30,3 +31,8 @@ and decode_name_simple reader =
let offset, parts = read_parts [] 0 in let offset, parts = read_parts [] 0 in
bytes_forward reader offset, String.concat "." parts 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 }
;;

View file

@ -12,3 +12,11 @@ type dns_question =
; type_ : int ; type_ : int
; class_ : int ; class_ : int
} }
type dns_record =
{ name : bytes
; type_ : int
; class_ : int
; ttl : int
; data : bytes
}