From 443c351724ef9b9eff3327ed13940207ddf3838e Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 25 Oct 1996 18:25:20 +0000 Subject: Clean up the getmxrecords interface. svn path=/trunk/; revision=380 --- driver.c | 10 +++++----- mx.h | 2 +- mxget.c | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/driver.c b/driver.c index b4a4048f..d008443d 100644 --- a/driver.c +++ b/driver.c @@ -343,12 +343,12 @@ struct hostrec *queryctl; */ for (i = 0; i < MX_RETRIES; i++) { - struct mxentry mxresp[32]; + struct mxentry *mxrecords, *mxp; int j; - n = getmxrecords(name, sizeof(mxresp)/sizeof(struct mxentry), mxresp); + mxrecords = getmxrecords(name); - if (n == -1) + if (mxrecords == (struct mxentry *)NULL) if (h_errno == TRY_AGAIN) { sleep(1); @@ -357,8 +357,8 @@ struct hostrec *queryctl; else break; - for (j = 0; j < n; j++) - if (strcmp(name, mxresp[i].name) == 0) + for (mxp = mxrecords; mxp->name; mxp++) + if (strcmp(name, mxp->name) == 0) return(TRUE); } diff --git a/mx.h b/mx.h index 5e2824c0..30da95b5 100644 --- a/mx.h +++ b/mx.h @@ -7,7 +7,7 @@ struct mxentry }; #ifdef HAVE_PROTOTYPES -extern int getmxrecords(char *, int, struct mxentry *); +extern struct mxentry * getmxrecords(const char *); #endif /* HAVE_PROTOTYPES */ /* mx.h ends here */ diff --git a/mxget.c b/mxget.c index 081c20f6..7dc2c8fd 100644 --- a/mxget.c +++ b/mxget.c @@ -6,7 +6,7 @@ * For license terms, see the file COPYING in this directory. */ -#include +#include "config.h" #ifdef HAVE_GETHOSTBYNAME #include #include @@ -19,26 +19,26 @@ * This ought to be in the bind library. It's adapted from sendmail. */ -int getmxrecords(name, nmx, pmx) +struct mxentry *getmxrecords(name) /* get MX records for given host */ -char *name; -int nmx; -struct mxentry *pmx; +const char *name; { unsigned char answer[PACKETSZ], MXHostBuf[PACKETSZ], *eom, *cp, *bp; int n, ancount, qdcount, buflen, type, pref, ind; + static struct mxentry pmx[(PACKETSZ - HFIXEDSZ) / sizeof(struct mxentry)]; HEADER *hp; n = res_search(name,C_IN,T_MX,(unsigned char*)&answer, sizeof(answer)); if (n == -1) - return(-1); + return((struct mxentry *)NULL); hp = (HEADER *)&answer; cp = answer + HFIXEDSZ; eom = answer + n; + h_errno = 0; for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ) if ((n = dn_skipname(cp, eom)) < 0) - return(-1); + return((struct mxentry *)NULL); buflen = sizeof(MXHostBuf) - 1; bp = MXHostBuf; ind = 0; @@ -63,19 +63,36 @@ struct mxentry *pmx; pmx[ind].name = bp; pmx[ind].pref = pref; - if (++ind > nmx) - break; + ++ind; n = strlen(bp); bp += n; *bp++ = '\0'; - buflen -= n + 1; } - return(ind); + pmx[ind].name = (char *)NULL; + pmx[ind].pref = -1; + return(pmx); } #endif /* HAVE_GETHOSTBYNAME */ +#ifdef TESTMAIN +main(int argc, char *argv[]) +{ + int count, i; + struct mxentry *responses; + + responses = getmxrecords(argv[1]); + if (responses == (struct mxentry *)NULL) + puts("No MX records found"); + else + do { + printf("%s %d\n", responses->name, responses->pref); + } while + ((++responses)->name); +} +#endif /* TESTMAIN */ + /* mxget.c ends here */ -- cgit v1.2.3