This repository has been archived on 2024-05-23. You can view files and clone it, but cannot push or open issues or pull requests.
dns-toy/lib/query.ml

35 lines
839 B
OCaml
Raw Normal View History

2023-06-05 20:38:10 +02:00
open Types
open Utils
let header_to_bytes header =
new_buffer
2023-06-05 21:23:53 +02:00
(2 * get_obj_size header)
2023-06-05 20:38:10 +02:00
[ header.id
; header.flags
; header.num_questions
; header.num_answers
; header.num_authorities
; header.num_additionals
]
;;
let question_to_bytes question =
2023-06-05 21:23:53 +02:00
let buffer =
new_buffer (2 * (get_obj_size question - 1)) [ question.type_; question.class_ ]
in
2023-06-05 20:42:05 +02:00
Bytes.cat buffer question.name
2023-06-05 20:38:10 +02:00
;;
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")
;;