implement compression support

This commit is contained in:
Mylloon 2023-06-19 19:38:35 +02:00
parent 4c7f0a237a
commit e3f13fea3b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -11,7 +11,7 @@ let parse_header reader =
;; ;;
let rec parse_question reader = let rec parse_question reader =
let name, offset_name = decode_name_simple reader in let name, offset_name = decode_name reader in
let data = bytes_forward reader.data (reader.pointer + offset_name) in let data = bytes_forward reader.data (reader.pointer + offset_name) in
let max_size = 2 in let max_size = 2 in
match List.init max_size (fun offset -> unpack_short_be data (offset * 2)) with match List.init max_size (fun offset -> unpack_short_be data (offset * 2)) with
@ -20,7 +20,7 @@ let rec parse_question reader =
, { name; type_; class_ } ) , { name; type_; class_ } )
| _ -> failwith "Invalid number of fields" | _ -> failwith "Invalid number of fields"
and decode_name_simple reader = and decode_name reader =
let rec read_parts parts pos = let rec read_parts parts pos =
let length = int_of_char (Bytes.get reader.data (reader.pointer + pos)) in let length = int_of_char (Bytes.get reader.data (reader.pointer + pos)) in
if length = 0 if length = 0
@ -37,38 +37,15 @@ and decode_name_simple reader =
let offset, parts = read_parts [] 0 in let offset, parts = read_parts [] 0 in
String.to_bytes (String.concat "." parts), offset String.to_bytes (String.concat "." parts), offset
(* and decode_compressed_name reader length =
--- let pointer =
def decode_compressed_name(length, reader): (length land 0b0011_1111) + int_of_char (Bytes.get reader.data (reader.pointer + 1))
pointer_bytes = bytes([length & 0b0011_1111]) + reader.read(1) in
pointer = struct.unpack("!H", pointer_bytes)[0] String.of_bytes (fst (decode_name { reader with pointer }))
current_pos = reader.tell()
reader.seek(pointer)
result = decode_name(reader)
reader.seek(current_pos)
return result
---
*)
and decode_compressed_name _reader _length =
(* TODO *)
String.empty
;; ;;
(*
---
def parse_record(reader):
name = decode_name_simple(reader)
# the the type, class, TTL, and data length together are 10 bytes (2 + 2 + 4 + 2 = 10)
# so we read 10 bytes
data = reader.read(10)
# HHIH means 2-byte int, 2-byte-int, 4-byte int, 2-byte int
type_, class_, ttl, data_len = struct.unpack("!HHIH", data)
data = reader.read(data_len)
return DNSRecord(name, type_, class_, ttl, data)
---
*)
let parse_record reader = let parse_record reader =
let name, _offset_name = decode_name_simple reader in let name, _offset_name = decode_name reader in
(* TODO *) (* TODO *)
let type_, class_, ttl, data = 0, 0, 0, Bytes.empty in let type_, class_, ttl, data = 0, 0, 0, Bytes.empty in
{ name; type_; class_; ttl; data } { name; type_; class_; ttl; data }