12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788openLwt.Infixletsrc=Logs.Src.create"dns_client_mirage"~doc:"effectful DNS client layer"moduleLog=(valLogs.src_logsrc:Logs.LOG)moduleMake(R:Mirage_random.S)(T:Mirage_time.S)(C:Mirage_clock.MCLOCK)(S:Mirage_stack.V4)=structmoduleTransport:Dns_client.Swithtypestack=S.tandtype+'aio='aLwt.tandtypeio_addr=Ipaddr.V4.t*int=structtypestack=S.ttypeio_addr=Ipaddr.V4.t*inttypens_addr=[`TCP|`UDP]*io_addrtype+'aio='aLwt.ttypet={nameserver:ns_addr;timeout_ns:int64;stack:stack;}typecontext={t:t;flow:S.TCPV4.flow;timeout_ns:int64ref}letcreate?(nameserver=`TCP,(Ipaddr.V4.of_string_exnDns_client.default_resolver,53))~timeoutstack={nameserver;timeout_ns=timeout;stack}letnameserver{nameserver;_}=nameserverletrng=R.generate?g:Noneletclock=C.elapsed_nsletwith_timeouttime_leftf=lettimeout=T.sleep_ns!time_left>|=fun()->Error(`Msg"DNS request timeout")inletstart=clock()inLwt.pick[f;timeout]>|=funresult->letstop=clock()intime_left:=Int64.sub!time_left(Int64.substopstart);resultletbind=Lwt.bindletlift=Lwt.returnletconnect?nameserver:nst=let_proto,addr=matchnswithNone->nameservert|Somex->xinlettime_left=reft.timeout_nsinwith_timeouttime_left(S.TCPV4.create_connection(S.tcpv4t.stack)addr>|=function|Errore->Log.err(funm->m"error connecting to nameserver %a"S.TCPV4.pp_errore);Error(`Msg"connect failure")|Okflow->Ok{t;flow;timeout_ns=time_left})letclose{flow;_}=S.TCPV4.closeflowletrecvctx=with_timeoutctx.timeout_ns(S.TCPV4.readctx.flow>|=function|Errore->Error(`Msg(Fmt.to_to_stringS.TCPV4.pp_errore))|Ok(`Datacs)->Okcs|Ok`Eof->OkCstruct.empty)letsendctxs=with_timeoutctx.timeout_ns(S.TCPV4.writectx.flows>|=function|Errore->Error(`Msg(Fmt.to_to_stringS.TCPV4.pp_write_errore))|Ok()->Ok())endincludeDns_client.Make(Transport)end(*
type dns_ty = Dns_client
let config : 'a Mirage.impl =
let open Mirage in
impl @@ object inherit Mirage.base_configurable
method module_name = "Dns_client"
method name = "Dns_client"
method ty : 'a typ = Type Dns_client
method! packages : package list value =
(Key.match_ Key.(value target) @@ begin function
| `Unix -> [package "dns-client.unix"]
| _ -> []
end
)
method! deps = []
end
*)