+#include "server.h"
+#include "internet_addressing.h"
+
+
+/*
+ * get_hosts() checks the Internet configuration for various types of
+ * entries and returns them in the same format as getmx() does -- fill the
+ * buffer with a delimited list of hosts and return the number of hosts.
+ *
+ * This is used to fetch MX smarthosts, SpamAssassin hosts, etc.
+ */
+int get_hosts(char *mxbuf, char *rectype) {
+ int config_lines;
+ int i;
+ char buf[256];
+ char host[256], type[256];
+ int total_smarthosts = 0;
+
+ if (inetcfg == NULL) return(0);
+ strcpy(mxbuf, "");
+
+ config_lines = num_tokens(inetcfg, '\n');
+ for (i=0; i<config_lines; ++i) {
+ extract_token(buf, inetcfg, i, '\n', sizeof buf);
+ extract_token(host, buf, 0, '|', sizeof host);
+ extract_token(type, buf, 1, '|', sizeof type);
+
+ if (!strcasecmp(type, rectype)) {
+ strcat(mxbuf, host);
+ strcat(mxbuf, "|");
+ ++total_smarthosts;
+ }
+ }
+
+ return(total_smarthosts);
+}
+
+
+/*
+ * Compare the preference of two MX records. First check by the actual
+ * number listed in the MX record. If they're identical, randomize the
+ * result.
+ */
+int mx_compare_pref(const void *mx1, const void *mx2) {
+ int pref1;
+ int pref2;
+
+ pref1 = ((const struct mx *)mx1)->pref;
+ pref2 = ((const struct mx *)mx2)->pref;
+
+ if (pref1 > pref2) {
+ return(1);
+ }
+ else if (pref1 < pref2) {
+ return(0);
+ }
+ else {
+ return(rand() % 2);
+ }
+}