summaryrefslogtreecommitdiff
path: root/usr.sbin/named/host/send.c
blob: e365e914a2510128ec0c4850944733c5be5a15f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/*	$OpenBSD: send.c,v 1.2 1997/03/12 10:41:57 downsj Exp $	*/

/*
 * Copyright (c) 1985, 1989 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that: (1) source distributions retain this entire copyright
 * notice and comment, and (2) distributions including binaries display
 * the following acknowledgement:  ``This product includes software
 * developed by the University of California, Berkeley and its contributors''
 * in the documentation or other materials provided with the distribution
 * and in all advertising materials mentioning features or use of this
 * software. Neither the name of the University nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char Version[] = "@(#)send.c	e07@nikhef.nl (Eric Wassenaar) 961013";
#endif

#if defined(apollo) && defined(lint)
#define __attribute(x)
#endif

#include <stdio.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>

#include <sys/types.h>		/* not always automatically included */
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>

#undef NOERROR			/* in <sys/streams.h> on solaris 2.x */
#include <arpa/nameser.h>
#include <resolv.h>

#include "port.h"		/* various portability definitions */
#include "conf.h"		/* various configuration definitions */

#define input			/* read-only input parameter */
#define output			/* modified output parameter */

#define bitset(a,b)	(((a) & (b)) != 0)
#define setalarm(n)	(void) alarm((unsigned int)(n))

extern int errno;
extern res_state_t _res;	/* defined in res_init.c */

char *dbprefix = DBPREFIX;	/* prefix for debug messages to stdout */

static int timeout;		/* connection read timeout */
static struct sockaddr_in from;	/* address of inbound packet */
static struct sockaddr *from_sa = (struct sockaddr *)&from;

char *inet_ntoa		PROTO((struct in_addr));
unsigned int alarm	PROTO((unsigned int));

#ifdef HOST_RES_SEND
int res_send		PROTO((CONST qbuf_t *, int, qbuf_t *, int));
void _res_close		PROTO((void));
static int check_from	PROTO((void));
static int send_stream	PROTO((struct sockaddr_in *, qbuf_t *, int, qbuf_t *, int));
static int send_dgram	PROTO((struct sockaddr_in *, qbuf_t *, int, qbuf_t *, int));
#endif /*HOST_RES_SEND*/
static sigtype_t timer	PROTO((int));
int _res_connect	PROTO((int, struct sockaddr_in *, int));
int _res_write		PROTO((int, struct sockaddr_in *, char *, char *, int));
int _res_read		PROTO((int, struct sockaddr_in *, char *, char *, int));
static int recv_sock	PROTO((int, char *, int));
void _res_perror	PROTO((struct sockaddr_in *, char *, char *));

#ifdef HOST_RES_SEND

/*
** RES_SEND -- Send nameserver query and retrieve answer
** -----------------------------------------------------
**
**	Returns:
**		Length of (untruncated) nameserver answer, if obtained.
**		-1 if an error occurred (errno set appropriately).
**
**	This is a simplified version of the BIND 4.8.3 res_send().
**	- Always use connected datagrams to get proper error messages.
**	- Do not only return ETIMEDOUT or ECONNREFUSED in datagram mode,
**	  but also host or network unreachable status if appropriate.
**	- Never leave a connection open after we got an answer.
**	- No special ECONNRESET handling when using virtual circuits.
**
**	Note that this private version of res_send() is not only called
**	directly by 'host' but also indirectly by gethostbyname() or by
**	gethostbyaddr() via their resolver interface routines.
*/

int
res_send(query, querylen, answer, anslen)
input CONST qbuf_t *query;		/* location of formatted query buffer */
input int querylen;			/* length of query buffer */
output qbuf_t *answer;			/* location of buffer to store answer */
input int anslen;			/* maximum size of answer buffer */
{
	HEADER *bp = (HEADER *)answer;
	struct sockaddr_in *addr;	/* the server address to connect to */
	int v_circuit;			/* virtual circuit or datagram switch */
	int servfail[MAXNS];		/* saved failure codes per nameserver */
	register int try, ns;
	register int n;

	/* make sure resolver has been initialized */
	if (!bitset(RES_INIT, _res.options) && res_init() == -1)
		return(-1);

	if (bitset(RES_DEBUG, _res.options))
	{
		printf("%sres_send()\n", dbprefix);
		pr_query(query, querylen, stdout);
	}

	/* use virtual circuit if requested or if necessary */
	v_circuit = bitset(RES_USEVC, _res.options) || (querylen > PACKETSZ);

	/* reset server failure codes */
	for (ns = 0; ns < MAXNS; ns++)
		servfail[ns] = 0;

/*
 * Do _res.retry attempts for each of the _res.nscount addresses.
 * Upon failure, the current server will be marked bad if we got
 * an error condition which makes it unlikely that we will succeed
 * the next time we try this server.
 * Internal operating system failures, such as temporary lack of
 * resources, do not fall in that category.
 */
	for (try = 0; try < _res.retry; try++)
	{
	    for (ns = 0; ns < _res.nscount; ns++)
	    {
		/* skip retry if server failed permanently */
		if (servfail[ns])
			continue;

		/* fetch server address */
		addr = &nslist(ns);
retry:
		if (bitset(RES_DEBUG, _res.options))
			printf("%sQuerying server (# %d) %s address = %s\n", dbprefix,
			    ns+1, v_circuit ? "tcp" : "udp", inet_ntoa(addr->sin_addr));

		if (v_circuit)
		{
			/* at most one attempt per server */
			try = _res.retry;

			/* connect via virtual circuit */
			n = send_stream(addr, query, querylen, answer, anslen);
		}
		else
		{
			/* set datagram read timeout for recv_sock() */
			timeout = (_res.retrans << try);
			if (try > 0)
				timeout /= _res.nscount;
			if (timeout <= 0)
				timeout = 1;

			/* connect via datagram */
			n = send_dgram(addr, query, querylen, answer, anslen);

			/* check truncation; use v_circuit with same server */
			if ((n > 0) && bp->tc)
			{
				if (bitset(RES_DEBUG, _res.options))
					printf("%struncated answer, %d bytes\n", dbprefix, n);

				if (!bitset(RES_IGNTC, _res.options))
				{
					v_circuit = 1;
					goto retry;
				}
			}
		}

		if (n <= 0)
		{
			switch (errno)
			{
			    case ECONNREFUSED:
			    case ENETDOWN:
			    case ENETUNREACH:
			    case EHOSTDOWN:
			    case EHOSTUNREACH:
				servfail[ns] = errno;
				break;
			}

			/* try next server */
			continue;
		}

		if (bitset(RES_DEBUG, _res.options))
		{
			printf("%sgot answer, %d bytes:\n", dbprefix, n);
			pr_query(answer, (n > anslen) ? anslen : n, stdout);
		}

		/* we have an answer; clear possible error condition */
		errno = 0;
		return(n);
	    }
	}

	/* no answer obtained; return error condition */
	return(-1);
}

/*
** _RES_CLOSE -- Close an open stream or dgram connection
** ------------------------------------------------------
**
**	Returns:
**		None.
*/

static int srvsock = -1;	/* socket descriptor */

void
_res_close()
{
	int save_errno = errno;		/* preserve state */

	/* close the connection if open */
	if (srvsock >= 0)
	{
		(void) close(srvsock);
		srvsock = -1;
	}

	/* restore state */
	errno = save_errno;
}

/*
** CHECK_FROM -- Make sure the response comes from a known server
** --------------------------------------------------------------
**
**	Returns:
**		Nonzero if the source address is known.
**		Zero otherwise.
*/

static int
check_from()
{
	struct sockaddr_in *addr;
	register int ns;

	for (ns = 0; ns < _res.nscount; ns++)
	{
		/* fetch server address */
		addr = &nslist(ns);

		if (from.sin_family != addr->sin_family)
			continue;

		if (from.sin_port != addr->sin_port)
			continue;

		/* this allows a reply from any responding server */
		if (addr->sin_addr.s_addr == INADDR_ANY)
			return(1);

		if (from.sin_addr.s_addr == addr->sin_addr.s_addr)
			return(1);
	}

	/* matches none of the known addresses */
	return(0);
}

/*
** SEND_STREAM -- Query nameserver via virtual circuit
** ---------------------------------------------------
**
**	Returns:
**		Length of (untruncated) nameserver answer, if obtained.
**		-1 if an error occurred.
**
**	A new socket is allocated for each call, and it is never
**	left open. Checking the packet id is rather redundant.
**
**	Note that connect() is the call that is allowed to fail
**	under normal circumstances. All other failures generate
**	an unconditional error message.
**	Note that truncation is handled within _res_read().
*/

static int
send_stream(addr, query, querylen, answer, anslen)
input struct sockaddr_in *addr;		/* the server address to connect to */
input qbuf_t *query;			/* location of formatted query buffer */
input int querylen;			/* length of query buffer */
output qbuf_t *answer;			/* location of buffer to store answer */
input int anslen;			/* maximum size of answer buffer */
{
	char *host = NULL;		/* name of server is unknown */
	HEADER *qp = (HEADER *)query;
	HEADER *bp = (HEADER *)answer;
	register int n;

/*
 * Setup a virtual circuit connection.
 */
	srvsock = socket(AF_INET, SOCK_STREAM, 0);
	if (srvsock < 0)
	{
		_res_perror(addr, host, "socket");
		return(-1);
	}

	if (_res_connect(srvsock, addr, sizeof(*addr)) < 0)
	{
		if (bitset(RES_DEBUG, _res.options))
			_res_perror(addr, host, "connect");
		_res_close();
		return(-1);
	}

	if (bitset(RES_DEBUG, _res.options))
		printf("%sconnected to %s\n", dbprefix, inet_ntoa(addr->sin_addr));

/*
 * Send the query buffer.
 */
	if (_res_write(srvsock, addr, host, (char *)query, querylen) < 0)
	{
		_res_close();
		return(-1);
	}

/*
 * Read the answer buffer.
 */
wait:
	n = _res_read(srvsock, addr, host, (char *)answer, anslen);
	if (n <= 0)
	{
		_res_close();
		return(-1);
	}

/*
 * Make sure it is the proper response by checking the packet id.
 */
	if (qp->id != bp->id)
	{
		if (bitset(RES_DEBUG, _res.options))
		{
			printf("%sunexpected answer:\n", dbprefix);
			pr_query(answer, (n > anslen) ? anslen : n, stdout);
		}
		goto wait;
	}

/*
 * Never leave the socket open.
 */
	_res_close();
	return(n);
}

/*
** SEND_DGRAM -- Query nameserver via datagram
** -------------------------------------------
**
**	Returns:
**		Length of nameserver answer, if obtained.
**		-1 if an error occurred.
**
**	Inputs:
**		The global variable ``timeout'' should have been
**		set with the desired timeout value in seconds.
**
**	Sending to a nameserver datagram port with no nameserver running
**	will cause an ICMP port unreachable message to be returned. If the
**	socket is connected, we get an ECONNREFUSED error on the next socket
**	operation, and select returns if the error message is received.
**	Also, we get ENETUNREACH or EHOSTUNREACH errors if appropriate.
**	We thus get a proper error status before timing out.
**	This method usually works only if BSD >= 43.
**
**	Note that send() and recvfrom() are now the calls that are allowed
**	to fail under normal circumstances. All other failures generate
**	an unconditional error message.
*/

static int
send_dgram(addr, query, querylen, answer, anslen)
input struct sockaddr_in *addr;		/* the server address to connect to */
input qbuf_t *query;			/* location of formatted query buffer */
input int querylen;			/* length of query buffer */
output qbuf_t *answer;			/* location of buffer to store answer */
input int anslen;			/* maximum size of answer buffer */
{
	char *host = NULL;		/* name of server is unknown */
	HEADER *qp = (HEADER *)query;
	HEADER *bp = (HEADER *)answer;
	register int n;

/*
 * Setup a connected datagram socket.
 */
	srvsock = socket(AF_INET, SOCK_DGRAM, 0);
	if (srvsock < 0)
	{
		_res_perror(addr, host, "socket");
		return(-1);
	}

	if (connect(srvsock, (struct sockaddr *)addr, sizeof(*addr)) < 0)
	{
		_res_perror(addr, host, "connect");
		_res_close();
		return(-1);
	}

/*
 * Send the query buffer.
 */
	if (send(srvsock, (char *)query, querylen, 0) != querylen)
	{
		if (bitset(RES_DEBUG, _res.options))
			_res_perror(addr, host, "send");
		_res_close();
		return(-1);
	}

/*
 * Wait for the arrival of a reply, timeout, or error message.
 */
wait:
	n = recv_sock(srvsock, (char *)answer, anslen);
	if (n <= 0)
	{
		if (bitset(RES_DEBUG, _res.options))
			_res_perror(addr, host, "recvfrom");
		_res_close();
		return(-1);
	}

/*
 * Make sure it is the proper response by checking the packet id.
 */
	if (qp->id != bp->id)
	{
		if (bitset(RES_DEBUG, _res.options))
		{
			printf("%sold answer:\n", dbprefix);
			pr_query(answer, (n > anslen) ? anslen : n, stdout);
		}
		goto wait;
	}

/*
 * Make sure it comes from a known server.
 */
	if (!check_from())
	{
		if (bitset(RES_DEBUG, _res.options))
		{
			printf("%sunknown server %s:\n", dbprefix, inet_ntoa(from.sin_addr));
			pr_query(answer, (n > anslen) ? anslen : n, stdout);
		}
		goto wait;
	}

/*
 * Never leave the socket open.
 */
	_res_close();
	return(n);
}

#endif /*HOST_RES_SEND*/

/*
** _RES_CONNECT -- Connect to a stream (virtual circuit) socket
** ------------------------------------------------------------
**
**	Returns:
**		0 if successfully connected.
**		-1 in case of failure or timeout.
**
**	Note that we use _res.retrans to override the default
**	connect timeout value.
*/

static jmp_buf timer_buf;

static sigtype_t
/*ARGSUSED*/
timer(sig)
int sig;
{
	longjmp(timer_buf, 1);
	/*NOTREACHED*/
}


int
_res_connect(sock, addr, addrlen)
input int sock;
input struct sockaddr_in *addr;		/* the server address to connect to */
input int addrlen;
{
	if (setjmp(timer_buf) != 0)
	{
		errno = ETIMEDOUT;
		setalarm(0);
		return(-1);
	}

	(void) signal(SIGALRM, timer);
	setalarm(_res.retrans);

	if (connect(sock, (struct sockaddr *)addr, addrlen) < 0)
	{
		if (errno == EINTR)
			errno = ETIMEDOUT;
		setalarm(0);
		return(-1);
	}

	setalarm(0);
	return(0);
}

/*
** _RES_WRITE -- Write the query buffer via a stream socket
** --------------------------------------------------------
**
**	Returns:
**		Length of buffer if successfully transmitted.
**		-1 in case of failure (error message is issued).
**
**	The query is sent in two steps: first a single word with
**	the length of the buffer, followed by the buffer itself.
*/

int
_res_write(sock, addr, host, buf, bufsize)
input int sock;
input struct sockaddr_in *addr;		/* the server address to connect to */
input char *host;			/* name of server to connect to */
input char *buf;			/* location of formatted query buffer */
input int bufsize;			/* length of query buffer */
{
	u_short len;

/*
 * Write the length of the query buffer.
 */
	/* len = htons(bufsize); */
	putshort((u_short)bufsize, (u_char *)&len);

	if (write(sock, (char *)&len, INT16SZ) != INT16SZ)
	{
		_res_perror(addr, host, "write query length");
		return(-1);
	}

/*
 * Write the query buffer itself.
 */
	if (write(sock, buf, bufsize) != bufsize)
	{
		_res_perror(addr, host, "write query");
		return(-1);
	}

	return(bufsize);
}

/*
** _RES_READ -- Read the answer buffer via a stream socket
** -------------------------------------------------------
**
**	Returns:
**		Length of (untruncated) answer if successfully received.
**		-1 in case of failure (error message is issued).
**
**	The answer is read in two steps: first a single word which
**	gives the length of the buffer, followed by the buffer itself.
**	If the answer is too long to fit into the supplied buffer,
**	only the portion that fits will be stored, the residu will be
**	flushed, and the truncation flag will be set.
**
**	Note. The returned length is that of the *un*truncated answer,
**	however, and not the amount of data that is actually available.
**	This may give the caller a hint about new buffer reallocation.
*/

int
_res_read(sock, addr, host, buf, bufsize)
input int sock;
input struct sockaddr_in *addr;		/* the server address to connect to */
input char *host;			/* name of server to connect to */
output char *buf;			/* location of buffer to store answer */
input int bufsize;			/* maximum size of answer buffer */
{
	u_short len;
	char *buffer;
	int buflen;
	int reslen;
	register int n;

	/* set stream timeout for recv_sock() */
	timeout = READTIMEOUT;

/*
 * Read the length of answer buffer.
 */
	buffer = (char *)&len;
	buflen = INT16SZ;

	while (buflen > 0 && (n = recv_sock(sock, buffer, buflen)) > 0)
	{
		buffer += n;
		buflen -= n;
	}

	if (buflen != 0)
	{
		_res_perror(addr, host, "read answer length");
		return(-1);
	}

/*
 * Terminate if length is zero.
 */
	/* len = ntohs(len); */
	len = _getshort((u_char *)&len);
	if (len == 0)
		return(0);

/*
 * Check for truncation.
 * Do not chop the returned length in case of buffer overflow.
 */
	reslen = 0;
	if ((int)len > bufsize)
	{
		reslen = len - bufsize;
		/* len = bufsize; */
	}

/*
 * Read the answer buffer itself.
 * Truncate the answer is the supplied buffer is not big enough.
 */
	buffer = buf;
	buflen = (reslen > 0) ? bufsize : len;

	while (buflen > 0 && (n = recv_sock(sock, buffer, buflen)) > 0)
	{
		buffer += n;
		buflen -= n;
	}

	if (buflen != 0)
	{
		_res_perror(addr, host, "read answer");
		return(-1);
	}

/*
 * Discard the residu to keep connection in sync.
 */
	if (reslen > 0)
	{
		HEADER *bp = (HEADER *)buf;
		char resbuf[PACKETSZ];

		buffer = resbuf;
		buflen = (reslen < sizeof(resbuf)) ? reslen : sizeof(resbuf);

		while (reslen > 0 && (n = recv_sock(sock, buffer, buflen)) > 0)
		{
			reslen -= n;
			buflen = (reslen < sizeof(resbuf)) ? reslen : sizeof(resbuf);
		}

		if (reslen != 0)
		{
			_res_perror(addr, host, "read residu");
			return(-1);
		}

		if (bitset(RES_DEBUG, _res.options))
			printf("%sresponse truncated to %d bytes\n", dbprefix, bufsize);

		/* set truncation flag */
		bp->tc = 1;
	}

	return(len);
}

/*
** RECV_SOCK -- Read from stream or datagram socket with timeout
** -------------------------------------------------------------
**
**	Returns:
**		Length of buffer if successfully received.
**		-1 in case of failure or timeout.
**	Inputs:
**		The global variable ``timeout'' should have been
**		set with the desired timeout value in seconds.
**	Outputs:
**		Sets ``from'' to the address of the packet sender.
*/

static int
recv_sock(sock, buffer, buflen)
input int sock;
output char *buffer;			/* current buffer address */
input int buflen;			/* remaining buffer size */
{
	fd_set fds;
	struct timeval wait;
	int fromlen;
	register int n;

	wait.tv_sec = timeout;
	wait.tv_usec = 0;
rewait:
	/* FD_ZERO(&fds); */
	bzero((char *)&fds, sizeof(fds));
	FD_SET(sock, &fds);

	/* wait for the arrival of data, or timeout */
	n = select(FD_SETSIZE, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait);
	if (n <= 0)
	{
		if (n < 0 && errno == EINTR)
			goto rewait;
		if (n == 0)
			errno = ETIMEDOUT;
		return(-1);
	}
reread:
	/* fake an error if nothing was actually read */
	fromlen = sizeof(from);
	n = recvfrom(sock, buffer, buflen, 0, from_sa, &fromlen);
	if (n < 0 && errno == EINTR)
		goto reread;
	if (n == 0)
		errno = ECONNRESET;
	return(n);
}

/*
 * Alternative version for systems with broken networking code.
 *
 * The select() system call may fail on the solaris 2.4 platform
 * without appropriate patches. However, these patches are reported
 * to break client NFS.
 *
 * This version uses an alarm() instead of select(). This introduces
 * additional system call overhead.
 */

#ifdef BROKEN_SELECT

static int
recv_sock(sock, buffer, buflen)
input int sock;
output char *buffer;			/* current buffer address */
input int buflen;			/* remaining buffer size */
{
	int fromlen;
	register int n;

	if (setjmp(timer_buf) != 0)
	{
		errno = ETIMEDOUT;
		setalarm(0);
		return(-1);
	}

	(void) signal(SIGALRM, timer);
	setalarm(timeout);
reread:
	/* fake an error if nothing was actually read */
	fromlen = sizeof(from);
	n = recvfrom(sock, buffer, buflen, 0, from_sa, &fromlen);
	if (n < 0 && errno == EINTR)
		goto reread;
	if (n == 0)
		errno = ECONNRESET;
	setalarm(0);
	return(n);
}

#endif /*BROKEN_SELECT*/

/*
** _RES_PERROR -- Issue perror message including host info
** -------------------------------------------------------
**
**	Returns:
**		None.
*/

void
_res_perror(addr, host, message)
input struct sockaddr_in *addr;		/* the server address to connect to */
input char *host;			/* name of server to connect to */
input char *message;			/* perror message string */
{
	int save_errno = errno;		/* preserve state */

	/* prepend server address and name */
	if (addr != NULL)
		(void) fprintf(stderr, "%s ", inet_ntoa(addr->sin_addr));
	if (host != NULL)
		(void) fprintf(stderr, "(%s) ", host);

	/* issue actual message */
	errno = save_errno;
	perror(message);

	/* restore state */
	errno = save_errno;
}