* 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 5.4 (Berkeley) %G%";
#include <arpa/nameser.h>
static char h_addr_buf
[sizeof(struct in_addr
) * XX
];
static char *h_addr_ptr
[XX
] = {
&h_addr_buf
[sizeof(struct in_addr
)]
static struct hostent host
= {
NULL
, /* official name of host */
0, /* host address type */
0, /* length of address */
h_addr_ptr
/* list of addresses from name server */
static char *host_aliases
[MAXALIASES
];
static char hostbuf
[BUFSIZ
+1];
getanswer(msg
, msglen
, iquery
)
int type
, class, ancount
, buflen
;
n
= res_send(msg
, msglen
, answer
, sizeof(answer
));
if (_res
.options
& RES_DEBUG
)
printf("res_send failed\n");
* find first satisfactory answer
ancount
= ntohs(hp
->ancount
);
if (hp
->rcode
!= NOERROR
|| ancount
== 0) {
if (_res
.options
& RES_DEBUG
)
printf("rcode = %d, ancount=%d\n", hp
->rcode
, ancount
);
buflen
= sizeof(hostbuf
);
cp
= answer
+ sizeof(HEADER
);
if ((n
= dn_expand(answer
, cp
, bp
, buflen
)) < 0)
cp
+= dn_skip(cp
) + QFIXEDSZ
;
while (--ancount
>= 0 && cp
< eom
) {
if ((n
= dn_expand(answer
, cp
, bp
, buflen
)) < 0)
cp
+= sizeof(u_short
) + sizeof(u_long
);
if (ap
>= &host_aliases
[MAXALIASES
-1])
if (type
!= T_A
|| n
!= 4) {
if (_res
.options
& RES_DEBUG
)
printf("unexpected answer type %d, size %d\n",
host
.h_aliases
= host_aliases
;
host
.h_addrtype
= class == C_IN
? AF_INET
: AF_UNSPEC
;
if (bp
+ n
>= &hostbuf
[sizeof(hostbuf
)]) {
if (_res
.options
& RES_DEBUG
)
printf("size (%d) too big\n", n
);
bcopy(cp
, host
.h_addr
= bp
, host
.h_length
= n
);
n
= res_mkquery(QUERY
, name
, C_ANY
, T_A
, NULL
, 0, NULL
,
hostbuf
, sizeof(hostbuf
));
if (_res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
return(getanswer(hostbuf
, n
, 0));
gethostbyaddr(addr
, len
, type
)
n
= res_mkquery(IQUERY
, NULL
, C_IN
, T_A
, addr
, len
, NULL
,
hostbuf
, sizeof(hostbuf
));
if (_res
.options
& RES_DEBUG
)
printf("res_mkquery failed\n");
return (getanswer(hostbuf
, n
, 1));