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 /* Imports */
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include <sys/types.h>
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include <netinet/in.h>
257c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <netdb.h>
307c478bd9Sstevel@tonic-gate #include <resolv.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
357c478bd9Sstevel@tonic-gate #include <irs.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include "port_after.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "irs_p.h"
407c478bd9Sstevel@tonic-gate #include "gen_p.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /* Definitions */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate struct pvt {
457c478bd9Sstevel@tonic-gate struct irs_rule * rules;
467c478bd9Sstevel@tonic-gate struct irs_rule * rule;
477c478bd9Sstevel@tonic-gate struct irs_ho * ho;
487c478bd9Sstevel@tonic-gate struct __res_state * res;
497c478bd9Sstevel@tonic-gate void (*free_res)(void *);
507c478bd9Sstevel@tonic-gate };
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /* Forwards */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static void ho_close(struct irs_ho *this);
557c478bd9Sstevel@tonic-gate static struct hostent * ho_byname(struct irs_ho *this, const char *name);
567c478bd9Sstevel@tonic-gate static struct hostent * ho_byname2(struct irs_ho *this, const char *name,
577c478bd9Sstevel@tonic-gate int af);
587c478bd9Sstevel@tonic-gate static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
597c478bd9Sstevel@tonic-gate int len, int af);
607c478bd9Sstevel@tonic-gate static struct hostent * ho_next(struct irs_ho *this);
617c478bd9Sstevel@tonic-gate static void ho_rewind(struct irs_ho *this);
627c478bd9Sstevel@tonic-gate static void ho_minimize(struct irs_ho *this);
637c478bd9Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this);
647c478bd9Sstevel@tonic-gate static void ho_res_set(struct irs_ho *this,
657c478bd9Sstevel@tonic-gate struct __res_state *res,
667c478bd9Sstevel@tonic-gate void (*free_res)(void *));
677c478bd9Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
687c478bd9Sstevel@tonic-gate const struct addrinfo *pai);
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static int init(struct irs_ho *this);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /* Exports */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate struct irs_ho *
irs_gen_ho(struct irs_acc * this)757c478bd9Sstevel@tonic-gate irs_gen_ho(struct irs_acc *this) {
767c478bd9Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private;
777c478bd9Sstevel@tonic-gate struct irs_ho *ho;
787c478bd9Sstevel@tonic-gate struct pvt *pvt;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
817c478bd9Sstevel@tonic-gate errno = ENOMEM;
827c478bd9Sstevel@tonic-gate return (NULL);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
857c478bd9Sstevel@tonic-gate if (!(ho = memget(sizeof *ho))) {
867c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
877c478bd9Sstevel@tonic-gate errno = ENOMEM;
887c478bd9Sstevel@tonic-gate return (NULL);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate memset(ho, 0x5e, sizeof *ho);
917c478bd9Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_ho];
927c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules;
937c478bd9Sstevel@tonic-gate ho->private = pvt;
947c478bd9Sstevel@tonic-gate ho->close = ho_close;
957c478bd9Sstevel@tonic-gate ho->byname = ho_byname;
967c478bd9Sstevel@tonic-gate ho->byname2 = ho_byname2;
977c478bd9Sstevel@tonic-gate ho->byaddr = ho_byaddr;
987c478bd9Sstevel@tonic-gate ho->next = ho_next;
997c478bd9Sstevel@tonic-gate ho->rewind = ho_rewind;
1007c478bd9Sstevel@tonic-gate ho->minimize = ho_minimize;
1017c478bd9Sstevel@tonic-gate ho->res_get = ho_res_get;
1027c478bd9Sstevel@tonic-gate ho->res_set = ho_res_set;
1037c478bd9Sstevel@tonic-gate ho->addrinfo = ho_addrinfo;
1047c478bd9Sstevel@tonic-gate return (ho);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /* Methods. */
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate static void
ho_close(struct irs_ho * this)1107c478bd9Sstevel@tonic-gate ho_close(struct irs_ho *this) {
1117c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate ho_minimize(this);
1147c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res)
1157c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
1167c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1177c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate static struct hostent *
ho_byname(struct irs_ho * this,const char * name)1217c478bd9Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
1227c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1237c478bd9Sstevel@tonic-gate struct irs_rule *rule;
1247c478bd9Sstevel@tonic-gate struct hostent *rval;
1257c478bd9Sstevel@tonic-gate struct irs_ho *ho;
1267c478bd9Sstevel@tonic-gate int therrno = NETDB_INTERNAL;
1277c478bd9Sstevel@tonic-gate int softerror = 0;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate if (init(this) == -1)
1307c478bd9Sstevel@tonic-gate return (NULL);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1337c478bd9Sstevel@tonic-gate ho = rule->inst->ho;
1347c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1357c478bd9Sstevel@tonic-gate errno = 0;
1367c478bd9Sstevel@tonic-gate rval = (*ho->byname)(ho, name);
1377c478bd9Sstevel@tonic-gate if (rval != NULL)
1387c478bd9Sstevel@tonic-gate return (rval);
1397c478bd9Sstevel@tonic-gate if (softerror == 0 &&
1407c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != HOST_NOT_FOUND &&
1417c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != NETDB_INTERNAL) {
1427c478bd9Sstevel@tonic-gate softerror = 1;
1437c478bd9Sstevel@tonic-gate therrno = pvt->res->res_h_errno;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (rule->flags & IRS_CONTINUE)
1467c478bd9Sstevel@tonic-gate continue;
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * The value TRY_AGAIN can mean that the service
1497c478bd9Sstevel@tonic-gate * is not available, or just that this particular name
1507c478bd9Sstevel@tonic-gate * cannot be resolved now. We use the errno ECONNREFUSED
1517c478bd9Sstevel@tonic-gate * to distinguish. If a lookup sets that errno when
1527c478bd9Sstevel@tonic-gate * H_ERRNO is TRY_AGAIN, we continue to try other lookup
1537c478bd9Sstevel@tonic-gate * functions, otherwise we return the TRY_AGAIN error.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
1567c478bd9Sstevel@tonic-gate break;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
1597c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, therrno);
1607c478bd9Sstevel@tonic-gate return (NULL);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate static struct hostent *
ho_byname2(struct irs_ho * this,const char * name,int af)1647c478bd9Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af) {
1657c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1667c478bd9Sstevel@tonic-gate struct irs_rule *rule;
1677c478bd9Sstevel@tonic-gate struct hostent *rval;
1687c478bd9Sstevel@tonic-gate struct irs_ho *ho;
1697c478bd9Sstevel@tonic-gate int therrno = NETDB_INTERNAL;
1707c478bd9Sstevel@tonic-gate int softerror = 0;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate if (init(this) == -1)
1737c478bd9Sstevel@tonic-gate return (NULL);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1767c478bd9Sstevel@tonic-gate ho = rule->inst->ho;
1777c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1787c478bd9Sstevel@tonic-gate errno = 0;
1797c478bd9Sstevel@tonic-gate rval = (*ho->byname2)(ho, name, af);
1807c478bd9Sstevel@tonic-gate if (rval != NULL)
1817c478bd9Sstevel@tonic-gate return (rval);
1827c478bd9Sstevel@tonic-gate if (softerror == 0 &&
1837c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != HOST_NOT_FOUND &&
1847c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != NETDB_INTERNAL) {
1857c478bd9Sstevel@tonic-gate softerror = 1;
1867c478bd9Sstevel@tonic-gate therrno = pvt->res->res_h_errno;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate if (rule->flags & IRS_CONTINUE)
1897c478bd9Sstevel@tonic-gate continue;
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * See the comments in ho_byname() explaining
1927c478bd9Sstevel@tonic-gate * the interpretation of TRY_AGAIN and ECONNREFUSED.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
1957c478bd9Sstevel@tonic-gate break;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
1987c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, therrno);
1997c478bd9Sstevel@tonic-gate return (NULL);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate static struct hostent *
ho_byaddr(struct irs_ho * this,const void * addr,int len,int af)2037c478bd9Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
2047c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2057c478bd9Sstevel@tonic-gate struct irs_rule *rule;
2067c478bd9Sstevel@tonic-gate struct hostent *rval;
2077c478bd9Sstevel@tonic-gate struct irs_ho *ho;
2087c478bd9Sstevel@tonic-gate int therrno = NETDB_INTERNAL;
2097c478bd9Sstevel@tonic-gate int softerror = 0;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if (init(this) == -1)
2137c478bd9Sstevel@tonic-gate return (NULL);
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
2167c478bd9Sstevel@tonic-gate ho = rule->inst->ho;
2177c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2187c478bd9Sstevel@tonic-gate errno = 0;
2197c478bd9Sstevel@tonic-gate rval = (*ho->byaddr)(ho, addr, len, af);
2207c478bd9Sstevel@tonic-gate if (rval != NULL)
2217c478bd9Sstevel@tonic-gate return (rval);
2227c478bd9Sstevel@tonic-gate if (softerror == 0 &&
2237c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != HOST_NOT_FOUND &&
2247c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != NETDB_INTERNAL) {
2257c478bd9Sstevel@tonic-gate softerror = 1;
2267c478bd9Sstevel@tonic-gate therrno = pvt->res->res_h_errno;
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (rule->flags & IRS_CONTINUE)
2307c478bd9Sstevel@tonic-gate continue;
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * See the comments in ho_byname() explaining
2337c478bd9Sstevel@tonic-gate * the interpretation of TRY_AGAIN and ECONNREFUSED.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
2367c478bd9Sstevel@tonic-gate break;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
2397c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, therrno);
2407c478bd9Sstevel@tonic-gate return (NULL);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate static struct hostent *
ho_next(struct irs_ho * this)2447c478bd9Sstevel@tonic-gate ho_next(struct irs_ho *this) {
2457c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2467c478bd9Sstevel@tonic-gate struct hostent *rval;
2477c478bd9Sstevel@tonic-gate struct irs_ho *ho;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate while (pvt->rule) {
2507c478bd9Sstevel@tonic-gate ho = pvt->rule->inst->ho;
2517c478bd9Sstevel@tonic-gate rval = (*ho->next)(ho);
2527c478bd9Sstevel@tonic-gate if (rval)
2537c478bd9Sstevel@tonic-gate return (rval);
2547c478bd9Sstevel@tonic-gate if (!(pvt->rule->flags & IRS_CONTINUE))
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate pvt->rule = pvt->rule->next;
2577c478bd9Sstevel@tonic-gate if (pvt->rule) {
2587c478bd9Sstevel@tonic-gate ho = pvt->rule->inst->ho;
2597c478bd9Sstevel@tonic-gate (*ho->rewind)(ho);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate return (NULL);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate static void
ho_rewind(struct irs_ho * this)2667c478bd9Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
2677c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2687c478bd9Sstevel@tonic-gate struct irs_ho *ho;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules;
2717c478bd9Sstevel@tonic-gate if (pvt->rule) {
2727c478bd9Sstevel@tonic-gate ho = pvt->rule->inst->ho;
2737c478bd9Sstevel@tonic-gate (*ho->rewind)(ho);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate static void
ho_minimize(struct irs_ho * this)2787c478bd9Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
2797c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2807c478bd9Sstevel@tonic-gate struct irs_rule *rule;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (pvt->res)
2837c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
2847c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2857c478bd9Sstevel@tonic-gate struct irs_ho *ho = rule->inst->ho;
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate (*ho->minimize)(ho);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate static struct __res_state *
ho_res_get(struct irs_ho * this)2927c478bd9Sstevel@tonic-gate ho_res_get(struct irs_ho *this) {
2937c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (!pvt->res) {
2967c478bd9Sstevel@tonic-gate struct __res_state *res;
2977c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
2987c478bd9Sstevel@tonic-gate if (!res) {
2997c478bd9Sstevel@tonic-gate errno = ENOMEM;
3007c478bd9Sstevel@tonic-gate return (NULL);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res);
3037c478bd9Sstevel@tonic-gate ho_res_set(this, res, free);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate return (pvt->res);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate static void
ho_res_set(struct irs_ho * this,struct __res_state * res,void (* free_res)(void *))3107c478bd9Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res,
3117c478bd9Sstevel@tonic-gate void (*free_res)(void *)) {
3127c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3137c478bd9Sstevel@tonic-gate struct irs_rule *rule;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) {
3167c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
3177c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate pvt->res = res;
3217c478bd9Sstevel@tonic-gate pvt->free_res = free_res;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
3247c478bd9Sstevel@tonic-gate struct irs_ho *ho = rule->inst->ho;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate (*ho->res_set)(ho, pvt->res, NULL);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate static struct addrinfo *
ho_addrinfo(struct irs_ho * this,const char * name,const struct addrinfo * pai)3317c478bd9Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3347c478bd9Sstevel@tonic-gate struct irs_rule *rule;
3357c478bd9Sstevel@tonic-gate struct addrinfo *rval = NULL;
3367c478bd9Sstevel@tonic-gate struct irs_ho *ho;
3377c478bd9Sstevel@tonic-gate int therrno = NETDB_INTERNAL;
3387c478bd9Sstevel@tonic-gate int softerror = 0;
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if (init(this) == -1)
3417c478bd9Sstevel@tonic-gate return (NULL);
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
3447c478bd9Sstevel@tonic-gate ho = rule->inst->ho;
3457c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3467c478bd9Sstevel@tonic-gate errno = 0;
347*9525b14bSRao Shoaib if (ho->addrinfo == NULL) /*%< for safety */
3487c478bd9Sstevel@tonic-gate continue;
3497c478bd9Sstevel@tonic-gate rval = (*ho->addrinfo)(ho, name, pai);
3507c478bd9Sstevel@tonic-gate if (rval != NULL)
3517c478bd9Sstevel@tonic-gate return (rval);
3527c478bd9Sstevel@tonic-gate if (softerror == 0 &&
3537c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != HOST_NOT_FOUND &&
3547c478bd9Sstevel@tonic-gate pvt->res->res_h_errno != NETDB_INTERNAL) {
3557c478bd9Sstevel@tonic-gate softerror = 1;
3567c478bd9Sstevel@tonic-gate therrno = pvt->res->res_h_errno;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate if (rule->flags & IRS_CONTINUE)
3597c478bd9Sstevel@tonic-gate continue;
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * See the comments in ho_byname() explaining
3627c478bd9Sstevel@tonic-gate * the interpretation of TRY_AGAIN and ECONNREFUSED.
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN ||
3657c478bd9Sstevel@tonic-gate errno != ECONNREFUSED)
3667c478bd9Sstevel@tonic-gate break;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
3697c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, therrno);
3707c478bd9Sstevel@tonic-gate return (NULL);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate static int
init(struct irs_ho * this)3747c478bd9Sstevel@tonic-gate init(struct irs_ho *this) {
3757c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate if (!pvt->res && !ho_res_get(this))
3787c478bd9Sstevel@tonic-gate return (-1);
3797c478bd9Sstevel@tonic-gate
380*9525b14bSRao Shoaib if (((pvt->res->options & RES_INIT) == 0U) &&
3817c478bd9Sstevel@tonic-gate (res_ninit(pvt->res) == -1))
3827c478bd9Sstevel@tonic-gate return (-1);
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate return (0);
3857c478bd9Sstevel@tonic-gate }
386*9525b14bSRao Shoaib
387*9525b14bSRao Shoaib /*! \file */
388