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.bi94
1 files changed, 38 insertions, 56 deletions
diff --git a/src/builtin/dns.bi b/src/builtin/dns.bi
index 46433a0..faf8998 100644
--- a/src/builtin/dns.bi
+++ b/src/builtin/dns.bi
@@ -21,20 +21,6 @@
#include "srvcfg.h"
#include "global.h"
-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)
{
char *hbuf;
@@ -75,8 +61,8 @@ END
static int
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)
return -1;
if (ipa > ipb)
@@ -86,37 +72,33 @@ ipaddr_cmp(const void *a, const void *b)
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) {
case dns_success: {
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;
char *q;
- addr.s_addr = ipbuf[i];
+ addr.s_addr = dns_reply_ip(&r, i);
q = inet_ntoa(addr);
if (i > 0)
MF_OBSTACK_1GROW(' ');
MF_OBSTACK_GROW(q);
}
- free(ipbuf);
+ dns_reply_free(&r);
MF_OBSTACK_1GROW(0);
MF_RETURN_OBSTACK();
}
case dns_not_found:
- free(ipbuf);
MF_RETURN("");
default:
- free(ipbuf);
MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)),
_("failed to get A record for %s"), string);
}
@@ -134,42 +116,33 @@ MF_DEFUN(dns_getname, STRING, STRING ipstr)
dns_status dnstat;
struct in_addr addr;
unsigned long ttl;
- char **names;
-
+ struct dns_reply r;
+
MF_ASSERT(inet_aton(ipstr, &addr),
mfe_invip,
_("invalid IP: %s"), 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) {
case dns_success: {
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]);
}
MF_OBSTACK_1GROW(0);
- for (; i < ncount; i++)
- free(names[i]);
- free(names);
+ dns_reply_free(&r);
MF_RETURN_OBSTACK();
}
case dns_not_found:
- free(names);
MF_RETURN("");
default:
- free(names);
MF_THROW(mf_status_to_exception(dns_to_mf_status(dnstat)),
_("failed to get PTR record for %s"), ipstr);
}
@@ -204,15 +177,12 @@ MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve)
GACOPYZ_UINT32_T *ipbuf;
size_t ipcount;
- 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),
_("cannot get MX records for %s"), domain);
}
if (mxstat == mf_not_found) {
- free(ipbuf);
MF_RETURN("");
} else {
int i;
@@ -233,8 +203,6 @@ MF_DEFUN(getmx, STRING, STRING domain, OPTIONAL, NUMBER no_resolve)
} else {
struct mxbuf mxbuf;
- mxbuf.mx_max = max_mx;
- mxbuf.mx_flags = MXF_MAX;
mxstat = getmx(domain, &mxbuf);
if (!mf_resolved(mxstat)) {
mxbuf_free(&mxbuf);
@@ -301,11 +269,9 @@ MF_DEFUN(primitive_ismx, NUMBER, STRING domain, STRING ipstr)
_("cannot resolve host name %s"), ipstr);
ip = ntohl(ip);
- 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),
_("cannot get MXs for %s"), domain);
}
@@ -328,6 +294,22 @@ MF_DEFUN(relayed, NUMBER, STRING s)
}
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

Return to:

Send suggestions and report system problems to the System administrator.