DNS client

Often you will find yourself in situations where you need to obtain DNS informations in an asynchronous fashion.

Unfortunally this is not possible with the API that is shipped with the Java Virtual Machine itself. Because of this Vert.x offers it’s own API for DNS resolution which is fully asynchronous.

To obtain a DnsClient instance you will create a new via the Vertx instance.

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");

Be aware that you can pass in a varargs of InetSocketAddress arguments to specifiy more then one DNS Server to try to query for DNS resolution. The DNS Servers will be queried in the same order as specified here. Where the next will be used once the first produce an error while be used.

lookup

Try to lookup the A (ipv4) or AAAA (ipv6) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using “nslookup” on your operation system.

To lookup the A / AAAA record for “vertx.io” you would typically use it like:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.lookup("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. System.out.println(ar.result());
  5. } else {
  6. System.out.println("Failed to resolve entry" + ar.cause());
  7. }
  8. });

lookup4

Try to lookup the A (ipv4) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using “nslookup” on your operation system.

To lookup the A record for “vertx.io” you would typically use it like:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.lookup4("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. System.out.println(ar.result());
  5. } else {
  6. System.out.println("Failed to resolve entry" + ar.cause());
  7. }
  8. });

lookup6

Try to lookup the AAAA (ipv6) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using “nslookup” on your operation system.

To lookup the A record for “vertx.io” you would typically use it like:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.lookup6("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. System.out.println(ar.result());
  5. } else {
  6. System.out.println("Failed to resolve entry" + ar.cause());
  7. }
  8. });

resolveA

Try to resolve all A (ipv4) records for a given name. This is quite similar to using “dig” on unix like operation systems.

To lookup all the A records for “vertx.io” you would typically do:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveA("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<String> records = ar.result();
  5. for (String record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

resolveAAAA

Try to resolve all AAAA (ipv6) records for a given name. This is quite similar to using “dig” on unix like operation systems.

To lookup all the AAAAA records for “vertx.io” you would typically do:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveAAAA("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<String> records = ar.result();
  5. for (String record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

resolveCNAME

Try to resolve all CNAME records for a given name. This is quite similar to using “dig” on unix like operation systems.

To lookup all the CNAME records for “vertx.io” you would typically do:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveCNAME("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<String> records = ar.result();
  5. for (String record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

resolveMX

Try to resolve all MX records for a given name. The MX records are used to define which Mail-Server accepts emails for a given domain.

To lookup all the MX records for “vertx.io” you would typically do:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveMX("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<MxRecord> records = ar.result();
  5. for (MxRecord record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

Be aware that the List will contain the MxRecord sorted by the priority of them, which means MX records with smaller priority coming first in the List.

The MxRecord allows you to access the priority and the name of the MX record by offer methods for it like:

  1. MxRecord record = getMxRecord();
  2. record.priority();
  3. record.name();

resolveTXT

Try to resolve all TXT records for a given name. TXT records are often used to define extra informations for a domain.

To resolve all the TXT records for “vertx.io” you could use something along these lines:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveTXT("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<String> records = ar.result();
  5. for (String record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

resolveNS

Try to resolve all NS records for a given name. The NS records specify which DNS Server hosts the DNS informations for a given domain.

To resolve all the NS records for “vertx.io” you could use something along these lines:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveNS("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<String> records = ar.result();
  5. for (String record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

resolveSRV

Try to resolve all SRV records for a given name. The SRV records are used to define extra informations like port and hostname of services. Some protocols need this extra informations.

To lookup all the SRV records for “vertx.io” you would typically do:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolveSRV("vertx.io", ar -> {
  3. if (ar.succeeded()) {
  4. List<SrvRecord> records = ar.result();
  5. for (SrvRecord record: records) {
  6. System.out.println(record);
  7. }
  8. } else {
  9. System.out.println("Failed to resolve entry" + ar.cause());
  10. }
  11. });

Be aware that the List will contain the SrvRecords sorted by the priority of them, which means SrvRecords with smaller priority coming first in the List.

The SrvRecord allows you to access all informations contained in the SRV record itself:

  1. SrvRecord record = getSrvRecord();
  2. record.priority();
  3. record.name();
  4. record.weight();
  5. record.port();
  6. record.protocol();
  7. record.service();
  8. record.target();

Please refer to the API docs for the exact details.

resolvePTR

Try to resolve the PTR record for a given name. The PTR record maps an ipaddress to a name.

To resolve the PTR record for the ipaddress 10.0.0.1 you would use the PTR notion of “1.0.0.10.in-addr.arpa”

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.resolvePTR("1.0.0.10.in-addr.arpa", ar -> {
  3. if (ar.succeeded()) {
  4. String record = ar.result();
  5. System.out.println(record);
  6. } else {
  7. System.out.println("Failed to resolve entry" + ar.cause());
  8. }
  9. });

reverseLookup

Try to do a reverse lookup for an ipaddress. This is basically the same as resolve a PTR record, but allows you to just pass in the ipaddress and not a valid PTR query string.

To do a reverse lookup for the ipaddress 10.0.0.1 do something similar like this:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.reverseLookup("10.0.0.1", ar -> {
  3. if (ar.succeeded()) {
  4. String record = ar.result();
  5. System.out.println(record);
  6. } else {
  7. System.out.println("Failed to resolve entry" + ar.cause());
  8. }
  9. });

Error handling

As you saw in previous sections the DnsClient allows you to pass in a Handler which will be notified with an AsyncResult once the query was complete.

In case of an error it will be notified with a DnsException which will hole a DnsResponseCode that indicate why the resolution failed. This DnsResponseCode can be used to inspect the cause in more detail.

Possible DnsResponseCodes are:

  • NOERROR No record was found for a given query

  • FORMERROR Format error

  • SERVFAIL Server failure

  • NXDOMAIN Name error

  • NOTIMPL Not implemented by DNS Server

  • REFUSED DNS Server refused the query

  • YXDOMAIN Domain name should not exist

  • YXRRSET Resource record should not exist

  • NXRRSET RRSET does not exist

  • NOTZONE Name not in zone

  • BADVERS Bad extension mechanism for version

  • BADSIG Bad signature

  • BADKEY Bad key

  • BADTIME Bad timestamp

All of those errors are “generated” by the DNS Server itself.

You can obtain the DnsResponseCode from the DnsException like:

  1. DnsClient client = vertx.createDnsClient(53, "10.0.0.1");
  2. client.lookup("nonexisting.vert.xio", ar -> {
  3. if (ar.succeeded()) {
  4. String record = ar.result();
  5. System.out.println(record);
  6. } else {
  7. Throwable cause = ar.cause();
  8. if (cause instanceof DnsException) {
  9. DnsException exception = (DnsException) cause;
  10. DnsResponseCode code = exception.code();
  11. // ...
  12. } else {
  13. System.out.println("Failed to resolve entry" + ar.cause());
  14. }
  15. }
  16. });