1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 1997-2000 by Sun Microsystems, Inc. 3*7c478bd9Sstevel@tonic-gate * All rights reserved. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate /* 7*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 10*7c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 11*7c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 12*7c478bd9Sstevel@tonic-gate * 13*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 14*7c478bd9Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 15*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 16*7c478bd9Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 17*7c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 18*7c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 19*7c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 20*7c478bd9Sstevel@tonic-gate * SOFTWARE. 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER) 26*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: gen_sv.c,v 1.12 1999/10/13 16:39:30 vixie Exp $"; 27*7c478bd9Sstevel@tonic-gate #endif 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* Imports */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include "port_before.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 35*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 36*7c478bd9Sstevel@tonic-gate #include <resolv.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <isc/memcluster.h> 43*7c478bd9Sstevel@tonic-gate #include <irs.h> 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include "port_after.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include "irs_p.h" 48*7c478bd9Sstevel@tonic-gate #include "gen_p.h" 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* Types */ 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate struct pvt { 53*7c478bd9Sstevel@tonic-gate struct irs_rule * rules; 54*7c478bd9Sstevel@tonic-gate struct irs_rule * rule; 55*7c478bd9Sstevel@tonic-gate struct __res_state * res; 56*7c478bd9Sstevel@tonic-gate void (*free_res)(void *); 57*7c478bd9Sstevel@tonic-gate }; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* Forward */ 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static void sv_close(struct irs_sv*); 62*7c478bd9Sstevel@tonic-gate static struct servent * sv_next(struct irs_sv *); 63*7c478bd9Sstevel@tonic-gate static struct servent * sv_byname(struct irs_sv *, const char *, 64*7c478bd9Sstevel@tonic-gate const char *); 65*7c478bd9Sstevel@tonic-gate static struct servent * sv_byport(struct irs_sv *, int, const char *); 66*7c478bd9Sstevel@tonic-gate static void sv_rewind(struct irs_sv *); 67*7c478bd9Sstevel@tonic-gate static void sv_minimize(struct irs_sv *); 68*7c478bd9Sstevel@tonic-gate static struct __res_state * sv_res_get(struct irs_sv *); 69*7c478bd9Sstevel@tonic-gate static void sv_res_set(struct irs_sv *, 70*7c478bd9Sstevel@tonic-gate struct __res_state *, 71*7c478bd9Sstevel@tonic-gate void (*)(void *)); 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* Public */ 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate struct irs_sv * 76*7c478bd9Sstevel@tonic-gate irs_gen_sv(struct irs_acc *this) { 77*7c478bd9Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private; 78*7c478bd9Sstevel@tonic-gate struct irs_sv *sv; 79*7c478bd9Sstevel@tonic-gate struct pvt *pvt; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate if (!(sv = memget(sizeof *sv))) { 82*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 83*7c478bd9Sstevel@tonic-gate return (NULL); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate memset(sv, 0x5e, sizeof *sv); 86*7c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 87*7c478bd9Sstevel@tonic-gate memput(sv, sizeof *sv); 88*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 89*7c478bd9Sstevel@tonic-gate return (NULL); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 92*7c478bd9Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_sv]; 93*7c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules; 94*7c478bd9Sstevel@tonic-gate sv->private = pvt; 95*7c478bd9Sstevel@tonic-gate sv->close = sv_close; 96*7c478bd9Sstevel@tonic-gate sv->next = sv_next; 97*7c478bd9Sstevel@tonic-gate sv->byname = sv_byname; 98*7c478bd9Sstevel@tonic-gate sv->byport = sv_byport; 99*7c478bd9Sstevel@tonic-gate sv->rewind = sv_rewind; 100*7c478bd9Sstevel@tonic-gate sv->minimize = sv_minimize; 101*7c478bd9Sstevel@tonic-gate sv->res_get = sv_res_get; 102*7c478bd9Sstevel@tonic-gate sv->res_set = sv_res_set; 103*7c478bd9Sstevel@tonic-gate return (sv); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate /* Methods */ 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate static void 109*7c478bd9Sstevel@tonic-gate sv_close(struct irs_sv *this) { 110*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 113*7c478bd9Sstevel@tonic-gate memput(this, sizeof *this); 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate static struct servent * 117*7c478bd9Sstevel@tonic-gate sv_next(struct irs_sv *this) { 118*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 119*7c478bd9Sstevel@tonic-gate struct servent *rval; 120*7c478bd9Sstevel@tonic-gate struct irs_sv *sv; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate while (pvt->rule) { 123*7c478bd9Sstevel@tonic-gate sv = pvt->rule->inst->sv; 124*7c478bd9Sstevel@tonic-gate rval = (*sv->next)(sv); 125*7c478bd9Sstevel@tonic-gate if (rval) 126*7c478bd9Sstevel@tonic-gate return (rval); 127*7c478bd9Sstevel@tonic-gate if (!(pvt->rule->flags & IRS_CONTINUE)) 128*7c478bd9Sstevel@tonic-gate break; 129*7c478bd9Sstevel@tonic-gate pvt->rule = pvt->rule->next; 130*7c478bd9Sstevel@tonic-gate if (pvt->rule) { 131*7c478bd9Sstevel@tonic-gate sv = pvt->rule->inst->sv; 132*7c478bd9Sstevel@tonic-gate (*sv->rewind)(sv); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate return (NULL); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate static struct servent * 139*7c478bd9Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) { 140*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 141*7c478bd9Sstevel@tonic-gate struct irs_rule *rule; 142*7c478bd9Sstevel@tonic-gate struct servent *rval; 143*7c478bd9Sstevel@tonic-gate struct irs_sv *sv; 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate rval = NULL; 146*7c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 147*7c478bd9Sstevel@tonic-gate sv = rule->inst->sv; 148*7c478bd9Sstevel@tonic-gate rval = (*sv->byname)(sv, name, proto); 149*7c478bd9Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate return (rval); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate static struct servent * 156*7c478bd9Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) { 157*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 158*7c478bd9Sstevel@tonic-gate struct irs_rule *rule; 159*7c478bd9Sstevel@tonic-gate struct servent *rval; 160*7c478bd9Sstevel@tonic-gate struct irs_sv *sv; 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate rval = NULL; 163*7c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 164*7c478bd9Sstevel@tonic-gate sv = rule->inst->sv; 165*7c478bd9Sstevel@tonic-gate rval = (*sv->byport)(sv, port, proto); 166*7c478bd9Sstevel@tonic-gate if (rval || !(rule->flags & IRS_CONTINUE)) 167*7c478bd9Sstevel@tonic-gate break; 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate return (rval); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate static void 173*7c478bd9Sstevel@tonic-gate sv_rewind(struct irs_sv *this) { 174*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 175*7c478bd9Sstevel@tonic-gate struct irs_sv *sv; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules; 178*7c478bd9Sstevel@tonic-gate if (pvt->rule) { 179*7c478bd9Sstevel@tonic-gate sv = pvt->rule->inst->sv; 180*7c478bd9Sstevel@tonic-gate (*sv->rewind)(sv); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate static void 185*7c478bd9Sstevel@tonic-gate sv_minimize(struct irs_sv *this) { 186*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 187*7c478bd9Sstevel@tonic-gate struct irs_rule *rule; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 190*7c478bd9Sstevel@tonic-gate struct irs_sv *sv = rule->inst->sv; 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate (*sv->minimize)(sv); 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate static struct __res_state * 197*7c478bd9Sstevel@tonic-gate sv_res_get(struct irs_sv *this) { 198*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate if (!pvt->res) { 201*7c478bd9Sstevel@tonic-gate struct __res_state *res; 202*7c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 203*7c478bd9Sstevel@tonic-gate if (!res) { 204*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 205*7c478bd9Sstevel@tonic-gate return (NULL); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res); 208*7c478bd9Sstevel@tonic-gate sv_res_set(this, res, free); 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate return (pvt->res); 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate static void 215*7c478bd9Sstevel@tonic-gate sv_res_set(struct irs_sv *this, struct __res_state *res, 216*7c478bd9Sstevel@tonic-gate void (*free_res)(void *)) { 217*7c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 218*7c478bd9Sstevel@tonic-gate struct irs_rule *rule; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 221*7c478bd9Sstevel@tonic-gate res_nclose(pvt->res); 222*7c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate pvt->res = res; 226*7c478bd9Sstevel@tonic-gate pvt->free_res = free_res; 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 229*7c478bd9Sstevel@tonic-gate struct irs_sv *sv = rule->inst->sv; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate if (sv->res_set) 232*7c478bd9Sstevel@tonic-gate (*sv->res_set)(sv, pvt->res, NULL); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate } 235