125243Skjd 218144Sralph /* 321388Sdist * Copyright (c) 1985 Regents of the University of California. 421388Sdist * All rights reserved. The Berkeley software License Agreement 521388Sdist * specifies the terms and conditions for redistribution. 618548Sralph */ 718548Sralph 826635Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*27025Sbloom static char sccsid[] = "@(#)res_send.c 6.9 (Berkeley) 04/10/86"; 1026635Sdonn #endif LIBC_SCCS and not lint 1121388Sdist 1218548Sralph /* 1318144Sralph * Send query to name server and wait for reply. 1418144Sralph */ 1518144Sralph 1626886Skjd #include <sys/param.h> 1718144Sralph #include <sys/time.h> 1818144Sralph #include <sys/socket.h> 1918144Sralph #include <netinet/in.h> 2018144Sralph #include <stdio.h> 2118144Sralph #include <errno.h> 2224083Skjd #include <arpa/nameser.h> 2326896Skjd #include <resolv.h> 2418144Sralph 2518144Sralph extern int errno; 2618144Sralph 27*27025Sbloom static int s = -1; /* socket used for communications */ 28*27025Sbloom 2926322Sbloom #define KEEPOPEN (RES_USEVC|RES_STAYOPEN) 3026322Sbloom 3118531Sralph res_send(buf, buflen, answer, anslen) 3218144Sralph char *buf; 3318144Sralph int buflen; 3418144Sralph char *answer; 3518144Sralph int anslen; 3618144Sralph { 3718144Sralph register int n; 3826322Sbloom int retry, v_circuit, resplen, ns; 3926483Skarels int gotsomewhere = 0; 4018144Sralph u_short id, len; 4118144Sralph char *cp; 4218144Sralph int dsmask; 4318144Sralph struct timeval timeout; 4418144Sralph HEADER *hp = (HEADER *) buf; 4518144Sralph HEADER *anhp = (HEADER *) answer; 4618144Sralph 4724734Sbloom #ifdef DEBUG 4818144Sralph if (_res.options & RES_DEBUG) { 4918531Sralph printf("res_send()\n"); 5018144Sralph p_query(buf); 5118144Sralph } 5225243Skjd #endif DEBUG 5318531Sralph if (!(_res.options & RES_INIT)) 5424734Sbloom if (res_init() == -1) { 5524734Sbloom return(-1); 5624734Sbloom } 5718144Sralph v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; 5818144Sralph id = hp->id; 5918144Sralph /* 6018144Sralph * Send request, RETRY times, or until successful 6118144Sralph */ 6218144Sralph for (retry = _res.retry; --retry >= 0; ) { 6325243Skjd for (ns = 0; ns < _res.nscount; ns++) { 6425243Skjd #ifdef DEBUG 6525243Skjd if (_res.options & RES_DEBUG) 6625243Skjd printf("Querying server (# %d) address = %s\n", ns+1, 6725243Skjd inet_ntoa(_res.nsaddr_list[ns].sin_addr.s_addr)); 6825243Skjd #endif DEBUG 6918144Sralph if (v_circuit) { 7018144Sralph /* 7118144Sralph * Use virtual circuit. 7218144Sralph */ 7326322Sbloom if (s < 0) { 7418144Sralph s = socket(AF_INET, SOCK_STREAM, 0); 7526483Skarels if (s < 0) { 7626483Skarels #ifdef DEBUG 7726483Skarels if (_res.options & RES_DEBUG) 7826483Skarels printf("socket failed %d\n",errno); 7926483Skarels #endif DEBUG 8026483Skarels continue; 8126483Skarels } 8226322Sbloom if (connect(s, &(_res.nsaddr_list[ns]), 8326322Sbloom sizeof(struct sockaddr)) < 0) { 8424734Sbloom #ifdef DEBUG 8526322Sbloom if (_res.options & RES_DEBUG) 8626322Sbloom printf("connect failed %d\n",errno); 8725243Skjd #endif DEBUG 8826322Sbloom (void) close(s); 8926322Sbloom s = -1; 9026322Sbloom continue; 9126322Sbloom } 9218144Sralph } 9318144Sralph /* 9418144Sralph * Send length & message 9518144Sralph */ 96*27025Sbloom len = htons((u_short)buflen); 9718144Sralph if (write(s, &len, sizeof(len)) != sizeof(len) || 9825243Skjd write(s, buf, buflen) != buflen) { 9924734Sbloom #ifdef DEBUG 10018144Sralph if (_res.options & RES_DEBUG) 10118144Sralph printf("write failed %d\n", errno); 10225243Skjd #endif DEBUG 10318144Sralph (void) close(s); 10418144Sralph s = -1; 10518144Sralph continue; 10618144Sralph } 10718144Sralph /* 10818144Sralph * Receive length & response 10918144Sralph */ 11018144Sralph cp = answer; 11118144Sralph len = sizeof(short); 11218144Sralph while (len > 0 && (n = read(s, cp, len)) > 0) { 11318144Sralph cp += n; 11418144Sralph len -= n; 11518144Sralph } 11618144Sralph if (n <= 0) { 11724734Sbloom #ifdef DEBUG 11818144Sralph if (_res.options & RES_DEBUG) 11918144Sralph printf("read failed %d\n", errno); 12025243Skjd #endif DEBUG 12118144Sralph (void) close(s); 12218144Sralph s = -1; 12318144Sralph continue; 12418144Sralph } 12518144Sralph cp = answer; 126*27025Sbloom resplen = len = ntohs(*(u_short *)cp); 12718144Sralph while (len > 0 && (n = read(s, cp, len)) > 0) { 12818144Sralph cp += n; 12918144Sralph len -= n; 13018144Sralph } 13118144Sralph if (n <= 0) { 13224734Sbloom #ifdef DEBUG 13318144Sralph if (_res.options & RES_DEBUG) 13418144Sralph printf("read failed %d\n", errno); 13525243Skjd #endif DEBUG 13618144Sralph (void) close(s); 13718144Sralph s = -1; 13818144Sralph continue; 13918144Sralph } 14018144Sralph } else { 14118144Sralph /* 14218144Sralph * Use datagrams. 14318144Sralph */ 14418144Sralph if (s < 0) 14518144Sralph s = socket(AF_INET, SOCK_DGRAM, 0); 14626483Skarels #if BSD >= 43 14726323Skarels if (connect(s, &_res.nsaddr_list[ns], 14826323Skarels sizeof(struct sockaddr)) < 0 || 14926323Skarels send(s, buf, buflen, 0) != buflen) { 15024734Sbloom #ifdef DEBUG 15125243Skjd if (_res.options & RES_DEBUG) 15226323Skarels printf("connect/send errno = %d\n", 15326323Skarels errno); 15425243Skjd #endif DEBUG 15526483Skarels continue; 15618144Sralph } 15726483Skarels #else BSD 15826483Skarels if (sendto(s, buf, buflen, 0, &_res.nsaddr_list[ns], 15926483Skarels sizeof(struct sockaddr)) != buflen) { 16026483Skarels #ifdef DEBUG 16126483Skarels if (_res.options & RES_DEBUG) 16226483Skarels printf("sendto errno = %d\n", errno); 16326483Skarels #endif DEBUG 16426483Skarels continue; 16526483Skarels } 16626483Skarels #endif BSD 16718144Sralph /* 16818144Sralph * Wait for reply 16918144Sralph */ 17025243Skjd timeout.tv_sec = 17125243Skjd ((_res.retrans * _res.retry) / _res.nscount); 17218144Sralph timeout.tv_usec = 0; 17326323Skarels wait: 17418144Sralph dsmask = 1 << s; 17518144Sralph n = select(s+1, &dsmask, 0, 0, &timeout); 17618144Sralph if (n < 0) { 17724734Sbloom #ifdef DEBUG 17818144Sralph if (_res.options & RES_DEBUG) 17918144Sralph printf("select errno = %d\n", errno); 18025243Skjd #endif DEBUG 18118144Sralph continue; 18218144Sralph } 18318144Sralph if (n == 0) { 18418144Sralph /* 18518144Sralph * timeout 18618144Sralph */ 18724734Sbloom #ifdef DEBUG 18818144Sralph if (_res.options & RES_DEBUG) 18918144Sralph printf("timeout\n"); 19025243Skjd #endif DEBUG 19126483Skarels gotsomewhere = 1; 19218144Sralph continue; 19318144Sralph } 19425332Skjd if ((resplen = recv(s, answer, anslen, 0)) <= 0) { 19524734Sbloom #ifdef DEBUG 19618144Sralph if (_res.options & RES_DEBUG) 19718144Sralph printf("recvfrom, errno=%d\n", errno); 19825243Skjd #endif DEBUG 19918144Sralph continue; 20018144Sralph } 20126483Skarels gotsomewhere = 1; 20218144Sralph if (id != anhp->id) { 20318144Sralph /* 20418144Sralph * response from old query, ignore it 20518144Sralph */ 20624734Sbloom #ifdef DEBUG 20718144Sralph if (_res.options & RES_DEBUG) { 20818144Sralph printf("old answer:\n"); 20918144Sralph p_query(answer); 21018144Sralph } 21125243Skjd #endif DEBUG 21226323Skarels goto wait; 21318144Sralph } 21418144Sralph if (!(_res.options & RES_IGNTC) && anhp->tc) { 21518144Sralph /* 21618144Sralph * get rest of answer 21718144Sralph */ 21824734Sbloom #ifdef DEBUG 21918144Sralph if (_res.options & RES_DEBUG) 22018144Sralph printf("truncated answer\n"); 22125243Skjd #endif DEBUG 22218144Sralph (void) close(s); 22318144Sralph s = -1; 22418144Sralph retry = _res.retry; 22518144Sralph v_circuit = 1; 22618144Sralph continue; 22718144Sralph } 22818144Sralph } 22924734Sbloom #ifdef DEBUG 23018144Sralph if (_res.options & RES_DEBUG) { 23118144Sralph printf("got answer:\n"); 23218144Sralph p_query(answer); 23318144Sralph } 23425243Skjd #endif DEBUG 23526322Sbloom /* 23626322Sbloom * We are going to assume that the first server is preferred 23726322Sbloom * over the rest (i.e. it is on the local machine) and only 23826322Sbloom * keep that one open. 23926322Sbloom */ 24026322Sbloom if ((_res.options & KEEPOPEN) == KEEPOPEN && ns == 0) { 24126322Sbloom return (resplen); 24226322Sbloom } else { 24326322Sbloom (void) close(s); 24426322Sbloom s = -1; 24526322Sbloom return (resplen); 24626322Sbloom } 24725243Skjd } 24818144Sralph } 24923873Skjd (void) close(s); 25026897Sbloom s = -1; 25126483Skarels if (v_circuit == 0 && gotsomewhere == 0) 25226483Skarels errno = ECONNREFUSED; 25326483Skarels else 25426483Skarels errno = ETIMEDOUT; 25518144Sralph return (-1); 25618144Sralph } 257*27025Sbloom 258*27025Sbloom /* 259*27025Sbloom * This routine is for closing the socket if a virtual circuit is used and 260*27025Sbloom * the program wants to close it. This provides support for endhostent() 261*27025Sbloom * which expects to close the socket. 262*27025Sbloom * 263*27025Sbloom * This routine is not expected to be user visible. 264*27025Sbloom */ 265*27025Sbloom _res_close() 266*27025Sbloom { 267*27025Sbloom if (s != -1) { 268*27025Sbloom (void) close(s); 269*27025Sbloom s = -1; 270*27025Sbloom } 271*27025Sbloom } 272