diff options
Diffstat (limited to 'src/builtin/dns.bi')
-rw-r--r-- | src/builtin/dns.bi | 94 |
1 files changed, 38 insertions, 56 deletions
diff --git a/src/builtin/dns.bi b/src/builtin/dns.bi index 46433a00..faf8998d 100644 --- a/src/builtin/dns.bi +++ b/src/builtin/dns.bi @@ -23,16 +23,2 @@ -static size_t max_ptr = MAX_DNS_PTR; -static size_t max_a = MAX_DNS_A; -static size_t max_mx = MAX_DNS_MX; -static struct mu_cfg_param dns_cfg_param[] = { - { "max-dns-reply-a", mu_c_size, &max_a, 0, NULL, - N_("Maximum number of A records in a DNS reply.") }, - { "max-dns-reply-ptr", mu_c_size, &max_a, 0, NULL, - N_("Maximum number of PTR records in a DNS reply.") }, - { "max-dns-reply-mx", mu_c_size, &max_mx, 0, NULL, - N_("Maximum number of MX records in a DNS reply.") }, - { NULL } -}; - - MF_DEFUN(primitive_hostname, STRING, STRING string) @@ -77,4 +63,4 @@ ipaddr_cmp(const void *a, const void *b) { - GACOPYZ_UINT32_T ipa = ntohl(*(GACOPYZ_UINT32_T*)a); - GACOPYZ_UINT32_T ipb = ntohl(*(GACOPYZ_UINT32_T*)b); + GACOPYZ_UINT32_T ipa = ntohl(**(GACOPYZ_UINT32_T**)a); + GACOPYZ_UINT32_T ipb = ntohl(**(GACOPYZ_UINT32_T**)b); if (ipa < ipb) @@ -88,10 +74,8 @@ MF_DEFUN(dns_getaddr, STRING, STRING string) { - GACOPYZ_UINT32_T *ipbuf; - size_t i, ipcount; + size_t i; unsigned long ttl; dns_status dnstat; - - ipbuf = mu_calloc(max_a, sizeof(ipbuf[0])); - dnstat = a_lookup(string, ipbuf, max_a, &ipcount, - &ttl, NULL, 0); + struct dns_reply r; + + dnstat = a_lookup(string, &r, &ttl); switch (dnstat) { @@ -99,4 +83,4 @@ MF_DEFUN(dns_getaddr, STRING, STRING string) MF_OBSTACK_BEGIN(); - qsort(ipbuf, ipcount, sizeof ipbuf[0], ipaddr_cmp); - for (i = 0; i < ipcount; i++) { + qsort(r.base, r.count, sizeof r.base[0], ipaddr_cmp); + for (i = 0; i < r.count; i++) { struct in_addr addr; @@ -104,3 +88,3 @@ MF_DEFUN(dns_getaddr, STRING, STRING string) - addr.s_addr = ipbuf[i]; + addr.s_addr = dns_reply_ip(&r, i); q = inet_ntoa(addr); @@ -110,3 +94,3 @@ MF_DEFUN(dns_getaddr, STRING, STRING string) } - free(ipbuf); + dns_reply_free(&r); MF_OBSTACK_1GROW(0); @@ -115,6 +99,4 @@ MF_DEFUN(dns_getaddr, STRING, STRING string) case dns_not_found: - free(ipbuf); MF_RETURN(""); default: - free(ipbuf); MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)), @@ -136,4 +118,4 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr) unsigned long ttl; - char **names; - + struct dns_reply r; + MF_ASSERT(inet_aton(ipstr, &addr), @@ -142,4 +124,3 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr) - names = mu_calloc(max_ptr, sizeof(names[0])); - dnstat = ptr_lookup(addr, names, max_ptr, &ttl, NULL, 0); + dnstat = ptr_lookup(addr, &r, &ttl); switch (dnstat) { @@ -147,14 +128,10 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr) size_t i; - size_t ncount; - for (ncount = 0; ncount < max_ptr && names[ncount]; - ncount++); - - qsort(names, ncount, sizeof names[0], hostname_cmp); + qsort(r.base, r.count, sizeof r.base[0], hostname_cmp); MF_OBSTACK_BEGIN(); - for (i = 0; i < ncount; i++) { + for (i = 0; i < r.count; i++) { if (i > 0) MF_OBSTACK_1GROW(' '); - MF_OBSTACK_GROW(names[i]); + MF_OBSTACK_GROW((char*)r.base[i]); } @@ -162,5 +139,3 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr) - for (; i < ncount; i++) - free(names[i]); - free(names); + dns_reply_free(&r); MF_RETURN_OBSTACK(); @@ -168,6 +143,4 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr) case dns_not_found: - free(names); MF_RETURN(""); default: - free(names); MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)), @@ -206,6 +179,4 @@ MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve) - ipbuf = mu_calloc(max_mx, sizeof(ipbuf[0])); - mxstat = getmxip(domain, ipbuf, max_mx, &ipcount); + mxstat = getmxip(domain, &ipbuf, &ipcount); if (!mf_resolved(mxstat)) { - free(ipbuf); MF_THROW(mf_status_to_exception(mxstat), @@ -214,3 +185,2 @@ MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve) if (mxstat == mf_not_found) { - free(ipbuf); MF_RETURN(""); @@ -235,4 +205,2 @@ MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve) - mxbuf.mx_max = max_mx; - mxbuf.mx_flags = MXF_MAX; mxstat = getmx(domain, &mxbuf); @@ -303,7 +271,5 @@ MF_DEFUN(primitive_ismx, NUMBER, STRING domain, STRING ipstr) - ipbuf = mu_calloc(max_mx, sizeof(ipbuf[0])); - mxstat = getmxip(domain, ipbuf, max_mx, &ipcount); + mxstat = getmxip(domain, &ipbuf, &ipcount); if (mxstat != mf_success) { - free(ipbuf); MF_THROW(mf_status_to_exception(mxstat), @@ -330,4 +296,20 @@ END -MF_INIT([< - mf_add_runtime_params(dns_cfg_param); - >]) +MF_DEFUN(ptr_validate, NUMBER, STRING s) +{ + int rc, res; + switch (rc = ptr_validate(s, NULL, NULL, NULL)) { + case dns_success: + res = 1; + break; + case dns_not_found: + res = 0; + break; + default: + MF_THROW(mf_status_to_exception(dns_to_mf_status(rc)), + _("failed to get PTR record for %s"), s); + } + MF_RETURN(res); +} +END + +MF_INIT |