split up files

This commit is contained in:
Mylloon 2023-06-05 20:38:10 +02:00
parent f5458a4b8c
commit a8bbd69b7e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 55 additions and 53 deletions

View file

@ -1,3 +1,3 @@
(library
(name dnstoy)
(modules utils))
(modules types utils query))

35
lib/query.ml Normal file
View file

@ -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")
;;

14
lib/types.ml Normal file
View file

@ -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
}

View file

@ -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")
;;

View file

@ -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"))
;;