* 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
[] = "@(#)res_send.c 5.6 (Berkeley) %G%";
* Send query to name server and wait for reply.
#include <arpa/nameser.h>
res_send(buf
, buflen
, answer
, anslen
)
int s
, retry
, v_circuit
, resplen
, ns
;
HEADER
*hp
= (HEADER
*) buf
;
HEADER
*anhp
= (HEADER
*) answer
;
if (_res
.options
& RES_DEBUG
) {
if (!(_res
.options
& RES_INIT
))
v_circuit
= (_res
.options
& RES_USEVC
) || buflen
> PACKETSZ
;
* Send request, RETRY times, or until successful
for (retry
= _res
.retry
; --retry
>= 0; ) {
for (ns
= 0; ns
< _res
.nscount
; ns
++) {
if (_res
.options
& RES_DEBUG
)
printf("Querying server (# %d) address = %s\n", ns
+1,
inet_ntoa(_res
.nsaddr_list
[ns
].sin_addr
.s_addr
));
s
= socket(AF_INET
, SOCK_STREAM
, 0);
if (connect(s
, &(_res
.nsaddr_list
[ns
]),
sizeof(struct sockaddr
)) < 0) {
if (_res
.options
& RES_DEBUG
)
printf("connect failed %d\n", errno
);
if (write(s
, &len
, sizeof(len
)) != sizeof(len
) ||
write(s
, buf
, buflen
) != buflen
) {
if (_res
.options
& RES_DEBUG
)
printf("write failed %d\n", errno
);
* Receive length & response
while (len
> 0 && (n
= read(s
, cp
, len
)) > 0) {
if (_res
.options
& RES_DEBUG
)
printf("read failed %d\n", errno
);
resplen
= len
= ntohs(*(short *)cp
);
while (len
> 0 && (n
= read(s
, cp
, len
)) > 0) {
if (_res
.options
& RES_DEBUG
)
printf("read failed %d\n", errno
);
s
= socket(AF_INET
, SOCK_DGRAM
, 0);
if (sendto(s
, buf
, buflen
, 0, &_res
.nsaddr_list
[ns
],
sizeof(struct sockaddr
)) != buflen
) {
if (_res
.options
& RES_DEBUG
)
printf("sendto errno = %d\n", errno
);
((_res
.retrans
* _res
.retry
) / _res
.nscount
);
n
= select(s
+1, &dsmask
, 0, 0, &timeout
);
if (_res
.options
& RES_DEBUG
)
printf("select errno = %d\n", errno
);
if (_res
.options
& RES_DEBUG
)
if ((resplen
= recvfrom(s
, answer
, anslen
,
if (_res
.options
& RES_DEBUG
)
printf("recvfrom, errno=%d\n", errno
);
* response from old query, ignore it
if (_res
.options
& RES_DEBUG
) {
if (!(_res
.options
& RES_IGNTC
) && anhp
->tc
) {
if (_res
.options
& RES_DEBUG
)
printf("truncated answer\n");
if (_res
.options
& RES_DEBUG
) {