summaryrefslogtreecommitdiffabout
path: root/src/builtin/dns.bi
Side-by-side diff
Diffstat (limited to 'src/builtin/dns.bi') (more/less context) (ignore whitespace changes)
-rw-r--r--src/builtin/dns.bi84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/builtin/dns.bi b/src/builtin/dns.bi
index 3f68f45..0e1e327 100644
--- a/src/builtin/dns.bi
+++ b/src/builtin/dns.bi
@@ -279,3 +279,87 @@ MF_DEFUN(ptr_validate, NUMBER, STRING s)
}
END
+MF_DEFUN(primitive_hasns, NUMBER, STRING dom)
+{
+ struct dns_reply repl;
+ mf_status stat = dns_to_mf_status(ns_lookup(dom, 0, &repl));
+ MF_ASSERT(stat == mf_success || stat == mf_not_found,
+ mf_status_to_exception(stat),
+ _("cannot get NS records for %s"),
+ dom);
+ dns_reply_free(&repl);
+ if (stat == mf_success) {
+ MF_RETURN(1);
+ }
+ MF_RETURN(0);
+}
+END
+
+static int
+cmp_ip(void const *a, void const *b)
+{
+ GACOPYZ_UINT32_T ipa = *(GACOPYZ_UINT32_T const *)a;
+ GACOPYZ_UINT32_T ipb = *(GACOPYZ_UINT32_T const *)b;
+ if (ipa < ipb)
+ return -1;
+ if (ipa > ipb)
+ return 1;
+ return 0;
+}
+
+static int
+cmp_str(void const *a, void const *b)
+{
+ char * const *stra = a;
+ char * const *strb = b;
+ return strcmp(*stra, *strb);
+}
+
+MF_DEFUN(getns, STRING, STRING domain, OPTIONAL, NUMBER resolve, NUMBER sort)
+{
+ mf_status stat;
+ struct dns_reply reply;
+ int i;
+
+ stat = dns_to_mf_status(ns_lookup(domain, MF_OPTVAL(resolve),
+ &reply));
+ if (!mf_resolved(stat)) {
+ MF_THROW(mf_status_to_exception(stat),
+ _("cannot get MX records for %s"), domain);
+ }
+ if (stat == mf_not_found) {
+ MF_RETURN("");
+ }
+
+ MF_OBSTACK_BEGIN();
+ if (reply.type == dns_reply_ip) {
+ if (sort)
+ qsort(reply.data.ip,
+ reply.count,
+ sizeof(reply.data.ip[0]),
+ cmp_ip);
+ for (i = 0; i < reply.count; i++) {
+ struct in_addr s;
+ s.s_addr = reply.data.ip[i];
+ if (i > 0)
+ MF_OBSTACK_1GROW(' ');
+ MF_OBSTACK_GROW(inet_ntoa(s));
+ }
+ } else {
+ if (sort)
+ qsort(reply.data.str,
+ reply.count,
+ sizeof(reply.data.str[0]),
+ cmp_str);
+ for (i = 0; i < reply.count; i++) {
+ if (i > 0)
+ MF_OBSTACK_1GROW(' ');
+ MF_OBSTACK_GROW(reply.data.str[i]);
+ }
+ }
+ MF_OBSTACK_1GROW(0);
+ dns_reply_free(&reply);
+ MF_RETURN_OBSTACK();
+}
+END
+

Return to:

Send suggestions and report system problems to the System administrator.