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 #include <netinet/in.h>
247c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <resolv.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
327c478bd9Sstevel@tonic-gate #include <irs.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "port_after.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "irs_p.h"
377c478bd9Sstevel@tonic-gate #include "gen_p.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /* Types */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate struct pvt {
427c478bd9Sstevel@tonic-gate struct irs_rule * rules;
437c478bd9Sstevel@tonic-gate struct irs_rule * rule;
447c478bd9Sstevel@tonic-gate struct __res_state * res;
457c478bd9Sstevel@tonic-gate void (*free_res)(void *);
467c478bd9Sstevel@tonic-gate };
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /* Forward */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static void pr_close(struct irs_pr*);
517c478bd9Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *);
527c478bd9Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *);
537c478bd9Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int);
547c478bd9Sstevel@tonic-gate static void pr_rewind(struct irs_pr *);
557c478bd9Sstevel@tonic-gate static void pr_minimize(struct irs_pr *);
567c478bd9Sstevel@tonic-gate static struct __res_state * pr_res_get(struct irs_pr *);
577c478bd9Sstevel@tonic-gate static void pr_res_set(struct irs_pr *,
587c478bd9Sstevel@tonic-gate struct __res_state *,
597c478bd9Sstevel@tonic-gate void (*)(void *));
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /* Public */
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate struct irs_pr *
irs_gen_pr(struct irs_acc * this)647c478bd9Sstevel@tonic-gate irs_gen_pr(struct irs_acc *this) {
657c478bd9Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private;
667c478bd9Sstevel@tonic-gate struct irs_pr *pr;
677c478bd9Sstevel@tonic-gate struct pvt *pvt;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) {
707c478bd9Sstevel@tonic-gate errno = ENOMEM;
717c478bd9Sstevel@tonic-gate return (NULL);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate memset(pr, 0x5e, sizeof *pr);
747c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
757c478bd9Sstevel@tonic-gate memput(pr, sizeof *pr);
767c478bd9Sstevel@tonic-gate errno = ENOMEM;
777c478bd9Sstevel@tonic-gate return (NULL);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
807c478bd9Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_pr];
817c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules;
827c478bd9Sstevel@tonic-gate pr->private = pvt;
837c478bd9Sstevel@tonic-gate pr->close = pr_close;
847c478bd9Sstevel@tonic-gate pr->next = pr_next;
857c478bd9Sstevel@tonic-gate pr->byname = pr_byname;
867c478bd9Sstevel@tonic-gate pr->bynumber = pr_bynumber;
877c478bd9Sstevel@tonic-gate pr->rewind = pr_rewind;
887c478bd9Sstevel@tonic-gate pr->minimize = pr_minimize;
897c478bd9Sstevel@tonic-gate pr->res_get = pr_res_get;
907c478bd9Sstevel@tonic-gate pr->res_set = pr_res_set;
917c478bd9Sstevel@tonic-gate return (pr);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /* Methods */
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate static void
pr_close(struct irs_pr * this)977c478bd9Sstevel@tonic-gate pr_close(struct irs_pr *this) {
987c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1017c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate static struct protoent *
pr_next(struct irs_pr * this)1057c478bd9Sstevel@tonic-gate pr_next(struct irs_pr *this) {
1067c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1077c478bd9Sstevel@tonic-gate struct protoent *rval;
1087c478bd9Sstevel@tonic-gate struct irs_pr *pr;
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate while (pvt->rule) {
1117c478bd9Sstevel@tonic-gate pr = pvt->rule->inst->pr;
1127c478bd9Sstevel@tonic-gate rval = (*pr->next)(pr);
1137c478bd9Sstevel@tonic-gate if (rval)
1147c478bd9Sstevel@tonic-gate return (rval);
1157c478bd9Sstevel@tonic-gate if (!(pvt->rules->flags & IRS_CONTINUE))
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate pvt->rule = pvt->rule->next;
1187c478bd9Sstevel@tonic-gate if (pvt->rule) {
1197c478bd9Sstevel@tonic-gate pr = pvt->rule->inst->pr;
1207c478bd9Sstevel@tonic-gate (*pr->rewind)(pr);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate return (NULL);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate static struct protoent *
pr_byname(struct irs_pr * this,const char * name)1277c478bd9Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) {
1287c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1297c478bd9Sstevel@tonic-gate struct irs_rule *rule;
1307c478bd9Sstevel@tonic-gate struct protoent *rval;
1317c478bd9Sstevel@tonic-gate struct irs_pr *pr;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate rval = NULL;
1347c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1357c478bd9Sstevel@tonic-gate pr = rule->inst->pr;
1367c478bd9Sstevel@tonic-gate rval = (*pr->byname)(pr, name);
1377c478bd9Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE))
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate return (rval);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate static struct protoent *
pr_bynumber(struct irs_pr * this,int proto)1447c478bd9Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) {
1457c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1467c478bd9Sstevel@tonic-gate struct irs_rule *rule;
1477c478bd9Sstevel@tonic-gate struct protoent *rval;
1487c478bd9Sstevel@tonic-gate struct irs_pr *pr;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate rval = NULL;
1517c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1527c478bd9Sstevel@tonic-gate pr = rule->inst->pr;
1537c478bd9Sstevel@tonic-gate rval = (*pr->bynumber)(pr, proto);
1547c478bd9Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE))
1557c478bd9Sstevel@tonic-gate break;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate return (rval);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate static void
pr_rewind(struct irs_pr * this)1617c478bd9Sstevel@tonic-gate pr_rewind(struct irs_pr *this) {
1627c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1637c478bd9Sstevel@tonic-gate struct irs_pr *pr;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules;
1667c478bd9Sstevel@tonic-gate if (pvt->rule) {
1677c478bd9Sstevel@tonic-gate pr = pvt->rule->inst->pr;
1687c478bd9Sstevel@tonic-gate (*pr->rewind)(pr);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate static void
pr_minimize(struct irs_pr * this)1737c478bd9Sstevel@tonic-gate pr_minimize(struct irs_pr *this) {
1747c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1757c478bd9Sstevel@tonic-gate struct irs_rule *rule;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
1787c478bd9Sstevel@tonic-gate struct irs_pr *pr = rule->inst->pr;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate (*pr->minimize)(pr);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static struct __res_state *
pr_res_get(struct irs_pr * this)1857c478bd9Sstevel@tonic-gate pr_res_get(struct irs_pr *this) {
1867c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (!pvt->res) {
1897c478bd9Sstevel@tonic-gate struct __res_state *res;
1907c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
1917c478bd9Sstevel@tonic-gate if (!res) {
1927c478bd9Sstevel@tonic-gate errno = ENOMEM;
1937c478bd9Sstevel@tonic-gate return (NULL);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res);
1967c478bd9Sstevel@tonic-gate pr_res_set(this, res, free);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate return (pvt->res);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate static void
pr_res_set(struct irs_pr * this,struct __res_state * res,void (* free_res)(void *))2037c478bd9Sstevel@tonic-gate pr_res_set(struct irs_pr *this, struct __res_state *res,
2047c478bd9Sstevel@tonic-gate void (*free_res)(void *)) {
2057c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2067c478bd9Sstevel@tonic-gate struct irs_rule *rule;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) {
2097c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
2107c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate pvt->res = res;
2147c478bd9Sstevel@tonic-gate pvt->free_res = free_res;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2177c478bd9Sstevel@tonic-gate struct irs_pr *pr = rule->inst->pr;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate if (pr->res_set)
2207c478bd9Sstevel@tonic-gate (*pr->res_set)(pr, pvt->res, NULL);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
223*9525b14bSRao Shoaib
224*9525b14bSRao Shoaib /*! \file */
225