1
+ open Eio
2
+
1
3
module Transport : Dns_client .S
2
4
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)
4
6
and type +'a io = 'a
5
7
= struct
6
- open Eio
7
-
8
8
type io_addr = Ipaddr .t * int
9
9
type stack = (Stdenv .t * Eio.Switch .t )
10
10
type +'a io = 'a
@@ -58,8 +58,6 @@ module Transport : Dns_client.S
58
58
|> List. map (fun ip -> ip, 53 )
59
59
|> ipv6_first_queue
60
60
61
- let ( let * ) = Result. bind
62
-
63
61
let create ?nameservers ~timeout (env , sw ) =
64
62
let nameservers =
65
63
match nameservers with
@@ -75,6 +73,7 @@ module Transport : Dns_client.S
75
73
| None ->
76
74
let nameservers, digest =
77
75
match
76
+ let ( let * ) = Result. bind in
78
77
let * data = read_file env " /etc/resolv.conf" in
79
78
let * ips = decode_resolv_conf data in
80
79
Ok (ips, Digest. string data)
@@ -160,35 +159,19 @@ module Transport : Dns_client.S
160
159
nameservers0 t
161
160
|> try_ns_connection t 0
162
161
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)" )
189
173
190
- let close flow = try Flow. close flow with _ -> ()
191
-
174
+ let close flow = try Flow. close flow with _ -> ()
192
175
let bind a f = f a
193
176
let lift v = v
194
177
end
0 commit comments