From 858b33924fa10528df6d1938b981841bcca86122 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Wed, 16 Feb 2000 01:19:39 +0000 Subject: [PATCH] * Vanquished the evil dn_expand() beast. getmx() now seems to be working. --- citadel/ChangeLog | 4 ++ citadel/Makefile.in | 2 +- citadel/domain.c | 105 ++++++++++++++++++++++++++++++++++---------- citadel/domain.h | 6 +++ 4 files changed, 94 insertions(+), 23 deletions(-) diff --git a/citadel/ChangeLog b/citadel/ChangeLog index 8fb8b16d7..424ec73aa 100644 --- a/citadel/ChangeLog +++ b/citadel/ChangeLog @@ -1,4 +1,7 @@ $Log$ +Revision 1.459 2000/02/16 01:19:39 ajc +* Vanquished the evil dn_expand() beast. getmx() now seems to be working. + Revision 1.458 2000/02/14 04:36:14 ajc * sysdep.c: added new event hook type EVT_TIMER. Timer event hooks are called once per minute by any worker thread. @@ -1623,3 +1626,4 @@ Sat Jul 11 00:20:48 EDT 1998 Nathan Bryant Fri Jul 10 1998 Art Cancro * Initial CVS import + diff --git a/citadel/Makefile.in b/citadel/Makefile.in index b29dcd5d7..3cf0d2cf3 100644 --- a/citadel/Makefile.in +++ b/citadel/Makefile.in @@ -64,7 +64,7 @@ AUTOCONF=@AUTOCONF@ VPATH=$(srcdir) SOURCES=aidepost.c citadel.c citmail.c citserver.c client_chat.c commands.c \ - config.c control.c cux2ascii.c database.c dynloader.c file_ops.c \ + config.c control.c database.c dynloader.c file_ops.c \ housekeeping.c internetmail.c ipc_c_tcp.c locate_host.c \ logging.c mailinglist.c messages.c msgbase.c msgform.c netmailer.c \ netpoll.c netproc.c netsetup.c policy.c proxy.c rcit.c readlog.c \ diff --git a/citadel/domain.c b/citadel/domain.c index c02aa303c..4c35a3bdb 100644 --- a/citadel/domain.c +++ b/citadel/domain.c @@ -8,6 +8,34 @@ #define SMART_HOST "gatekeeper.wdcs.com" +/* + * sort_mxrecs() + * + * Sort a pile of MX records (struct mx, definted in domain.h) by preference + * + */ +void sort_mxrecs(struct mx *mxrecs, int num_mxrecs) { + int a, b; + struct mx hold1, hold2; + + if (num_mxrecs < 2) return; + + /* do the sort */ + for (a = num_mxrecs - 2; a >= 0; --a) { + for (b = 0; b <= a; ++b) { + if (mxrecs[b].pref > mxrecs[b+1].pref) { + + memcpy(hold1, mxrefs[b], sizeof(struct mx)); + memcpy(hold2, mxrefs[b+1], sizeof(struct mx)); + memcpy(mxrefs[b], hold2, sizeof(struct mx)); + memcpy(mxrefs[b+1], hold1, sizeof(struct mx)); + } + } + } +} + + + /* * getmx() * @@ -26,12 +54,16 @@ int getmx(char *mxbuf, char *dest) { char expanded_buf[1024]; unsigned short pref, type; int n; + HEADER *hp; + int qdcount; + struct mx *mxrecs = NULL; + int num_mxrecs = 0; /* If we're configured to send all mail to a smart-host, then our * job here is really easy. */ - if (1) { /* FIX */ + if (0) { /* FIX */ strcpy(mxbuf, SMART_HOST); return(1); } @@ -40,9 +72,7 @@ int getmx(char *mxbuf, char *dest) { */ ret = res_query( dest, - C_IN, T_MX, answer, sizeof(answer) ); - - lprintf(9, "res_query() returned %d\n", ret); + C_IN, T_MX, (unsigned char *)answer, sizeof(answer) ); if (ret < 0) { lprintf(5, "No MX found\n"); @@ -52,13 +82,19 @@ int getmx(char *mxbuf, char *dest) { /* If we had to truncate, shrink the number to avoid fireworks */ if (ret > sizeof(answer)) ret = sizeof(answer); - lprintf(9, "size of answer is %d\n", ret); + hp = (HEADER *)&answer[0]; startptr = &answer[0]; /* start and end of buffer */ endptr = &answer[ret]; - ptr = startptr + HFIXEDSZ; /* advance past header */ + for (qdcount = ntohs(hp->qdcount); qdcount--; ptr += ret + QFIXEDSZ) { + if ((ret = dn_skipname(ptr, endptr)) < 0) { + lprintf(9, "dn_skipname error\n"); + return(0); + } + } + while(1) { memset(expanded_buf, 0, sizeof(expanded_buf)); ret = dn_expand(startptr, @@ -73,23 +109,48 @@ int getmx(char *mxbuf, char *dest) { GETSHORT(type, ptr); ptr += INT16SZ + INT32SZ; GETSHORT(n, ptr); - ptr += n; - lprintf(9, "ret=%d, type=%d\n", ret, type); + if (type != T_MX) { + ptr += n; + } + + else { + GETSHORT(pref, ptr); + ret = dn_expand(startptr, + endptr, + ptr, + expanded_buf, + sizeof(expanded_buf) + ); + ptr += ret; + + ++num_mxrecs; + if (mxrecs == NULL) { + mxrecs = mallok(sizeof(struct mx)); + } + else { + mxrecs = reallok(mxrecs, + (sizeof(struct mx) * num_mxrecs) ); + } + + mxrecs[num_mxrecs - 1].pref = pref; + strcpy(mxrecs[num_mxrecs - 1].host, expanded_buf); + } } - - - - - - - - - - - - - /* FIX not done yet */ - return(0); + lprintf(9, "unsorted...\n"); + for (n=0; n