7894968d13a7285d4358ddc40296643fcb01d34a
** Copyright (c) 1986 Eric P. Allman
** Copyright (c) 1986 Regents of the University of California.
** All rights reserved. The Berkeley software License Agreement
** specifies the terms and conditions for redistribution.
static char SccsId
[] = "@(#)domain.c 5.6 (Berkeley) %G% (no MXDOMAIN)";
static char SccsId
[] = "@(#)domain.c 5.6 (Berkeley) %G%";
# include <arpa/nameser.h>
static char hostbuf
[BUFSIZ
];
extern u_short
getshort();
getmxrr(host
, mxhosts
, maxmx
, localhost
)
int n
, n1
, i
, j
, nmx
, ancount
, qdcount
, buflen
;
u_short pref
, localpref
, type
, class;
n
= res_mkquery(QUERY
, host
, C_IN
, T_MX
, (char *)NULL
, 0, NULL
,
(char *)&buf
, sizeof(buf
));
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
n
= res_send((char *)&buf
, n
, (char *)&answer
, sizeof(answer
));
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("res_send failed\n");
eom
= (char *)&answer
+ n
;
* find first satisfactory answer
ancount
= ntohs(hp
->ancount
);
qdcount
= ntohs(hp
->qdcount
);
if (hp
->rcode
!= NOERROR
|| ancount
== 0) {
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("rcode = %d, ancount=%d\n", hp
->rcode
, ancount
);
/* Check if it's an authoritive answer */
h_errno
= HOST_NOT_FOUND
;
* Jeeves (TOPS-20 server) still does not
* support MX records. For the time being,
* we must accept FORMERRs as the same as
(void) strcpy(hostbuf
, host
);
buflen
= sizeof(hostbuf
);
cp
= (char *)&answer
+ sizeof(HEADER
);
cp
+= dn_skip(cp
) + QFIXEDSZ
;
cp
+= dn_skip(cp
) + QFIXEDSZ
;
while (--ancount
>= 0 && cp
< eom
&& nmx
< maxmx
) {
if ((n
= dn_expand((char *)&answer
, eom
, cp
, bp
, buflen
)) < 0)
cp
+= sizeof(u_short
) + sizeof(u_long
);
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("unexpected answer type %d, size %d\n",
if ((n
= dn_expand((char *)&answer
, eom
, cp
, bp
, buflen
)) < 0)
if (sameword(bp
, localhost
))
(void) strcpy(hostbuf
, host
);
for (i
= 0; i
< nmx
; i
++) {
for (j
= i
+ 1; j
< nmx
; j
++) {
if (prefer
[i
] > prefer
[j
]) {
if (seenlocal
&& (prefer
[i
] >= localpref
))
* We are the first MX, might as well try delivering
* since nobody is supposed to have more info.
(void) strcpy(hostbuf
, host
);
getcanonname(host
, hbsize
)
n
= res_mkquery(QUERY
, host
, C_IN
, T_ANY
, (char *)NULL
, 0, NULL
,
(char *)&buf
, sizeof(buf
));
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
n
= res_send((char *)&buf
, n
, (char *)&answer
, sizeof(answer
));
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("res_send failed\n");
eom
= (char *)&answer
+ n
;
* find first satisfactory answer
ancount
= ntohs(hp
->ancount
);
qdcount
= ntohs(hp
->qdcount
);
* We don't care about errors here, only if we got an answer
if (tTd(8, 1) || _res
.options
& RES_DEBUG
)
printf("rcode = %d, ancount=%d\n", hp
->rcode
, ancount
);
cp
= (char *)&answer
+ sizeof(HEADER
);
cp
+= dn_skip(cp
) + QFIXEDSZ
;
cp
+= dn_skip(cp
) + QFIXEDSZ
;
while (--ancount
>= 0 && cp
< eom
) {
if ((n
= dn_expand((char *)&answer
, eom
, cp
, nbuf
,
(void)strncpy(host
, nbuf
, hbsize
);
cp
+= sizeof(u_short
) + sizeof(u_long
);
* Assume that only one cname will be found. More
if ((n
= dn_expand((char *)&answer
, eom
, cp
, nbuf
,
(void)strncpy(host
, nbuf
, hbsize
);
getcanonname(host
, hbsize
);
register u_char
*p
= (u_char
*) msgp
;
return ((u_short
)(u
| *p
));