aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>1996-10-25 18:08:43 +0000
committerEric S. Raymond <esr@thyrsus.com>1996-10-25 18:08:43 +0000
commit4fbbabc2705e0c7320516b673f855494d48ec81d (patch)
tree495ca03682e822bc61bc11a028d1a3c263da00c3
parentb0bb563ec965e7b863e86585ae2d148f9915ee60 (diff)
downloadfetchmail-4fbbabc2705e0c7320516b673f855494d48ec81d.tar.gz
fetchmail-4fbbabc2705e0c7320516b673f855494d48ec81d.tar.bz2
fetchmail-4fbbabc2705e0c7320516b673f855494d48ec81d.zip
Initial revision
svn path=/trunk/; revision=379
-rw-r--r--mx.h13
-rw-r--r--mxget.c81
2 files changed, 94 insertions, 0 deletions
diff --git a/mx.h b/mx.h
new file mode 100644
index 00000000..5e2824c0
--- /dev/null
+++ b/mx.h
@@ -0,0 +1,13 @@
+/* mx.h -- name-to-preference association for MX records */
+
+struct mxentry
+{
+ char *name;
+ int pref;
+};
+
+#ifdef HAVE_PROTOTYPES
+extern int getmxrecords(char *, int, struct mxentry *);
+#endif /* HAVE_PROTOTYPES */
+
+/* mx.h ends here */
diff --git a/mxget.c b/mxget.c
new file mode 100644
index 00000000..081c20f6
--- /dev/null
+++ b/mxget.c
@@ -0,0 +1,81 @@
+/*
+ * mxget.c -- fetch MX records for given DNS name
+ *
+ * Copyright 1996 by Eric S. Raymond
+ * All rights reserved.
+ * For license terms, see the file COPYING in this directory.
+ */
+
+#include <config.h>
+#ifdef HAVE_GETHOSTBYNAME
+#include <netdb.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+#include "mx.h"
+
+/*
+ * This ought to be in the bind library. It's adapted from sendmail.
+ */
+
+int getmxrecords(name, nmx, pmx)
+/* get MX records for given host */
+char *name;
+int nmx;
+struct mxentry *pmx;
+{
+ unsigned char answer[PACKETSZ], MXHostBuf[PACKETSZ], *eom, *cp, *bp;
+ int n, ancount, qdcount, buflen, type, pref, ind;
+ HEADER *hp;
+
+ n = res_search(name,C_IN,T_MX,(unsigned char*)&answer, sizeof(answer));
+ if (n == -1)
+ return(-1);
+
+ hp = (HEADER *)&answer;
+ cp = answer + HFIXEDSZ;
+ eom = answer + n;
+ for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
+ if ((n = dn_skipname(cp, eom)) < 0)
+ return(-1);
+ buflen = sizeof(MXHostBuf) - 1;
+ bp = MXHostBuf;
+ ind = 0;
+ ancount = ntohs(hp->ancount);
+ while (--ancount >= 0 && cp < eom)
+ {
+ if ((n = dn_expand(answer, eom, cp, bp, buflen)) < 0)
+ break;
+ cp += n;
+ GETSHORT(type, cp);
+ cp += INT16SZ + INT32SZ;
+ GETSHORT(n, cp);
+ if (type != T_MX)
+ {
+ cp += n;
+ continue;
+ }
+ GETSHORT(pref, cp);
+ if ((n = dn_expand(answer, eom, cp, bp, buflen)) < 0)
+ break;
+ cp += n;
+
+ pmx[ind].name = bp;
+ pmx[ind].pref = pref;
+ if (++ind > nmx)
+ break;
+
+ n = strlen(bp);
+ bp += n;
+ *bp++ = '\0';
+
+
+ buflen -= n + 1;
+ }
+
+ return(ind);
+}
+#endif /* HAVE_GETHOSTBYNAME */
+
+/* mxget.c ends here */