* Copyright (c) 1985 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)gethostnamadr.c 6.4 (Berkeley) %G%";
#include <arpa/nameser.h>
static char *h_addr_ptrs
[MAXADDRS
+ 1];
static struct hostent host
;
static char *host_aliases
[MAXALIASES
];
static char hostbuf
[BUFSIZ
+1];
static struct in_addr host_addr
;
getanswer(msg
, msglen
, iquery
)
int type
, class, ancount
, qdcount
, buflen
;
int haveanswer
, getclass
;
n
= res_send(msg
, msglen
, (char *)&answer
, sizeof(answer
));
if (_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 (_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
;
buflen
= sizeof(hostbuf
);
cp
= (char *)&answer
+ sizeof(HEADER
);
if ((n
= dn_expand((char *)&answer
, eom
,
cp
+= dn_skip(cp
) + QFIXEDSZ
;
cp
+= dn_skip(cp
) + QFIXEDSZ
;
h_errno
= HOST_NOT_FOUND
;
host
.h_aliases
= host_aliases
;
host
.h_addr_list
= h_addr_ptrs
;
while (--ancount
>= 0 && cp
< eom
) {
if ((n
= dn_expand((char *)&answer
, eom
, cp
, bp
, buflen
)) < 0)
cp
+= sizeof(u_short
) + sizeof(u_long
);
if (ap
>= &host_aliases
[MAXALIASES
-1])
if ((n
= dn_expand((char *)&answer
, eom
, cp
, bp
, buflen
)) <
if (_res
.options
& RES_DEBUG
)
printf("unexpected answer type %d, size %d\n",
if (n
!= host
.h_length
) {
host
.h_addrtype
= (class == C_IN
) ? AF_INET
: AF_UNSPEC
;
bp
+= ((u_long
)bp
% sizeof(align
));
if (bp
+ n
>= &hostbuf
[sizeof(hostbuf
)]) {
if (_res
.options
& RES_DEBUG
)
printf("size (%d) too big\n", n
);
bcopy(cp
, *hap
++ = bp
, n
);
n
= res_mkquery(QUERY
, name
, C_ANY
, T_A
, (char *)NULL
, 0, NULL
,
(char *)&buf
, sizeof(buf
));
if (_res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
return(getanswer((char *)&buf
, n
, 0));
gethostbyaddr(addr
, len
, type
)
register struct hostent
*hp
;
(void)sprintf(qbuf
, "%d.%d.%d.%d.in-addr.arpa",
((unsigned)addr
[3] & 0xff),
((unsigned)addr
[2] & 0xff),
((unsigned)addr
[1] & 0xff),
((unsigned)addr
[0] & 0xff));
n
= res_mkquery(QUERY
, qbuf
, C_IN
, T_PTR
, NULL
, 0, NULL
,
(char *)&buf
, sizeof(buf
));
if (_res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
if ((hp
= getanswer((char *)&buf
, n
, 1)) == NULL
)
h_addr_ptrs
[0] = (char *)&host_addr
;
h_addr_ptrs
[1] = (char *)0;
host_addr
= *(struct in_addr
*)addr
;