DNS client library

This https://github.com/mirage/ocaml-dns is implementation of DNS protocol.

Whereas I need a client DNS library. One of the needs, but not the only one, I have is to query MX records and data associated with it, of a domain.

I’ve found several libraries at https://opam.ocaml.org/packages and checked out some of them.

For instance, https://opam.ocaml.org/packages/dns-client/ – unclear what it does, whether it’s what I want or not, no examples.

Which one is the one(s) I’m looking for?

dns-client is a part of the ocaml-dns distribution which provides 3 sub-packages:

  • dns-client.unix for an usual use of it
  • dns-client.lwt which is the same as dns-client.unix but with lwt
  • dns-client.mirage which is a special one for MirageOS

Let’s pick the first one. To ask the MX record to a domain, you should write:

let getmxbydomain domain =
  let dns = Dns_client_unix.create () in
  Dns_client_unix.getaddrinfo dns Dns.Rr_map.Mx domain

let () =
  let domain = Domain_name.of_string_exn Sys.argv.(1) in
  let _ttl, response = Rresult.R.failwith_error_msg (getmxbydomain domain) in
  Fmt.pr "@[<hov>%a@]\n%!" Fmt.(Dump.list Dns.Mx.pp) (Dns.Rr_map.Mx_set.elements response)

Then, you can compile and use it:

$ ocamlfind opt -linkpkg -package dns-client.unix,fmt,rresult main.ml
$ ./a.out gmail.com
[MX 5 gmail-smtp-in.l.google.com; ...]
6 Likes

It doesn’t exist, only dns-client does

dns-client is the opam package. When you install it, you get several findlib packages, including dns-client.unix. I just did so to verify.

$ opam install dns-client
[NOTE] Package dns-client is already installed (current version is 4.6.2).

$  ocamlfind opt -linkpkg -package dns-client.unix,fmt,rresult bin/main.ml
ocamlfind: Package `dns-client.unix' not found

No idea what’s going wrong, but this is what I get (and as I said, I -just- installed dns-client to test this):

chet@digg:~$ ocamlfind list | grep dns
dns                 (version: 4.6.2)
dns-client          (version: 4.6.2)
dns-client.lwt      (version: 4.6.2)
dns-client.mirage   (version: 4.6.2)
dns-client.unix     (version: 4.6.2)
dns.cache           (version: 4.6.2)
chet@digg:~$ opam list | grep dns-client
dns-client              4.6.2       Pure DNS resolver API

If you have a small test program and a command to compile, paste the program with the command, and I can try to compile it.

May be your forget to initialize your environment with: eval $(opam env).

Hi, if you’re still having a problem, and can provide me with a sample bit of code to compile and the invocation you used, I’m happy to help you debug.