enum_query() returns success if records were found in DNS and returns -1 under all other conditions:
- DNS query error
- DNS returned no results
- DNS record did not exist
- transient DNS failure
But it would be very useful to know what kind of failure occurred. For example, if the DNS record did not exist at all, 404 would be an appropriate response, but if a temporary nameserver failure occurred then 503 would be the correct reply.
This patch changed enum_query() to return 4 different negative failures for the 4 conditions listed above:
- DNS query error: -1
- DNS returned no results (answer section empty): -2
- DNS record did not exist (nxdomain): -3
- temporary failure: -4
You can use it like this:
enum_query("domain.");
switch ($retcode) {
case -1:
sl_send_reply("500", "Server Internal Error (DNS query problem)");
exit;
case -2:
sl_send_reply("404", "Not Found");
exit;
case -3:
sl_send_reply("484", "Address Incomplete");
exit;
case -4:
sl_send_reply("503", "DNS Failure");
exit;
}
(The reason for the 484 response on -3 result is not obvious. If you query DNS for a completely nonexistent name then you will get nxdomain and case -2 will apply. But if you query for a name that has no resource records in DNS but has at least one extant subdomain, you get NODATA. It's a clue that if you add more digits then the query will succeed.)
TODO: Documentation
patch against opensips-1.4.1-tls