diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-01-03 12:40:52 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-01-03 12:40:52 +0200 |
commit | c70f5d055d37bba1b26beafec8bfee902dc1b758 (patch) | |
tree | 7f7c140369fbab7c9d37765eec814e2fb81cf7fc /src | |
parent | 33f88906896d016ebd4e387ddc7a29c9f6c0dbc7 (diff) | |
download | mailfromd-c70f5d055d37bba1b26beafec8bfee902dc1b758.tar.gz mailfromd-c70f5d055d37bba1b26beafec8bfee902dc1b758.tar.bz2 |
Version 8.7release_8_7
* NEWS: Update.
* configure.ac: Raise minor version.
* doc/functions.texi: Document new functions
Include the implementation of the NS resolving MFL functions, as
proposed by Jan Rafaj
* lib/dns.c (ns_lookup): New function.
* lib/dns.h (ns_lookup): New proto.
* mflib/dns.mf4 (hasns): New function.
* src/builtin/dns.bi (primitive_hasns, getns): New functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/builtin/dns.bi | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/builtin/dns.bi b/src/builtin/dns.bi index 3f68f459..0e1e3279 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 + |