xref: /titanic_51/usr/src/lib/libresolv2/common/irs/gen_ho.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: gen_ho.c,v 1.5 2006/03/09 23:57:56 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /* Imports */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <netinet/in.h>
297c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <netdb.h>
347c478bd9Sstevel@tonic-gate #include <resolv.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
397c478bd9Sstevel@tonic-gate #include <irs.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "port_after.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "irs_p.h"
447c478bd9Sstevel@tonic-gate #include "gen_p.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* Definitions */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate struct pvt {
497c478bd9Sstevel@tonic-gate 	struct irs_rule *	rules;
507c478bd9Sstevel@tonic-gate 	struct irs_rule *	rule;
517c478bd9Sstevel@tonic-gate 	struct irs_ho *		ho;
527c478bd9Sstevel@tonic-gate 	struct __res_state *	res;
537c478bd9Sstevel@tonic-gate 	void			(*free_res)(void *);
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* Forwards */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void		ho_close(struct irs_ho *this);
597c478bd9Sstevel@tonic-gate static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
607c478bd9Sstevel@tonic-gate static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
617c478bd9Sstevel@tonic-gate 				   int af);
627c478bd9Sstevel@tonic-gate static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
637c478bd9Sstevel@tonic-gate 				  int len, int af);
647c478bd9Sstevel@tonic-gate static struct hostent *	ho_next(struct irs_ho *this);
657c478bd9Sstevel@tonic-gate static void		ho_rewind(struct irs_ho *this);
667c478bd9Sstevel@tonic-gate static void		ho_minimize(struct irs_ho *this);
677c478bd9Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this);
687c478bd9Sstevel@tonic-gate static void		ho_res_set(struct irs_ho *this,
697c478bd9Sstevel@tonic-gate 				   struct __res_state *res,
707c478bd9Sstevel@tonic-gate 				   void (*free_res)(void *));
717c478bd9Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
727c478bd9Sstevel@tonic-gate 				     const struct addrinfo *pai);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static int		init(struct irs_ho *this);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* Exports */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct irs_ho *
797c478bd9Sstevel@tonic-gate irs_gen_ho(struct irs_acc *this) {
807c478bd9Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
817c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
827c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
857c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
867c478bd9Sstevel@tonic-gate 		return (NULL);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
897c478bd9Sstevel@tonic-gate 	if (!(ho = memget(sizeof *ho))) {
907c478bd9Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
917c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
927c478bd9Sstevel@tonic-gate 		return (NULL);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	memset(ho, 0x5e, sizeof *ho);
957c478bd9Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_ho];
967c478bd9Sstevel@tonic-gate 	pvt->rule = pvt->rules;
977c478bd9Sstevel@tonic-gate 	ho->private = pvt;
987c478bd9Sstevel@tonic-gate 	ho->close = ho_close;
997c478bd9Sstevel@tonic-gate 	ho->byname = ho_byname;
1007c478bd9Sstevel@tonic-gate 	ho->byname2 = ho_byname2;
1017c478bd9Sstevel@tonic-gate 	ho->byaddr = ho_byaddr;
1027c478bd9Sstevel@tonic-gate 	ho->next = ho_next;
1037c478bd9Sstevel@tonic-gate 	ho->rewind = ho_rewind;
1047c478bd9Sstevel@tonic-gate 	ho->minimize = ho_minimize;
1057c478bd9Sstevel@tonic-gate 	ho->res_get = ho_res_get;
1067c478bd9Sstevel@tonic-gate 	ho->res_set = ho_res_set;
1077c478bd9Sstevel@tonic-gate 	ho->addrinfo = ho_addrinfo;
1087c478bd9Sstevel@tonic-gate 	return (ho);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Methods. */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static void
1147c478bd9Sstevel@tonic-gate ho_close(struct irs_ho *this) {
1157c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	ho_minimize(this);
1187c478bd9Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
1197c478bd9Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
1207c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1217c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static struct hostent *
1257c478bd9Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
1267c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1277c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
1287c478bd9Sstevel@tonic-gate 	struct hostent *rval;
1297c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
1307c478bd9Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
1317c478bd9Sstevel@tonic-gate 	int softerror = 0;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
1347c478bd9Sstevel@tonic-gate 		return (NULL);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
1377c478bd9Sstevel@tonic-gate 		ho = rule->inst->ho;
1387c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1397c478bd9Sstevel@tonic-gate 		errno = 0;
1407c478bd9Sstevel@tonic-gate 		rval = (*ho->byname)(ho, name);
1417c478bd9Sstevel@tonic-gate 		if (rval != NULL)
1427c478bd9Sstevel@tonic-gate 			return (rval);
1437c478bd9Sstevel@tonic-gate 		if (softerror == 0 &&
1447c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
1457c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
1467c478bd9Sstevel@tonic-gate 			softerror = 1;
1477c478bd9Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 		/*
1527c478bd9Sstevel@tonic-gate 		 * The value TRY_AGAIN can mean that the service
1537c478bd9Sstevel@tonic-gate 		 * is not available, or just that this particular name
1547c478bd9Sstevel@tonic-gate 		 * cannot be resolved now.  We use the errno ECONNREFUSED
1557c478bd9Sstevel@tonic-gate 		 * to distinguish.  If a lookup sets that errno when
1567c478bd9Sstevel@tonic-gate 		 * H_ERRNO is TRY_AGAIN, we continue to try other lookup
1577c478bd9Sstevel@tonic-gate 		 * functions, otherwise we return the TRY_AGAIN error.
1587c478bd9Sstevel@tonic-gate 		 */
1597c478bd9Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
1637c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
1647c478bd9Sstevel@tonic-gate 	return (NULL);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static struct hostent *
1687c478bd9Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af) {
1697c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1707c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
1717c478bd9Sstevel@tonic-gate 	struct hostent *rval;
1727c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
1737c478bd9Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
1747c478bd9Sstevel@tonic-gate 	int softerror = 0;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
1777c478bd9Sstevel@tonic-gate 		return (NULL);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
1807c478bd9Sstevel@tonic-gate 		ho = rule->inst->ho;
1817c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1827c478bd9Sstevel@tonic-gate 		errno = 0;
1837c478bd9Sstevel@tonic-gate 		rval = (*ho->byname2)(ho, name, af);
1847c478bd9Sstevel@tonic-gate 		if (rval != NULL)
1857c478bd9Sstevel@tonic-gate 			return (rval);
1867c478bd9Sstevel@tonic-gate 		if (softerror == 0 &&
1877c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
1887c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
1897c478bd9Sstevel@tonic-gate 			softerror = 1;
1907c478bd9Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
1937c478bd9Sstevel@tonic-gate 			continue;
1947c478bd9Sstevel@tonic-gate 		/*
1957c478bd9Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
1967c478bd9Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
1977c478bd9Sstevel@tonic-gate 		 */
1987c478bd9Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
2027c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
2037c478bd9Sstevel@tonic-gate 	return (NULL);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static struct hostent *
2077c478bd9Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
2087c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2097c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
2107c478bd9Sstevel@tonic-gate 	struct hostent *rval;
2117c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2127c478bd9Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
2137c478bd9Sstevel@tonic-gate 	int softerror = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
2177c478bd9Sstevel@tonic-gate 		return (NULL);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
2207c478bd9Sstevel@tonic-gate 		ho = rule->inst->ho;
2217c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2227c478bd9Sstevel@tonic-gate 		errno = 0;
2237c478bd9Sstevel@tonic-gate 		rval = (*ho->byaddr)(ho, addr, len, af);
2247c478bd9Sstevel@tonic-gate 		if (rval != NULL)
2257c478bd9Sstevel@tonic-gate 			return (rval);
2267c478bd9Sstevel@tonic-gate 		if (softerror == 0 &&
2277c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
2287c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
2297c478bd9Sstevel@tonic-gate 			softerror = 1;
2307c478bd9Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
2377c478bd9Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
2387c478bd9Sstevel@tonic-gate 		 */
2397c478bd9Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
2407c478bd9Sstevel@tonic-gate 			break;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
2437c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
2447c478bd9Sstevel@tonic-gate 	return (NULL);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static struct hostent *
2487c478bd9Sstevel@tonic-gate ho_next(struct irs_ho *this) {
2497c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2507c478bd9Sstevel@tonic-gate 	struct hostent *rval;
2517c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	while (pvt->rule) {
2547c478bd9Sstevel@tonic-gate 		ho = pvt->rule->inst->ho;
2557c478bd9Sstevel@tonic-gate 		rval = (*ho->next)(ho);
2567c478bd9Sstevel@tonic-gate 		if (rval)
2577c478bd9Sstevel@tonic-gate 			return (rval);
2587c478bd9Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
2617c478bd9Sstevel@tonic-gate 		if (pvt->rule) {
2627c478bd9Sstevel@tonic-gate 			ho = pvt->rule->inst->ho;
2637c478bd9Sstevel@tonic-gate 			(*ho->rewind)(ho);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	return (NULL);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void
2707c478bd9Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
2717c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2727c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	pvt->rule = pvt->rules;
2757c478bd9Sstevel@tonic-gate 	if (pvt->rule) {
2767c478bd9Sstevel@tonic-gate 		ho = pvt->rule->inst->ho;
2777c478bd9Sstevel@tonic-gate 		(*ho->rewind)(ho);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static void
2827c478bd9Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
2837c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2847c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (pvt->res)
2877c478bd9Sstevel@tonic-gate 		res_nclose(pvt->res);
2887c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2897c478bd9Sstevel@tonic-gate 		struct irs_ho *ho = rule->inst->ho;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 		(*ho->minimize)(ho);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static struct __res_state *
2967c478bd9Sstevel@tonic-gate ho_res_get(struct irs_ho *this) {
2977c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (!pvt->res) {
3007c478bd9Sstevel@tonic-gate 		struct __res_state *res;
3017c478bd9Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
3027c478bd9Sstevel@tonic-gate 		if (!res) {
3037c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
3047c478bd9Sstevel@tonic-gate 			return (NULL);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
3077c478bd9Sstevel@tonic-gate 		ho_res_set(this, res, free);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	return (pvt->res);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static void
3147c478bd9Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res,
3157c478bd9Sstevel@tonic-gate 		void (*free_res)(void *)) {
3167c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3177c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
3207c478bd9Sstevel@tonic-gate 		res_nclose(pvt->res);
3217c478bd9Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	pvt->res = res;
3257c478bd9Sstevel@tonic-gate 	pvt->free_res = free_res;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
3287c478bd9Sstevel@tonic-gate 		struct irs_ho *ho = rule->inst->ho;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		(*ho->res_set)(ho, pvt->res, NULL);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate static struct addrinfo *
3357c478bd9Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3387c478bd9Sstevel@tonic-gate 	struct irs_rule *rule;
3397c478bd9Sstevel@tonic-gate 	struct addrinfo *rval = NULL;
3407c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
3417c478bd9Sstevel@tonic-gate 	int therrno = NETDB_INTERNAL;
3427c478bd9Sstevel@tonic-gate 	int softerror = 0;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
3457c478bd9Sstevel@tonic-gate 		return (NULL);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
3487c478bd9Sstevel@tonic-gate 		ho = rule->inst->ho;
3497c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3507c478bd9Sstevel@tonic-gate 		errno = 0;
351*9525b14bSRao Shoaib 		if (ho->addrinfo == NULL) /*%< for safety */
3527c478bd9Sstevel@tonic-gate 			continue;
3537c478bd9Sstevel@tonic-gate 		rval = (*ho->addrinfo)(ho, name, pai);
3547c478bd9Sstevel@tonic-gate 		if (rval != NULL)
3557c478bd9Sstevel@tonic-gate 			return (rval);
3567c478bd9Sstevel@tonic-gate 		if (softerror == 0 &&
3577c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
3587c478bd9Sstevel@tonic-gate 		    pvt->res->res_h_errno != NETDB_INTERNAL) {
3597c478bd9Sstevel@tonic-gate 			softerror = 1;
3607c478bd9Sstevel@tonic-gate 			therrno = pvt->res->res_h_errno;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		if (rule->flags & IRS_CONTINUE)
3637c478bd9Sstevel@tonic-gate 			continue;
3647c478bd9Sstevel@tonic-gate 		/*
3657c478bd9Sstevel@tonic-gate 		 * See the comments in ho_byname() explaining
3667c478bd9Sstevel@tonic-gate 		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
3677c478bd9Sstevel@tonic-gate 		 */
3687c478bd9Sstevel@tonic-gate 		if (pvt->res->res_h_errno != TRY_AGAIN ||
3697c478bd9Sstevel@tonic-gate 		    errno != ECONNREFUSED)
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
3737c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, therrno);
3747c478bd9Sstevel@tonic-gate 	return (NULL);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static int
3787c478bd9Sstevel@tonic-gate init(struct irs_ho *this) {
3797c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate         if (!pvt->res && !ho_res_get(this))
3827c478bd9Sstevel@tonic-gate                 return (-1);
3837c478bd9Sstevel@tonic-gate 
384*9525b14bSRao Shoaib         if (((pvt->res->options & RES_INIT) == 0U) &&
3857c478bd9Sstevel@tonic-gate             (res_ninit(pvt->res) == -1))
3867c478bd9Sstevel@tonic-gate                 return (-1);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate         return (0);
3897c478bd9Sstevel@tonic-gate }
390*9525b14bSRao Shoaib 
391*9525b14bSRao Shoaib /*! \file */
392