diff options
author | Eric S. Raymond <esr@thyrsus.com> | 1996-10-25 18:25:20 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 1996-10-25 18:25:20 +0000 |
commit | 443c351724ef9b9eff3327ed13940207ddf3838e (patch) | |
tree | c47cd89748a94cfbcb7a1599629ff17c88f55e6e | |
parent | 4fbbabc2705e0c7320516b673f855494d48ec81d (diff) | |
download | fetchmail-443c351724ef9b9eff3327ed13940207ddf3838e.tar.gz fetchmail-443c351724ef9b9eff3327ed13940207ddf3838e.tar.bz2 fetchmail-443c351724ef9b9eff3327ed13940207ddf3838e.zip |
Clean up the getmxrecords interface.
svn path=/trunk/; revision=380
-rw-r--r-- | driver.c | 10 | ||||
-rw-r--r-- | mx.h | 2 | ||||
-rw-r--r-- | mxget.c | 39 |
3 files changed, 34 insertions, 17 deletions
@@ -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); } @@ -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 */ @@ -6,7 +6,7 @@ * For license terms, see the file COPYING in this directory. */ -#include <config.h> +#include "config.h" #ifdef HAVE_GETHOSTBYNAME #include <netdb.h> #include <sys/types.h> @@ -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 */ |