Skip to content

Commit e34ec8f

Browse files
committed
eio(client): implement send_recv
1 parent ca58855 commit e34ec8f

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

eio/client/dns_client_eio.ml

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
open Eio
2+
13
module Transport : Dns_client.S
24
with type io_addr = Ipaddr.t * int
3-
and type stack = (Eio.Stdenv.t * Eio.Switch.t)
5+
and type stack = (Stdenv.t * Switch.t)
46
and type +'a io = 'a
57
= struct
6-
open Eio
7-
88
type io_addr = Ipaddr.t * int
99
type stack = (Stdenv.t * Eio.Switch.t)
1010
type +'a io = 'a
@@ -58,8 +58,6 @@ module Transport : Dns_client.S
5858
|> List.map (fun ip -> ip, 53)
5959
|> ipv6_first_queue
6060

61-
let ( let* ) = Result.bind
62-
6361
let create ?nameservers ~timeout (env, sw) =
6462
let nameservers =
6563
match nameservers with
@@ -75,6 +73,7 @@ module Transport : Dns_client.S
7573
| None ->
7674
let nameservers, digest =
7775
match
76+
let ( let* ) = Result.bind in
7877
let* data = read_file env "/etc/resolv.conf" in
7978
let* ips = decode_resolv_conf data in
8079
Ok (ips, Digest.string data)
@@ -160,35 +159,19 @@ module Transport : Dns_client.S
160159
nameservers0 t
161160
|> try_ns_connection t 0
162161

163-
(* let handle_received_data data = *)
164-
(* let len = Cstruct.length data in *)
165-
(* if len > 2 then *)
166-
(* let len' = Cstruct.BE.get_uint16 data 0 in *)
167-
(* if len - 2 >= len' then *)
168-
(* let packet, rest = *)
169-
(* if len - 2 = len *)
170-
(* then data, Cstruct.empty *)
171-
(* else Cstruct.split data (len' + 2) *)
172-
(* in *)
173-
(* let id = *)
174-
(* else *)
175-
176-
(* let rec read_loop buf ctx = *)
177-
(* try *)
178-
179-
let send_recv _ctx _tx = failwith ""
180-
(* if Cstruct.length tx > 4 then *)
181-
(* try *)
182-
(* let tx = Flow.cstruct_source [tx] in *)
183-
(* Flow.copy src tx; *)
184-
(* let recv_buffer = Cstruct.create 2048 in *)
185-
(* match Flow.read ctx recv_buffer with *)
186-
(* | _got -> *)
187-
(* else *)
188-
(* Error (`Msg "invalid DNS packet(data length <= 4)") *)
162+
let send_recv ctx dns_query =
163+
if Cstruct.length dns_query > 4 then
164+
try
165+
let src = Flow.cstruct_source [dns_query] in
166+
Flow.copy src ctx;
167+
let dns_response = Cstruct.create 2048 in
168+
let got = Flow.read ctx dns_response in
169+
Ok (Cstruct.sub dns_response 0 got)
170+
with e -> Error (`Msg (Printexc.to_string e))
171+
else
172+
Error (`Msg "Invalid DNS query packet (data length <= 4)")
189173

190-
let close flow = try Flow.close flow with _ -> ()
191-
174+
let close flow = try Flow.close flow with _ -> ()
192175
let bind a f = f a
193176
let lift v = v
194177
end

0 commit comments

Comments
 (0)