* Vanquished the evil dn_expand() beast. getmx() now seems to be working.
authorArt Cancro <ajc@citadel.org>
Wed, 16 Feb 2000 01:19:39 +0000 (01:19 +0000)
committerArt Cancro <ajc@citadel.org>
Wed, 16 Feb 2000 01:19:39 +0000 (01:19 +0000)
citadel/ChangeLog
citadel/Makefile.in
citadel/domain.c
citadel/domain.h

index 8fb8b16d743cbc4e3c91be8ff4bb690a4e8a3fc8..424ec73aab1b25bd2d0c7bd5639cb89fb6cbfc73 100644 (file)
@@ -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 <bryant@cs.usm.maine.edu>
 
 Fri Jul 10 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
        * Initial CVS import 
+
index b29dcd5d7ed67a879fa2fae4c0d67a4818eec04e..3cf0d2cf3ffd232976cd7b1abb31f70fad9a3896 100644 (file)
@@ -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 \
index c02aa303cae3c64686d573874a0a8b2d10ec18d8..4c35a3bdb050a863826714c4b7c7081546a28aee 100644 (file)
@@ -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<num_mxrecs; ++n)
+               lprintf(9, "%10d %s\n", mxrecs[n].pref, mxrecs[n].host);
+       sort_mxrecs(mxrecs, num_mxrecs);
+       lprintf(9, "sorted...\n");
+       for (n=0; n<num_mxrecs; ++n)
+               lprintf(9, "%10d %s\n", mxrecs[n].pref, mxrecs[n].host);
+
+       strcpy(mxbuf, "");
+       for (n=0; n<num_mxrecs; ++n) {
+               strcat(mxbuf, mxrecs[n].host);
+               strcat(mxbuf, "|");
+       }
+       phree(mxrecs);
+       return(num_msrecs);
 }
index 10d17be0f71d88dbe69c330058b6c93ac1846931..b209417251af671591ca1d97d16e2dfd77397500 100644 (file)
@@ -1 +1,7 @@
+
+struct mx {
+       int pref;
+       char host[1024];
+};
+
 int getmx(char *mxbuf, char *dest);