1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: gen_ho.c,v 1.16 2001/05/29 05:48:36 marka Exp $";
27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Imports */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "port_before.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <netinet/in.h>
36*0Sstevel@tonic-gate #include <arpa/nameser.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <netdb.h>
41*0Sstevel@tonic-gate #include <resolv.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <isc/memcluster.h>
46*0Sstevel@tonic-gate #include <irs.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include "port_after.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include "irs_p.h"
51*0Sstevel@tonic-gate #include "gen_p.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /* Definitions */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate struct pvt {
56*0Sstevel@tonic-gate 	struct irs_rule *	rules;
57*0Sstevel@tonic-gate 	struct irs_rule *	rule;
58*0Sstevel@tonic-gate 	struct irs_ho *		ho;
59*0Sstevel@tonic-gate 	struct __res_state *	res;
60*0Sstevel@tonic-gate 	void			(*free_res)(void *);
61*0Sstevel@tonic-gate };
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /* Forwards */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static void		ho_close(struct irs_ho *this);
66*0Sstevel@tonic-gate static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
67*0Sstevel@tonic-gate static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
68*0Sstevel@tonic-gate 				   int af);
69*0Sstevel@tonic-gate static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
70*0Sstevel@tonic-gate 				  int len, int af);
71*0Sstevel@tonic-gate static struct hostent *	ho_next(struct irs_ho *this);
72*0Sstevel@tonic-gate static void		ho_rewind(struct irs_ho *this);
73*0Sstevel@tonic-gate static void		ho_minimize(struct irs_ho *this);
74*0Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this);
75*0Sstevel@tonic-gate static void		ho_res_set(struct irs_ho *this,
76*0Sstevel@tonic-gate 				   struct __res_state *res,
77*0Sstevel@tonic-gate 				   void (*free_res)(void *));
78*0Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
79*0Sstevel@tonic-gate 				     const struct addrinfo *pai);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static int		init(struct irs_ho *this);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /* Exports */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate struct irs_ho *
86*0Sstevel@tonic-gate irs_gen_ho(struct irs_acc *this) {
87*0Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
88*0Sstevel@tonic-gate 	struct irs_ho *ho;
89*0Sstevel@tonic-gate 	struct pvt *pvt;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
92*0Sstevel@tonic-gate 		errno = ENOMEM;
93*0Sstevel@tonic-gate 		return (NULL);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
96*0Sstevel@tonic-gate 	if (!(ho = memget(sizeof *ho))) {
97*0Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
98*0Sstevel@tonic-gate 		errno = ENOMEM;
99*0Sstevel@tonic-gate 		return (NULL);
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 	memset(ho, 0x5e, sizeof *ho);
102*0Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_ho];
103*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
104*0Sstevel@tonic-gate 	ho->private = pvt;
105*0Sstevel@tonic-gate 	ho->close = ho_close;
106*0Sstevel@tonic-gate 	ho->byname = ho_byname;
107*0Sstevel@tonic-gate 	ho->byname2 = ho_byname2;
108*0Sstevel@tonic-gate 	ho->byaddr = ho_byaddr;
109*0Sstevel@tonic-gate 	ho->next = ho_next;
110*0Sstevel@tonic-gate 	ho->rewind = ho_rewind;
111*0Sstevel@tonic-gate 	ho->minimize = ho_minimize;
112*0Sstevel@tonic-gate 	ho->res_get = ho_res_get;
113*0Sstevel@tonic-gate 	ho->res_set = ho_res_set;
114*0Sstevel@tonic-gate 	ho->addrinfo = ho_addrinfo;
115*0Sstevel@tonic-gate 	return (ho);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /* Methods. */
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static void
121*0Sstevel@tonic-gate ho_close(struct irs_ho *this) {
122*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	ho_minimize(this);
125*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
126*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
127*0Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
128*0Sstevel@tonic-gate 	memput(this, sizeof *this);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static struct hostent *
132*0Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
133*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
134*0Sstevel@tonic-gate 	struct irs_rule *rule;
135*0Sstevel@tonic-gate 	struct hostent *rval;
136*0Sstevel@tonic-gate 	struct irs_ho *ho;
137*0Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
138*0Sstevel@tonic-gate 	int softerror = 0;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (init(this) == -1)
141*0Sstevel@tonic-gate 		return (NULL);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
144*0Sstevel@tonic-gate 		ho = rule->inst->ho;
145*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
146*0Sstevel@tonic-gate 		errno = 0;
147*0Sstevel@tonic-gate 		rval = (*ho->byname)(ho, name);
148*0Sstevel@tonic-gate 		if (rval != NULL)
149*0Sstevel@tonic-gate 			return (rval);
150*0Sstevel@tonic-gate 		if (softerror == 0 &&
151*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
152*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
153*0Sstevel@tonic-gate 			softerror = 1;
154*0Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
157*0Sstevel@tonic-gate 			continue;
158*0Sstevel@tonic-gate 		/*
159*0Sstevel@tonic-gate 		 * The value TRY_AGAIN can mean that the service
160*0Sstevel@tonic-gate 		 * is not available, or just that this particular name
161*0Sstevel@tonic-gate 		 * cannot be resolved now.  We use the errno ECONNREFUSED
162*0Sstevel@tonic-gate 		 * to distinguish.  If a lookup sets that errno when
163*0Sstevel@tonic-gate 		 * H_ERRNO is TRY_AGAIN, we continue to try other lookup
164*0Sstevel@tonic-gate 		 * functions, otherwise we return the TRY_AGAIN error.
165*0Sstevel@tonic-gate 		 */
166*0Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
167*0Sstevel@tonic-gate 			break;
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
170*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
171*0Sstevel@tonic-gate 	return (NULL);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate static struct hostent *
175*0Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af) {
176*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
177*0Sstevel@tonic-gate 	struct irs_rule *rule;
178*0Sstevel@tonic-gate 	struct hostent *rval;
179*0Sstevel@tonic-gate 	struct irs_ho *ho;
180*0Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
181*0Sstevel@tonic-gate 	int softerror = 0;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (init(this) == -1)
184*0Sstevel@tonic-gate 		return (NULL);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
187*0Sstevel@tonic-gate 		ho = rule->inst->ho;
188*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
189*0Sstevel@tonic-gate 		errno = 0;
190*0Sstevel@tonic-gate 		rval = (*ho->byname2)(ho, name, af);
191*0Sstevel@tonic-gate 		if (rval != NULL)
192*0Sstevel@tonic-gate 			return (rval);
193*0Sstevel@tonic-gate 		if (softerror == 0 &&
194*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
195*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
196*0Sstevel@tonic-gate 			softerror = 1;
197*0Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
200*0Sstevel@tonic-gate 			continue;
201*0Sstevel@tonic-gate 		/*
202*0Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
203*0Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
204*0Sstevel@tonic-gate 		 */
205*0Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
206*0Sstevel@tonic-gate 			break;
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
209*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
210*0Sstevel@tonic-gate 	return (NULL);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate static struct hostent *
214*0Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
215*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
216*0Sstevel@tonic-gate 	struct irs_rule *rule;
217*0Sstevel@tonic-gate 	struct hostent *rval;
218*0Sstevel@tonic-gate 	struct irs_ho *ho;
219*0Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
220*0Sstevel@tonic-gate 	int softerror = 0;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	if (init(this) == -1)
224*0Sstevel@tonic-gate 		return (NULL);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
227*0Sstevel@tonic-gate 		ho = rule->inst->ho;
228*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
229*0Sstevel@tonic-gate 		errno = 0;
230*0Sstevel@tonic-gate 		rval = (*ho->byaddr)(ho, addr, len, af);
231*0Sstevel@tonic-gate 		if (rval != NULL)
232*0Sstevel@tonic-gate 			return (rval);
233*0Sstevel@tonic-gate 		if (softerror == 0 &&
234*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
235*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
236*0Sstevel@tonic-gate 			softerror = 1;
237*0Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
241*0Sstevel@tonic-gate 			continue;
242*0Sstevel@tonic-gate 		/*
243*0Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
244*0Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
245*0Sstevel@tonic-gate 		 */
246*0Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
247*0Sstevel@tonic-gate 			break;
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
250*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
251*0Sstevel@tonic-gate 	return (NULL);
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate static struct hostent *
255*0Sstevel@tonic-gate ho_next(struct irs_ho *this) {
256*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
257*0Sstevel@tonic-gate 	struct hostent *rval;
258*0Sstevel@tonic-gate 	struct irs_ho *ho;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	while (pvt->rule) {
261*0Sstevel@tonic-gate 		ho = pvt->rule->inst->ho;
262*0Sstevel@tonic-gate 		rval = (*ho->next)(ho);
263*0Sstevel@tonic-gate 		if (rval)
264*0Sstevel@tonic-gate 			return (rval);
265*0Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
266*0Sstevel@tonic-gate 			break;
267*0Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
268*0Sstevel@tonic-gate 		if (pvt->rule) {
269*0Sstevel@tonic-gate 			ho = pvt->rule->inst->ho;
270*0Sstevel@tonic-gate 			(*ho->rewind)(ho);
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 	return (NULL);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate static void
277*0Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
278*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
279*0Sstevel@tonic-gate 	struct irs_ho *ho;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
282*0Sstevel@tonic-gate 	if (pvt->rule) {
283*0Sstevel@tonic-gate 		ho = pvt->rule->inst->ho;
284*0Sstevel@tonic-gate 		(*ho->rewind)(ho);
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate static void
289*0Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
290*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
291*0Sstevel@tonic-gate 	struct irs_rule *rule;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if (pvt->res)
294*0Sstevel@tonic-gate 		res_nclose(pvt->res);
295*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
296*0Sstevel@tonic-gate 		struct irs_ho *ho = rule->inst->ho;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 		(*ho->minimize)(ho);
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static struct __res_state *
303*0Sstevel@tonic-gate ho_res_get(struct irs_ho *this) {
304*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if (!pvt->res) {
307*0Sstevel@tonic-gate 		struct __res_state *res;
308*0Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
309*0Sstevel@tonic-gate 		if (!res) {
310*0Sstevel@tonic-gate 			errno = ENOMEM;
311*0Sstevel@tonic-gate 			return (NULL);
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
314*0Sstevel@tonic-gate 		ho_res_set(this, res, free);
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	return (pvt->res);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate static void
321*0Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res,
322*0Sstevel@tonic-gate 		void (*free_res)(void *)) {
323*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
324*0Sstevel@tonic-gate 	struct irs_rule *rule;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
327*0Sstevel@tonic-gate 		res_nclose(pvt->res);
328*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	pvt->res = res;
332*0Sstevel@tonic-gate 	pvt->free_res = free_res;
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
335*0Sstevel@tonic-gate 		struct irs_ho *ho = rule->inst->ho;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 		(*ho->res_set)(ho, pvt->res, NULL);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate static struct addrinfo *
342*0Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
345*0Sstevel@tonic-gate 	struct irs_rule *rule;
346*0Sstevel@tonic-gate 	struct addrinfo *rval = NULL;
347*0Sstevel@tonic-gate 	struct irs_ho *ho;
348*0Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
349*0Sstevel@tonic-gate 	int softerror = 0;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	if (init(this) == -1)
352*0Sstevel@tonic-gate 		return (NULL);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
355*0Sstevel@tonic-gate 		ho = rule->inst->ho;
356*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
357*0Sstevel@tonic-gate 		errno = 0;
358*0Sstevel@tonic-gate 		if (ho->addrinfo == NULL) /* for safety */
359*0Sstevel@tonic-gate 			continue;
360*0Sstevel@tonic-gate 		rval = (*ho->addrinfo)(ho, name, pai);
361*0Sstevel@tonic-gate 		if (rval != NULL)
362*0Sstevel@tonic-gate 			return (rval);
363*0Sstevel@tonic-gate 		if (softerror == 0 &&
364*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
365*0Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
366*0Sstevel@tonic-gate 			softerror = 1;
367*0Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
368*0Sstevel@tonic-gate 		}
369*0Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
370*0Sstevel@tonic-gate 			continue;
371*0Sstevel@tonic-gate 		/*
372*0Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
373*0Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
374*0Sstevel@tonic-gate 		 */
375*0Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN ||
376*0Sstevel@tonic-gate 		    errno != ECONNREFUSED)
377*0Sstevel@tonic-gate 			break;
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
380*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
381*0Sstevel@tonic-gate 	if (rval)
382*0Sstevel@tonic-gate 		freeaddrinfo(rval);
383*0Sstevel@tonic-gate 	return (NULL);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate static int
387*0Sstevel@tonic-gate init(struct irs_ho *this) {
388*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate         if (!pvt->res && !ho_res_get(this))
391*0Sstevel@tonic-gate                 return (-1);
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate         if (((pvt->res->options & RES_INIT) == 0) &&
394*0Sstevel@tonic-gate             (res_ninit(pvt->res) == -1))
395*0Sstevel@tonic-gate                 return (-1);
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate         return (0);
398*0Sstevel@tonic-gate }
399