diff --git a/bin/main.ml b/bin/main.ml index aa85030..d062677 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -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) ;; diff --git a/lib/debug.ml b/lib/debug.ml index 1b241ee..8fe99c3 100644 --- a/lib/debug.ml +++ b/lib/debug.ml @@ -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) +;; diff --git a/lib/response.ml b/lib/response.ml index 0cb7144..ea031ca 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -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)) } ;; diff --git a/lib/types.ml b/lib/types.ml index 0406bbe..6d8059b 100644 --- a/lib/types.ml +++ b/lib/types.ml @@ -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 + }