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*27040Skjd static char sccsid[] = "@(#)res_send.c 6.11 (Berkeley) 04/11/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 2727025Sbloom static int s = -1; /* socket used for communications */ 2827025Sbloom 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 */ 62*27040Skjd for (retry = _res.retry; retry > 0; retry--) { 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, 6727031Skjd 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) 7827031Skjd perror("socket failed"); 7926483Skarels #endif DEBUG 8026483Skarels continue; 8126483Skarels } 8227031Skjd if (connect(s, &(_res.nsaddr_list[ns]), 8326322Sbloom sizeof(struct sockaddr)) < 0) { 8424734Sbloom #ifdef DEBUG 8526322Sbloom if (_res.options & RES_DEBUG) 8627031Skjd perror("connect failed"); 8725243Skjd #endif DEBUG 8826322Sbloom (void) close(s); 8926322Sbloom s = -1; 9026322Sbloom continue; 9126322Sbloom } 9218144Sralph } 9318144Sralph /* 9418144Sralph * Send length & message 9518144Sralph */ 9627025Sbloom len = htons((u_short)buflen); 9727031Skjd if (write(s, (char *)&len, sizeof(len)) != sizeof(len)|| 9827031Skjd write(s, buf, buflen) != buflen) { 9924734Sbloom #ifdef DEBUG 10018144Sralph if (_res.options & RES_DEBUG) 10127031Skjd perror("write failed"); 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); 11227031Skjd while (len > 0 && 11327031Skjd (n = read(s, (char *)cp, (int)len)) > 0) { 11418144Sralph cp += n; 11518144Sralph len -= n; 11618144Sralph } 11718144Sralph if (n <= 0) { 11824734Sbloom #ifdef DEBUG 11918144Sralph if (_res.options & RES_DEBUG) 12027031Skjd perror("read failed"); 12125243Skjd #endif DEBUG 12218144Sralph (void) close(s); 12318144Sralph s = -1; 12418144Sralph continue; 12518144Sralph } 12618144Sralph cp = answer; 12727025Sbloom resplen = len = ntohs(*(u_short *)cp); 12827031Skjd while (len > 0 && 12927031Skjd (n = read(s, (char *)cp, (int)len)) > 0) { 13018144Sralph cp += n; 13118144Sralph len -= n; 13218144Sralph } 13318144Sralph if (n <= 0) { 13424734Sbloom #ifdef DEBUG 13518144Sralph if (_res.options & RES_DEBUG) 13627031Skjd perror("read failed"); 13725243Skjd #endif DEBUG 13818144Sralph (void) close(s); 13918144Sralph s = -1; 14018144Sralph continue; 14118144Sralph } 14218144Sralph } else { 14318144Sralph /* 14418144Sralph * Use datagrams. 14518144Sralph */ 14618144Sralph if (s < 0) 14718144Sralph s = socket(AF_INET, SOCK_DGRAM, 0); 14826483Skarels #if BSD >= 43 14926323Skarels if (connect(s, &_res.nsaddr_list[ns], 15026323Skarels sizeof(struct sockaddr)) < 0 || 15126323Skarels send(s, buf, buflen, 0) != buflen) { 15224734Sbloom #ifdef DEBUG 15327031Skjd if (_res.options & RES_DEBUG) 15427031Skjd perror("connect"); 15525243Skjd #endif DEBUG 15626483Skarels continue; 15718144Sralph } 15826483Skarels #else BSD 15926483Skarels if (sendto(s, buf, buflen, 0, &_res.nsaddr_list[ns], 16026483Skarels sizeof(struct sockaddr)) != buflen) { 16126483Skarels #ifdef DEBUG 16227031Skjd if (_res.options & RES_DEBUG) 16327031Skjd perror("sendto"); 16426483Skarels #endif DEBUG 16526483Skarels continue; 16626483Skarels } 16726483Skarels #endif BSD 16818144Sralph /* 16927031Skjd * Wait for reply 17018144Sralph */ 17127031Skjd timeout.tv_sec = (_res.retrans << (_res.retry - retry)) 17227031Skjd / _res.nscount; 17327031Skjd if (timeout.tv_sec <= 0) 17427031Skjd timeout.tv_sec = 1; 17518144Sralph timeout.tv_usec = 0; 17626323Skarels wait: 17718144Sralph dsmask = 1 << s; 17818144Sralph n = select(s+1, &dsmask, 0, 0, &timeout); 17918144Sralph if (n < 0) { 18024734Sbloom #ifdef DEBUG 18118144Sralph if (_res.options & RES_DEBUG) 18227031Skjd perror("select"); 18325243Skjd #endif DEBUG 18418144Sralph continue; 18518144Sralph } 18618144Sralph if (n == 0) { 18718144Sralph /* 18818144Sralph * timeout 18918144Sralph */ 19024734Sbloom #ifdef DEBUG 19118144Sralph if (_res.options & RES_DEBUG) 19218144Sralph printf("timeout\n"); 19325243Skjd #endif DEBUG 19426483Skarels gotsomewhere = 1; 19518144Sralph continue; 19618144Sralph } 19725332Skjd if ((resplen = recv(s, answer, anslen, 0)) <= 0) { 19824734Sbloom #ifdef DEBUG 19918144Sralph if (_res.options & RES_DEBUG) 20027031Skjd perror("recvfrom"); 20125243Skjd #endif DEBUG 20218144Sralph continue; 20318144Sralph } 20426483Skarels gotsomewhere = 1; 20518144Sralph if (id != anhp->id) { 20618144Sralph /* 20718144Sralph * response from old query, ignore it 20818144Sralph */ 20924734Sbloom #ifdef DEBUG 21018144Sralph if (_res.options & RES_DEBUG) { 21118144Sralph printf("old answer:\n"); 21218144Sralph p_query(answer); 21318144Sralph } 21425243Skjd #endif DEBUG 21526323Skarels goto wait; 21618144Sralph } 21718144Sralph if (!(_res.options & RES_IGNTC) && anhp->tc) { 21818144Sralph /* 21918144Sralph * get rest of answer 22018144Sralph */ 22124734Sbloom #ifdef DEBUG 22218144Sralph if (_res.options & RES_DEBUG) 22318144Sralph printf("truncated answer\n"); 22425243Skjd #endif DEBUG 22518144Sralph (void) close(s); 22618144Sralph s = -1; 227*27040Skjd /* 228*27040Skjd * retry decremented on continue 229*27040Skjd * to desired starting value 230*27040Skjd */ 231*27040Skjd retry = _res.retry + 1; 23218144Sralph v_circuit = 1; 23318144Sralph continue; 23418144Sralph } 23518144Sralph } 23624734Sbloom #ifdef DEBUG 23718144Sralph if (_res.options & RES_DEBUG) { 23818144Sralph printf("got answer:\n"); 23918144Sralph p_query(answer); 24018144Sralph } 24125243Skjd #endif DEBUG 24226322Sbloom /* 24326322Sbloom * We are going to assume that the first server is preferred 24426322Sbloom * over the rest (i.e. it is on the local machine) and only 24526322Sbloom * keep that one open. 24626322Sbloom */ 24726322Sbloom if ((_res.options & KEEPOPEN) == KEEPOPEN && ns == 0) { 24826322Sbloom return (resplen); 24926322Sbloom } else { 25026322Sbloom (void) close(s); 25126322Sbloom s = -1; 25226322Sbloom return (resplen); 25326322Sbloom } 25425243Skjd } 25518144Sralph } 25623873Skjd (void) close(s); 25726897Sbloom s = -1; 25826483Skarels if (v_circuit == 0 && gotsomewhere == 0) 25926483Skarels errno = ECONNREFUSED; 26026483Skarels else 26126483Skarels errno = ETIMEDOUT; 26218144Sralph return (-1); 26318144Sralph } 26427025Sbloom 26527025Sbloom /* 26627025Sbloom * This routine is for closing the socket if a virtual circuit is used and 26727025Sbloom * the program wants to close it. This provides support for endhostent() 26827025Sbloom * which expects to close the socket. 26927025Sbloom * 27027025Sbloom * This routine is not expected to be user visible. 27127025Sbloom */ 27227025Sbloom _res_close() 27327025Sbloom { 27427025Sbloom if (s != -1) { 27527025Sbloom (void) close(s); 27627025Sbloom s = -1; 27727025Sbloom } 27827025Sbloom } 279