WIP: parse dns packet

This commit is contained in:
Mylloon 2023-06-19 21:52:39 +02:00
parent 0166c01eba
commit 9525c64917
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 44 additions and 12 deletions

View file

@ -1,12 +1,8 @@
open Dnstoy
let () =
let response = Network.send_request "8.8.8.8" "www.example.com" in
let response = Network.send_request "8.8.8.8" "www.mylloon.fr" in
print_endline (Utils.get_bytecode response);
let reader, dns_header = Response.parse_header { data = response; pointer = 0 } in
let reader', dns_question = Response.parse_question reader in
let dns_record = Response.parse_record reader' in
Debug.print_dns_header dns_header;
Debug.print_dns_question dns_question;
Debug.print_dns_record dns_record
let dns_packet = Response.parse_dns_packet response in
print_endline (Debug.dns_packet dns_packet)
;;

View file

@ -27,3 +27,15 @@ let dns_record (record : Types.dns_record) =
record.ttl
(Utils.get_bytecode ~json:true record.data)
;;
let dns_packet (record : Types.dns_packet) =
let list fn l = String.concat ", " (List.map (fun el -> fn el) l) in
Printf.sprintf
"{ \"header\": %s, \"questions\": [%s], \"answers\": [%s], \"authorities\": [%s], \
\"additionals\": [%s] }"
(dns_header record.header)
(list dns_question record.questions)
(list dns_record record.answers)
(list dns_record record.authorities)
(list dns_record record.additionals)
;;

View file

@ -47,10 +47,26 @@ and decode_compressed_name reader length =
let parse_record reader =
let name, offset_name = decode_name reader in
let data = bytes_forward reader.data (offset_name + reader.pointer) in
{ name
; type_ = unpack_short_be data 0
; class_ = unpack_short_be data 2
; ttl = unpack_int_be data 4
; data = Bytes.sub data 10 (unpack_short_be data 8)
let data_len = unpack_short_be data 8 in
let record_len = 10 in
( { reader with pointer = reader.pointer + offset_name + record_len + data_len }
, { name
; type_ = unpack_short_be data 0
; class_ = unpack_short_be data 2
; ttl = unpack_int_be data 4
; data = Bytes.sub data record_len data_len
} )
;;
let parse_dns_packet data =
let reader, dns_header = parse_header { data; pointer = 0 } in
(* Quid du pointeur du reader, pas récupéré à chaque itération... *)
{ header = dns_header
; questions = List.init dns_header.num_questions (fun _ -> snd (parse_question reader))
; answers = List.init dns_header.num_answers (fun _ -> snd (parse_record reader))
; authorities =
List.init dns_header.num_authorities (fun _ -> snd (parse_record reader))
; additionals =
List.init dns_header.num_additionals (fun _ -> snd (parse_record reader))
}
;;

View file

@ -25,3 +25,11 @@ type dns_record =
; ttl : int
; data : bytes
}
type dns_packet =
{ header : dns_header
; questions : dns_question list
; answers : dns_record list
; authorities : dns_record list
; additionals : dns_record list
}