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_sv.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 #include <resolv.h> 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <errno.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 sv_close(struct irs_sv*); 550Sstevel@tonic-gate static struct servent * sv_next(struct irs_sv *); 560Sstevel@tonic-gate static struct servent * sv_byname(struct irs_sv *, const char *, 570Sstevel@tonic-gate const char *); 580Sstevel@tonic-gate static struct servent * sv_byport(struct irs_sv *, int, const char *); 590Sstevel@tonic-gate static void sv_rewind(struct irs_sv *); 600Sstevel@tonic-gate static void sv_minimize(struct irs_sv *); 610Sstevel@tonic-gate static struct __res_state * sv_res_get(struct irs_sv *); 620Sstevel@tonic-gate static void sv_res_set(struct irs_sv *, 630Sstevel@tonic-gate struct __res_state *, 640Sstevel@tonic-gate void (*)(void *)); 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* Public */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate struct irs_sv * 690Sstevel@tonic-gate irs_gen_sv(struct irs_acc *this) { 700Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private; 710Sstevel@tonic-gate struct irs_sv *sv; 720Sstevel@tonic-gate struct pvt *pvt; 730Sstevel@tonic-gate 740Sstevel@tonic-gate if (!(sv = memget(sizeof *sv))) { 750Sstevel@tonic-gate errno = ENOMEM; 760Sstevel@tonic-gate return (NULL); 770Sstevel@tonic-gate } 780Sstevel@tonic-gate memset(sv, 0x5e, sizeof *sv); 790Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 800Sstevel@tonic-gate memput(sv, sizeof *sv); 810Sstevel@tonic-gate errno = ENOMEM; 820Sstevel@tonic-gate return (NULL); 830Sstevel@tonic-gate } 840Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 850Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_sv]; 860Sstevel@tonic-gate pvt->rule = pvt->rules; 870Sstevel@tonic-gate sv->private = pvt; 880Sstevel@tonic-gate sv->close = sv_close; 890Sstevel@tonic-gate sv->next = sv_next; 900Sstevel@tonic-gate sv->byname = sv_byname; 910Sstevel@tonic-gate sv->byport = sv_byport; 920Sstevel@tonic-gate sv->rewind = sv_rewind; 930Sstevel@tonic-gate sv->minimize = sv_minimize; 940Sstevel@tonic-gate sv->res_get = sv_res_get; 950Sstevel@tonic-gate sv->res_set = sv_res_set; 960Sstevel@tonic-gate return (sv); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* Methods */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static void 1020Sstevel@tonic-gate sv_close(struct irs_sv *this) { 1030Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1060Sstevel@tonic-gate memput(this, sizeof *this); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate static struct servent * 1100Sstevel@tonic-gate sv_next(struct irs_sv *this) { 1110Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1120Sstevel@tonic-gate struct servent *rval; 1130Sstevel@tonic-gate struct irs_sv *sv; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate while (pvt->rule) { 1160Sstevel@tonic-gate sv = pvt->rule->inst->sv; 1170Sstevel@tonic-gate rval = (*sv->next)(sv); 1180Sstevel@tonic-gate if (rval) 1190Sstevel@tonic-gate return (rval); 1200Sstevel@tonic-gate if (!(pvt->rule->flags & IRS_CONTINUE)) 1210Sstevel@tonic-gate break; 1220Sstevel@tonic-gate pvt->rule = pvt->rule->next; 1230Sstevel@tonic-gate if (pvt->rule) { 1240Sstevel@tonic-gate sv = pvt->rule->inst->sv; 1250Sstevel@tonic-gate (*sv->rewind)(sv); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate return (NULL); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static struct servent * 1320Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) { 1330Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1340Sstevel@tonic-gate struct irs_rule *rule; 1350Sstevel@tonic-gate struct servent *rval; 1360Sstevel@tonic-gate struct irs_sv *sv; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate rval = NULL; 1390Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1400Sstevel@tonic-gate sv = rule->inst->sv; 1410Sstevel@tonic-gate rval = (*sv->byname)(sv, name, proto); 1420Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 1430Sstevel@tonic-gate break; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate return (rval); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate static struct servent * 1490Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) { 1500Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1510Sstevel@tonic-gate struct irs_rule *rule; 1520Sstevel@tonic-gate struct servent *rval; 1530Sstevel@tonic-gate struct irs_sv *sv; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate rval = NULL; 1560Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1570Sstevel@tonic-gate sv = rule->inst->sv; 1580Sstevel@tonic-gate rval = (*sv->byport)(sv, port, proto); 1590Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 1600Sstevel@tonic-gate break; 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate return (rval); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static void 1660Sstevel@tonic-gate sv_rewind(struct irs_sv *this) { 1670Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1680Sstevel@tonic-gate struct irs_sv *sv; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate pvt->rule = pvt->rules; 1710Sstevel@tonic-gate if (pvt->rule) { 1720Sstevel@tonic-gate sv = pvt->rule->inst->sv; 1730Sstevel@tonic-gate (*sv->rewind)(sv); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate static void 1780Sstevel@tonic-gate sv_minimize(struct irs_sv *this) { 1790Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1800Sstevel@tonic-gate struct irs_rule *rule; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 1830Sstevel@tonic-gate struct irs_sv *sv = rule->inst->sv; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate (*sv->minimize)(sv); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate static struct __res_state * 1900Sstevel@tonic-gate sv_res_get(struct irs_sv *this) { 1910Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (!pvt->res) { 1940Sstevel@tonic-gate struct __res_state *res; 1950Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 1960Sstevel@tonic-gate if (!res) { 1970Sstevel@tonic-gate errno = ENOMEM; 1980Sstevel@tonic-gate return (NULL); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate memset(res, 0, sizeof *res); 2010Sstevel@tonic-gate sv_res_set(this, res, free); 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate return (pvt->res); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate static void 2080Sstevel@tonic-gate sv_res_set(struct irs_sv *this, struct __res_state *res, 2090Sstevel@tonic-gate void (*free_res)(void *)) { 2100Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2110Sstevel@tonic-gate struct irs_rule *rule; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 2140Sstevel@tonic-gate res_nclose(pvt->res); 2150Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate pvt->res = res; 2190Sstevel@tonic-gate pvt->free_res = free_res; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 2220Sstevel@tonic-gate struct irs_sv *sv = rule->inst->sv; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate if (sv->res_set) 2250Sstevel@tonic-gate (*sv->res_set)(sv, pvt->res, NULL); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate } 228*11038SRao.Shoaib@Sun.COM 229*11038SRao.Shoaib@Sun.COM /*! \file */ 230