static char sccsid
[] = "@(#)res_send.c 4.4 (Berkeley) %G%";
* Copyright (c) 1985 Regents of the University of California
* Send query to name server and wait for reply.
res_send(buf
, buflen
, answer
, anslen
)
int s
, retry
, v_circuit
, resplen
;
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; ) {
s
= socket(AF_INET
, SOCK_STREAM
, 0);
if (connect(s
, &_res
.nsaddr
, sizeof(_res
.nsaddr
)) < 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
,
sizeof(_res
.nsaddr
)) != buflen
) {
if (_res
.options
& RES_DEBUG
)
printf("sendto errno = %d\n", errno
);
timeout
.tv_sec
= _res
.retrans
;
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
) {