From a8bbd69b7ebb483d1d0ce68a0140a446a060ae08 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 5 Jun 2023 20:38:10 +0200 Subject: [PATCH] split up files --- lib/dune | 2 +- lib/query.ml | 35 +++++++++++++++++++++++++++++++++++ lib/types.ml | 14 ++++++++++++++ lib/utils.ml | 48 ------------------------------------------------ test/test.ml | 9 +++++---- 5 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 lib/query.ml create mode 100644 lib/types.ml diff --git a/lib/dune b/lib/dune index ec4f448..07319ae 100644 --- a/lib/dune +++ b/lib/dune @@ -1,3 +1,3 @@ (library (name dnstoy) - (modules utils)) + (modules types utils query)) diff --git a/lib/query.ml b/lib/query.ml new file mode 100644 index 0000000..f5454e7 --- /dev/null +++ b/lib/query.ml @@ -0,0 +1,35 @@ +open Types +open Utils + +let header_to_bytes header = + new_buffer + header + [ header.id + ; header.flags + ; header.num_questions + ; header.num_answers + ; header.num_authorities + ; header.num_additionals + ] +;; + +let question_to_bytes question = + let buffer1 = new_buffer question [ question.type_; question.class_ ] in + let buffer2 = Buffer.create (Bytes.length question.name + Bytes.length buffer1) in + Buffer.add_bytes buffer2 question.name; + Buffer.add_bytes buffer2 buffer1; + buffer2 +;; + +let encode_dns_name domain_name = + let parts = String.split_on_char '.' domain_name in + let encoded_parts = + List.map + (fun part -> + let len_part = String.length part in + let len_byte = Char.chr len_part in + Bytes.cat (Bytes.of_string (String.make 1 len_byte)) (Bytes.of_string part)) + parts + in + Bytes.cat (Bytes.concat Bytes.empty encoded_parts) (Bytes.of_string "\x00") +;; diff --git a/lib/types.ml b/lib/types.ml new file mode 100644 index 0000000..ccfc017 --- /dev/null +++ b/lib/types.ml @@ -0,0 +1,14 @@ +type dns_header = + { id : int + ; flags : int + ; num_questions : int + ; num_answers : int + ; num_authorities : int + ; num_additionals : int + } + +type dns_question = + { name : bytes + ; type_ : int + ; class_ : int + } diff --git a/lib/utils.ml b/lib/utils.ml index dc57118..771e1a3 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -1,18 +1,3 @@ -type dns_header = - { id : int - ; flags : int - ; num_questions : int - ; num_answers : int - ; num_authorities : int - ; num_additionals : int - } - -type dns_question = - { name : bytes - ; type_ : int - ; class_ : int - } - let get_bytecode data = let result = List.map @@ -40,36 +25,3 @@ let new_buffer obj list = if verification != size then failwith "Issue converting header to bytes"; buffer ;; - -let header_to_bytes header = - new_buffer - header - [ header.id - ; header.flags - ; header.num_questions - ; header.num_answers - ; header.num_authorities - ; header.num_additionals - ] -;; - -let question_to_bytes question = - let buffer1 = new_buffer question [ question.type_; question.class_ ] in - let buffer2 = Buffer.create (Bytes.length question.name + Bytes.length buffer1) in - Buffer.add_bytes buffer2 question.name; - Buffer.add_bytes buffer2 buffer1; - buffer2 -;; - -let encode_dns_name domain_name = - let parts = String.split_on_char '.' domain_name in - let encoded_parts = - List.map - (fun part -> - let len_part = String.length part in - let len_byte = Char.chr len_part in - Bytes.cat (Bytes.of_string (String.make 1 len_byte)) (Bytes.of_string part)) - parts - in - Bytes.cat (Bytes.concat Bytes.empty encoded_parts) (Bytes.of_string "\x00") -;; diff --git a/test/test.ml b/test/test.ml index 965882e..7f45cd0 100644 --- a/test/test.ml +++ b/test/test.ml @@ -1,7 +1,7 @@ -open Dnstoy.Utils +open Dnstoy let () = - let data = + let data : Types.dns_header = { id = 0x1314 ; flags = 0 ; num_questions = 1 @@ -12,6 +12,7 @@ let () = in assert ( "\\x13\\x14\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00" - = get_bytecode (header_to_bytes data)); - assert ("\\x06google\\x03com\\x00" = get_bytecode (encode_dns_name "google.com")) + = Utils.get_bytecode (Query.header_to_bytes data)); + assert ( + "\\x06google\\x03com\\x00" = Utils.get_bytecode (Query.encode_dns_name "google.com")) ;;