10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 30Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER) 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: gen_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp $"; 200Sstevel@tonic-gate #endif 210Sstevel@tonic-gate 220Sstevel@tonic-gate /* Imports */ 230Sstevel@tonic-gate 240Sstevel@tonic-gate #include "port_before.h" 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/types.h> 270Sstevel@tonic-gate #include <netinet/in.h> 280Sstevel@tonic-gate #include <arpa/nameser.h> 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <errno.h> 310Sstevel@tonic-gate #include <resolv.h> 320Sstevel@tonic-gate #include <stdlib.h> 330Sstevel@tonic-gate #include <string.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <isc/memcluster.h> 360Sstevel@tonic-gate #include <irs.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include "port_after.h" 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include "irs_p.h" 410Sstevel@tonic-gate #include "gen_p.h" 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* Types */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate struct pvt { 460Sstevel@tonic-gate struct irs_rule * rules; 470Sstevel@tonic-gate struct irs_rule * rule; 480Sstevel@tonic-gate struct __res_state * res; 490Sstevel@tonic-gate void (*free_res)(void *); 500Sstevel@tonic-gate }; 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* Forward */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate static void pr_close(struct irs_pr*); 550Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *); 560Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *); 570Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int); 580Sstevel@tonic-gate static void pr_rewind(struct irs_pr *); 590Sstevel@tonic-gate static void pr_minimize(struct irs_pr *); 600Sstevel@tonic-gate static struct __res_state * pr_res_get(struct irs_pr *); 610Sstevel@tonic-gate static void pr_res_set(struct irs_pr *, 620Sstevel@tonic-gate struct __res_state *, 630Sstevel@tonic-gate void (*)(void *)); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* Public */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate struct irs_pr * 680Sstevel@tonic-gate irs_gen_pr(struct irs_acc *this) { 690Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private; 700Sstevel@tonic-gate struct irs_pr *pr; 710Sstevel@tonic-gate struct pvt *pvt; 720Sstevel@tonic-gate 730Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) { 740Sstevel@tonic-gate errno = ENOMEM; 750Sstevel@tonic-gate return (NULL); 760Sstevel@tonic-gate } 770Sstevel@tonic-gate memset(pr, 0x5e, sizeof *pr); 780Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 790Sstevel@tonic-gate memput(pr, sizeof *pr); 800Sstevel@tonic-gate errno = ENOMEM; 810Sstevel@tonic-gate return (NULL); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 840Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_pr]; 850Sstevel@tonic-gate pvt->rule = pvt->rules; 860Sstevel@tonic-gate pr->private = pvt; 870Sstevel@tonic-gate pr->close = pr_close; 880Sstevel@tonic-gate pr->next = pr_next; 890Sstevel@tonic-gate pr->byname = pr_byname; 900Sstevel@tonic-gate pr->bynumber = pr_bynumber; 910Sstevel@tonic-gate pr->rewind = pr_rewind; 920Sstevel@tonic-gate pr->minimize = pr_minimize; 930Sstevel@tonic-gate pr->res_get = pr_res_get; 940Sstevel@tonic-gate pr->res_set = pr_res_set; 950Sstevel@tonic-gate return (pr); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* Methods */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate static void 1010Sstevel@tonic-gate pr_close(struct irs_pr *this) { 1020Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1050Sstevel@tonic-gate memput(this, sizeof *this); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static struct protoent * 1090Sstevel@tonic-gate pr_next(struct irs_pr *this) { 1100Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1110Sstevel@tonic-gate struct protoent *rval; 1120Sstevel@tonic-gate struct irs_pr *pr; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate while (pvt->rule) { 1150Sstevel@tonic-gate pr = pvt->rule->inst->pr; 1160Sstevel@tonic-gate rval = (*pr->next)(pr); 1170Sstevel@tonic-gate if (rval) 1180Sstevel@tonic-gate return (rval); 1190Sstevel@tonic-gate if (!(pvt->rules->flags & IRS_CONTINUE)) 1200Sstevel@tonic-gate break; 1210Sstevel@tonic-gate pvt->rule = pvt->rule->next; 1220Sstevel@tonic-gate if (pvt->rule) { 1230Sstevel@tonic-gate pr = pvt->rule->inst->pr; 1240Sstevel@tonic-gate (*pr->rewind)(pr); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate return (NULL); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate static struct protoent * 1310Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) { 1320Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1330Sstevel@tonic-gate struct irs_rule *rule; 1340Sstevel@tonic-gate struct protoent *rval; 1350Sstevel@tonic-gate struct irs_pr *pr; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate rval = NULL; 1380Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1390Sstevel@tonic-gate pr = rule->inst->pr; 1400Sstevel@tonic-gate rval = (*pr->byname)(pr, name); 1410Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 1420Sstevel@tonic-gate break; 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate return (rval); 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate static struct protoent * 1480Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) { 1490Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1500Sstevel@tonic-gate struct irs_rule *rule; 1510Sstevel@tonic-gate struct protoent *rval; 1520Sstevel@tonic-gate struct irs_pr *pr; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate rval = NULL; 1550Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1560Sstevel@tonic-gate pr = rule->inst->pr; 1570Sstevel@tonic-gate rval = (*pr->bynumber)(pr, proto); 1580Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate return (rval); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static void 1650Sstevel@tonic-gate pr_rewind(struct irs_pr *this) { 1660Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1670Sstevel@tonic-gate struct irs_pr *pr; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate pvt->rule = pvt->rules; 1700Sstevel@tonic-gate if (pvt->rule) { 1710Sstevel@tonic-gate pr = pvt->rule->inst->pr; 1720Sstevel@tonic-gate (*pr->rewind)(pr); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static void 1770Sstevel@tonic-gate pr_minimize(struct irs_pr *this) { 1780Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1790Sstevel@tonic-gate struct irs_rule *rule; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 1820Sstevel@tonic-gate struct irs_pr *pr = rule->inst->pr; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate (*pr->minimize)(pr); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate static struct __res_state * 1890Sstevel@tonic-gate pr_res_get(struct irs_pr *this) { 1900Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate if (!pvt->res) { 1930Sstevel@tonic-gate struct __res_state *res; 1940Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 1950Sstevel@tonic-gate if (!res) { 1960Sstevel@tonic-gate errno = ENOMEM; 1970Sstevel@tonic-gate return (NULL); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate memset(res, 0, sizeof *res); 2000Sstevel@tonic-gate pr_res_set(this, res, free); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate return (pvt->res); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate static void 2070Sstevel@tonic-gate pr_res_set(struct irs_pr *this, struct __res_state *res, 2080Sstevel@tonic-gate void (*free_res)(void *)) { 2090Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2100Sstevel@tonic-gate struct irs_rule *rule; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 2130Sstevel@tonic-gate res_nclose(pvt->res); 2140Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate pvt->res = res; 2180Sstevel@tonic-gate pvt->free_res = free_res; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 2210Sstevel@tonic-gate struct irs_pr *pr = rule->inst->pr; 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate if (pr->res_set) 2240Sstevel@tonic-gate (*pr->res_set)(pr, pvt->res, NULL); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate } 227*11038SRao.Shoaib@Sun.COM 228*11038SRao.Shoaib@Sun.COM /*! \file */ 229