1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static char Version[] = "@(#)mxrr.c e07@nikhef.nl (Eric Wassenaar) 990522";
#endif
/* _getshort is internal libc6 function in libreolv
* See http://www.mail-archive.com/gentoo-dev@lists.gentoo.org/msg25398.html
*/
#include <resolv.h>
extern u_int16_t _getshort(const u_char *src);
#include "vrfy.h"
extern int verbose;
extern int debug;
#if PACKETSZ > 8192
#define MAXPACKET PACKETSZ /* PACKETSZ should be the max udp size (512) */
#else
#define MAXPACKET 8192 /* but tcp packets can be considerably larger */
#endif
typedef union {
HEADER hdr;
u_char buf[MAXPACKET];
} querybuf;
#ifndef HFIXEDSZ
#define HFIXEDSZ 12 /* actually sizeof(HEADER) */
#endif
#define MAXMXBUFSIZ (MAXMXHOSTS * (MAXHOST+1))
static char hostbuf[MAXMXBUFSIZ];
char *MxHosts[MAXMXHOSTS]; /* list of names of mx hosts found */
char *dbprefix = DBPREFIX; /* prefix for debug messages to stdout */
/*
** GETMXBYNAME -- Fetch mx hosts for a domain
** ------------------------------------------
**
** Returns:
** Number of mx hosts found.
**
** Outputs:
** The global array MxHosts contains the mx names.
*/
int
getmxbyname(domain)
char *domain; /* domain to get mx hosts for */
{
querybuf answer; /* answer buffer from nameserver */
HEADER *hp; /* answer buffer header */
int ancount, qdcount; /* answer count and query count */
u_char *msg, *eom, *cp; /* answer buffer positions */
int type, class, dlen; /* record type, class and length */
u_short pref; /* mx preference value */
u_short prefer[MAXMXHOSTS]; /* saved preferences of mx records */
char *bp; /* hostbuf pointer */
int nmx; /* number of mx hosts found */
register int i;
register int j;
register int n;
/*
* Query the nameserver to retrieve mx records for the given domain.
*/
errno = 0; /* reset before querying nameserver */
h_errno = 0;
n = res_search(domain, C_IN, T_MX, (u_char *)&answer, sizeof(answer));
if (n < 0)
{
if (_res.options & RES_DEBUG)
printf("%sres_search failed\n", dbprefix);
return(0);
}
errno = 0; /* reset after we got an answer */
if (n < HFIXEDSZ)
{
h_errno = NO_RECOVERY;
return(0);
}
/* avoid problems after truncation in tcp packets */
if (n > sizeof(answer))
n = sizeof(answer);
/*
* Valid answer received. Skip the query record.
*/
hp = (HEADER *)&answer;
qdcount = ntohs((u_short)hp->qdcount);
ancount = ntohs((u_short)hp->ancount);
msg = (u_char *)&answer;
eom = (u_char *)&answer + n;
cp = (u_char *)&answer + HFIXEDSZ;
while (qdcount-- > 0 && cp < eom)
{
n = dn_skipname(cp, eom);
if (n < 0)
return(0);
cp += n;
cp += QFIXEDSZ;
}
/*
* Loop through the answer buffer and extract mx records.
*/
nmx = 0;
bp = hostbuf;
while (ancount-- > 0 && cp < eom && nmx < MAXMXHOSTS)
{
#ifdef obsolete
if (verbose >= 4 || debug)
(void) p_rr((qbuf_t *)cp, (qbuf_t *)msg, stdout);
#endif /*obsolete*/
n = dn_expand(msg, eom, cp, (nbuf_t *)bp, MAXHOST);
if (n < 0)
break;
cp += n;
type = _getshort(cp);
cp += INT16SZ;
class = _getshort(cp);
cp += INT16SZ;
/* ttl = _getlong(cp); */
cp += INT32SZ;
dlen = _getshort(cp);
cp += INT16SZ;
if (type != T_MX || class != C_IN)
{
cp += dlen;
continue;
}
pref = _getshort(cp);
cp += INT16SZ;
n = dn_expand(msg, eom, cp, (nbuf_t *)bp, MAXHOST);
if (n < 0)
break;
cp += n;
prefer[nmx] = pref;
MxHosts[nmx] = bp;
nmx++;
n = strlength(bp) + 1;
bp += n;
}
/*
* Sort all records by preference.
*/
for (i = 0; i < nmx; i++)
{
for (j = i + 1; j < nmx; j++)
{
if (prefer[i] > prefer[j])
{
register u_short tmppref;
register char *tmphost;
tmppref = prefer[i];
prefer[i] = prefer[j];
prefer[j] = tmppref;
tmphost = MxHosts[i];
MxHosts[i] = MxHosts[j];
MxHosts[j] = tmphost;
}
}
}
return(nmx);
}
|